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
|
chris@190
|
28 #elif __unix__ || __unix
|
chris@190
|
29 #define HV_UNIX 1
|
chris@190
|
30 #else
|
chris@190
|
31 #warning Could not detect platform. Assuming Unix-like.
|
chris@190
|
32 #endif
|
chris@190
|
33
|
chris@190
|
34 // basic includes
|
chris@190
|
35 #include <stdarg.h>
|
chris@190
|
36 #include <stdio.h>
|
chris@190
|
37 #include <stdlib.h>
|
chris@190
|
38
|
chris@190
|
39 // type definitions
|
chris@190
|
40 #include <stdint.h>
|
chris@190
|
41 #include <stdbool.h>
|
chris@190
|
42 #define hv_size_t size_t
|
chris@190
|
43 #define hv_uint32_t uint32_t
|
chris@190
|
44 #define hv_int32_t int32_t
|
chris@190
|
45 #define hv_uint16_t uint16_t
|
chris@190
|
46
|
chris@190
|
47 // SIMD-specific includes
|
chris@190
|
48 #ifndef HV_SIMD_NONE
|
chris@190
|
49 #define HV_SIMD_NEON __ARM_NEON__
|
chris@190
|
50 #define HV_SIMD_SSE (__SSE__ && __SSE2__ && __SSE3__ && __SSSE3__ && __SSE4_1__)
|
chris@190
|
51 #define HV_SIMD_AVX (__AVX__ && HV_SIMD_SSE)
|
chris@190
|
52 #define HV_SIMD_FMA __FMA__
|
chris@190
|
53 #endif
|
chris@190
|
54
|
chris@190
|
55 #if HV_SIMD_AVX || HV_SIMD_SSE
|
chris@190
|
56 #include <immintrin.h>
|
chris@190
|
57 #elif HV_SIMD_NEON
|
chris@190
|
58 #include <arm_neon.h>
|
chris@190
|
59 #endif
|
chris@190
|
60
|
chris@190
|
61 #if HV_SIMD_NEON // NEON
|
chris@190
|
62 #define HV_N_SIMD 4
|
chris@190
|
63 #define hv_bufferf_t float32x4_t
|
chris@190
|
64 #define hv_bufferi_t int32x4_t
|
chris@190
|
65 #define hv_bInf_t float32x4_t
|
chris@190
|
66 #define hv_bOutf_t float32x4_t*
|
chris@190
|
67 #define hv_bIni_t int32x4_t
|
chris@190
|
68 #define hv_bOuti_t int32x4_t*
|
chris@190
|
69 #define VIf(_x) (_x)
|
chris@190
|
70 #define VOf(_x) (&_x)
|
chris@190
|
71 #define VIi(_x) (_x)
|
chris@190
|
72 #define VOi(_x) (&_x)
|
chris@190
|
73 #elif HV_SIMD_AVX // AVX
|
chris@190
|
74 #define HV_N_SIMD 8
|
chris@190
|
75 #define hv_bufferf_t __m256
|
chris@190
|
76 #define hv_bufferi_t __m256i
|
chris@190
|
77 #define hv_bInf_t __m256
|
chris@190
|
78 #define hv_bOutf_t __m256*
|
chris@190
|
79 #define hv_bIni_t __m256i
|
chris@190
|
80 #define hv_bOuti_t __m256i*
|
chris@190
|
81 #define VIf(_x) (_x)
|
chris@190
|
82 #define VOf(_x) (&_x)
|
chris@190
|
83 #define VIi(_x) (_x)
|
chris@190
|
84 #define VOi(_x) (&_x)
|
chris@190
|
85 #elif HV_SIMD_SSE // SSE
|
chris@190
|
86 #define HV_N_SIMD 4
|
chris@190
|
87 #define hv_bufferf_t __m128
|
chris@190
|
88 #define hv_bufferi_t __m128i
|
chris@190
|
89 #define hv_bInf_t __m128
|
chris@190
|
90 #define hv_bOutf_t __m128*
|
chris@190
|
91 #define hv_bIni_t __m128i
|
chris@190
|
92 #define hv_bOuti_t __m128i*
|
chris@190
|
93 #define VIf(_x) (_x)
|
chris@190
|
94 #define VOf(_x) (&_x)
|
chris@190
|
95 #define VIi(_x) (_x)
|
chris@190
|
96 #define VOi(_x) (&_x)
|
chris@190
|
97 #else // DEFAULT
|
chris@190
|
98 #define HV_N_SIMD 1
|
chris@190
|
99 #undef HV_SIMD_NONE
|
chris@190
|
100 #define HV_SIMD_NONE 1
|
chris@190
|
101 #define hv_bufferf_t float
|
chris@190
|
102 #define hv_bufferi_t int
|
chris@190
|
103 #define hv_bInf_t float
|
chris@190
|
104 #define hv_bOutf_t float*
|
chris@190
|
105 #define hv_bIni_t int
|
chris@190
|
106 #define hv_bOuti_t int*
|
chris@190
|
107 #define VIf(_x) (_x)
|
chris@190
|
108 #define VOf(_x) (&_x)
|
chris@190
|
109 #define VIi(_x) (_x)
|
chris@190
|
110 #define VOi(_x) (&_x)
|
chris@190
|
111 #endif
|
chris@190
|
112
|
chris@190
|
113 #define HV_N_SIMD_MASK (HV_N_SIMD-1)
|
chris@190
|
114
|
chris@190
|
115 // Strings
|
chris@190
|
116 #include <string.h>
|
chris@190
|
117 #define hv_strlen(a) strlen(a)
|
chris@190
|
118 #define hv_strncpy(a, b, c) strncpy(a, b, c)
|
chris@190
|
119 #define hv_strcmp(a, b) strcmp(a, b)
|
chris@190
|
120 #define hv_snprintf(a, b, c, ...) snprintf(a, b, c, __VA_ARGS__)
|
chris@190
|
121
|
chris@190
|
122 // Memory management
|
chris@190
|
123 #define hv_realloc(a, b) realloc(a, b)
|
chris@190
|
124 #define hv_memcpy(a, b, c) memcpy(a, b, c)
|
chris@190
|
125 #define hv_memclear(a, b) memset(a, 0, b)
|
chris@190
|
126 #if HV_MSVC
|
chris@190
|
127 #include <malloc.h>
|
chris@190
|
128 #define hv_alloca(_n) _alloca(_n)
|
chris@190
|
129 #if HV_SIMD_AVX
|
chris@190
|
130 #define hv_malloc(_n) _aligned_malloc(_n, 32)
|
chris@190
|
131 #define hv_free(x) _aligned_free(x)
|
chris@190
|
132 #elif HV_SIMD_SSE || HV_SIMD_NEON
|
chris@190
|
133 #define hv_malloc(_n) _aligned_malloc(_n, 16)
|
chris@190
|
134 #define hv_free(x) _aligned_free(x)
|
chris@190
|
135 #else // HV_SIMD_NONE
|
chris@190
|
136 #define hv_malloc(_n) malloc(_n)
|
chris@190
|
137 #define hv_free(_n) free(_n)
|
chris@190
|
138 #endif
|
chris@190
|
139 #elif HV_APPLE
|
chris@190
|
140 #define hv_alloca(_n) alloca(_n)
|
chris@190
|
141 #if HV_SIMD_AVX
|
chris@190
|
142 #define hv_malloc(_n) _mm_malloc(_n, 32)
|
chris@190
|
143 #define hv_free(x) _mm_free(x)
|
chris@190
|
144 #elif HV_SIMD_SSE || HV_SIMD_NEON
|
chris@190
|
145 #define hv_malloc(_n) _mm_malloc(_n, 16)
|
chris@190
|
146 #define hv_free(x) _mm_free(x)
|
chris@190
|
147 #else // HV_SIMD_NONE
|
chris@190
|
148 #define hv_malloc(_n) malloc(_n)
|
chris@190
|
149 #define hv_free(x) free(x)
|
chris@190
|
150 #endif
|
chris@190
|
151 #else
|
chris@190
|
152 #include <alloca.h>
|
chris@190
|
153 #define hv_alloca(_n) alloca(_n)
|
chris@190
|
154 #if HV_SIMD_AVX
|
chris@190
|
155 #define hv_malloc(_n) aligned_alloc(32, _n)
|
chris@190
|
156 #define hv_free(x) free(x)
|
chris@190
|
157 #elif HV_SIMD_SSE || HV_SIMD_NEON
|
chris@190
|
158 //#define hv_malloc(_n) aligned_alloc(16, _n)
|
chris@190
|
159 #define hv_malloc(_n) memalign(16, _n)
|
chris@190
|
160 #define hv_free(x) free(x)
|
chris@190
|
161 #else // HV_SIMD_NONE
|
chris@190
|
162 #define hv_malloc(_n) malloc(_n)
|
chris@190
|
163 #define hv_free(_n) free(_n)
|
chris@190
|
164 #endif
|
chris@190
|
165 #endif
|
chris@190
|
166
|
chris@190
|
167 // Assert
|
chris@190
|
168 #include <assert.h>
|
chris@190
|
169 #define hv_assert(e) assert(e)
|
chris@190
|
170
|
chris@190
|
171 // Export and Inline
|
chris@190
|
172 #if HV_MSVC
|
chris@190
|
173 #define HV_EXPORT __declspec(dllexport)
|
chris@190
|
174 #define inline __inline
|
chris@190
|
175 #define HV_FORCE_INLINE __forceinline
|
chris@190
|
176 #else
|
chris@190
|
177 #define HV_EXPORT
|
chris@190
|
178 #define HV_FORCE_INLINE inline __attribute__((always_inline))
|
chris@190
|
179 #endif
|
chris@190
|
180
|
chris@190
|
181 // Math
|
chris@190
|
182 #include <math.h>
|
chris@190
|
183 static inline hv_size_t __hv_utils_max_ui(hv_size_t x, hv_size_t y) { return (x > y) ? x : y; }
|
chris@190
|
184 static inline hv_size_t __hv_utils_min_ui(hv_size_t x, hv_size_t y) { return (x < y) ? x : y; }
|
chris@190
|
185 static inline hv_int32_t __hv_utils_max_i(hv_int32_t x, hv_int32_t y) { return (x > y) ? x : y; }
|
chris@190
|
186 static inline hv_int32_t __hv_utils_min_i(hv_int32_t x, hv_int32_t y) { return (x < y) ? x : y; }
|
chris@190
|
187 #define hv_max_ui(a, b) __hv_utils_max_ui(a, b)
|
chris@190
|
188 #define hv_min_ui(a, b) __hv_utils_min_ui(a, b)
|
chris@190
|
189 #define hv_max_i(a, b) __hv_utils_max_i(a, b)
|
chris@190
|
190 #define hv_min_i(a, b) __hv_utils_min_i(a, b)
|
chris@190
|
191 #define hv_max_f(a, b) fmaxf(a, b)
|
chris@190
|
192 #define hv_min_f(a, b) fminf(a, b)
|
chris@190
|
193 #define hv_max_d(a, b) fmax(a, b)
|
chris@190
|
194 #define hv_min_d(a, b) fmin(a, b)
|
chris@190
|
195 #define hv_sin_f(a) sinf(a)
|
chris@190
|
196 #define hv_sinh_f(a) sinhf(a)
|
chris@190
|
197 #define hv_cos_f(a) cosf(a)
|
chris@190
|
198 #define hv_cosh_f(a) coshf(a)
|
chris@190
|
199 #define hv_tan_f(a) tanf(a)
|
chris@190
|
200 #define hv_tanh_f(a) tanhf(a)
|
chris@190
|
201 #define hv_asin_f(a) asinf(a)
|
chris@190
|
202 #define hv_asinh_f(a) asinhf(a)
|
chris@190
|
203 #define hv_acos_f(a) acosf(a)
|
chris@190
|
204 #define hv_acosh_f(a) acoshf(a)
|
chris@190
|
205 #define hv_atan_f(a) atanf(a)
|
chris@190
|
206 #define hv_atanh_f(a) atanhf(a)
|
chris@190
|
207 #define hv_atan2_f(a, b) atan2f(a, b)
|
chris@190
|
208 #define hv_exp_f(a) expf(a)
|
chris@190
|
209 #define hv_abs_f(a) fabsf(a)
|
chris@190
|
210 #define hv_sqrt_f(a) sqrtf(a)
|
chris@190
|
211 #define hv_log_f(a) logf(a)
|
chris@190
|
212 #define hv_log2_f(a) log2f(a)
|
chris@190
|
213 #define hv_log10_f(a) log10f(a)
|
chris@190
|
214 #define hv_ceil_f(a) ceilf(a)
|
chris@190
|
215 #define hv_floor_f(a) floorf(a)
|
chris@190
|
216 #define hv_round_f(a) roundf(a)
|
chris@190
|
217 #define hv_pow_f(a, b) powf(a, b)
|
chris@190
|
218 #define hv_fma_f(a, b, c) ((a*b)+c) // TODO(joe): use 'fmaf(a, b, c)' once emscripten supports it
|
chris@190
|
219 #if HV_MSVC
|
chris@190
|
220 // finds ceil(log2(x))
|
chris@190
|
221 #include <intrin.h>
|
chris@190
|
222 static inline hv_uint32_t __hv_utils_min_max_log2(hv_uint32_t x) {
|
chris@190
|
223 unsigned long z = 0;
|
chris@190
|
224 _BitScanReverse(&z, x);
|
chris@190
|
225 return (hv_uint32_t) (z+1);
|
chris@190
|
226 }
|
chris@190
|
227 #else
|
chris@190
|
228 static inline hv_uint32_t __hv_utils_min_max_log2(hv_uint32_t x) {
|
chris@190
|
229 return (hv_uint32_t) (32 - __builtin_clz(x-1));
|
chris@190
|
230 }
|
chris@190
|
231 #endif
|
chris@190
|
232 #define hv_min_max_log2(a) __hv_utils_min_max_log2(a)
|
chris@190
|
233
|
chris@190
|
234 #endif // _HEAVY_UTILS_H_
|