annotate 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
rev   line source
chris@190 1 /**
chris@190 2 * Copyright (c) 2014,2015,2016 Enzien Audio Ltd.
chris@190 3 *
chris@190 4 * Permission to use, copy, modify, and/or distribute this software for any
chris@190 5 * purpose with or without fee is hereby granted, provided that the above
chris@190 6 * copyright notice and this permission notice appear in all copies.
chris@190 7 *
chris@190 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
chris@190 9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
chris@190 10 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
chris@190 11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
chris@190 12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
chris@190 13 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
chris@190 14 * PERFORMANCE OF THIS SOFTWARE.
chris@190 15 */
chris@190 16
chris@190 17 #ifndef _HEAVY_UTILS_H_
chris@190 18 #define _HEAVY_UTILS_H_
chris@190 19
chris@190 20 // platform definitions
chris@190 21 #if _WIN32 || _WIN64
chris@190 22 #define HV_WIN 1
chris@190 23 #ifdef _MSC_VER
chris@190 24 #define HV_MSVC 1
chris@190 25 #endif
chris@190 26 #elif __APPLE__
chris@190 27 #define HV_APPLE 1
giuliomoro@292 28 #elif __ANDROID__
giuliomoro@292 29 #define HV_ANDROID 1
chris@190 30 #elif __unix__ || __unix
chris@190 31 #define HV_UNIX 1
chris@190 32 #else
chris@190 33 #warning Could not detect platform. Assuming Unix-like.
chris@190 34 #endif
chris@190 35
chris@190 36 // basic includes
chris@190 37 #include <stdarg.h>
chris@190 38 #include <stdio.h>
chris@190 39 #include <stdlib.h>
chris@190 40
chris@190 41 // type definitions
chris@190 42 #include <stdint.h>
chris@190 43 #include <stdbool.h>
chris@190 44 #define hv_size_t size_t
chris@190 45 #define hv_uint32_t uint32_t
chris@190 46 #define hv_int32_t int32_t
chris@190 47 #define hv_uint16_t uint16_t
chris@190 48
chris@190 49 // SIMD-specific includes
giuliomoro@292 50 #if !(HV_SIMD_NONE || HV_SIMD_NEON || HV_SIMD_SSE || HV_SIMD_AVX)
chris@190 51 #define HV_SIMD_NEON __ARM_NEON__
chris@190 52 #define HV_SIMD_SSE (__SSE__ && __SSE2__ && __SSE3__ && __SSSE3__ && __SSE4_1__)
chris@190 53 #define HV_SIMD_AVX (__AVX__ && HV_SIMD_SSE)
giuliomoro@292 54 #endif
giuliomoro@292 55 #ifndef HV_SIMD_FMA
chris@190 56 #define HV_SIMD_FMA __FMA__
chris@190 57 #endif
chris@190 58
chris@190 59 #if HV_SIMD_AVX || HV_SIMD_SSE
chris@190 60 #include <immintrin.h>
chris@190 61 #elif HV_SIMD_NEON
chris@190 62 #include <arm_neon.h>
chris@190 63 #endif
chris@190 64
chris@190 65 #if HV_SIMD_NEON // NEON
chris@190 66 #define HV_N_SIMD 4
chris@190 67 #define hv_bufferf_t float32x4_t
chris@190 68 #define hv_bufferi_t int32x4_t
chris@190 69 #define hv_bInf_t float32x4_t
chris@190 70 #define hv_bOutf_t float32x4_t*
chris@190 71 #define hv_bIni_t int32x4_t
chris@190 72 #define hv_bOuti_t int32x4_t*
chris@190 73 #define VIf(_x) (_x)
chris@190 74 #define VOf(_x) (&_x)
chris@190 75 #define VIi(_x) (_x)
chris@190 76 #define VOi(_x) (&_x)
chris@190 77 #elif HV_SIMD_AVX // AVX
chris@190 78 #define HV_N_SIMD 8
chris@190 79 #define hv_bufferf_t __m256
chris@190 80 #define hv_bufferi_t __m256i
chris@190 81 #define hv_bInf_t __m256
chris@190 82 #define hv_bOutf_t __m256*
chris@190 83 #define hv_bIni_t __m256i
chris@190 84 #define hv_bOuti_t __m256i*
chris@190 85 #define VIf(_x) (_x)
chris@190 86 #define VOf(_x) (&_x)
chris@190 87 #define VIi(_x) (_x)
chris@190 88 #define VOi(_x) (&_x)
chris@190 89 #elif HV_SIMD_SSE // SSE
chris@190 90 #define HV_N_SIMD 4
chris@190 91 #define hv_bufferf_t __m128
chris@190 92 #define hv_bufferi_t __m128i
chris@190 93 #define hv_bInf_t __m128
chris@190 94 #define hv_bOutf_t __m128*
chris@190 95 #define hv_bIni_t __m128i
chris@190 96 #define hv_bOuti_t __m128i*
chris@190 97 #define VIf(_x) (_x)
chris@190 98 #define VOf(_x) (&_x)
chris@190 99 #define VIi(_x) (_x)
chris@190 100 #define VOi(_x) (&_x)
chris@190 101 #else // DEFAULT
chris@190 102 #define HV_N_SIMD 1
chris@190 103 #undef HV_SIMD_NONE
chris@190 104 #define HV_SIMD_NONE 1
chris@190 105 #define hv_bufferf_t float
chris@190 106 #define hv_bufferi_t int
chris@190 107 #define hv_bInf_t float
chris@190 108 #define hv_bOutf_t float*
chris@190 109 #define hv_bIni_t int
chris@190 110 #define hv_bOuti_t int*
chris@190 111 #define VIf(_x) (_x)
chris@190 112 #define VOf(_x) (&_x)
chris@190 113 #define VIi(_x) (_x)
chris@190 114 #define VOi(_x) (&_x)
chris@190 115 #endif
chris@190 116
chris@190 117 #define HV_N_SIMD_MASK (HV_N_SIMD-1)
chris@190 118
chris@190 119 // Strings
chris@190 120 #include <string.h>
chris@190 121 #define hv_strlen(a) strlen(a)
chris@190 122 #define hv_strncpy(a, b, c) strncpy(a, b, c)
chris@190 123 #define hv_strcmp(a, b) strcmp(a, b)
chris@190 124 #define hv_snprintf(a, b, c, ...) snprintf(a, b, c, __VA_ARGS__)
chris@190 125
chris@190 126 // Memory management
chris@190 127 #define hv_realloc(a, b) realloc(a, b)
chris@190 128 #define hv_memcpy(a, b, c) memcpy(a, b, c)
chris@190 129 #define hv_memclear(a, b) memset(a, 0, b)
chris@190 130 #if HV_MSVC
chris@190 131 #include <malloc.h>
chris@190 132 #define hv_alloca(_n) _alloca(_n)
chris@190 133 #if HV_SIMD_AVX
chris@190 134 #define hv_malloc(_n) _aligned_malloc(_n, 32)
chris@190 135 #define hv_free(x) _aligned_free(x)
chris@190 136 #elif HV_SIMD_SSE || HV_SIMD_NEON
chris@190 137 #define hv_malloc(_n) _aligned_malloc(_n, 16)
chris@190 138 #define hv_free(x) _aligned_free(x)
chris@190 139 #else // HV_SIMD_NONE
chris@190 140 #define hv_malloc(_n) malloc(_n)
chris@190 141 #define hv_free(_n) free(_n)
chris@190 142 #endif
chris@190 143 #elif HV_APPLE
chris@190 144 #define hv_alloca(_n) alloca(_n)
chris@190 145 #if HV_SIMD_AVX
giuliomoro@292 146 #include <mm_malloc.h>
chris@190 147 #define hv_malloc(_n) _mm_malloc(_n, 32)
chris@190 148 #define hv_free(x) _mm_free(x)
giuliomoro@292 149 #elif HV_SIMD_SSE
giuliomoro@292 150 #include <mm_malloc.h>
chris@190 151 #define hv_malloc(_n) _mm_malloc(_n, 16)
chris@190 152 #define hv_free(x) _mm_free(x)
giuliomoro@292 153 #elif HV_SIMD_NEON
giuliomoro@292 154 // malloc on ios always has 16-byte alignment
giuliomoro@292 155 #define hv_malloc(_n) malloc(_n)
giuliomoro@292 156 #define hv_free(x) free(x)
chris@190 157 #else // HV_SIMD_NONE
chris@190 158 #define hv_malloc(_n) malloc(_n)
chris@190 159 #define hv_free(x) free(x)
chris@190 160 #endif
chris@190 161 #else
chris@190 162 #include <alloca.h>
chris@190 163 #define hv_alloca(_n) alloca(_n)
chris@190 164 #if HV_SIMD_AVX
chris@190 165 #define hv_malloc(_n) aligned_alloc(32, _n)
chris@190 166 #define hv_free(x) free(x)
giuliomoro@292 167 #elif HV_SIMD_SSE
giuliomoro@292 168 #define hv_malloc(_n) aligned_alloc(16, _n)
chris@190 169 #define hv_free(x) free(x)
giuliomoro@292 170 #elif HV_SIMD_NEON
giuliomoro@292 171 #if HV_ANDROID
giuliomoro@292 172 #define hv_malloc(_n) memalign(16, _n)
giuliomoro@292 173 #define hv_free(x) free(x)
giuliomoro@292 174 #else
giuliomoro@292 175 //#define hv_malloc(_n) aligned_alloc(16, _n)
giuliomoro@292 176 #define hv_malloc(_n) memalign(16, _n)
giuliomoro@292 177 #define hv_free(x) free(x)
giuliomoro@292 178 #endif
chris@190 179 #else // HV_SIMD_NONE
chris@190 180 #define hv_malloc(_n) malloc(_n)
chris@190 181 #define hv_free(_n) free(_n)
chris@190 182 #endif
chris@190 183 #endif
chris@190 184
chris@190 185 // Assert
chris@190 186 #include <assert.h>
chris@190 187 #define hv_assert(e) assert(e)
chris@190 188
chris@190 189 // Export and Inline
chris@190 190 #if HV_MSVC
chris@190 191 #define HV_EXPORT __declspec(dllexport)
chris@190 192 #define inline __inline
chris@190 193 #define HV_FORCE_INLINE __forceinline
chris@190 194 #else
chris@190 195 #define HV_EXPORT
chris@190 196 #define HV_FORCE_INLINE inline __attribute__((always_inline))
chris@190 197 #endif
chris@190 198
chris@190 199 // Math
chris@190 200 #include <math.h>
chris@190 201 static inline hv_size_t __hv_utils_max_ui(hv_size_t x, hv_size_t y) { return (x > y) ? x : y; }
chris@190 202 static inline hv_size_t __hv_utils_min_ui(hv_size_t x, hv_size_t y) { return (x < y) ? x : y; }
chris@190 203 static inline hv_int32_t __hv_utils_max_i(hv_int32_t x, hv_int32_t y) { return (x > y) ? x : y; }
chris@190 204 static inline hv_int32_t __hv_utils_min_i(hv_int32_t x, hv_int32_t y) { return (x < y) ? x : y; }
chris@190 205 #define hv_max_ui(a, b) __hv_utils_max_ui(a, b)
chris@190 206 #define hv_min_ui(a, b) __hv_utils_min_ui(a, b)
chris@190 207 #define hv_max_i(a, b) __hv_utils_max_i(a, b)
chris@190 208 #define hv_min_i(a, b) __hv_utils_min_i(a, b)
chris@190 209 #define hv_max_f(a, b) fmaxf(a, b)
chris@190 210 #define hv_min_f(a, b) fminf(a, b)
chris@190 211 #define hv_max_d(a, b) fmax(a, b)
chris@190 212 #define hv_min_d(a, b) fmin(a, b)
chris@190 213 #define hv_sin_f(a) sinf(a)
chris@190 214 #define hv_sinh_f(a) sinhf(a)
chris@190 215 #define hv_cos_f(a) cosf(a)
chris@190 216 #define hv_cosh_f(a) coshf(a)
chris@190 217 #define hv_tan_f(a) tanf(a)
chris@190 218 #define hv_tanh_f(a) tanhf(a)
chris@190 219 #define hv_asin_f(a) asinf(a)
chris@190 220 #define hv_asinh_f(a) asinhf(a)
chris@190 221 #define hv_acos_f(a) acosf(a)
chris@190 222 #define hv_acosh_f(a) acoshf(a)
chris@190 223 #define hv_atan_f(a) atanf(a)
chris@190 224 #define hv_atanh_f(a) atanhf(a)
chris@190 225 #define hv_atan2_f(a, b) atan2f(a, b)
chris@190 226 #define hv_exp_f(a) expf(a)
chris@190 227 #define hv_abs_f(a) fabsf(a)
chris@190 228 #define hv_sqrt_f(a) sqrtf(a)
chris@190 229 #define hv_log_f(a) logf(a)
giuliomoro@292 230 #if HV_ANDROID
giuliomoro@292 231 // NOTE(mhroth): for whatever silly reason, log2f is not defined!
giuliomoro@292 232 #define hv_log2_f(a) (1.44269504088896f*logf(a))
giuliomoro@292 233 #else
giuliomoro@292 234 #define hv_log2_f(a) log2f(a)
giuliomoro@292 235 #endif // HV_ANDROID
chris@190 236 #define hv_log10_f(a) log10f(a)
chris@190 237 #define hv_ceil_f(a) ceilf(a)
chris@190 238 #define hv_floor_f(a) floorf(a)
chris@190 239 #define hv_round_f(a) roundf(a)
chris@190 240 #define hv_pow_f(a, b) powf(a, b)
chris@190 241 #define hv_fma_f(a, b, c) ((a*b)+c) // TODO(joe): use 'fmaf(a, b, c)' once emscripten supports it
chris@190 242 #if HV_MSVC
chris@190 243 // finds ceil(log2(x))
chris@190 244 #include <intrin.h>
chris@190 245 static inline hv_uint32_t __hv_utils_min_max_log2(hv_uint32_t x) {
chris@190 246 unsigned long z = 0;
chris@190 247 _BitScanReverse(&z, x);
chris@190 248 return (hv_uint32_t) (z+1);
chris@190 249 }
chris@190 250 #else
chris@190 251 static inline hv_uint32_t __hv_utils_min_max_log2(hv_uint32_t x) {
chris@190 252 return (hv_uint32_t) (32 - __builtin_clz(x-1));
chris@190 253 }
chris@190 254 #endif
chris@190 255 #define hv_min_max_log2(a) __hv_utils_min_max_log2(a)
chris@190 256
giuliomoro@292 257 // Atomics
giuliomoro@292 258 #if HV_WIN
giuliomoro@292 259 #include <Windows.h>
giuliomoro@292 260 #define hv_atomic_bool volatile LONG
giuliomoro@292 261 #define HV_SPINLOCK_ACQUIRE(_x) \
giuliomoro@292 262 while (InterlockedCompareExchange(&_x, true, false)) { }
giuliomoro@292 263 #define HV_SPINLOCK_RELEASE(_x) (_x = false)
giuliomoro@292 264 #elif defined(__has_include)
giuliomoro@292 265 #if __has_include(<stdatomic.h>)
giuliomoro@292 266 #include <stdatomic.h>
giuliomoro@292 267 #define hv_atomic_bool volatile atomic_bool
giuliomoro@292 268 #define HV_SPINLOCK_ACQUIRE(_x) \
giuliomoro@292 269 bool expected = false; \
giuliomoro@292 270 while (!atomic_compare_exchange_strong(&_x, &expected, true)) { expected = false; }
giuliomoro@292 271 #define HV_SPINLOCK_RELEASE(_x) atomic_store(&_x, false)
giuliomoro@292 272 #else
giuliomoro@292 273 #define hv_atomic_bool volatile bool
giuliomoro@292 274 #define HV_SPINLOCK_ACQUIRE(_x) _x = true;
giuliomoro@292 275 #define HV_SPINLOCK_RELEASE(_x) _x = false;
giuliomoro@292 276 #endif
giuliomoro@292 277 #else
giuliomoro@292 278 #define hv_atomic_bool volatile bool
giuliomoro@292 279 #define HV_SPINLOCK_ACQUIRE(_x) _x = true;
giuliomoro@292 280 #define HV_SPINLOCK_RELEASE(_x) _x = false;
giuliomoro@292 281 #endif
giuliomoro@292 282
giuliomoro@292 283
chris@190 284 #endif // _HEAVY_UTILS_H_