Amuse
Common.hpp
1 #ifndef __AMUSE_COMMON_HPP__
2 #define __AMUSE_COMMON_HPP__
3 
4 #include <algorithm>
5 #include <limits.h>
6 
7 namespace amuse
8 {
9 
10 #ifndef PRISize
11 #ifdef _MSC_VER
12 #define PRISize "Iu"
13 #else
14 #define PRISize "zu"
15 #endif
16 #endif
17 
18 template <typename T>
19 static inline T clamp(T a, T val, T b) {return std::max<T>(a, std::min<T>(b, val));}
20 
21 template <typename T>
22 static inline T ClampFull(float in) {return in;}
23 
24 template <>
25 inline int16_t ClampFull<int16_t>(float in)
26 {
27  if (in < SHRT_MIN)
28  return SHRT_MIN;
29  else if (in > SHRT_MAX)
30  return SHRT_MAX;
31  return in;
32 }
33 
34 template <>
35 inline int32_t ClampFull<int32_t>(float in)
36 {
37  if (in < INT_MIN)
38  return INT_MIN;
39  else if (in > INT_MAX)
40  return INT_MAX;
41  return in;
42 }
43 
44 template <>
45 inline float ClampFull<float>(float in)
46 {
47  if (in < -1.f)
48  return -1.f;
49  else if (in > 1.f)
50  return 1.f;
51  return in;
52 }
53 
54 #ifndef M_PIF
55 #define M_PIF 3.14159265358979323846f /* pi */
56 #endif
57 
58 #undef bswap16
59 #undef bswap32
60 #undef bswap64
61 
62 /* Type-sensitive byte swappers */
63 template <typename T>
64 static inline T bswap16(T val)
65 {
66 #if __GNUC__
67  return __builtin_bswap16(val);
68 #elif _WIN32
69  return _byteswap_ushort(val);
70 #else
71  return (val = (val << 8) | ((val >> 8) & 0xFF));
72 #endif
73 }
74 
75 template <typename T>
76 static inline T bswap32(T val)
77 {
78 #if __GNUC__
79  return __builtin_bswap32(val);
80 #elif _WIN32
81  return _byteswap_ulong(val);
82 #else
83  val = (val & 0x0000FFFF) << 16 | (val & 0xFFFF0000) >> 16;
84  val = (val & 0x00FF00FF) << 8 | (val & 0xFF00FF00) >> 8;
85  return val;
86 #endif
87 }
88 
89 template <typename T>
90 static inline T bswap64(T val)
91 {
92 #if __GNUC__
93  return __builtin_bswap64(val);
94 #elif _WIN32
95  return _byteswap_uint64(val);
96 #else
97  return ((val & 0xFF00000000000000ULL) >> 56) |
98  ((val & 0x00FF000000000000ULL) >> 40) |
99  ((val & 0x0000FF0000000000ULL) >> 24) |
100  ((val & 0x000000FF00000000ULL) >> 8) |
101  ((val & 0x00000000FF000000ULL) << 8) |
102  ((val & 0x0000000000FF0000ULL) << 24) |
103  ((val & 0x000000000000FF00ULL) << 40) |
104  ((val & 0x00000000000000FFULL) << 56);
105 #endif
106 }
107 
108 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
109 static inline int16_t SBig(int16_t val) {return bswap16(val);}
110 static inline uint16_t SBig(uint16_t val) {return bswap16(val);}
111 static inline int32_t SBig(int32_t val) {return bswap32(val);}
112 static inline uint32_t SBig(uint32_t val) {return bswap32(val);}
113 static inline int64_t SBig(int64_t val) {return bswap64(val);}
114 static inline uint64_t SBig(uint64_t val) {return bswap64(val);}
115 static inline float SBig(float val)
116 {
117  int32_t ival = bswap32(*((int32_t*)(&val)));
118  return *((float*)(&ival));
119 }
120 static inline double SBig(double val)
121 {
122  int64_t ival = bswap64(*((int64_t*)(&val)));
123  return *((double*)(&ival));
124 }
125 #ifndef SBIG
126 #define SBIG(q) ( ( (q) & 0x000000FF ) << 24 | ( (q) & 0x0000FF00 ) << 8 \
127  | ( (q) & 0x00FF0000 ) >> 8 | ( (q) & 0xFF000000 ) >> 24 )
128 #endif
129 
130 static inline int16_t SLittle(int16_t val) {return val;}
131 static inline uint16_t SLittle(uint16_t val) {return val;}
132 static inline int32_t SLittle(int32_t val) {return val;}
133 static inline uint32_t SLittle(uint32_t val) {return val;}
134 static inline int64_t SLittle(int64_t val) {return val;}
135 static inline uint64_t SLittle(uint64_t val) {return val;}
136 static inline float SLittle(float val) {return val;}
137 static inline double SLittle(double val) {return val;}
138 #ifndef SLITTLE
139 #define SLITTLE(q) (q)
140 #endif
141 #else
142 static inline int16_t SLittle(int16_t val) {return bswap16(val);}
143 static inline uint16_t SLittle(uint16_t val) {return bswap16(val);}
144 static inline int32_t SLittle(int32_t val) {return bswap32(val);}
145 static inline uint32_t SLittle(uint32_t val) {return bswap32(val);}
146 static inline int64_t SLittle(int64_t val) {return bswap64(val);}
147 static inline uint64_t SLittle(uint64_t val) {return bswap64(val);}
148 static inline float SLittle(float val)
149 {
150  int32_t ival = bswap32(*((int32_t*)(&val)));
151  return *((float*)(&ival));
152 }
153 static inline double SLittle(double val)
154 {
155  int64_t ival = bswap64(*((int64_t*)(&val)));
156  return *((double*)(&ival));
157 }
158 #ifndef SLITTLE
159 #define SLITTLE(q) ( ( (q) & 0x000000FF ) << 24 | ( (q) & 0x0000FF00 ) << 8 \
160  | ( (q) & 0x00FF0000 ) >> 8 | ( (q) & 0xFF000000 ) >> 24 )
161 #endif
162 
163 static inline int16_t SBig(int16_t val) {return val;}
164 static inline uint16_t SBig(uint16_t val) {return val;}
165 static inline int32_t SBig(int32_t val) {return val;}
166 static inline uint32_t SBig(uint32_t val) {return val;}
167 static inline int64_t SBig(int64_t val) {return val;}
168 static inline uint64_t SBig(uint64_t val) {return val;}
169 static inline float SBig(float val) {return val;}
170 static inline double SBig(double val) {return val;}
171 #ifndef SBIG
172 #define SBIG(q) (q)
173 #endif
174 #endif
175 
176 }
177 
178 #endif // __AMUSE_COMMON_HPP__