comparison scripts/hvresources/HvUtils.h @ 292:3d26df8a8a6c

Updated Heavy files to match latest Heavy API
author Giulio Moro <giuliomoro@yahoo.it>
date Tue, 24 May 2016 01:10:04 +0100
parents 3b8a28edae41
children b6eb94378ca9
comparison
equal deleted inserted replaced
291:46c12546664f 292:3d26df8a8a6c
23 #ifdef _MSC_VER 23 #ifdef _MSC_VER
24 #define HV_MSVC 1 24 #define HV_MSVC 1
25 #endif 25 #endif
26 #elif __APPLE__ 26 #elif __APPLE__
27 #define HV_APPLE 1 27 #define HV_APPLE 1
28 #elif __ANDROID__
29 #define HV_ANDROID 1
28 #elif __unix__ || __unix 30 #elif __unix__ || __unix
29 #define HV_UNIX 1 31 #define HV_UNIX 1
30 #else 32 #else
31 #warning Could not detect platform. Assuming Unix-like. 33 #warning Could not detect platform. Assuming Unix-like.
32 #endif 34 #endif
43 #define hv_uint32_t uint32_t 45 #define hv_uint32_t uint32_t
44 #define hv_int32_t int32_t 46 #define hv_int32_t int32_t
45 #define hv_uint16_t uint16_t 47 #define hv_uint16_t uint16_t
46 48
47 // SIMD-specific includes 49 // SIMD-specific includes
48 #ifndef HV_SIMD_NONE 50 #if !(HV_SIMD_NONE || HV_SIMD_NEON || HV_SIMD_SSE || HV_SIMD_AVX)
49 #define HV_SIMD_NEON __ARM_NEON__ 51 #define HV_SIMD_NEON __ARM_NEON__
50 #define HV_SIMD_SSE (__SSE__ && __SSE2__ && __SSE3__ && __SSSE3__ && __SSE4_1__) 52 #define HV_SIMD_SSE (__SSE__ && __SSE2__ && __SSE3__ && __SSSE3__ && __SSE4_1__)
51 #define HV_SIMD_AVX (__AVX__ && HV_SIMD_SSE) 53 #define HV_SIMD_AVX (__AVX__ && HV_SIMD_SSE)
54 #endif
55 #ifndef HV_SIMD_FMA
52 #define HV_SIMD_FMA __FMA__ 56 #define HV_SIMD_FMA __FMA__
53 #endif 57 #endif
54 58
55 #if HV_SIMD_AVX || HV_SIMD_SSE 59 #if HV_SIMD_AVX || HV_SIMD_SSE
56 #include <immintrin.h> 60 #include <immintrin.h>
137 #define hv_free(_n) free(_n) 141 #define hv_free(_n) free(_n)
138 #endif 142 #endif
139 #elif HV_APPLE 143 #elif HV_APPLE
140 #define hv_alloca(_n) alloca(_n) 144 #define hv_alloca(_n) alloca(_n)
141 #if HV_SIMD_AVX 145 #if HV_SIMD_AVX
146 #include <mm_malloc.h>
142 #define hv_malloc(_n) _mm_malloc(_n, 32) 147 #define hv_malloc(_n) _mm_malloc(_n, 32)
143 #define hv_free(x) _mm_free(x) 148 #define hv_free(x) _mm_free(x)
144 #elif HV_SIMD_SSE || HV_SIMD_NEON 149 #elif HV_SIMD_SSE
150 #include <mm_malloc.h>
145 #define hv_malloc(_n) _mm_malloc(_n, 16) 151 #define hv_malloc(_n) _mm_malloc(_n, 16)
146 #define hv_free(x) _mm_free(x) 152 #define hv_free(x) _mm_free(x)
153 #elif HV_SIMD_NEON
154 // malloc on ios always has 16-byte alignment
155 #define hv_malloc(_n) malloc(_n)
156 #define hv_free(x) free(x)
147 #else // HV_SIMD_NONE 157 #else // HV_SIMD_NONE
148 #define hv_malloc(_n) malloc(_n) 158 #define hv_malloc(_n) malloc(_n)
149 #define hv_free(x) free(x) 159 #define hv_free(x) free(x)
150 #endif 160 #endif
151 #else 161 #else
152 #include <alloca.h> 162 #include <alloca.h>
153 #define hv_alloca(_n) alloca(_n) 163 #define hv_alloca(_n) alloca(_n)
154 #if HV_SIMD_AVX 164 #if HV_SIMD_AVX
155 #define hv_malloc(_n) aligned_alloc(32, _n) 165 #define hv_malloc(_n) aligned_alloc(32, _n)
156 #define hv_free(x) free(x) 166 #define hv_free(x) free(x)
157 #elif HV_SIMD_SSE || HV_SIMD_NEON 167 #elif HV_SIMD_SSE
158 //#define hv_malloc(_n) aligned_alloc(16, _n) 168 #define hv_malloc(_n) aligned_alloc(16, _n)
159 #define hv_malloc(_n) memalign(16, _n) 169 #define hv_free(x) free(x)
160 #define hv_free(x) free(x) 170 #elif HV_SIMD_NEON
171 #if HV_ANDROID
172 #define hv_malloc(_n) memalign(16, _n)
173 #define hv_free(x) free(x)
174 #else
175 //#define hv_malloc(_n) aligned_alloc(16, _n)
176 #define hv_malloc(_n) memalign(16, _n)
177 #define hv_free(x) free(x)
178 #endif
161 #else // HV_SIMD_NONE 179 #else // HV_SIMD_NONE
162 #define hv_malloc(_n) malloc(_n) 180 #define hv_malloc(_n) malloc(_n)
163 #define hv_free(_n) free(_n) 181 #define hv_free(_n) free(_n)
164 #endif 182 #endif
165 #endif 183 #endif
207 #define hv_atan2_f(a, b) atan2f(a, b) 225 #define hv_atan2_f(a, b) atan2f(a, b)
208 #define hv_exp_f(a) expf(a) 226 #define hv_exp_f(a) expf(a)
209 #define hv_abs_f(a) fabsf(a) 227 #define hv_abs_f(a) fabsf(a)
210 #define hv_sqrt_f(a) sqrtf(a) 228 #define hv_sqrt_f(a) sqrtf(a)
211 #define hv_log_f(a) logf(a) 229 #define hv_log_f(a) logf(a)
212 #define hv_log2_f(a) log2f(a) 230 #if HV_ANDROID
231 // NOTE(mhroth): for whatever silly reason, log2f is not defined!
232 #define hv_log2_f(a) (1.44269504088896f*logf(a))
233 #else
234 #define hv_log2_f(a) log2f(a)
235 #endif // HV_ANDROID
213 #define hv_log10_f(a) log10f(a) 236 #define hv_log10_f(a) log10f(a)
214 #define hv_ceil_f(a) ceilf(a) 237 #define hv_ceil_f(a) ceilf(a)
215 #define hv_floor_f(a) floorf(a) 238 #define hv_floor_f(a) floorf(a)
216 #define hv_round_f(a) roundf(a) 239 #define hv_round_f(a) roundf(a)
217 #define hv_pow_f(a, b) powf(a, b) 240 #define hv_pow_f(a, b) powf(a, b)
229 return (hv_uint32_t) (32 - __builtin_clz(x-1)); 252 return (hv_uint32_t) (32 - __builtin_clz(x-1));
230 } 253 }
231 #endif 254 #endif
232 #define hv_min_max_log2(a) __hv_utils_min_max_log2(a) 255 #define hv_min_max_log2(a) __hv_utils_min_max_log2(a)
233 256
257 // Atomics
258 #if HV_WIN
259 #include <Windows.h>
260 #define hv_atomic_bool volatile LONG
261 #define HV_SPINLOCK_ACQUIRE(_x) \
262 while (InterlockedCompareExchange(&_x, true, false)) { }
263 #define HV_SPINLOCK_RELEASE(_x) (_x = false)
264 #elif defined(__has_include)
265 #if __has_include(<stdatomic.h>)
266 #include <stdatomic.h>
267 #define hv_atomic_bool volatile atomic_bool
268 #define HV_SPINLOCK_ACQUIRE(_x) \
269 bool expected = false; \
270 while (!atomic_compare_exchange_strong(&_x, &expected, true)) { expected = false; }
271 #define HV_SPINLOCK_RELEASE(_x) atomic_store(&_x, false)
272 #else
273 #define hv_atomic_bool volatile bool
274 #define HV_SPINLOCK_ACQUIRE(_x) _x = true;
275 #define HV_SPINLOCK_RELEASE(_x) _x = false;
276 #endif
277 #else
278 #define hv_atomic_bool volatile bool
279 #define HV_SPINLOCK_ACQUIRE(_x) _x = true;
280 #define HV_SPINLOCK_RELEASE(_x) _x = false;
281 #endif
282
283
234 #endif // _HEAVY_UTILS_H_ 284 #endif // _HEAVY_UTILS_H_