GADGET-4
idstorage.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * \copyright This file is part of the GADGET4 N-body/SPH code developed
3  * \copyright by Volker Springel. Copyright (C) 2014-2020 by Volker Springel
4  * \copyright (vspringel@mpa-garching.mpg.de) and all contributing authors.
5  *******************************************************************************/
6 
12 #ifndef IDSTORAGE_H
13 #define IDSTORAGE_H
14 
15 #include <climits>
16 
17 #if !defined(IDS_48BIT)
18 #define ID_MSB ((MyIDType)(~((MyIDType)(~((MyIDType)0)) >> ((MyIDType)1))))
19 #define ID_MSK ((MyIDType)(((MyIDType)(~((MyIDType)0)) >> ((MyIDType)1))))
20 #define HALONR_MAX ((MyIDType)(((MyIDType)(~((MyIDType)0)) >> ((MyIDType)1))))
21 #else
22 #define ID_MSB ((unsigned short)(~((unsigned short)(~((unsigned short)0)) >> ((unsigned short)1))))
23 #define ID_MSK ((unsigned short)(((unsigned short)(~((unsigned short)0)) >> ((unsigned short)1))))
24 #define HALONR_MAX ((MyIDType)(((MyIDType)(~((MyIDType)0)) >> ((MyIDType)17))))
25 #endif
26 
27 /* used to store a subhalo len in an approximate (quite accurate) way in just two bytes */
28 struct approxlen
29 {
30 #define ALEN_MAX 1000000000.0
31 #define ALEN_MIN 10.0
32 
33  private:
34  unsigned short alen;
35 
36  public:
37  inline void set(long long size)
38  {
39  double l = log(size / ALEN_MIN) / log(ALEN_MAX / ALEN_MIN) * (USHRT_MAX - 1) + 1;
40 
41  if(l < 1)
42  alen = 0;
43  else if(l > USHRT_MAX)
44  alen = USHRT_MAX;
45  else
46  alen = (unsigned short)(l + 0.5);
47  }
48 
49  inline long long get(void)
50  {
51  // relatice accuracy of this encoding is about ~0.00012 for particle numbers between 10 to 10^9
52  if(alen == 0)
53  return 0;
54  else
55  {
56  return (long long)(ALEN_MIN * exp((alen - 1.0) / (USHRT_MAX - 1) * log(ALEN_MAX / ALEN_MIN)) + 0.5);
57  }
58  }
59 };
60 
62 {
63  private:
64  unsigned char rank;
65 
66  public:
67  inline void set(MyLenType nr)
68  {
69  if(nr > UCHAR_MAX)
70  nr = UCHAR_MAX;
71  rank = nr;
72  }
73 
74  inline unsigned char get(void) { return rank; }
75 };
76 
78 {
79  private:
80 #if !defined(IDS_48BIT)
81  MyIDType id;
82 #else
83  unsigned short id[3];
84 #endif
85 
86  public:
87  inline MyIDType get(void) const
88  {
89 #if !defined(IDS_48BIT)
90  return id & ID_MSK;
91 #else
92  return (((MyIDType)(id[0] & ID_MSK)) << 32) + (((MyIDType)id[1]) << 16) + id[2];
93 #endif
94  }
95 
96  inline void set(MyIDType ID)
97  {
98 #if !defined(IDS_48BIT)
99  id = ID;
100 #else
101  id[2] = (unsigned short)ID;
102  id[1] = (unsigned short)(ID >> 16);
103  id[0] = (unsigned short)(ID >> 32);
104 #endif
105  }
106 
107  inline void mark_as_formerly_most_bound(void)
108  {
109  /* we set the most significant bit */
110 #if !defined(IDS_48BIT)
111  id |= ID_MSB;
112 #else
113  id[0] |= ID_MSB;
114 #endif
115  }
116 
117  inline bool is_previously_most_bound(void)
118  {
119  /* we set the most significant bit */
120 #if defined(IDS_48BIT)
121  if(id[0] & ID_MSB)
122  return true;
123 #else
124  if(id & ID_MSB)
125  return true;
126 #endif
127  return false;
128  }
129 };
130 
131 class MyHaloNrType : public MyIDStorage
132 {
133  public:
134  inline MyHaloNrType &operator+=(const long long &x)
135  {
136  set(get() + x);
137  return *this;
138  }
139 };
140 
141 inline bool operator<(const MyHaloNrType &left, const MyHaloNrType &right) { return left.get() < right.get(); }
142 
143 inline bool operator>(const MyHaloNrType &left, const MyHaloNrType &right) { return left.get() > right.get(); }
144 
145 inline bool operator!=(const MyHaloNrType &left, const MyHaloNrType &right) { return left.get() != right.get(); }
146 
147 inline bool operator==(const MyHaloNrType &left, const MyHaloNrType &right) { return left.get() == right.get(); }
148 
149 #endif /* IDSTORAGE_H */
MyHaloNrType & operator+=(const long long &x)
Definition: idstorage.h:134
void mark_as_formerly_most_bound(void)
Definition: idstorage.h:107
MyIDType get(void) const
Definition: idstorage.h:87
void set(MyIDType ID)
Definition: idstorage.h:96
bool is_previously_most_bound(void)
Definition: idstorage.h:117
int MyLenType
Definition: dtypes.h:76
unsigned int MyIDType
Definition: dtypes.h:68
#define ID_MSB
Definition: idstorage.h:18
bool operator!=(const MyHaloNrType &left, const MyHaloNrType &right)
Definition: idstorage.h:145
#define ID_MSK
Definition: idstorage.h:19
bool operator>(const MyHaloNrType &left, const MyHaloNrType &right)
Definition: idstorage.h:143
#define ALEN_MIN
Definition: idstorage.h:31
#define ALEN_MAX
Definition: idstorage.h:30
bool operator==(const MyHaloNrType &left, const MyHaloNrType &right)
Definition: idstorage.h:147
bool operator<(const MyHaloNrType &left, const MyHaloNrType &right)
Definition: idstorage.h:141
expr exp(half arg)
Definition: half.hpp:2724
expr log(half arg)
Definition: half.hpp:2745
void set(long long size)
Definition: idstorage.h:37
long long get(void)
Definition: idstorage.h:49
unsigned char get(void)
Definition: idstorage.h:74
void set(MyLenType nr)
Definition: idstorage.h:67