1 #ifndef __AMUSE_COMMON_HPP__ 2 #define __AMUSE_COMMON_HPP__ 19 static inline T clamp(T a, T val, T b) {
return std::max<T>(a, std::min<T>(b, val));}
22 static inline T ClampFull(
float in) {
return in;}
25 inline int16_t ClampFull<int16_t>(
float in)
29 else if (in > SHRT_MAX)
35 inline int32_t ClampFull<int32_t>(
float in)
39 else if (in > INT_MAX)
45 inline float ClampFull<float>(
float in)
55 #define M_PIF 3.14159265358979323846f 64 static inline T bswap16(T val)
67 return __builtin_bswap16(val);
69 return _byteswap_ushort(val);
71 return (val = (val << 8) | ((val >> 8) & 0xFF));
76 static inline T bswap32(T val)
79 return __builtin_bswap32(val);
81 return _byteswap_ulong(val);
83 val = (val & 0x0000FFFF) << 16 | (val & 0xFFFF0000) >> 16;
84 val = (val & 0x00FF00FF) << 8 | (val & 0xFF00FF00) >> 8;
90 static inline T bswap64(T val)
93 return __builtin_bswap64(val);
95 return _byteswap_uint64(val);
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);
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)
117 int32_t ival = bswap32(*((int32_t*)(&val)));
118 return *((
float*)(&ival));
120 static inline double SBig(
double val)
122 int64_t ival = bswap64(*((int64_t*)(&val)));
123 return *((
double*)(&ival));
126 #define SBIG(q) ( ( (q) & 0x000000FF ) << 24 | ( (q) & 0x0000FF00 ) << 8 \ 127 | ( (q) & 0x00FF0000 ) >> 8 | ( (q) & 0xFF000000 ) >> 24 ) 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;}
139 #define SLITTLE(q) (q) 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)
150 int32_t ival = bswap32(*((int32_t*)(&val)));
151 return *((
float*)(&ival));
153 static inline double SLittle(
double val)
155 int64_t ival = bswap64(*((int64_t*)(&val)));
156 return *((
double*)(&ival));
159 #define SLITTLE(q) ( ( (q) & 0x000000FF ) << 24 | ( (q) & 0x0000FF00 ) << 8 \ 160 | ( (q) & 0x00FF0000 ) >> 8 | ( (q) & 0xFF000000 ) >> 24 ) 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;}
178 #endif // __AMUSE_COMMON_HPP__