chris@162: /** chris@162: * Copyright (c) 2014, 2015, Enzien Audio Ltd. chris@162: * chris@162: * Permission to use, copy, modify, and/or distribute this software for any chris@162: * purpose with or without fee is hereby granted, provided that the above chris@162: * copyright notice and this permission notice appear in all copies. chris@162: * chris@162: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH chris@162: * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY chris@162: * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, chris@162: * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM chris@162: * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR chris@162: * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR chris@162: * PERFORMANCE OF THIS SOFTWARE. chris@162: */ chris@162: chris@162: #ifndef _HEAVY_UTILS_H_ chris@162: #define _HEAVY_UTILS_H_ chris@162: chris@162: // Type definitions chris@162: #if _WIN32 || _WIN64 || WINAPI_FAMILY chris@162: #define HV_WIN 1 chris@162: #include chris@162: #if defined (_MSC_VER) chris@162: #define HV_MSVC 1 chris@162: #endif chris@162: #define hv_size_t unsigned long chris@162: #define hv_uint32_t unsigned int chris@162: #define hv_uint16_t unsigned short chris@162: #define hv_int32_t int chris@162: #elif __APPLE__ && __MACH__ chris@162: #define HV_APPLE 1 chris@162: #include chris@162: #define hv_size_t size_t chris@162: #define hv_uint32_t unsigned int chris@162: #define hv_uint16_t unsigned short chris@162: #define hv_int32_t int chris@162: #elif __unix__ || __unix chris@162: #define HV_UNIX 1 chris@162: #include chris@162: #include chris@162: #define hv_size_t size_t chris@162: #define hv_uint32_t uint32_t chris@162: #define hv_uint16_t uint16_t chris@162: #define hv_int32_t int32_t chris@162: #else chris@162: #error Unsupported platform chris@162: #endif chris@162: chris@162: // Memory management chris@162: extern void *hv_alloca(hv_size_t numbytes); chris@162: extern void *hv_malloc(hv_size_t numbytes); // allocates memory on 16 byte boundaries and clears it to zero chris@162: extern void hv_free(void *ptr); // frees aligned memory chris@162: extern void *hv_realloc(void *ptr, hv_size_t numBytes); chris@162: extern void *hv_memcpy(void *dest, const void *src, hv_size_t numbytes); chris@162: extern void *hv_memset(void *ptr, hv_size_t numbytes); // sets everything to 0 chris@162: chris@162: // String handling chris@162: extern hv_size_t hv_strlen(const char *str); chris@162: extern char *hv_strncat(char *dest, const char *str, hv_size_t n); chris@162: extern char *hv_strncpy(char *dest, const char *str, hv_size_t n); chris@162: extern int hv_strcmp(const char *str1, const char *str2); chris@162: extern int hv_strncmp(const char *str1, const char *str2, hv_size_t n); chris@162: extern int hv_snprintf(char *dest, hv_size_t n, const char *format, ...); chris@162: chris@162: // Math chris@162: extern int hv_max_i(int x, int y); chris@162: extern hv_size_t hv_max_ui(hv_size_t x, hv_size_t y); chris@162: extern int hv_min_i(int x, int y); chris@162: extern hv_size_t hv_min_ui(hv_size_t x, hv_size_t y); chris@162: extern float hv_max_f(float x, float y); chris@162: extern float hv_min_f(float x, float y); chris@162: extern double hv_max_d(double x, double y); chris@162: extern double hv_min_d(double x, double y); chris@162: extern float hv_sin_f(float x); chris@162: extern float hv_sinh_f(float x); chris@162: extern float hv_cos_f(float x); chris@162: extern float hv_cosh_f(float x); chris@162: extern float hv_tan_f(float x); chris@162: extern float hv_tanh_f(float x); chris@162: extern float hv_asin_f(float x); chris@162: extern float hv_asinh_f(float x); chris@162: extern float hv_acos_f(float x); chris@162: extern float hv_acosh_f(float x); chris@162: extern float hv_atan_f(float x); chris@162: extern float hv_atanh_f(float x); chris@162: extern float hv_atan2_f(float x, float y); chris@162: extern float hv_exp_f(float x); chris@162: extern float hv_abs_f(float x); chris@162: extern float hv_sqrt_f(float x); chris@162: extern float hv_log_f(float x); chris@162: extern float hv_log2_f(float x); chris@162: extern float hv_log10_f(float x); chris@162: extern float hv_ceil_f(float x); chris@162: extern float hv_floor_f(float x); chris@162: extern float hv_round_f(float x); chris@162: extern float hv_pow_f(float x, float y); chris@162: extern float hv_fma_f(float x, float y, float z); chris@162: chris@162: // Utilities chris@162: extern void hv_assert(int e); chris@162: extern void hv_clear_buffer(float *b, int n); chris@162: extern hv_uint32_t hv_min_max_log2(hv_uint32_t x); chris@162: chris@162: // SIMD chris@162: #ifndef HV_SIMD_NONE chris@162: #define HV_SIMD_NEON __ARM_NEON__ chris@162: #define HV_SIMD_SSE (__SSE__ && __SSE2__ && __SSE3__ && __SSSE3__ && __SSE4_1__) chris@162: #define HV_SIMD_AVX (__AVX__ && HV_SIMD_SSE) // it is required that if AVX exists then SSE will also be available chris@162: #define HV_SIMD_FMA __FMA__ chris@162: #endif chris@162: chris@162: #ifdef HV_WIN chris@162: #include "Utils_windows.h" chris@162: #elif HV_APPLE chris@162: #include "Utils_mac.h" chris@162: #elif HV_UNIX chris@162: #include "Utils_unix.h" chris@162: #else chris@162: #error Unsupported platform chris@162: #endif chris@162: chris@162: #if HV_SIMD_NEON // NEON chris@162: #define HV_N_SIMD 4 chris@162: #define hv_bufferf_t float32x4_t chris@162: #define hv_bufferi_t int32x4_t chris@162: #define hv_bInf_t float32x4_t chris@162: #define hv_bOutf_t float32x4_t* chris@162: #define hv_bIni_t int32x4_t chris@162: #define hv_bOuti_t int32x4_t* chris@162: #define VIf(_x) (_x) chris@162: #define VOf(_x) (&_x) chris@162: #define VIi(_x) (_x) chris@162: #define VOi(_x) (&_x) chris@162: #elif HV_SIMD_AVX // AVX chris@162: #define HV_N_SIMD 8 chris@162: #define hv_bufferf_t __m256 chris@162: #define hv_bufferi_t __m256i chris@162: #define hv_bInf_t __m256 chris@162: #define hv_bOutf_t __m256* chris@162: #define hv_bIni_t __m256i chris@162: #define hv_bOuti_t __m256i* chris@162: #define VIf(_x) (_x) chris@162: #define VOf(_x) (&_x) chris@162: #define VIi(_x) (_x) chris@162: #define VOi(_x) (&_x) chris@162: #elif HV_SIMD_SSE // SSE chris@162: #define HV_N_SIMD 4 chris@162: #define hv_bufferf_t __m128 chris@162: #define hv_bufferi_t __m128i chris@162: #define hv_bInf_t __m128 chris@162: #define hv_bOutf_t __m128* chris@162: #define hv_bIni_t __m128i chris@162: #define hv_bOuti_t __m128i* chris@162: #define VIf(_x) (_x) chris@162: #define VOf(_x) (&_x) chris@162: #define VIi(_x) (_x) chris@162: #define VOi(_x) (&_x) chris@162: #else // DEFAULT chris@162: #define HV_N_SIMD 1 chris@162: #undef HV_SIMD_NONE chris@162: #define HV_SIMD_NONE 1 chris@162: #define hv_bufferf_t float chris@162: #define hv_bufferi_t int chris@162: #define hv_bInf_t float chris@162: #define hv_bOutf_t float* chris@162: #define hv_bIni_t int chris@162: #define hv_bOuti_t int* chris@162: #define VIf(_x) (_x) chris@162: #define VOf(_x) (&_x) chris@162: #define VIi(_x) (_x) chris@162: #define VOi(_x) (&_x) chris@162: #endif chris@162: chris@162: #define HV_N_SIMD_MASK (HV_N_SIMD-1) chris@162: chris@162: #endif // _HEAVY_UTILS_H_