chris@163
|
1 /**
|
chris@163
|
2 * Copyright (c) 2014, 2015, Enzien Audio Ltd.
|
chris@163
|
3 *
|
chris@163
|
4 * Permission to use, copy, modify, and/or distribute this software for any
|
chris@163
|
5 * purpose with or without fee is hereby granted, provided that the above
|
chris@163
|
6 * copyright notice and this permission notice appear in all copies.
|
chris@163
|
7 *
|
chris@163
|
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
chris@163
|
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
chris@163
|
10 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
chris@163
|
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
chris@163
|
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
chris@163
|
13 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
chris@163
|
14 * PERFORMANCE OF THIS SOFTWARE.
|
chris@163
|
15 */
|
chris@163
|
16
|
chris@163
|
17 #ifndef _HEAVY_UTILS_H_
|
chris@163
|
18 #define _HEAVY_UTILS_H_
|
chris@163
|
19
|
chris@163
|
20 // Type definitions
|
chris@163
|
21 #if _WIN32 || _WIN64 || WINAPI_FAMILY
|
chris@163
|
22 #define HV_WIN 1
|
chris@163
|
23 #include <stddef.h>
|
chris@163
|
24 #if defined (_MSC_VER)
|
chris@163
|
25 #define HV_MSVC 1
|
chris@163
|
26 #endif
|
chris@163
|
27 #define hv_size_t unsigned long
|
chris@163
|
28 #define hv_uint32_t unsigned int
|
chris@163
|
29 #define hv_uint16_t unsigned short
|
chris@163
|
30 #define hv_int32_t int
|
chris@163
|
31 #elif __APPLE__ && __MACH__
|
chris@163
|
32 #define HV_APPLE 1
|
chris@163
|
33 #include <stddef.h>
|
chris@163
|
34 #define hv_size_t size_t
|
chris@163
|
35 #define hv_uint32_t unsigned int
|
chris@163
|
36 #define hv_uint16_t unsigned short
|
chris@163
|
37 #define hv_int32_t int
|
chris@163
|
38 #elif __unix__ || __unix
|
chris@163
|
39 #define HV_UNIX 1
|
chris@163
|
40 #include <stddef.h>
|
chris@163
|
41 #include <stdint.h>
|
chris@163
|
42 #define hv_size_t size_t
|
chris@163
|
43 #define hv_uint32_t uint32_t
|
chris@163
|
44 #define hv_uint16_t uint16_t
|
chris@163
|
45 #define hv_int32_t int32_t
|
chris@163
|
46 #else
|
chris@163
|
47 #error Unsupported platform
|
chris@163
|
48 #endif
|
chris@163
|
49
|
chris@163
|
50 // Memory management
|
chris@163
|
51 extern void *hv_alloca(hv_size_t numbytes);
|
chris@163
|
52 extern void *hv_malloc(hv_size_t numbytes); // allocates memory on 16 byte boundaries and clears it to zero
|
chris@163
|
53 extern void hv_free(void *ptr); // frees aligned memory
|
chris@163
|
54 extern void *hv_realloc(void *ptr, hv_size_t numBytes);
|
chris@163
|
55 extern void *hv_memcpy(void *dest, const void *src, hv_size_t numbytes);
|
chris@163
|
56 extern void *hv_memset(void *ptr, hv_size_t numbytes); // sets everything to 0
|
chris@163
|
57
|
chris@163
|
58 // String handling
|
chris@163
|
59 extern hv_size_t hv_strlen(const char *str);
|
chris@163
|
60 extern char *hv_strncat(char *dest, const char *str, hv_size_t n);
|
chris@163
|
61 extern char *hv_strncpy(char *dest, const char *str, hv_size_t n);
|
chris@163
|
62 extern int hv_strcmp(const char *str1, const char *str2);
|
chris@163
|
63 extern int hv_strncmp(const char *str1, const char *str2, hv_size_t n);
|
chris@163
|
64 extern int hv_snprintf(char *dest, hv_size_t n, const char *format, ...);
|
chris@163
|
65
|
chris@163
|
66 // Math
|
chris@163
|
67 extern int hv_max_i(int x, int y);
|
chris@163
|
68 extern hv_size_t hv_max_ui(hv_size_t x, hv_size_t y);
|
chris@163
|
69 extern int hv_min_i(int x, int y);
|
chris@163
|
70 extern hv_size_t hv_min_ui(hv_size_t x, hv_size_t y);
|
chris@163
|
71 extern float hv_max_f(float x, float y);
|
chris@163
|
72 extern float hv_min_f(float x, float y);
|
chris@163
|
73 extern double hv_max_d(double x, double y);
|
chris@163
|
74 extern double hv_min_d(double x, double y);
|
chris@163
|
75 extern float hv_sin_f(float x);
|
chris@163
|
76 extern float hv_sinh_f(float x);
|
chris@163
|
77 extern float hv_cos_f(float x);
|
chris@163
|
78 extern float hv_cosh_f(float x);
|
chris@163
|
79 extern float hv_tan_f(float x);
|
chris@163
|
80 extern float hv_tanh_f(float x);
|
chris@163
|
81 extern float hv_asin_f(float x);
|
chris@163
|
82 extern float hv_asinh_f(float x);
|
chris@163
|
83 extern float hv_acos_f(float x);
|
chris@163
|
84 extern float hv_acosh_f(float x);
|
chris@163
|
85 extern float hv_atan_f(float x);
|
chris@163
|
86 extern float hv_atanh_f(float x);
|
chris@163
|
87 extern float hv_atan2_f(float x, float y);
|
chris@163
|
88 extern float hv_exp_f(float x);
|
chris@163
|
89 extern float hv_abs_f(float x);
|
chris@163
|
90 extern float hv_sqrt_f(float x);
|
chris@163
|
91 extern float hv_log_f(float x);
|
chris@163
|
92 extern float hv_log2_f(float x);
|
chris@163
|
93 extern float hv_log10_f(float x);
|
chris@163
|
94 extern float hv_ceil_f(float x);
|
chris@163
|
95 extern float hv_floor_f(float x);
|
chris@163
|
96 extern float hv_round_f(float x);
|
chris@163
|
97 extern float hv_pow_f(float x, float y);
|
chris@163
|
98 extern float hv_fma_f(float x, float y, float z);
|
chris@163
|
99
|
chris@163
|
100 // Utilities
|
chris@163
|
101 extern void hv_assert(int e);
|
chris@163
|
102 extern void hv_clear_buffer(float *b, int n);
|
chris@163
|
103 extern hv_uint32_t hv_min_max_log2(hv_uint32_t x);
|
chris@163
|
104
|
chris@163
|
105 // SIMD
|
chris@163
|
106 #ifndef HV_SIMD_NONE
|
chris@163
|
107 #define HV_SIMD_NEON __ARM_NEON__
|
chris@163
|
108 #define HV_SIMD_SSE (__SSE__ && __SSE2__ && __SSE3__ && __SSSE3__ && __SSE4_1__)
|
chris@163
|
109 #define HV_SIMD_AVX (__AVX__ && HV_SIMD_SSE) // it is required that if AVX exists then SSE will also be available
|
chris@163
|
110 #define HV_SIMD_FMA __FMA__
|
chris@163
|
111 #endif
|
chris@163
|
112
|
chris@163
|
113 #ifdef HV_WIN
|
chris@163
|
114 #include "Utils_windows.h"
|
chris@163
|
115 #elif HV_APPLE
|
chris@163
|
116 #include "Utils_mac.h"
|
chris@163
|
117 #elif HV_UNIX
|
chris@163
|
118 #include "Utils_unix.h"
|
chris@163
|
119 #else
|
chris@163
|
120 #error Unsupported platform
|
chris@163
|
121 #endif
|
chris@163
|
122
|
chris@163
|
123 #if HV_SIMD_NEON // NEON
|
chris@163
|
124 #define HV_N_SIMD 4
|
chris@163
|
125 #define hv_bufferf_t float32x4_t
|
chris@163
|
126 #define hv_bufferi_t int32x4_t
|
chris@163
|
127 #define hv_bInf_t float32x4_t
|
chris@163
|
128 #define hv_bOutf_t float32x4_t*
|
chris@163
|
129 #define hv_bIni_t int32x4_t
|
chris@163
|
130 #define hv_bOuti_t int32x4_t*
|
chris@163
|
131 #define VIf(_x) (_x)
|
chris@163
|
132 #define VOf(_x) (&_x)
|
chris@163
|
133 #define VIi(_x) (_x)
|
chris@163
|
134 #define VOi(_x) (&_x)
|
chris@163
|
135 #elif HV_SIMD_AVX // AVX
|
chris@163
|
136 #define HV_N_SIMD 8
|
chris@163
|
137 #define hv_bufferf_t __m256
|
chris@163
|
138 #define hv_bufferi_t __m256i
|
chris@163
|
139 #define hv_bInf_t __m256
|
chris@163
|
140 #define hv_bOutf_t __m256*
|
chris@163
|
141 #define hv_bIni_t __m256i
|
chris@163
|
142 #define hv_bOuti_t __m256i*
|
chris@163
|
143 #define VIf(_x) (_x)
|
chris@163
|
144 #define VOf(_x) (&_x)
|
chris@163
|
145 #define VIi(_x) (_x)
|
chris@163
|
146 #define VOi(_x) (&_x)
|
chris@163
|
147 #elif HV_SIMD_SSE // SSE
|
chris@163
|
148 #define HV_N_SIMD 4
|
chris@163
|
149 #define hv_bufferf_t __m128
|
chris@163
|
150 #define hv_bufferi_t __m128i
|
chris@163
|
151 #define hv_bInf_t __m128
|
chris@163
|
152 #define hv_bOutf_t __m128*
|
chris@163
|
153 #define hv_bIni_t __m128i
|
chris@163
|
154 #define hv_bOuti_t __m128i*
|
chris@163
|
155 #define VIf(_x) (_x)
|
chris@163
|
156 #define VOf(_x) (&_x)
|
chris@163
|
157 #define VIi(_x) (_x)
|
chris@163
|
158 #define VOi(_x) (&_x)
|
chris@163
|
159 #else // DEFAULT
|
chris@163
|
160 #define HV_N_SIMD 1
|
chris@163
|
161 #undef HV_SIMD_NONE
|
chris@163
|
162 #define HV_SIMD_NONE 1
|
chris@163
|
163 #define hv_bufferf_t float
|
chris@163
|
164 #define hv_bufferi_t int
|
chris@163
|
165 #define hv_bInf_t float
|
chris@163
|
166 #define hv_bOutf_t float*
|
chris@163
|
167 #define hv_bIni_t int
|
chris@163
|
168 #define hv_bOuti_t int*
|
chris@163
|
169 #define VIf(_x) (_x)
|
chris@163
|
170 #define VOf(_x) (&_x)
|
chris@163
|
171 #define VIi(_x) (_x)
|
chris@163
|
172 #define VOi(_x) (&_x)
|
chris@163
|
173 #endif
|
chris@163
|
174
|
chris@163
|
175 #define HV_N_SIMD_MASK (HV_N_SIMD-1)
|
chris@163
|
176
|
chris@163
|
177 #endif // _HEAVY_UTILS_H_
|