-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathCodeGen_C_prologue.template.cpp
More file actions
320 lines (298 loc) · 6.51 KB
/
CodeGen_C_prologue.template.cpp
File metadata and controls
320 lines (298 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* MACHINE GENERATED By Halide. */
#if !(__cplusplus >= 201103L || _MSVC_LANG >= 201103L)
#error "This code requires C++11 (or later); please upgrade your compiler."
#endif
#if !defined(__has_builtin)
#define __has_builtin(x) 0
#endif
#include <assert.h>
#include <fenv.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <type_traits>
extern "C" {
int64_t halide_current_time_ns(void *ctx);
void halide_profiler_pipeline_end(void *, void *);
struct halide_buffer_t;
char *halide_buffer_to_string(char *, char *, const halide_buffer_t *);
}
#ifdef _WIN32
#ifndef _MT
__declspec(dllimport) float __cdecl roundf(float);
__declspec(dllimport) double __cdecl round(double);
#endif
#else
inline float asinh_f32(float x) {
return asinhf(x);
}
inline float acosh_f32(float x) {
return acoshf(x);
}
inline float atanh_f32(float x) {
return atanhf(x);
}
inline double asinh_f64(double x) {
return asinh(x);
}
inline double acosh_f64(double x) {
return acosh(x);
}
inline double atanh_f64(double x) {
return atanh(x);
}
#endif
inline float sqrt_f32(float x) {
return sqrtf(x);
}
inline float sin_f32(float x) {
return sinf(x);
}
inline float asin_f32(float x) {
return asinf(x);
}
inline float cos_f32(float x) {
return cosf(x);
}
inline float acos_f32(float x) {
return acosf(x);
}
inline float tan_f32(float x) {
return tanf(x);
}
inline float atan_f32(float x) {
return atanf(x);
}
inline float atan2_f32(float x, float y) {
return atan2f(x, y);
}
inline float sinh_f32(float x) {
return sinhf(x);
}
inline float cosh_f32(float x) {
return coshf(x);
}
inline float tanh_f32(float x) {
return tanhf(x);
}
inline float hypot_f32(float x, float y) {
return hypotf(x, y);
}
inline float exp_f32(float x) {
return expf(x);
}
inline float log_f32(float x) {
return logf(x);
}
inline float pow_f32(float x, float y) {
return powf(x, y);
}
inline float floor_f32(float x) {
return floorf(x);
}
inline float ceil_f32(float x) {
return ceilf(x);
}
inline double sqrt_f64(double x) {
return sqrt(x);
}
inline double sin_f64(double x) {
return sin(x);
}
inline double asin_f64(double x) {
return asin(x);
}
inline double cos_f64(double x) {
return cos(x);
}
inline double acos_f64(double x) {
return acos(x);
}
inline double tan_f64(double x) {
return tan(x);
}
inline double atan_f64(double x) {
return atan(x);
}
inline double atan2_f64(double x, double y) {
return atan2(x, y);
}
inline double sinh_f64(double x) {
return sinh(x);
}
inline double cosh_f64(double x) {
return cosh(x);
}
inline double tanh_f64(double x) {
return tanh(x);
}
inline double hypot_f64(double x, double y) {
return hypot(x, y);
}
inline double exp_f64(double x) {
return exp(x);
}
inline double log_f64(double x) {
return log(x);
}
inline double pow_f64(double x, double y) {
return pow(x, y);
}
inline double floor_f64(double x) {
return floor(x);
}
inline double ceil_f64(double x) {
return ceil(x);
}
inline float nan_f32() {
return NAN;
}
inline float neg_inf_f32() {
return -INFINITY;
}
inline float inf_f32() {
return INFINITY;
}
inline bool is_nan_f32(float x) {
return isnan(x);
}
inline bool is_nan_f64(double x) {
return isnan(x);
}
inline bool is_inf_f32(float x) {
return isinf(x);
}
inline bool is_inf_f64(double x) {
return isinf(x);
}
inline bool is_finite_f32(float x) {
return isfinite(x);
}
inline bool is_finite_f64(double x) {
return isfinite(x);
}
template<typename A, typename B>
inline A reinterpret(const B &b) {
static_assert(sizeof(A) == sizeof(B), "type size mismatch");
A a;
memcpy(&a, &b, sizeof(a));
return a;
}
inline float float_from_bits(uint32_t bits) {
return reinterpret<float, uint32_t>(bits);
}
template<typename T>
inline int halide_popcount_fallback(T a) {
int bits_set = 0;
while (a != 0) {
bits_set += 1;
// See: https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
a &= a - 1;
}
return bits_set;
}
template<typename T>
inline int halide_popcount(T a) {
return halide_popcount_fallback<T>(a);
}
template<>
inline int halide_popcount<uint64_t>(uint64_t a) {
#ifdef _MSC_VER
#if defined(_WIN64)
return __popcnt64(a);
#else
return __popcnt((uint32_t)(a >> 32)) + __popcnt((uint32_t)(a & 0xffffffff));
#endif
#else
#if defined(__builtin_popcountll)
static_assert(sizeof(unsigned long long) >= sizeof(uint64_t), "");
return return __builtin_popcountll(a);
#else
return halide_popcount_fallback<uint64_t>(a);
#endif
#endif
}
template<typename T>
inline int halide_count_leading_zeros(T a) {
int leading_zeros = 0;
int bit = sizeof(a) * 8 - 1;
while (bit >= 0 && (a & (((T)1) << bit)) == 0) {
leading_zeros++;
bit--;
}
return leading_zeros;
}
template<typename T>
inline int halide_count_trailing_zeros(T a) {
int trailing_zeros = 0;
constexpr int bits = sizeof(a) * 8;
int bit = 0;
while (bit < bits && (a & (((T)1) << bit)) == 0) {
trailing_zeros++;
bit++;
}
return trailing_zeros;
}
template<typename T>
inline T halide_cpp_max(const T &a, const T &b) {
return (a > b) ? a : b;
}
template<typename T>
inline T halide_cpp_min(const T &a, const T &b) {
return (a < b) ? a : b;
}
template<typename T>
inline T halide_cpp_fma(const T &a, const T &b, const T &c) {
#if __has_builtin(__builtin_fma)
return __builtin_fma(a, b, c);
#else
if (sizeof(T) == sizeof(float)) {
return fmaf(a, b, c);
} else {
return (T)fma((double)a, (double)b, (double)c);
}
#endif
}
template<typename T>
inline T halide_cpp_fmod(const T &a, const T &b) {
#if __has_builtin(__builtin_fmod)
return __builtin_fmod(a, b);
#else
if (sizeof(T) == sizeof(float)) {
return fmod(a, b);
} else {
return (T)fmod((double)a, (double)b);
}
#endif
}
template<typename T>
inline void halide_maybe_unused(const T &) {
}
template<typename A, typename B>
const B &return_second(const A &a, const B &b) {
halide_maybe_unused(a);
return b;
}
namespace {
template<void (*FreeFn)(void *, void *)>
class HalideFreeHelper {
void *const user_context;
void *ptr;
public:
HalideFreeHelper(void *user_context, void *ptr)
: user_context(user_context), ptr(ptr) {
}
~HalideFreeHelper() {
free();
}
void free() {
if (ptr) {
FreeFn(user_context, ptr);
ptr = nullptr;
}
}
};
} // namespace