chris@190: /** chris@190: * Copyright (c) 2014,2015,2016 Enzien Audio Ltd. chris@190: * chris@190: * Permission to use, copy, modify, and/or distribute this software for any chris@190: * purpose with or without fee is hereby granted, provided that the above chris@190: * copyright notice and this permission notice appear in all copies. chris@190: * chris@190: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH chris@190: * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY chris@190: * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, chris@190: * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM chris@190: * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR chris@190: * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR chris@190: * PERFORMANCE OF THIS SOFTWARE. chris@190: */ chris@190: chris@190: #ifndef _HEAVY_UTILS_H_ chris@190: #define _HEAVY_UTILS_H_ chris@190: chris@190: // platform definitions chris@190: #if _WIN32 || _WIN64 chris@190: #define HV_WIN 1 chris@190: #ifdef _MSC_VER chris@190: #define HV_MSVC 1 chris@190: #endif chris@190: #elif __APPLE__ chris@190: #define HV_APPLE 1 chris@190: #elif __unix__ || __unix chris@190: #define HV_UNIX 1 chris@190: #else chris@190: #warning Could not detect platform. Assuming Unix-like. chris@190: #endif chris@190: chris@190: // basic includes chris@190: #include chris@190: #include chris@190: #include chris@190: chris@190: // type definitions chris@190: #include chris@190: #include chris@190: #define hv_size_t size_t chris@190: #define hv_uint32_t uint32_t chris@190: #define hv_int32_t int32_t chris@190: #define hv_uint16_t uint16_t chris@190: chris@190: // SIMD-specific includes chris@190: #ifndef HV_SIMD_NONE chris@190: #define HV_SIMD_NEON __ARM_NEON__ chris@190: #define HV_SIMD_SSE (__SSE__ && __SSE2__ && __SSE3__ && __SSSE3__ && __SSE4_1__) chris@190: #define HV_SIMD_AVX (__AVX__ && HV_SIMD_SSE) chris@190: #define HV_SIMD_FMA __FMA__ chris@190: #endif chris@190: chris@190: #if HV_SIMD_AVX || HV_SIMD_SSE chris@190: #include chris@190: #elif HV_SIMD_NEON chris@190: #include chris@190: #endif chris@190: chris@190: #if HV_SIMD_NEON // NEON chris@190: #define HV_N_SIMD 4 chris@190: #define hv_bufferf_t float32x4_t chris@190: #define hv_bufferi_t int32x4_t chris@190: #define hv_bInf_t float32x4_t chris@190: #define hv_bOutf_t float32x4_t* chris@190: #define hv_bIni_t int32x4_t chris@190: #define hv_bOuti_t int32x4_t* chris@190: #define VIf(_x) (_x) chris@190: #define VOf(_x) (&_x) chris@190: #define VIi(_x) (_x) chris@190: #define VOi(_x) (&_x) chris@190: #elif HV_SIMD_AVX // AVX chris@190: #define HV_N_SIMD 8 chris@190: #define hv_bufferf_t __m256 chris@190: #define hv_bufferi_t __m256i chris@190: #define hv_bInf_t __m256 chris@190: #define hv_bOutf_t __m256* chris@190: #define hv_bIni_t __m256i chris@190: #define hv_bOuti_t __m256i* chris@190: #define VIf(_x) (_x) chris@190: #define VOf(_x) (&_x) chris@190: #define VIi(_x) (_x) chris@190: #define VOi(_x) (&_x) chris@190: #elif HV_SIMD_SSE // SSE chris@190: #define HV_N_SIMD 4 chris@190: #define hv_bufferf_t __m128 chris@190: #define hv_bufferi_t __m128i chris@190: #define hv_bInf_t __m128 chris@190: #define hv_bOutf_t __m128* chris@190: #define hv_bIni_t __m128i chris@190: #define hv_bOuti_t __m128i* chris@190: #define VIf(_x) (_x) chris@190: #define VOf(_x) (&_x) chris@190: #define VIi(_x) (_x) chris@190: #define VOi(_x) (&_x) chris@190: #else // DEFAULT chris@190: #define HV_N_SIMD 1 chris@190: #undef HV_SIMD_NONE chris@190: #define HV_SIMD_NONE 1 chris@190: #define hv_bufferf_t float chris@190: #define hv_bufferi_t int chris@190: #define hv_bInf_t float chris@190: #define hv_bOutf_t float* chris@190: #define hv_bIni_t int chris@190: #define hv_bOuti_t int* chris@190: #define VIf(_x) (_x) chris@190: #define VOf(_x) (&_x) chris@190: #define VIi(_x) (_x) chris@190: #define VOi(_x) (&_x) chris@190: #endif chris@190: chris@190: #define HV_N_SIMD_MASK (HV_N_SIMD-1) chris@190: chris@190: // Strings chris@190: #include chris@190: #define hv_strlen(a) strlen(a) chris@190: #define hv_strncpy(a, b, c) strncpy(a, b, c) chris@190: #define hv_strcmp(a, b) strcmp(a, b) chris@190: #define hv_snprintf(a, b, c, ...) snprintf(a, b, c, __VA_ARGS__) chris@190: chris@190: // Memory management chris@190: #define hv_realloc(a, b) realloc(a, b) chris@190: #define hv_memcpy(a, b, c) memcpy(a, b, c) chris@190: #define hv_memclear(a, b) memset(a, 0, b) chris@190: #if HV_MSVC chris@190: #include chris@190: #define hv_alloca(_n) _alloca(_n) chris@190: #if HV_SIMD_AVX chris@190: #define hv_malloc(_n) _aligned_malloc(_n, 32) chris@190: #define hv_free(x) _aligned_free(x) chris@190: #elif HV_SIMD_SSE || HV_SIMD_NEON chris@190: #define hv_malloc(_n) _aligned_malloc(_n, 16) chris@190: #define hv_free(x) _aligned_free(x) chris@190: #else // HV_SIMD_NONE chris@190: #define hv_malloc(_n) malloc(_n) chris@190: #define hv_free(_n) free(_n) chris@190: #endif chris@190: #elif HV_APPLE chris@190: #define hv_alloca(_n) alloca(_n) chris@190: #if HV_SIMD_AVX chris@190: #define hv_malloc(_n) _mm_malloc(_n, 32) chris@190: #define hv_free(x) _mm_free(x) chris@190: #elif HV_SIMD_SSE || HV_SIMD_NEON chris@190: #define hv_malloc(_n) _mm_malloc(_n, 16) chris@190: #define hv_free(x) _mm_free(x) chris@190: #else // HV_SIMD_NONE chris@190: #define hv_malloc(_n) malloc(_n) chris@190: #define hv_free(x) free(x) chris@190: #endif chris@190: #else chris@190: #include chris@190: #define hv_alloca(_n) alloca(_n) chris@190: #if HV_SIMD_AVX chris@190: #define hv_malloc(_n) aligned_alloc(32, _n) chris@190: #define hv_free(x) free(x) chris@190: #elif HV_SIMD_SSE || HV_SIMD_NEON chris@190: //#define hv_malloc(_n) aligned_alloc(16, _n) chris@190: #define hv_malloc(_n) memalign(16, _n) chris@190: #define hv_free(x) free(x) chris@190: #else // HV_SIMD_NONE chris@190: #define hv_malloc(_n) malloc(_n) chris@190: #define hv_free(_n) free(_n) chris@190: #endif chris@190: #endif chris@190: chris@190: // Assert chris@190: #include chris@190: #define hv_assert(e) assert(e) chris@190: chris@190: // Export and Inline chris@190: #if HV_MSVC chris@190: #define HV_EXPORT __declspec(dllexport) chris@190: #define inline __inline chris@190: #define HV_FORCE_INLINE __forceinline chris@190: #else chris@190: #define HV_EXPORT chris@190: #define HV_FORCE_INLINE inline __attribute__((always_inline)) chris@190: #endif chris@190: chris@190: // Math chris@190: #include chris@190: static inline hv_size_t __hv_utils_max_ui(hv_size_t x, hv_size_t y) { return (x > y) ? x : y; } chris@190: static inline hv_size_t __hv_utils_min_ui(hv_size_t x, hv_size_t y) { return (x < y) ? x : y; } chris@190: static inline hv_int32_t __hv_utils_max_i(hv_int32_t x, hv_int32_t y) { return (x > y) ? x : y; } chris@190: static inline hv_int32_t __hv_utils_min_i(hv_int32_t x, hv_int32_t y) { return (x < y) ? x : y; } chris@190: #define hv_max_ui(a, b) __hv_utils_max_ui(a, b) chris@190: #define hv_min_ui(a, b) __hv_utils_min_ui(a, b) chris@190: #define hv_max_i(a, b) __hv_utils_max_i(a, b) chris@190: #define hv_min_i(a, b) __hv_utils_min_i(a, b) chris@190: #define hv_max_f(a, b) fmaxf(a, b) chris@190: #define hv_min_f(a, b) fminf(a, b) chris@190: #define hv_max_d(a, b) fmax(a, b) chris@190: #define hv_min_d(a, b) fmin(a, b) chris@190: #define hv_sin_f(a) sinf(a) chris@190: #define hv_sinh_f(a) sinhf(a) chris@190: #define hv_cos_f(a) cosf(a) chris@190: #define hv_cosh_f(a) coshf(a) chris@190: #define hv_tan_f(a) tanf(a) chris@190: #define hv_tanh_f(a) tanhf(a) chris@190: #define hv_asin_f(a) asinf(a) chris@190: #define hv_asinh_f(a) asinhf(a) chris@190: #define hv_acos_f(a) acosf(a) chris@190: #define hv_acosh_f(a) acoshf(a) chris@190: #define hv_atan_f(a) atanf(a) chris@190: #define hv_atanh_f(a) atanhf(a) chris@190: #define hv_atan2_f(a, b) atan2f(a, b) chris@190: #define hv_exp_f(a) expf(a) chris@190: #define hv_abs_f(a) fabsf(a) chris@190: #define hv_sqrt_f(a) sqrtf(a) chris@190: #define hv_log_f(a) logf(a) chris@190: #define hv_log2_f(a) log2f(a) chris@190: #define hv_log10_f(a) log10f(a) chris@190: #define hv_ceil_f(a) ceilf(a) chris@190: #define hv_floor_f(a) floorf(a) chris@190: #define hv_round_f(a) roundf(a) chris@190: #define hv_pow_f(a, b) powf(a, b) chris@190: #define hv_fma_f(a, b, c) ((a*b)+c) // TODO(joe): use 'fmaf(a, b, c)' once emscripten supports it chris@190: #if HV_MSVC chris@190: // finds ceil(log2(x)) chris@190: #include chris@190: static inline hv_uint32_t __hv_utils_min_max_log2(hv_uint32_t x) { chris@190: unsigned long z = 0; chris@190: _BitScanReverse(&z, x); chris@190: return (hv_uint32_t) (z+1); chris@190: } chris@190: #else chris@190: static inline hv_uint32_t __hv_utils_min_max_log2(hv_uint32_t x) { chris@190: return (hv_uint32_t) (32 - __builtin_clz(x-1)); chris@190: } chris@190: #endif chris@190: #define hv_min_max_log2(a) __hv_utils_min_max_log2(a) chris@190: chris@190: #endif // _HEAVY_UTILS_H_