comparison scripts/hvresources/HvUtils.h @ 190:3b8a28edae41

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