-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathmth_mask.awk
426 lines (387 loc) · 12.4 KB
/
mth_mask.awk
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
function print_hdrs()
{
print "\
/*\n\
* Copyright (c) 2017-2018, NVIDIA CORPORATION. All rights reserved.\n\
*\n\
* NVIDIA CORPORATION and its licensors retain all intellectual property\n\
* and proprietary rights in and to this software, related documentation\n\
* and any modifications thereto. Any use, reproduction, disclosure or\n\
* distribution of this software and related documentation without an express\n\
* license agreement from NVIDIA CORPORATION is strictly prohibited.\n\
*\n\
*/\n\
\n\n\
#ifdef __cplusplus\n\
extern \"C\" {\n\
#endif\n\n\
#include \"mth_intrinsics.h\" \n\
#include \"mth_tbldefs.h\" \n\
\n\n\
static const vrs4_t Csp1_4={1.0, 1.0, 1.0, 1.0}; \n\
static const vrd2_t Cdp1_2={1.0, 1.0}; \n\
static const vrs8_t Csp1_8={1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}; \n\
static const vrd4_t Cdp1_4={1.0, 1.0, 1.0, 1.0}; \n\
static const vrs16_t Csp1_16={1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, \n\
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}; \n\
static const vrd8_t Cdp1_8={1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}; \n\
\n\n\
#ifdef __cplusplus\n\
}\n\
#endif\n\
#if defined (TARGET_X8664) \n\
#include \"immintrin.h\" \n\
#elif defined (TARGET_LINUX_POWER) \n\
#include \"altivec.h\" \n\
#elif defined(TARGET_ARM64) \n\
#include \"arm64intrin.h\" \n\
#include <search.h> \n\
#else \n\
#include <stddef.h> \n\
#include <stdint.h> \n\
#include <search.h> \n\
#include <assert.h> \n\
#endif\n\
\n\
#if !defined(TARGET_X8664) && !defined(TARGET_LINUX_POWER) && !defined(TARGET_WIN_ARM64)\n\
static int u8nonzero(const void *a, const void *b) \n\
{ \n\
assert(!a); \n\
assert(b); \n\
return !(*((uint8_t *)b)); \n\
} \n\
\n\
static inline int is_zero(const void *val, size_t nmemb) \n\
{ \n\
return !lfind(NULL, val, &nmemb, sizeof(uint8_t), u8nonzero); \n\
} \n\
\n\
#endif \n\
\n\
"
}
function extern_c_begin()
{
if (TARGET == "ARM64") {
print "\n#ifdef __cplusplus"
print "extern \"C\" {"
print "#endif"
}
}
function extern_c_end()
{
if (TARGET == "ARM64") {
print "\n#ifdef __cplusplus"
print "}"
print "#endif"
}
}
function init_target_arrays()
{
if (TARGET == "POWER") {
divsd["fs"] = "vec_div(x, y)"
divsd["fd"] = "vec_div(x, y)"
divsd["rs"] = "vec_div(x, y)"
divsd["rd"] = "vec_div(x, y)"
# For some unexplained reason, the native and llvm compilers implements
# relaxed divide on POWER using reciprocal and a multiply.
divsd["rs"] = "vec_mul(x, vec_div(Csp1_" VL("s") ", y))"
divsd["rd"] = "vec_mul(x, vec_div(Cdp1_" VL("d") ", y))"
divsd["ps"] = "vec_div(x, y)"
divsd["pd"] = "vec_div(x, y)"
sqrtsd["fs"] = "vec_sqrt(x)"
sqrtsd["fd"] = "vec_sqrt(x)"
sqrtsd["rs"] = "vec_sqrt(x)"
sqrtsd["rd"] = "vec_sqrt(x)"
sqrtsd["ps"] = "vec_sqrt(x)"
sqrtsd["pd"] = "vec_sqrt(x)"
mask_all_zero = "(vec_all_eq(mask, vec_xor(mask,mask)) == 1)"
} else if (TARGET == "X8664") {
if (VLS == 4) {
_mm = "_mm"
__m = "__m128"
_si = "_si128"
} else if (VLS == 8) {
_mm = "_mm256"
__m = "__m256"
_si = "_si256"
} else {
_mm = "_mm512"
__m = "__m512"
_si = "_si512"
}
divsd["fs"] = _mm "_div_ps((" __m ")x, (" __m ")y)"
divsd["fd"] = _mm "_div_pd((" __m "d)x, (" __m "d)y)"
divsd["rs"] = _mm "_div_ps((" __m ")x, (" __m ")y)"
divsd["rd"] = _mm "_div_pd((" __m "d)x, (" __m "d)y)"
# For some unexplained reason, the native and llvm compilers implements
# relaxed divide on X86-64 using reciprocal and a multiply.
divsd["rs"] = _mm "_mul_ps((" __m ")x, " _mm "_div_ps(Csp1_" VL("s") ", ( " __m ")y))"
divsd["rd"] = _mm "_mul_pd((" __m "d)x, " _mm "_div_pd(Cdp1_" VL("d") ", ( " __m "d)y))"
divsd["ps"] = _mm "_div_ps((" __m ")x, (" __m ")y)"
divsd["pd"] = _mm "_div_pd((" __m "d)x, (" __m "d)y)"
sqrtsd["fs"] = _mm "_sqrt_ps((" __m ")x)"
sqrtsd["fd"] = _mm "_sqrt_pd((" __m "d)x)"
sqrtsd["rs"] = _mm "_sqrt_ps((" __m ")x)"
sqrtsd["rd"] = _mm "_sqrt_pd((" __m "d)x)"
sqrtsd["ps"] = _mm "_sqrt_ps((" __m ")x)"
sqrtsd["pd"] = _mm "_sqrt_pd((" __m "d)x)"
# For vector register size == 128, it would be faster to use the
# (_mm_testz_si128((__m128i)mask, _mm_set1_epi32(-1) == 1), but we
# compile mth_128mask.c for core2 processors (gcc -march=core2),
# and the ptest instruction (_mm_testz_si128()) is not available
# until SSE4.1.
# mask_all_zero = (VLS == 4) ? \
# "(_mm_movemask_ps((__m128) _mm_cmpeq_epi32((__m128i)mask, \
# _mm_xor_si128((__m128i)mask,(__m128i)mask))) == 15)" : \
# "(_mm256_testz_si256((__m256i)mask, _mm256_set1_epi32(-1)) == 1)"
if (VLS == 4) {
mask_all_zero = \
"(_mm_movemask_ps((__m128) _mm_cmpeq_epi32((__m128i)mask, " \
"_mm_xor_si128((__m128i)mask,(__m128i)mask))) == 15)"
} else if (VLS == 8) {
mask_all_zero = \
"(_mm256_testz_si256((__m256i)mask, _mm256_set1_epi32(-1)) == 1)"
} else {
mask_all_zero = \
"(_mm512_test_epi32_mask((__m512i)mask, _mm512_set1_epi32(-1)) == 0)"
}
} else if (TARGET == "ARM64" && TARGET_OS == "WIN") {
_mm = "_mm"
__m = "__m128"
divsd["fs"] = _mm "_div_ps((" __m ")x, (" __m ")y)"
divsd["fd"] = _mm "_div_pd((" __m "d)x, (" __m "d)y)"
divsd["rs"] = _mm "_div_ps((" __m ")x, (" __m ")y)"
divsd["rd"] = _mm "_div_pd((" __m "d)x, (" __m "d)y)"
divsd["rs"] = _mm "_mul_ps((" __m ")x, " _mm "_div_ps(Csp1_" VL("s") ", ( " __m ")y))"
divsd["rd"] = _mm "_mul_pd((" __m "d)x, " _mm "_div_pd(Cdp1_" VL("d") ", ( " __m "d)y))"
divsd["ps"] = _mm "_div_ps((" __m ")x, (" __m ")y)"
divsd["pd"] = _mm "_div_pd((" __m "d)x, (" __m "d)y)"
sqrtsd["fs"] = _mm "_sqrt_ps((" __m ")x)"
sqrtsd["fd"] = _mm "_sqrt_pd((" __m "d)x)"
sqrtsd["rs"] = _mm "_sqrt_ps((" __m ")x)"
sqrtsd["rd"] = _mm "_sqrt_pd((" __m "d)x)"
sqrtsd["ps"] = _mm "_sqrt_ps((" __m ")x)"
sqrtsd["pd"] = _mm "_sqrt_pd((" __m "d)x)"
mask_all_zero = \
"(_mm_movemask_epi32(_mm_cmpeq_epi32((__m128i)mask, " \
"_mm_xor_si128((__m128i)mask,(__m128i)mask))) == 15)"
} else {
divsd["fs"] = "((x) / (y))"
divsd["fd"] = "((x) / (y))"
divsd["rs"] = "((x) / (y))"
divsd["rd"] = "((x) / (y))"
divsd["rs"] = "((x) / (y))"
divsd["rd"] = "((x) / (y))"
divsd["ps"] = "((x) / (y))"
divsd["pd"] = "((x) / (y))"
sqrtsd["fs"] = "(assert(!\"vsqrt\"), x)"
sqrtsd["fd"] = "(assert(!\"vsqrt\"), x)"
sqrtsd["rs"] = "(assert(!\"vsqrt\"), x)"
sqrtsd["rd"] = "(assert(!\"vsqrt\"), x)"
sqrtsd["ps"] = "(assert(!\"vsqrt\"), x)"
sqrtsd["pd"] = "(assert(!\"vsqrt\"), x)"
mask_all_zero = "(is_zero(&mask, sizeof mask))"
}
frps["f"]= ""
frps["r"]= ""
frps["p"]= ""
sds["s"]= ""
sds["d"]= ""
iks["i"]= ""
iks["k"]= ""
}
function VL(sd)
{
return sd == "s" ? VLS : VLD
}
function VR_T(sd) {
return "vr" sd (sd == "s" ? VLS : VLD) "_t"
}
function VI_T(sd) {
return "vi" sd (sd == "s" ? VLS : VLD) "_t"
}
function arg_ne_0(yarg, a, b)
{
return yarg != 0 ? a : b
}
function func_r_decl(name, frp, sd, yarg)
{
extern_c_begin()
print "\n" VR_T(sd)
print "__" frp sd "_" name "_" VL(sd) "_mn" \
"(" VR_T(sd) " x" \
arg_ne_0(yarg, ", " VR_T(sd) " y", "") \
", " VI_T(sd) " mask)"
}
function func_rr_def(name, frp, sd, safeval, yarg) {
func_r_decl(name, frp, sd, yarg)
print "{"
print " " \
VR_T(sd) " (*fptr) (" VR_T(sd) \
arg_ne_0(yarg, ", " VR_T(sd), "") \
");"
print " (void) fptr;"
# X86-64 tests assume input vector is return if mask is all zero.
# print " if(" mask_all_zero ") return (" VR_T(sd) ")mask;"
print " if(" mask_all_zero ") return x;"
print " x = (" VR_T(sd) ")((((" VI_T(sd) ")x & mask))" \
arg_ne_0(safeval, " | ((" VI_T(sd) ")C" sd "p1_" VL(sd) " & ~mask)", "") \
");"
if (yarg != 0) {
print " y = (" VR_T(sd) ")((((" VI_T(sd) ")y & mask))" \
arg_ne_0(safeval, " | ((" VI_T(sd) ")C" sd "p1_" VL(sd) " & ~mask)", "") \
");"
}
if (name != "div" && name != "sqrt") {
print " fptr = (" VR_T(sd) "(*) (" VR_T(sd), \
(yarg != 0) ? ", " VR_T(sd) : "", \
")) MTH_DISPATCH_TBL[func_" name "][sv_" sd "v" VL(sd) "][frp_" frp "];"
print " return (fptr(x", (yarg != 0) ? ", y" : "", "));"
} else {
print " return (", (name == "div") ? divsd[frp sd] : sqrtsd[frp sd], ");"
}
print "}\n"
extern_c_end()
}
function func_pow_args_nomask(sd, is_scalar, ik, with_vars)
{
ll = VR_T(sd) arg_ne_0(with_vars, " x", "") ", "
if (is_scalar) {
ll = ll ((ik == "i") ? "int32_t" : "int64_t") arg_ne_0(with_vars, " iy", "")
} else {
if (sd == "s" && ik == "k") {
ll = ll VI_T("d") arg_ne_0(with_vars, " iyu", "") ", " \
VI_T("d") arg_ne_0(with_vars, " iyl", "")
} else {
ll = ll VI_T(ik == "i" ? "s" : "d") arg_ne_0(with_vars, " iy", "")
}
}
return ll
}
function func_pow_decl(name, frp, sd, is_scalar, ik)
{
extern_c_begin()
print "\n" VR_T(sd)
l = "__" frp sd "_" name arg_ne_0(is_scalar, ik"1", ik)"_" VL(sd) "_mn" "("
l = l func_pow_args_nomask(sd, is_scalar, ik, 1)
l = l ", " VI_T(sd) " mask)"
print l
}
function func_pow_def(name, frp, sd, is_scalar, ik)
{
func_pow_decl(name, frp, sd, is_scalar, ik)
print "{"
print " "\
VR_T(sd) " (*fptr) (" func_pow_args_nomask(sd, is_scalar, ik, 0) ");"
# X86-64 tests assume input vector is return if mask is all zero.
# print " if(" mask_all_zero ") return (" VR_T(sd) ")mask;"
print " if(" mask_all_zero ") return x;"
print " x = ("VR_T(sd) ")((" VI_T(sd) ")x & mask);"
if (is_scalar == 0) {
if((sd == "s" && ik == "i") || (sd == "d" && ik == "k")) {
print " iy = iy & mask;"
} else {
print " {\n"\
" int i;\n"\
" for (i = 0 ; i < " VL(sd) "; i++) {\n"\
" if (mask[i] == 0) {"
if (sd == "s") {
print " if(i < " VL(d) ") {\n"\
" iyu[i] = 0;\n"\
" } else {\n"\
" iyl[i-" VL(d) "] = 0;\n"\
" }"
} else {
print " iy[i] = 0;"\
}
print " }\n }\n }"
}
}
print " fptr = (" VR_T(sd) "(*) (" \
func_pow_args_nomask(sd, is_scalar, ik, 0) \
")) MTH_DISPATCH_TBL[func_" name arg_ne_0(is_scalar, ik"1", ik) \
"][sv_" sd "v" VL(sd) "][frp_" frp "];"
print " return (fptr(x, ", \
arg_ne_0(is_scalar == 0 && sd == "s" && ik == "k", "iyu, iyl", "iy") \
"));"
print "}"
extern_c_end()
}
function do_all_rr(name, safeval, yarg)
{
for (frp in frps) {
for (sd in sds) {
func_rr_def(name, frp, sd, safeval, yarg)
}
}
}
function do_all_pow_r2i()
{
for (frp in frps) {
for (sd in sds) {
for (ik in iks) {
func_pow_def("pow", frp, sd, 1, ik)
func_pow_def("pow", frp, sd, 0, ik)
}
}
}
}
BEGIN {
if (TARGET == "POWER") {
if (MAX_VREG_SIZE != 128) {
print "TARGET == POWER, MAX_VREG_SIZE must be 128"
exit(1)
}
} else if (TARGET == "ARM64") {
if (MAX_VREG_SIZE != 128) {
print "TARGET == ARM64, MAX_VREG_SIZE must be 128"
exit(1)
}
} else if (MAX_VREG_SIZE != 128 && MAX_VREG_SIZE != 256 && MAX_VREG_SIZE != 512) {
print "TARGET == X8664, MAX_VREG_SIZE must be either 128, 256, or 512"
exit(1)
}
if (MAX_VREG_SIZE == 128) {
VLS = 4
VLD = 2
} else if (MAX_VREG_SIZE == 256) {
VLS = 8
VLD = 4
} else {
VLS = 16
VLD = 8
}
# Initialize some associative arrays
init_target_arrays()
print_hdrs()
one_arg = 0
two_args = 1
do_all_rr("acos", 0, one_arg)
do_all_rr("asin", 0, one_arg)
do_all_rr("atan", 0, one_arg)
do_all_rr("atan2", 1, two_args)
do_all_rr("cos", 0, one_arg)
do_all_rr("sin", 0, one_arg)
do_all_rr("tan", 0, one_arg)
do_all_rr("sincos", 0, one_arg)
do_all_rr("cosh", 0, one_arg)
do_all_rr("sinh", 0, one_arg)
do_all_rr("tanh", 0, one_arg)
do_all_rr("exp", 0, one_arg)
do_all_rr("log", 1, one_arg)
do_all_rr("log10", 1, one_arg)
do_all_rr("pow", 0, two_args)
do_all_rr("div", 1, two_args)
do_all_rr("sqrt", 0, one_arg)
do_all_rr("mod", 1, two_args)
do_all_rr("aint", 0, one_arg)
do_all_rr("ceil", 0, one_arg)
do_all_rr("floor", 0, one_arg)
do_all_pow_r2i()
}