rs_cl.c revision 54cd5d1771ea5c95e181befc66ef8e2a2c1b78cd
1#include "rs_core.rsh"
2
3extern float2 __attribute__((overloadable)) convert_float2(int2 c);
4extern float3 __attribute__((overloadable)) convert_float3(int3 c);
5extern float4 __attribute__((overloadable)) convert_float4(int4 c);
6
7extern int2 __attribute__((overloadable)) convert_int2(float2 c);
8extern int3 __attribute__((overloadable)) convert_int3(float3 c);
9extern int4 __attribute__((overloadable)) convert_int4(float4 c);
10
11
12extern float __attribute__((overloadable)) fmin(float v, float v2);
13extern float2 __attribute__((overloadable)) fmin(float2 v, float v2);
14extern float3 __attribute__((overloadable)) fmin(float3 v, float v2);
15extern float4 __attribute__((overloadable)) fmin(float4 v, float v2);
16
17extern float __attribute__((overloadable)) fmax(float v, float v2);
18extern float2 __attribute__((overloadable)) fmax(float2 v, float v2);
19extern float3 __attribute__((overloadable)) fmax(float3 v, float v2);
20extern float4 __attribute__((overloadable)) fmax(float4 v, float v2);
21
22// Float ops, 6.11.2
23
24#define FN_FUNC_FN(fnc)                                         \
25extern float2 __attribute__((overloadable)) fnc(float2 v) { \
26    float2 r;                                                   \
27    r.x = fnc(v.x);                                             \
28    r.y = fnc(v.y);                                             \
29    return r;                                                   \
30}                                                               \
31extern float3 __attribute__((overloadable)) fnc(float3 v) { \
32    float3 r;                                                   \
33    r.x = fnc(v.x);                                             \
34    r.y = fnc(v.y);                                             \
35    r.z = fnc(v.z);                                             \
36    return r;                                                   \
37}                                                               \
38extern float4 __attribute__((overloadable)) fnc(float4 v) { \
39    float4 r;                                                   \
40    r.x = fnc(v.x);                                             \
41    r.y = fnc(v.y);                                             \
42    r.z = fnc(v.z);                                             \
43    r.w = fnc(v.w);                                             \
44    return r;                                                   \
45}
46
47#define IN_FUNC_FN(fnc)                                         \
48extern int2 __attribute__((overloadable)) fnc(float2 v) {   \
49    int2 r;                                                     \
50    r.x = fnc(v.x);                                             \
51    r.y = fnc(v.y);                                             \
52    return r;                                                   \
53}                                                               \
54extern int3 __attribute__((overloadable)) fnc(float3 v) {   \
55    int3 r;                                                     \
56    r.x = fnc(v.x);                                             \
57    r.y = fnc(v.y);                                             \
58    r.z = fnc(v.z);                                             \
59    return r;                                                   \
60}                                                               \
61extern int4 __attribute__((overloadable)) fnc(float4 v) {   \
62    int4 r;                                                     \
63    r.x = fnc(v.x);                                             \
64    r.y = fnc(v.y);                                             \
65    r.z = fnc(v.z);                                             \
66    r.w = fnc(v.w);                                             \
67    return r;                                                   \
68}
69
70#define FN_FUNC_FN_FN(fnc)                                                  \
71extern float2 __attribute__((overloadable)) fnc(float2 v1, float2 v2) { \
72    float2 r;                                                               \
73    r.x = fnc(v1.x, v2.x);                                                  \
74    r.y = fnc(v1.y, v2.y);                                                  \
75    return r;                                                               \
76}                                                                           \
77extern float3 __attribute__((overloadable)) fnc(float3 v1, float3 v2) { \
78    float3 r;                                                               \
79    r.x = fnc(v1.x, v2.x);                                                  \
80    r.y = fnc(v1.y, v2.y);                                                  \
81    r.z = fnc(v1.z, v2.z);                                                  \
82    return r;                                                               \
83}                                                                           \
84extern float4 __attribute__((overloadable)) fnc(float4 v1, float4 v2) { \
85    float4 r;                                                               \
86    r.x = fnc(v1.x, v2.x);                                                  \
87    r.y = fnc(v1.y, v2.y);                                                  \
88    r.z = fnc(v1.z, v2.z);                                                  \
89    r.w = fnc(v1.w, v2.w);                                                  \
90    return r;                                                               \
91}
92
93#define FN_FUNC_FN_F(fnc)                                                   \
94extern float2 __attribute__((overloadable)) fnc(float2 v1, float v2) {  \
95    float2 r;                                                               \
96    r.x = fnc(v1.x, v2);                                                    \
97    r.y = fnc(v1.y, v2);                                                    \
98    return r;                                                               \
99}                                                                           \
100extern float3 __attribute__((overloadable)) fnc(float3 v1, float v2) {  \
101    float3 r;                                                               \
102    r.x = fnc(v1.x, v2);                                                    \
103    r.y = fnc(v1.y, v2);                                                    \
104    r.z = fnc(v1.z, v2);                                                    \
105    return r;                                                               \
106}                                                                           \
107extern float4 __attribute__((overloadable)) fnc(float4 v1, float v2) {  \
108    float4 r;                                                               \
109    r.x = fnc(v1.x, v2);                                                    \
110    r.y = fnc(v1.y, v2);                                                    \
111    r.z = fnc(v1.z, v2);                                                    \
112    r.w = fnc(v1.w, v2);                                                    \
113    return r;                                                               \
114}
115
116#define FN_FUNC_FN_IN(fnc)                                                  \
117extern float2 __attribute__((overloadable)) fnc(float2 v1, int2 v2) {   \
118    float2 r;                                                               \
119    r.x = fnc(v1.x, v2.x);                                                  \
120    r.y = fnc(v1.y, v2.y);                                                  \
121    return r;                                                               \
122}                                                                           \
123extern float3 __attribute__((overloadable)) fnc(float3 v1, int3 v2) {   \
124    float3 r;                                                               \
125    r.x = fnc(v1.x, v2.x);                                                  \
126    r.y = fnc(v1.y, v2.y);                                                  \
127    r.z = fnc(v1.z, v2.z);                                                  \
128    return r;                                                               \
129}                                                                           \
130extern float4 __attribute__((overloadable)) fnc(float4 v1, int4 v2) {   \
131    float4 r;                                                               \
132    r.x = fnc(v1.x, v2.x);                                                  \
133    r.y = fnc(v1.y, v2.y);                                                  \
134    r.z = fnc(v1.z, v2.z);                                                  \
135    r.w = fnc(v1.w, v2.w);                                                  \
136    return r;                                                               \
137}
138
139#define FN_FUNC_FN_I(fnc)                                                   \
140extern float2 __attribute__((overloadable)) fnc(float2 v1, int v2) {    \
141    float2 r;                                                               \
142    r.x = fnc(v1.x, v2);                                                    \
143    r.y = fnc(v1.y, v2);                                                    \
144    return r;                                                               \
145}                                                                           \
146extern float3 __attribute__((overloadable)) fnc(float3 v1, int v2) {    \
147    float3 r;                                                               \
148    r.x = fnc(v1.x, v2);                                                    \
149    r.y = fnc(v1.y, v2);                                                    \
150    r.z = fnc(v1.z, v2);                                                    \
151    return r;                                                               \
152}                                                                           \
153extern float4 __attribute__((overloadable)) fnc(float4 v1, int v2) {    \
154    float4 r;                                                               \
155    r.x = fnc(v1.x, v2);                                                    \
156    r.y = fnc(v1.y, v2);                                                    \
157    r.z = fnc(v1.z, v2);                                                    \
158    r.w = fnc(v1.w, v2);                                                    \
159    return r;                                                               \
160}
161
162#define FN_FUNC_FN_PFN(fnc)                     \
163extern float2 __attribute__((overloadable)) \
164        fnc(float2 v1, float2 *v2) {            \
165    float2 r;                                   \
166    float t[2];                                 \
167    r.x = fnc(v1.x, &t[0]);                     \
168    r.y = fnc(v1.y, &t[1]);                     \
169    v2->x = t[0];                               \
170    v2->y = t[1];                               \
171    return r;                                   \
172}                                               \
173extern float3 __attribute__((overloadable)) \
174        fnc(float3 v1, float3 *v2) {            \
175    float3 r;                                   \
176    float t[3];                                 \
177    r.x = fnc(v1.x, &t[0]);                     \
178    r.y = fnc(v1.y, &t[1]);                     \
179    r.z = fnc(v1.z, &t[2]);                     \
180    v2->x = t[0];                               \
181    v2->y = t[1];                               \
182    v2->z = t[2];                               \
183    return r;                                   \
184}                                               \
185extern float4 __attribute__((overloadable)) \
186        fnc(float4 v1, float4 *v2) {            \
187    float4 r;                                   \
188    float t[4];                                 \
189    r.x = fnc(v1.x, &t[0]);                     \
190    r.y = fnc(v1.y, &t[1]);                     \
191    r.z = fnc(v1.z, &t[2]);                     \
192    r.w = fnc(v1.w, &t[3]);                     \
193    v2->x = t[0];                               \
194    v2->y = t[1];                               \
195    v2->z = t[2];                               \
196    v2->w = t[3];                               \
197    return r;                                   \
198}
199
200#define FN_FUNC_FN_PIN(fnc)                                                 \
201extern float2 __attribute__((overloadable)) fnc(float2 v1, int2 *v2) {  \
202    float2 r;                                                               \
203    int t[2];                                                               \
204    r.x = fnc(v1.x, &t[0]);                                                 \
205    r.y = fnc(v1.y, &t[1]);                                                 \
206    v2->x = t[0];                                                           \
207    v2->y = t[1];                                                           \
208    return r;                                                               \
209}                                                                           \
210extern float3 __attribute__((overloadable)) fnc(float3 v1, int3 *v2) {  \
211    float3 r;                                                               \
212    int t[3];                                                               \
213    r.x = fnc(v1.x, &t[0]);                                                 \
214    r.y = fnc(v1.y, &t[1]);                                                 \
215    r.z = fnc(v1.z, &t[2]);                                                 \
216    v2->x = t[0];                                                           \
217    v2->y = t[1];                                                           \
218    v2->z = t[2];                                                           \
219    return r;                                                               \
220}                                                                           \
221extern float4 __attribute__((overloadable)) fnc(float4 v1, int4 *v2) {  \
222    float4 r;                                                               \
223    int t[4];                                                               \
224    r.x = fnc(v1.x, &t[0]);                                                 \
225    r.y = fnc(v1.y, &t[1]);                                                 \
226    r.z = fnc(v1.z, &t[2]);                                                 \
227    r.w = fnc(v1.w, &t[3]);                                                 \
228    v2->x = t[0];                                                           \
229    v2->y = t[1];                                                           \
230    v2->z = t[2];                                                           \
231    v2->w = t[3];                                                           \
232    return r;                                                               \
233}
234
235#define FN_FUNC_FN_FN_FN(fnc)                   \
236extern float2 __attribute__((overloadable)) \
237        fnc(float2 v1, float2 v2, float2 v3) {  \
238    float2 r;                                   \
239    r.x = fnc(v1.x, v2.x, v3.x);                \
240    r.y = fnc(v1.y, v2.y, v3.y);                \
241    return r;                                   \
242}                                               \
243extern float3 __attribute__((overloadable)) \
244        fnc(float3 v1, float3 v2, float3 v3) {  \
245    float3 r;                                   \
246    r.x = fnc(v1.x, v2.x, v3.x);                \
247    r.y = fnc(v1.y, v2.y, v3.y);                \
248    r.z = fnc(v1.z, v2.z, v3.z);                \
249    return r;                                   \
250}                                               \
251extern float4 __attribute__((overloadable)) \
252        fnc(float4 v1, float4 v2, float4 v3) {  \
253    float4 r;                                   \
254    r.x = fnc(v1.x, v2.x, v3.x);                \
255    r.y = fnc(v1.y, v2.y, v3.y);                \
256    r.z = fnc(v1.z, v2.z, v3.z);                \
257    r.w = fnc(v1.w, v2.w, v3.w);                \
258    return r;                                   \
259}
260
261#define FN_FUNC_FN_FN_PIN(fnc)                  \
262extern float2 __attribute__((overloadable)) \
263        fnc(float2 v1, float2 v2, int2 *v3) {   \
264    float2 r;                                   \
265    int t[2];                                   \
266    r.x = fnc(v1.x, v2.x, &t[0]);               \
267    r.y = fnc(v1.y, v2.y, &t[1]);               \
268    v3->x = t[0];                               \
269    v3->y = t[1];                               \
270    return r;                                   \
271}                                               \
272extern float3 __attribute__((overloadable)) \
273        fnc(float3 v1, float3 v2, int3 *v3) {   \
274    float3 r;                                   \
275    int t[3];                                   \
276    r.x = fnc(v1.x, v2.x, &t[0]);               \
277    r.y = fnc(v1.y, v2.y, &t[1]);               \
278    r.z = fnc(v1.z, v2.z, &t[2]);               \
279    v3->x = t[0];                               \
280    v3->y = t[1];                               \
281    v3->z = t[2];                               \
282    return r;                                   \
283}                                               \
284extern float4 __attribute__((overloadable)) \
285        fnc(float4 v1, float4 v2, int4 *v3) {   \
286    float4 r;                                   \
287    int t[4];                                   \
288    r.x = fnc(v1.x, v2.x, &t[0]);               \
289    r.y = fnc(v1.y, v2.y, &t[1]);               \
290    r.z = fnc(v1.z, v2.z, &t[2]);               \
291    r.w = fnc(v1.w, v2.w, &t[3]);               \
292    v3->x = t[0];                               \
293    v3->y = t[1];                               \
294    v3->z = t[2];                               \
295    v3->w = t[3];                               \
296    return r;                                   \
297}
298
299static const int iposinf = 0x7f800000;
300static const int ineginf = 0xff800000;
301
302static const float posinf() {
303    float f = *((float*)&iposinf);
304    return f;
305}
306
307static const float neginf() {
308    float f = *((float*)&ineginf);
309    return f;
310}
311
312static bool isinf(float f) {
313    int i = *((int*)(void*)&f);
314    return (i == iposinf) || (i == ineginf);
315}
316
317static bool isnan(float f) {
318    int i = *((int*)(void*)&f);
319    return (((i & 0x7f800000) == 0x7f800000) && (i & 0x007fffff));
320}
321
322static bool isposzero(float f) {
323    int i = *((int*)(void*)&f);
324    return (i == 0x00000000);
325}
326
327static bool isnegzero(float f) {
328    int i = *((int*)(void*)&f);
329    return (i == 0x80000000);
330}
331
332static bool iszero(float f) {
333    return isposzero(f) || isnegzero(f);
334}
335
336
337extern float __attribute__((overloadable)) acos(float);
338FN_FUNC_FN(acos)
339
340extern float __attribute__((overloadable)) acosh(float);
341FN_FUNC_FN(acosh)
342
343
344extern float __attribute__((overloadable)) acospi(float v) {
345    return acos(v) / M_PI;
346}
347FN_FUNC_FN(acospi)
348
349extern float __attribute__((overloadable)) asin(float);
350FN_FUNC_FN(asin)
351
352extern float __attribute__((overloadable)) asinh(float);
353FN_FUNC_FN(asinh)
354
355extern float __attribute__((overloadable)) asinpi(float v) {
356    return asin(v) / M_PI;
357}
358FN_FUNC_FN(asinpi)
359
360extern float __attribute__((overloadable)) atan(float);
361FN_FUNC_FN(atan)
362
363extern float __attribute__((overloadable)) atan2(float, float);
364FN_FUNC_FN_FN(atan2)
365
366extern float __attribute__((overloadable)) atanh(float);
367FN_FUNC_FN(atanh)
368
369extern float __attribute__((overloadable)) atanpi(float v) {
370    return atan(v) / M_PI;
371}
372FN_FUNC_FN(atanpi)
373
374
375extern float __attribute__((overloadable)) atan2pi(float y, float x) {
376    return atan2(y, x) / M_PI;
377}
378FN_FUNC_FN_FN(atan2pi)
379
380extern float __attribute__((overloadable)) cbrt(float);
381FN_FUNC_FN(cbrt)
382
383extern float __attribute__((overloadable)) ceil(float);
384FN_FUNC_FN(ceil)
385
386extern float __attribute__((overloadable)) copysign(float, float);
387FN_FUNC_FN_FN(copysign)
388
389extern float __attribute__((overloadable)) cos(float);
390FN_FUNC_FN(cos)
391
392extern float __attribute__((overloadable)) cosh(float);
393FN_FUNC_FN(cosh)
394
395extern float __attribute__((overloadable)) cospi(float v) {
396    return cos(v * M_PI);
397}
398FN_FUNC_FN(cospi)
399
400extern float __attribute__((overloadable)) erfc(float);
401FN_FUNC_FN(erfc)
402
403extern float __attribute__((overloadable)) erf(float);
404FN_FUNC_FN(erf)
405
406extern float __attribute__((overloadable)) exp(float);
407FN_FUNC_FN(exp)
408
409extern float __attribute__((overloadable)) exp2(float);
410FN_FUNC_FN(exp2)
411
412extern float __attribute__((overloadable)) pow(float, float);
413
414extern float __attribute__((overloadable)) exp10(float v) {
415    return exp2(v * 3.321928095f);
416}
417FN_FUNC_FN(exp10)
418
419extern float __attribute__((overloadable)) expm1(float);
420FN_FUNC_FN(expm1)
421
422extern float __attribute__((overloadable)) fabs(float v) {
423    int i = *((int*)(void*)&v) & 0x7fffffff;
424    return  *((float*)(void*)&i);
425}
426FN_FUNC_FN(fabs)
427
428extern float __attribute__((overloadable)) fdim(float, float);
429FN_FUNC_FN_FN(fdim)
430
431extern float __attribute__((overloadable)) floor(float);
432FN_FUNC_FN(floor)
433
434extern float __attribute__((overloadable)) fma(float, float, float);
435FN_FUNC_FN_FN_FN(fma)
436
437extern float __attribute__((overloadable)) fmin(float, float);
438
439extern float __attribute__((overloadable)) fmod(float, float);
440FN_FUNC_FN_FN(fmod)
441
442extern float __attribute__((overloadable)) fract(float v, float *iptr) {
443    int i = (int)floor(v);
444    if (iptr) {
445        iptr[0] = i;
446    }
447    return fmin(v - i, 0x1.fffffep-1f);
448}
449FN_FUNC_FN_PFN(fract)
450
451extern float __attribute__((overloadable)) frexp(float, int *);
452FN_FUNC_FN_PIN(frexp)
453
454extern float __attribute__((overloadable)) hypot(float, float);
455FN_FUNC_FN_FN(hypot)
456
457extern int __attribute__((overloadable)) ilogb(float);
458IN_FUNC_FN(ilogb)
459
460extern float __attribute__((overloadable)) ldexp(float, int);
461FN_FUNC_FN_IN(ldexp)
462FN_FUNC_FN_I(ldexp)
463
464extern float __attribute__((overloadable)) lgamma(float);
465FN_FUNC_FN(lgamma)
466extern float __attribute__((overloadable)) lgamma(float, int*);
467FN_FUNC_FN_PIN(lgamma)
468
469extern float __attribute__((overloadable)) log(float);
470FN_FUNC_FN(log)
471
472extern float __attribute__((overloadable)) log10(float);
473FN_FUNC_FN(log10)
474
475
476extern float __attribute__((overloadable)) log2(float v) {
477    return log10(v) * 3.321928095f;
478}
479FN_FUNC_FN(log2)
480
481extern float __attribute__((overloadable)) log1p(float);
482FN_FUNC_FN(log1p)
483
484extern float __attribute__((overloadable)) logb(float);
485FN_FUNC_FN(logb)
486
487extern float __attribute__((overloadable)) mad(float a, float b, float c) {
488    return a * b + c;
489}
490extern float2 __attribute__((overloadable)) mad(float2 a, float2 b, float2 c) {
491    return a * b + c;
492}
493extern float3 __attribute__((overloadable)) mad(float3 a, float3 b, float3 c) {
494    return a * b + c;
495}
496extern float4 __attribute__((overloadable)) mad(float4 a, float4 b, float4 c) {
497    return a * b + c;
498}
499
500extern float __attribute__((overloadable)) modf(float, float *);
501FN_FUNC_FN_PFN(modf);
502
503extern float __attribute__((overloadable)) nan(uint v) {
504    float f[1];
505    uint32_t *ip = (uint32_t *)f;
506    *ip = v | 0x7fc00000;
507    return f[0];
508}
509
510extern float __attribute__((overloadable)) nextafter(float, float);
511FN_FUNC_FN_FN(nextafter)
512
513FN_FUNC_FN_FN(pow)
514
515extern float __attribute__((overloadable)) pown(float v, int p) {
516    /* The mantissa of a float has fewer bits than an int (24 effective vs. 31).
517     * For very large ints, we'll lose whether the exponent is even or odd, making
518     * the selection of a correct sign incorrect.  We correct this.  Use copysign
519     * to handle the negative zero case.
520     */
521    float sign = (p & 0x1) ? copysign(1.f, v) : 1.f;
522    float f = pow(v, (float)p);
523    return copysign(f, sign);
524}
525FN_FUNC_FN_IN(pown)
526
527extern float __attribute__((overloadable)) powr(float v, float p) {
528    return pow(v, p);
529}
530extern float2 __attribute__((overloadable)) powr(float2 v, float2 p) {
531    return pow(v, p);
532}
533extern float3 __attribute__((overloadable)) powr(float3 v, float3 p) {
534    return pow(v, p);
535}
536extern float4 __attribute__((overloadable)) powr(float4 v, float4 p) {
537    return pow(v, p);
538}
539
540extern float __attribute__((overloadable)) remainder(float, float);
541FN_FUNC_FN_FN(remainder)
542
543extern float __attribute__((overloadable)) remquo(float, float, int *);
544FN_FUNC_FN_FN_PIN(remquo)
545
546extern float __attribute__((overloadable)) rint(float);
547FN_FUNC_FN(rint)
548
549extern float __attribute__((overloadable)) rootn(float v, int r) {
550    if (r == 0) {
551        return posinf();
552    }
553
554    if (iszero(v)) {
555        if (r < 0) {
556            if (r & 1) {
557                return copysign(posinf(), v);
558            } else {
559                return posinf();
560            }
561        } else {
562            if (r & 1) {
563                return copysign(0.f, v);
564            } else {
565                return 0.f;
566            }
567        }
568    }
569
570    if (!isinf(v) && !isnan(v) && (v < 0.f)) {
571        if (r & 1) {
572            return (-1.f * pow(-1.f * v, 1.f / r));
573        } else {
574            return nan(0);
575        }
576    }
577
578    return pow(v, 1.f / r);
579}
580FN_FUNC_FN_IN(rootn);
581
582extern float __attribute__((overloadable)) round(float);
583FN_FUNC_FN(round)
584
585
586extern float __attribute__((overloadable)) sqrt(float);
587extern float __attribute__((overloadable)) rsqrt(float v) {
588    return 1.f / sqrt(v);
589}
590
591#if !defined(ARCH_X86_HAVE_SSSE3) || defined(RS_DEBUG_RUNTIME)
592// These functions must be defined here if we are not using the SSE
593// implementation, which includes when we are built as part of the
594// debug runtime (libclcore_debug.bc).
595FN_FUNC_FN(sqrt)
596#else
597extern float2 __attribute__((overloadable)) sqrt(float2);
598extern float3 __attribute__((overloadable)) sqrt(float3);
599extern float4 __attribute__((overloadable)) sqrt(float4);
600#endif // !defined(ARCH_X86_HAVE_SSSE3) || defined(RS_DEBUG_RUNTIME)
601
602FN_FUNC_FN(rsqrt)
603
604extern float __attribute__((overloadable)) sin(float);
605FN_FUNC_FN(sin)
606
607extern float __attribute__((overloadable)) sincos(float v, float *cosptr) {
608    *cosptr = cos(v);
609    return sin(v);
610}
611extern float2 __attribute__((overloadable)) sincos(float2 v, float2 *cosptr) {
612    *cosptr = cos(v);
613    return sin(v);
614}
615extern float3 __attribute__((overloadable)) sincos(float3 v, float3 *cosptr) {
616    *cosptr = cos(v);
617    return sin(v);
618}
619extern float4 __attribute__((overloadable)) sincos(float4 v, float4 *cosptr) {
620    *cosptr = cos(v);
621    return sin(v);
622}
623
624extern float __attribute__((overloadable)) sinh(float);
625FN_FUNC_FN(sinh)
626
627extern float __attribute__((overloadable)) sinpi(float v) {
628    return sin(v * M_PI);
629}
630FN_FUNC_FN(sinpi)
631
632extern float __attribute__((overloadable)) tan(float);
633FN_FUNC_FN(tan)
634
635extern float __attribute__((overloadable)) tanh(float);
636FN_FUNC_FN(tanh)
637
638extern float __attribute__((overloadable)) tanpi(float v) {
639    return tan(v * M_PI);
640}
641FN_FUNC_FN(tanpi)
642
643
644extern float __attribute__((overloadable)) tgamma(float);
645FN_FUNC_FN(tgamma)
646
647extern float __attribute__((overloadable)) trunc(float);
648FN_FUNC_FN(trunc)
649
650// Int ops (partial), 6.11.3
651
652#define XN_FUNC_YN(typeout, fnc, typein)                                \
653extern typeout __attribute__((overloadable)) fnc(typein);               \
654extern typeout##2 __attribute__((overloadable)) fnc(typein##2 v) {  \
655    typeout##2 r;                                                       \
656    r.x = fnc(v.x);                                                     \
657    r.y = fnc(v.y);                                                     \
658    return r;                                                           \
659}                                                                       \
660extern typeout##3 __attribute__((overloadable)) fnc(typein##3 v) {  \
661    typeout##3 r;                                                       \
662    r.x = fnc(v.x);                                                     \
663    r.y = fnc(v.y);                                                     \
664    r.z = fnc(v.z);                                                     \
665    return r;                                                           \
666}                                                                       \
667extern typeout##4 __attribute__((overloadable)) fnc(typein##4 v) {  \
668    typeout##4 r;                                                       \
669    r.x = fnc(v.x);                                                     \
670    r.y = fnc(v.y);                                                     \
671    r.z = fnc(v.z);                                                     \
672    r.w = fnc(v.w);                                                     \
673    return r;                                                           \
674}
675
676
677#define UIN_FUNC_IN(fnc)          \
678XN_FUNC_YN(uchar, fnc, char)      \
679XN_FUNC_YN(ushort, fnc, short)    \
680XN_FUNC_YN(uint, fnc, int)
681
682#define IN_FUNC_IN(fnc)           \
683XN_FUNC_YN(uchar, fnc, uchar)     \
684XN_FUNC_YN(char, fnc, char)       \
685XN_FUNC_YN(ushort, fnc, ushort)   \
686XN_FUNC_YN(short, fnc, short)     \
687XN_FUNC_YN(uint, fnc, uint)       \
688XN_FUNC_YN(int, fnc, int)
689
690
691#define XN_FUNC_XN_XN_BODY(type, fnc, body)         \
692extern type __attribute__((overloadable))       \
693        fnc(type v1, type v2) {                     \
694    return body;                                    \
695}                                                   \
696extern type##2 __attribute__((overloadable))    \
697        fnc(type##2 v1, type##2 v2) {               \
698    type##2 r;                                      \
699    r.x = fnc(v1.x, v2.x);                          \
700    r.y = fnc(v1.y, v2.y);                          \
701    return r;                                       \
702}                                                   \
703extern type##3 __attribute__((overloadable))    \
704        fnc(type##3 v1, type##3 v2) {               \
705    type##3 r;                                      \
706    r.x = fnc(v1.x, v2.x);                          \
707    r.y = fnc(v1.y, v2.y);                          \
708    r.z = fnc(v1.z, v2.z);                          \
709    return r;                                       \
710}                                                   \
711extern type##4 __attribute__((overloadable))    \
712        fnc(type##4 v1, type##4 v2) {               \
713    type##4 r;                                      \
714    r.x = fnc(v1.x, v2.x);                          \
715    r.y = fnc(v1.y, v2.y);                          \
716    r.z = fnc(v1.z, v2.z);                          \
717    r.w = fnc(v1.w, v2.w);                          \
718    return r;                                       \
719}
720
721#define IN_FUNC_IN_IN_BODY(fnc, body) \
722XN_FUNC_XN_XN_BODY(uchar, fnc, body)  \
723XN_FUNC_XN_XN_BODY(char, fnc, body)   \
724XN_FUNC_XN_XN_BODY(ushort, fnc, body) \
725XN_FUNC_XN_XN_BODY(short, fnc, body)  \
726XN_FUNC_XN_XN_BODY(uint, fnc, body)   \
727XN_FUNC_XN_XN_BODY(int, fnc, body)    \
728XN_FUNC_XN_XN_BODY(float, fnc, body)
729
730
731/**
732 * abs
733 */
734extern uint32_t __attribute__((overloadable)) abs(int32_t v) {
735    if (v < 0)
736        return -v;
737    return v;
738}
739extern uint16_t __attribute__((overloadable)) abs(int16_t v) {
740    if (v < 0)
741        return -v;
742    return v;
743}
744extern uint8_t __attribute__((overloadable)) abs(int8_t v) {
745    if (v < 0)
746        return -v;
747    return v;
748}
749
750/**
751 * clz
752 * __builtin_clz only accepts a 32-bit unsigned int, so every input will be
753 * expanded to 32 bits. For our smaller data types, we need to subtract off
754 * these unused top bits (that will be always be composed of zeros).
755 */
756extern uint32_t __attribute__((overloadable)) clz(uint32_t v) {
757    return __builtin_clz(v);
758}
759extern uint16_t __attribute__((overloadable)) clz(uint16_t v) {
760    return __builtin_clz(v) - 16;
761}
762extern uint8_t __attribute__((overloadable)) clz(uint8_t v) {
763    return __builtin_clz(v) - 24;
764}
765extern int32_t __attribute__((overloadable)) clz(int32_t v) {
766    return __builtin_clz(v);
767}
768extern int16_t __attribute__((overloadable)) clz(int16_t v) {
769    return __builtin_clz(((uint32_t)v) & 0x0000ffff) - 16;
770}
771extern int8_t __attribute__((overloadable)) clz(int8_t v) {
772    return __builtin_clz(((uint32_t)v) & 0x000000ff) - 24;
773}
774
775
776UIN_FUNC_IN(abs)
777IN_FUNC_IN(clz)
778
779
780// 6.11.4
781
782
783extern float __attribute__((overloadable)) degrees(float radians) {
784    return radians * (180.f / M_PI);
785}
786extern float2 __attribute__((overloadable)) degrees(float2 radians) {
787    return radians * (180.f / M_PI);
788}
789extern float3 __attribute__((overloadable)) degrees(float3 radians) {
790    return radians * (180.f / M_PI);
791}
792extern float4 __attribute__((overloadable)) degrees(float4 radians) {
793    return radians * (180.f / M_PI);
794}
795
796extern float __attribute__((overloadable)) mix(float start, float stop, float amount) {
797    return start + (stop - start) * amount;
798}
799extern float2 __attribute__((overloadable)) mix(float2 start, float2 stop, float2 amount) {
800    return start + (stop - start) * amount;
801}
802extern float3 __attribute__((overloadable)) mix(float3 start, float3 stop, float3 amount) {
803    return start + (stop - start) * amount;
804}
805extern float4 __attribute__((overloadable)) mix(float4 start, float4 stop, float4 amount) {
806    return start + (stop - start) * amount;
807}
808extern float2 __attribute__((overloadable)) mix(float2 start, float2 stop, float amount) {
809    return start + (stop - start) * amount;
810}
811extern float3 __attribute__((overloadable)) mix(float3 start, float3 stop, float amount) {
812    return start + (stop - start) * amount;
813}
814extern float4 __attribute__((overloadable)) mix(float4 start, float4 stop, float amount) {
815    return start + (stop - start) * amount;
816}
817
818extern float __attribute__((overloadable)) radians(float degrees) {
819    return degrees * (M_PI / 180.f);
820}
821extern float2 __attribute__((overloadable)) radians(float2 degrees) {
822    return degrees * (M_PI / 180.f);
823}
824extern float3 __attribute__((overloadable)) radians(float3 degrees) {
825    return degrees * (M_PI / 180.f);
826}
827extern float4 __attribute__((overloadable)) radians(float4 degrees) {
828    return degrees * (M_PI / 180.f);
829}
830
831extern float __attribute__((overloadable)) step(float edge, float v) {
832    return (v < edge) ? 0.f : 1.f;
833}
834extern float2 __attribute__((overloadable)) step(float2 edge, float2 v) {
835    float2 r;
836    r.x = (v.x < edge.x) ? 0.f : 1.f;
837    r.y = (v.y < edge.y) ? 0.f : 1.f;
838    return r;
839}
840extern float3 __attribute__((overloadable)) step(float3 edge, float3 v) {
841    float3 r;
842    r.x = (v.x < edge.x) ? 0.f : 1.f;
843    r.y = (v.y < edge.y) ? 0.f : 1.f;
844    r.z = (v.z < edge.z) ? 0.f : 1.f;
845    return r;
846}
847extern float4 __attribute__((overloadable)) step(float4 edge, float4 v) {
848    float4 r;
849    r.x = (v.x < edge.x) ? 0.f : 1.f;
850    r.y = (v.y < edge.y) ? 0.f : 1.f;
851    r.z = (v.z < edge.z) ? 0.f : 1.f;
852    r.w = (v.w < edge.w) ? 0.f : 1.f;
853    return r;
854}
855extern float2 __attribute__((overloadable)) step(float2 edge, float v) {
856    float2 r;
857    r.x = (v < edge.x) ? 0.f : 1.f;
858    r.y = (v < edge.y) ? 0.f : 1.f;
859    return r;
860}
861extern float3 __attribute__((overloadable)) step(float3 edge, float v) {
862    float3 r;
863    r.x = (v < edge.x) ? 0.f : 1.f;
864    r.y = (v < edge.y) ? 0.f : 1.f;
865    r.z = (v < edge.z) ? 0.f : 1.f;
866    return r;
867}
868extern float4 __attribute__((overloadable)) step(float4 edge, float v) {
869    float4 r;
870    r.x = (v < edge.x) ? 0.f : 1.f;
871    r.y = (v < edge.y) ? 0.f : 1.f;
872    r.z = (v < edge.z) ? 0.f : 1.f;
873    r.w = (v < edge.w) ? 0.f : 1.f;
874    return r;
875}
876extern float2 __attribute__((overloadable)) step(float edge, float2 v) {
877    float2 r;
878    r.x = (v.x < edge) ? 0.f : 1.f;
879    r.y = (v.y < edge) ? 0.f : 1.f;
880    return r;
881}
882extern float3 __attribute__((overloadable)) step(float edge, float3 v) {
883    float3 r;
884    r.x = (v.x < edge) ? 0.f : 1.f;
885    r.y = (v.y < edge) ? 0.f : 1.f;
886    r.z = (v.z < edge) ? 0.f : 1.f;
887    return r;
888}
889extern float4 __attribute__((overloadable)) step(float edge, float4 v) {
890    float4 r;
891    r.x = (v.x < edge) ? 0.f : 1.f;
892    r.y = (v.y < edge) ? 0.f : 1.f;
893    r.z = (v.z < edge) ? 0.f : 1.f;
894    r.w = (v.w < edge) ? 0.f : 1.f;
895    return r;
896}
897
898extern float __attribute__((overloadable)) sign(float v) {
899    if (v > 0) return 1.f;
900    if (v < 0) return -1.f;
901    return v;
902}
903FN_FUNC_FN(sign)
904
905
906// 6.11.5
907extern float3 __attribute__((overloadable)) cross(float3 lhs, float3 rhs) {
908    float3 r;
909    r.x = lhs.y * rhs.z  - lhs.z * rhs.y;
910    r.y = lhs.z * rhs.x  - lhs.x * rhs.z;
911    r.z = lhs.x * rhs.y  - lhs.y * rhs.x;
912    return r;
913}
914
915extern float4 __attribute__((overloadable)) cross(float4 lhs, float4 rhs) {
916    float4 r;
917    r.x = lhs.y * rhs.z  - lhs.z * rhs.y;
918    r.y = lhs.z * rhs.x  - lhs.x * rhs.z;
919    r.z = lhs.x * rhs.y  - lhs.y * rhs.x;
920    r.w = 0.f;
921    return r;
922}
923
924#if !defined(ARCH_X86_HAVE_SSSE3) || defined(RS_DEBUG_RUNTIME)
925// These functions must be defined here if we are not using the SSE
926// implementation, which includes when we are built as part of the
927// debug runtime (libclcore_debug.bc).
928
929extern float __attribute__((overloadable)) dot(float lhs, float rhs) {
930    return lhs * rhs;
931}
932extern float __attribute__((overloadable)) dot(float2 lhs, float2 rhs) {
933    return lhs.x*rhs.x + lhs.y*rhs.y;
934}
935extern float __attribute__((overloadable)) dot(float3 lhs, float3 rhs) {
936    return lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z;
937}
938extern float __attribute__((overloadable)) dot(float4 lhs, float4 rhs) {
939    return lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z + lhs.w*rhs.w;
940}
941
942extern float __attribute__((overloadable)) length(float v) {
943    return fabs(v);
944}
945extern float __attribute__((overloadable)) length(float2 v) {
946    return sqrt(v.x*v.x + v.y*v.y);
947}
948extern float __attribute__((overloadable)) length(float3 v) {
949    return sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
950}
951extern float __attribute__((overloadable)) length(float4 v) {
952    return sqrt(v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w);
953}
954
955#else
956
957extern float __attribute__((overloadable)) length(float v);
958extern float __attribute__((overloadable)) length(float2 v);
959extern float __attribute__((overloadable)) length(float3 v);
960extern float __attribute__((overloadable)) length(float4 v);
961
962#endif // !defined(ARCH_X86_HAVE_SSSE3) || defined(RS_DEBUG_RUNTIME)
963
964extern float __attribute__((overloadable)) distance(float lhs, float rhs) {
965    return length(lhs - rhs);
966}
967extern float __attribute__((overloadable)) distance(float2 lhs, float2 rhs) {
968    return length(lhs - rhs);
969}
970extern float __attribute__((overloadable)) distance(float3 lhs, float3 rhs) {
971    return length(lhs - rhs);
972}
973extern float __attribute__((overloadable)) distance(float4 lhs, float4 rhs) {
974    return length(lhs - rhs);
975}
976
977/* For the normalization functions, vectors of length 0 should simply be
978 * returned (i.e. all the components of that vector are 0).
979 */
980extern float __attribute__((overloadable)) normalize(float v) {
981    if (v == 0.0f) {
982        return 0.0f;
983    } else if (v < 0.0f) {
984        return -1.0f;
985    } else {
986        return 1.0f;
987    }
988}
989extern float2 __attribute__((overloadable)) normalize(float2 v) {
990    float l = length(v);
991    return l == 0.0f ? v : v / l;
992}
993extern float3 __attribute__((overloadable)) normalize(float3 v) {
994    float l = length(v);
995    return l == 0.0f ? v : v / l;
996}
997extern float4 __attribute__((overloadable)) normalize(float4 v) {
998    float l = length(v);
999    return l == 0.0f ? v : v / l;
1000}
1001
1002extern float __attribute__((overloadable)) half_sqrt(float v) {
1003    return sqrt(v);
1004}
1005FN_FUNC_FN(half_sqrt)
1006
1007extern float __attribute__((overloadable)) fast_length(float v) {
1008    return fabs(v);
1009}
1010extern float __attribute__((overloadable)) fast_length(float2 v) {
1011    return half_sqrt(v.x*v.x + v.y*v.y);
1012}
1013extern float __attribute__((overloadable)) fast_length(float3 v) {
1014    return half_sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
1015}
1016extern float __attribute__((overloadable)) fast_length(float4 v) {
1017    return half_sqrt(v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w);
1018}
1019
1020extern float __attribute__((overloadable)) fast_distance(float lhs, float rhs) {
1021    return fast_length(lhs - rhs);
1022}
1023extern float __attribute__((overloadable)) fast_distance(float2 lhs, float2 rhs) {
1024    return fast_length(lhs - rhs);
1025}
1026extern float __attribute__((overloadable)) fast_distance(float3 lhs, float3 rhs) {
1027    return fast_length(lhs - rhs);
1028}
1029extern float __attribute__((overloadable)) fast_distance(float4 lhs, float4 rhs) {
1030    return fast_length(lhs - rhs);
1031}
1032
1033extern float __attribute__((overloadable)) half_rsqrt(float);
1034
1035/* For the normalization functions, vectors of length 0 should simply be
1036 * returned (i.e. all the components of that vector are 0).
1037 */
1038extern float __attribute__((overloadable)) fast_normalize(float v) {
1039    if (v == 0.0f) {
1040        return 0.0f;
1041    } else if (v < 0.0f) {
1042        return -1.0f;
1043    } else {
1044        return 1.0f;
1045    }
1046}
1047// If the length is 0, then rlength should be NaN.
1048extern float2 __attribute__((overloadable)) fast_normalize(float2 v) {
1049    float rlength = half_rsqrt(v.x*v.x + v.y*v.y);
1050    return (rlength == rlength) ? v * rlength : v;
1051}
1052extern float3 __attribute__((overloadable)) fast_normalize(float3 v) {
1053    float rlength = half_rsqrt(v.x*v.x + v.y*v.y + v.z*v.z);
1054    return (rlength == rlength) ? v * rlength : v;
1055}
1056extern float4 __attribute__((overloadable)) fast_normalize(float4 v) {
1057    float rlength = half_rsqrt(v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w);
1058    return (rlength == rlength) ? v * rlength : v;
1059}
1060
1061extern float __attribute__((overloadable)) half_recip(float v) {
1062    return 1.f / v;
1063}
1064
1065/*
1066extern float __attribute__((overloadable)) approx_atan(float x) {
1067    if (x == 0.f)
1068        return 0.f;
1069    if (x < 0.f)
1070        return -1.f * approx_atan(-1.f * x);
1071    if (x > 1.f)
1072        return M_PI_2 - approx_atan(approx_recip(x));
1073    return x * approx_recip(1.f + 0.28f * x*x);
1074}
1075FN_FUNC_FN(approx_atan)
1076*/
1077
1078typedef union
1079{
1080  float fv;
1081  int32_t iv;
1082} ieee_float_shape_type;
1083
1084/* Get a 32 bit int from a float.  */
1085
1086#define GET_FLOAT_WORD(i,d)                 \
1087do {                                \
1088  ieee_float_shape_type gf_u;                   \
1089  gf_u.fv = (d);                     \
1090  (i) = gf_u.iv;                      \
1091} while (0)
1092
1093/* Set a float from a 32 bit int.  */
1094
1095#define SET_FLOAT_WORD(d,i)                 \
1096do {                                \
1097  ieee_float_shape_type sf_u;                   \
1098  sf_u.iv = (i);                      \
1099  (d) = sf_u.fv;                     \
1100} while (0)
1101
1102
1103
1104// Valid -125 to 125
1105extern float __attribute__((overloadable)) native_exp2(float v) {
1106    int32_t iv = (int)v;
1107    int32_t x = iv + (iv >> 31); // ~floor(v)
1108    float r = (v - x);
1109
1110    float fo;
1111    SET_FLOAT_WORD(fo, (x + 127) << 23);
1112
1113    r *= 0.694f; // ~ log(e) / log(2)
1114    float r2 = r*r;
1115    float adj = 1.f + r + (r2 * 0.5f) + (r2*r * 0.166666f) + (r2*r2 * 0.0416666f);
1116    return fo * adj;
1117}
1118
1119extern float2 __attribute__((overloadable)) native_exp2(float2 v) {
1120    int2 iv = convert_int2(v);
1121    int2 x = iv + (iv >> (int2)31);//floor(v);
1122    float2 r = (v - convert_float2(x));
1123
1124    x += 127;
1125
1126    float2 fo = (float2)(x << (int2)23);
1127
1128    r *= 0.694f; // ~ log(e) / log(2)
1129    float2 r2 = r*r;
1130    float2 adj = 1.f + r + (r2 * 0.5f) + (r2*r * 0.166666f) + (r2*r2 * 0.0416666f);
1131    return fo * adj;
1132}
1133
1134extern float4 __attribute__((overloadable)) native_exp2(float4 v) {
1135    int4 iv = convert_int4(v);
1136    int4 x = iv + (iv >> (int4)31);//floor(v);
1137    float4 r = (v - convert_float4(x));
1138
1139    x += 127;
1140
1141    float4 fo = (float4)(x << (int4)23);
1142
1143    r *= 0.694f; // ~ log(e) / log(2)
1144    float4 r2 = r*r;
1145    float4 adj = 1.f + r + (r2 * 0.5f) + (r2*r * 0.166666f) + (r2*r2 * 0.0416666f);
1146    return fo * adj;
1147}
1148
1149extern float3 __attribute__((overloadable)) native_exp2(float3 v) {
1150    float4 t = 1.f;
1151    t.xyz = v;
1152    return native_exp2(t).xyz;
1153}
1154
1155
1156extern float __attribute__((overloadable)) native_exp(float v) {
1157    return native_exp2(v * 1.442695041f);
1158}
1159extern float2 __attribute__((overloadable)) native_exp(float2 v) {
1160    return native_exp2(v * 1.442695041f);
1161}
1162extern float3 __attribute__((overloadable)) native_exp(float3 v) {
1163    return native_exp2(v * 1.442695041f);
1164}
1165extern float4 __attribute__((overloadable)) native_exp(float4 v) {
1166    return native_exp2(v * 1.442695041f);
1167}
1168
1169extern float __attribute__((overloadable)) native_exp10(float v) {
1170    return native_exp2(v * 3.321928095f);
1171}
1172extern float2 __attribute__((overloadable)) native_exp10(float2 v) {
1173    return native_exp2(v * 3.321928095f);
1174}
1175extern float3 __attribute__((overloadable)) native_exp10(float3 v) {
1176    return native_exp2(v * 3.321928095f);
1177}
1178extern float4 __attribute__((overloadable)) native_exp10(float4 v) {
1179    return native_exp2(v * 3.321928095f);
1180}
1181
1182extern float __attribute__((overloadable)) native_log2(float v) {
1183    int32_t ibits;
1184    GET_FLOAT_WORD(ibits, v);
1185
1186    int32_t e = (ibits >> 23) & 0xff;
1187
1188    ibits &= 0x7fffff;
1189    ibits |= 127 << 23;
1190
1191    float ir;
1192    SET_FLOAT_WORD(ir, ibits);
1193    ir -= 1.5f;
1194    float ir2 = ir*ir;
1195    float adj2 = (0.405465108f / 0.693147181f) +
1196                 ((0.666666667f / 0.693147181f) * ir) -
1197                 ((0.222222222f / 0.693147181f) * ir2) +
1198                 ((0.098765432f / 0.693147181f) * ir*ir2) -
1199                 ((0.049382716f / 0.693147181f) * ir2*ir2) +
1200                 ((0.026337449f / 0.693147181f) * ir*ir2*ir2) -
1201                 ((0.014631916f / 0.693147181f) * ir2*ir2*ir2);
1202    return (float)(e - 127) + adj2;
1203}
1204extern float2 __attribute__((overloadable)) native_log2(float2 v) {
1205    float2 v2 = {native_log2(v.x), native_log2(v.y)};
1206    return v2;
1207}
1208extern float3 __attribute__((overloadable)) native_log2(float3 v) {
1209    float3 v2 = {native_log2(v.x), native_log2(v.y), native_log2(v.z)};
1210    return v2;
1211}
1212extern float4 __attribute__((overloadable)) native_log2(float4 v) {
1213    float4 v2 = {native_log2(v.x), native_log2(v.y), native_log2(v.z), native_log2(v.w)};
1214    return v2;
1215}
1216
1217extern float __attribute__((overloadable)) native_log(float v) {
1218    return native_log2(v) * (1.f / 1.442695041f);
1219}
1220extern float2 __attribute__((overloadable)) native_log(float2 v) {
1221    return native_log2(v) * (1.f / 1.442695041f);
1222}
1223extern float3 __attribute__((overloadable)) native_log(float3 v) {
1224    return native_log2(v) * (1.f / 1.442695041f);
1225}
1226extern float4 __attribute__((overloadable)) native_log(float4 v) {
1227    return native_log2(v) * (1.f / 1.442695041f);
1228}
1229
1230extern float __attribute__((overloadable)) native_log10(float v) {
1231    return native_log2(v) * (1.f / 3.321928095f);
1232}
1233extern float2 __attribute__((overloadable)) native_log10(float2 v) {
1234    return native_log2(v) * (1.f / 3.321928095f);
1235}
1236extern float3 __attribute__((overloadable)) native_log10(float3 v) {
1237    return native_log2(v) * (1.f / 3.321928095f);
1238}
1239extern float4 __attribute__((overloadable)) native_log10(float4 v) {
1240    return native_log2(v) * (1.f / 3.321928095f);
1241}
1242
1243
1244extern float __attribute__((overloadable)) native_powr(float v, float y) {
1245    float v2 = native_log2(v);
1246    v2 = fmax(v2 * y, -125.f);
1247    return native_exp2(v2);
1248}
1249extern float2 __attribute__((overloadable)) native_powr(float2 v, float2 y) {
1250    float2 v2 = native_log2(v);
1251    v2 = fmax(v2 * y, -125.f);
1252    return native_exp2(v2);
1253}
1254extern float3 __attribute__((overloadable)) native_powr(float3 v, float3 y) {
1255    float3 v2 = native_log2(v);
1256    v2 = fmax(v2 * y, -125.f);
1257    return native_exp2(v2);
1258}
1259extern float4 __attribute__((overloadable)) native_powr(float4 v, float4 y) {
1260    float4 v2 = native_log2(v);
1261    v2 = fmax(v2 * y, -125.f);
1262    return native_exp2(v2);
1263}
1264
1265extern double __attribute__((overloadable)) min(double v1, double v2) {
1266    return v1 < v2 ? v1 : v2;
1267}
1268
1269extern double2 __attribute__((overloadable)) min(double2 v1, double2 v2) {
1270    double2 r;
1271    r.x = v1.x < v2.x ? v1.x : v2.x;
1272    r.y = v1.y < v2.y ? v1.y : v2.y;
1273    return r;
1274}
1275
1276extern double3 __attribute__((overloadable)) min(double3 v1, double3 v2) {
1277    double3 r;
1278    r.x = v1.x < v2.x ? v1.x : v2.x;
1279    r.y = v1.y < v2.y ? v1.y : v2.y;
1280    r.z = v1.z < v2.z ? v1.z : v2.z;
1281    return r;
1282}
1283
1284extern double4 __attribute__((overloadable)) min(double4 v1, double4 v2) {
1285    double4 r;
1286    r.x = v1.x < v2.x ? v1.x : v2.x;
1287    r.y = v1.y < v2.y ? v1.y : v2.y;
1288    r.z = v1.z < v2.z ? v1.z : v2.z;
1289    r.w = v1.w < v2.w ? v1.w : v2.w;
1290    return r;
1291}
1292
1293extern long __attribute__((overloadable)) min(long v1, long v2) {
1294    return v1 < v2 ? v1 : v2;
1295}
1296extern long2 __attribute__((overloadable)) min(long2 v1, long2 v2) {
1297    long2 r;
1298    r.x = v1.x < v2.x ? v1.x : v2.x;
1299    r.y = v1.y < v2.y ? v1.y : v2.y;
1300    return r;
1301}
1302extern long3 __attribute__((overloadable)) min(long3 v1, long3 v2) {
1303    long3 r;
1304    r.x = v1.x < v2.x ? v1.x : v2.x;
1305    r.y = v1.y < v2.y ? v1.y : v2.y;
1306    r.z = v1.z < v2.z ? v1.z : v2.z;
1307    return r;
1308}
1309extern long4 __attribute__((overloadable)) min(long4 v1, long4 v2) {
1310    long4 r;
1311    r.x = v1.x < v2.x ? v1.x : v2.x;
1312    r.y = v1.y < v2.y ? v1.y : v2.y;
1313    r.z = v1.z < v2.z ? v1.z : v2.z;
1314    r.w = v1.w < v2.w ? v1.w : v2.w;
1315    return r;
1316}
1317
1318extern ulong __attribute__((overloadable)) min(ulong v1, ulong v2) {
1319    return v1 < v2 ? v1 : v2;
1320}
1321extern ulong2 __attribute__((overloadable)) min(ulong2 v1, ulong2 v2) {
1322    ulong2 r;
1323    r.x = v1.x < v2.x ? v1.x : v2.x;
1324    r.y = v1.y < v2.y ? v1.y : v2.y;
1325    return r;
1326}
1327extern ulong3 __attribute__((overloadable)) min(ulong3 v1, ulong3 v2) {
1328    ulong3 r;
1329    r.x = v1.x < v2.x ? v1.x : v2.x;
1330    r.y = v1.y < v2.y ? v1.y : v2.y;
1331    r.z = v1.z < v2.z ? v1.z : v2.z;
1332    return r;
1333}
1334extern ulong4 __attribute__((overloadable)) min(ulong4 v1, ulong4 v2) {
1335    ulong4 r;
1336    r.x = v1.x < v2.x ? v1.x : v2.x;
1337    r.y = v1.y < v2.y ? v1.y : v2.y;
1338    r.z = v1.z < v2.z ? v1.z : v2.z;
1339    r.w = v1.w < v2.w ? v1.w : v2.w;
1340    return r;
1341}
1342
1343extern double __attribute__((overloadable)) max(double v1, double v2) {
1344    return v1 > v2 ? v1 : v2;
1345}
1346
1347extern double2 __attribute__((overloadable)) max(double2 v1, double2 v2) {
1348    double2 r;
1349    r.x = v1.x > v2.x ? v1.x : v2.x;
1350    r.y = v1.y > v2.y ? v1.y : v2.y;
1351    return r;
1352}
1353
1354extern double3 __attribute__((overloadable)) max(double3 v1, double3 v2) {
1355    double3 r;
1356    r.x = v1.x > v2.x ? v1.x : v2.x;
1357    r.y = v1.y > v2.y ? v1.y : v2.y;
1358    r.z = v1.z > v2.z ? v1.z : v2.z;
1359    return r;
1360}
1361
1362extern double4 __attribute__((overloadable)) max(double4 v1, double4 v2) {
1363    double4 r;
1364    r.x = v1.x > v2.x ? v1.x : v2.x;
1365    r.y = v1.y > v2.y ? v1.y : v2.y;
1366    r.z = v1.z > v2.z ? v1.z : v2.z;
1367    r.w = v1.w > v2.w ? v1.w : v2.w;
1368    return r;
1369}
1370
1371extern long __attribute__((overloadable)) max(long v1, long v2) {
1372    return v1 > v2 ? v1 : v2;
1373}
1374extern long2 __attribute__((overloadable)) max(long2 v1, long2 v2) {
1375    long2 r;
1376    r.x = v1.x > v2.x ? v1.x : v2.x;
1377    r.y = v1.y > v2.y ? v1.y : v2.y;
1378    return r;
1379}
1380extern long3 __attribute__((overloadable)) max(long3 v1, long3 v2) {
1381    long3 r;
1382    r.x = v1.x > v2.x ? v1.x : v2.x;
1383    r.y = v1.y > v2.y ? v1.y : v2.y;
1384    r.z = v1.z > v2.z ? v1.z : v2.z;
1385    return r;
1386}
1387extern long4 __attribute__((overloadable)) max(long4 v1, long4 v2) {
1388    long4 r;
1389    r.x = v1.x > v2.x ? v1.x : v2.x;
1390    r.y = v1.y > v2.y ? v1.y : v2.y;
1391    r.z = v1.z > v2.z ? v1.z : v2.z;
1392    r.w = v1.w > v2.w ? v1.w : v2.w;
1393    return r;
1394}
1395
1396extern ulong __attribute__((overloadable)) max(ulong v1, ulong v2) {
1397    return v1 > v2 ? v1 : v2;
1398}
1399extern ulong2 __attribute__((overloadable)) max(ulong2 v1, ulong2 v2) {
1400    ulong2 r;
1401    r.x = v1.x > v2.x ? v1.x : v2.x;
1402    r.y = v1.y > v2.y ? v1.y : v2.y;
1403    return r;
1404}
1405extern ulong3 __attribute__((overloadable)) max(ulong3 v1, ulong3 v2) {
1406    ulong3 r;
1407    r.x = v1.x > v2.x ? v1.x : v2.x;
1408    r.y = v1.y > v2.y ? v1.y : v2.y;
1409    r.z = v1.z > v2.z ? v1.z : v2.z;
1410    return r;
1411}
1412extern ulong4 __attribute__((overloadable)) max(ulong4 v1, ulong4 v2) {
1413    ulong4 r;
1414    r.x = v1.x > v2.x ? v1.x : v2.x;
1415    r.y = v1.y > v2.y ? v1.y : v2.y;
1416    r.z = v1.z > v2.z ? v1.z : v2.z;
1417    r.w = v1.w > v2.w ? v1.w : v2.w;
1418    return r;
1419}
1420
1421#define THUNK_NATIVE_F(fn) \
1422    float __attribute__((overloadable)) native_##fn(float v) { return fn(v);} \
1423    float2 __attribute__((overloadable)) native_##fn(float2 v) { return fn(v);} \
1424    float3 __attribute__((overloadable)) native_##fn(float3 v) { return fn(v);} \
1425    float4 __attribute__((overloadable)) native_##fn(float4 v) { return fn(v);}
1426
1427#define THUNK_NATIVE_F_F(fn) \
1428    float __attribute__((overloadable)) native_##fn(float v1, float v2) { return fn(v1, v2);} \
1429    float2 __attribute__((overloadable)) native_##fn(float2 v1, float2 v2) { return fn(v1, v2);} \
1430    float3 __attribute__((overloadable)) native_##fn(float3 v1, float3 v2) { return fn(v1, v2);} \
1431    float4 __attribute__((overloadable)) native_##fn(float4 v1, float4 v2) { return fn(v1, v2);}
1432
1433#define THUNK_NATIVE_F_FP(fn) \
1434    float __attribute__((overloadable)) native_##fn(float v1, float *v2) { return fn(v1, v2);} \
1435    float2 __attribute__((overloadable)) native_##fn(float2 v1, float2 *v2) { return fn(v1, v2);} \
1436    float3 __attribute__((overloadable)) native_##fn(float3 v1, float3 *v2) { return fn(v1, v2);} \
1437    float4 __attribute__((overloadable)) native_##fn(float4 v1, float4 *v2) { return fn(v1, v2);}
1438
1439#define THUNK_NATIVE_F_I(fn) \
1440    float __attribute__((overloadable)) native_##fn(float v1, int v2) { return fn(v1, v2);} \
1441    float2 __attribute__((overloadable)) native_##fn(float2 v1, int2 v2) { return fn(v1, v2);} \
1442    float3 __attribute__((overloadable)) native_##fn(float3 v1, int3 v2) { return fn(v1, v2);} \
1443    float4 __attribute__((overloadable)) native_##fn(float4 v1, int4 v2) { return fn(v1, v2);}
1444
1445THUNK_NATIVE_F(acos)
1446THUNK_NATIVE_F(acosh)
1447THUNK_NATIVE_F(acospi)
1448THUNK_NATIVE_F(asin)
1449THUNK_NATIVE_F(asinh)
1450THUNK_NATIVE_F(asinpi)
1451THUNK_NATIVE_F(atan)
1452THUNK_NATIVE_F_F(atan2)
1453THUNK_NATIVE_F(atanh)
1454THUNK_NATIVE_F(atanpi)
1455THUNK_NATIVE_F_F(atan2pi)
1456THUNK_NATIVE_F(cbrt)
1457THUNK_NATIVE_F(cos)
1458THUNK_NATIVE_F(cosh)
1459THUNK_NATIVE_F(cospi)
1460THUNK_NATIVE_F(expm1)
1461THUNK_NATIVE_F_F(hypot)
1462THUNK_NATIVE_F(log1p)
1463THUNK_NATIVE_F_I(rootn)
1464THUNK_NATIVE_F(rsqrt)
1465THUNK_NATIVE_F(sqrt)
1466THUNK_NATIVE_F(sin)
1467THUNK_NATIVE_F_FP(sincos)
1468THUNK_NATIVE_F(sinh)
1469THUNK_NATIVE_F(sinpi)
1470THUNK_NATIVE_F(tan)
1471THUNK_NATIVE_F(tanh)
1472THUNK_NATIVE_F(tanpi)
1473
1474#undef THUNK_NATIVE_F
1475#undef THUNK_NATIVE_F_F
1476#undef THUNK_NATIVE_F_I
1477#undef THUNK_NATIVE_F_FP
1478
1479float __attribute__((overloadable)) native_normalize(float v) { return fast_normalize(v);}
1480float2 __attribute__((overloadable)) native_normalize(float2 v) { return fast_normalize(v);}
1481float3 __attribute__((overloadable)) native_normalize(float3 v) { return fast_normalize(v);}
1482float4 __attribute__((overloadable)) native_normalize(float4 v) { return fast_normalize(v);}
1483
1484float __attribute__((overloadable)) native_distance(float v1, float v2) { return fast_distance(v1, v2);}
1485float __attribute__((overloadable)) native_distance(float2 v1, float2 v2) { return fast_distance(v1, v2);}
1486float __attribute__((overloadable)) native_distance(float3 v1, float3 v2) { return fast_distance(v1, v2);}
1487float __attribute__((overloadable)) native_distance(float4 v1, float4 v2) { return fast_distance(v1, v2);}
1488
1489float __attribute__((overloadable)) native_length(float v) { return fast_length(v);}
1490float __attribute__((overloadable)) native_length(float2 v) { return fast_length(v);}
1491float __attribute__((overloadable)) native_length(float3 v) { return fast_length(v);}
1492float __attribute__((overloadable)) native_length(float4 v) { return fast_length(v);}
1493
1494float __attribute__((overloadable)) native_divide(float v1, float v2) { return v1 / v2;}
1495float2 __attribute__((overloadable)) native_divide(float2 v1, float2 v2) { return v1 / v2;}
1496float3 __attribute__((overloadable)) native_divide(float3 v1, float3 v2) { return v1 / v2;}
1497float4 __attribute__((overloadable)) native_divide(float4 v1, float4 v2) { return v1 / v2;}
1498
1499float __attribute__((overloadable)) native_recip(float v) { return 1.f / v;}
1500float2 __attribute__((overloadable)) native_recip(float2 v) { return ((float2)1.f) / v;}
1501float3 __attribute__((overloadable)) native_recip(float3 v) { return ((float3)1.f) / v;}
1502float4 __attribute__((overloadable)) native_recip(float4 v) { return ((float4)1.f) / v;}
1503
1504
1505
1506
1507
1508#undef FN_FUNC_FN
1509#undef IN_FUNC_FN
1510#undef FN_FUNC_FN_FN
1511#undef FN_FUNC_FN_F
1512#undef FN_FUNC_FN_IN
1513#undef FN_FUNC_FN_I
1514#undef FN_FUNC_FN_PFN
1515#undef FN_FUNC_FN_PIN
1516#undef FN_FUNC_FN_FN_FN
1517#undef FN_FUNC_FN_FN_PIN
1518#undef XN_FUNC_YN
1519#undef UIN_FUNC_IN
1520#undef IN_FUNC_IN
1521#undef XN_FUNC_XN_XN_BODY
1522#undef IN_FUNC_IN_IN_BODY
1523
1524typedef union {
1525  half hval;
1526  short sval;
1527} fp16_shape_type;
1528
1529/* half h = unsigned short s; */
1530#define SET_HALF_WORD(h, s) \
1531do {                        \
1532  fp16_shape_type fp16_u;   \
1533  fp16_u.sval = (s);        \
1534  (h) = fp16_u.hval;        \
1535} while (0)
1536
1537static const unsigned short kHalfPositiveInfinity = 0x7c00;
1538
1539/* Define f16 functions of the form
1540 *     HN output = fn(HN input)
1541 * where HN is scalar or vector half type
1542 */
1543#define HN_FUNC_HN(fn)                                                    \
1544extern half __attribute__((overloadable)) fn(half h) {                    \
1545    return (half) fn((float) h);                                          \
1546}                                                                         \
1547extern half2 __attribute__((overloadable)) fn(half2 v) {                  \
1548  return convert_half2(fn(convert_float2(v)));                            \
1549}                                                                         \
1550extern half3 __attribute__((overloadable)) fn(half3 v) {                  \
1551  return convert_half3(fn(convert_float3(v)));                            \
1552}                                                                         \
1553extern half4 __attribute__((overloadable)) fn(half4 v) {                  \
1554  return convert_half4(fn(convert_float4(v)));                            \
1555}
1556
1557/* Define f16 functions of the form
1558 *     HN output = fn(HN input1, HN input2)
1559 * where HN is scalar or vector half type
1560 */
1561#define HN_FUNC_HN_HN(fn)                                                 \
1562extern half __attribute__((overloadable)) fn(half h1, half h2) {          \
1563    return (half) fn((float) h1, (float) h2);                             \
1564}                                                                         \
1565extern half2 __attribute__((overloadable)) fn(half2 v1, half2 v2) {       \
1566  return convert_half2(fn(convert_float2(v1),                             \
1567                          convert_float2(v2)));                           \
1568}                                                                         \
1569extern half3 __attribute__((overloadable)) fn(half3 v1, half3 v2) {       \
1570  return convert_half3(fn(convert_float3(v1),                             \
1571                          convert_float3(v2)));                           \
1572}                                                                         \
1573extern half4 __attribute__((overloadable)) fn(half4 v1, half4 v2) {       \
1574  return convert_half4(fn(convert_float4(v1),                             \
1575                          convert_float4(v2)));                           \
1576}
1577
1578/* Define f16 functions of the form
1579 *     HN output = fn(HN input1, half input2)
1580 * where HN is scalar or vector half type
1581 */
1582#define HN_FUNC_HN_H(fn)                                                  \
1583extern half2 __attribute__((overloadable)) fn(half2 v1, half v2) {        \
1584  return convert_half2(fn(convert_float2(v1), (float) v2));               \
1585}                                                                         \
1586extern half3 __attribute__((overloadable)) fn(half3 v1, half v2) {        \
1587  return convert_half3(fn(convert_float3(v1), (float) v2));               \
1588}                                                                         \
1589extern half4 __attribute__((overloadable)) fn(half4 v1, half v2) {        \
1590  return convert_half4(fn(convert_float4(v1), (float) v2));               \
1591}
1592
1593/* Define f16 functions of the form
1594 *     HN output = fn(HN input1, HN input2, HN input3)
1595 * where HN is scalar or vector half type
1596 */
1597#define HN_FUNC_HN_HN_HN(fn)                                                   \
1598extern half __attribute__((overloadable)) fn(half h1, half h2, half h3) {      \
1599    return (half) fn((float) h1, (float) h2, (float) h3);                      \
1600}                                                                              \
1601extern half2 __attribute__((overloadable)) fn(half2 v1, half2 v2, half2 v3) {  \
1602  return convert_half2(fn(convert_float2(v1),                                  \
1603                          convert_float2(v2),                                  \
1604                          convert_float2(v3)));                                \
1605}                                                                              \
1606extern half3 __attribute__((overloadable)) fn(half3 v1, half3 v2, half3 v3) {  \
1607  return convert_half3(fn(convert_float3(v1),                                  \
1608                          convert_float3(v2),                                  \
1609                          convert_float3(v3)));                                \
1610}                                                                              \
1611extern half4 __attribute__((overloadable)) fn(half4 v1, half4 v2, half4 v3) {  \
1612  return convert_half4(fn(convert_float4(v1),                                  \
1613                          convert_float4(v2),                                  \
1614                          convert_float4(v3)));                                \
1615}
1616
1617/* Define f16 functions of the form
1618 *     HN output = fn(HN input1, IN input2)
1619 * where HN is scalar or vector half type and IN the equivalent integer type
1620 * of same vector length.
1621 */
1622#define HN_FUNC_HN_IN(fn)                                                 \
1623extern half __attribute__((overloadable)) fn(half h1, int v) {            \
1624    return (half) fn((float) h1, v);                                      \
1625}                                                                         \
1626extern half2 __attribute__((overloadable)) fn(half2 v1, int2 v2) {        \
1627  return convert_half2(fn(convert_float2(v1), v2));                       \
1628}                                                                         \
1629extern half3 __attribute__((overloadable)) fn(half3 v1, int3 v2) {        \
1630  return convert_half3(fn(convert_float3(v1), v2));                       \
1631}                                                                         \
1632extern half4 __attribute__((overloadable)) fn(half4 v1, int4 v2) {        \
1633  return convert_half4(fn(convert_float4(v1), v2));                       \
1634}
1635
1636/* Define f16 functions of the form
1637 *     half output = fn(HN input1)
1638 * where HN is a scalar or vector half type.
1639 */
1640#define H_FUNC_HN(fn)                                                     \
1641extern half __attribute__((overloadable)) fn(half h) {                    \
1642    return (half) fn((float) h);                                          \
1643}                                                                         \
1644extern half __attribute__((overloadable)) fn(half2 v) {                   \
1645  return fn(convert_float2(v));                                           \
1646}                                                                         \
1647extern half __attribute__((overloadable)) fn(half3 v) {                   \
1648  return fn(convert_float3(v));                                           \
1649}                                                                         \
1650extern half __attribute__((overloadable)) fn(half4 v) {                   \
1651  return fn(convert_float4(v));                                           \
1652}
1653
1654/* Define f16 functions of the form
1655 *     half output = fn(HN input1, HN input2)
1656 * where HN is a scalar or vector half type.
1657 */
1658#define H_FUNC_HN_HN(fn)                                                  \
1659extern half __attribute__((overloadable)) fn(half h1, half h2) {          \
1660    return (half) fn((float) h1, (float) h2);                             \
1661}                                                                         \
1662extern half __attribute__((overloadable)) fn(half2 v1, half2 v2) {        \
1663  return fn(convert_float2(v1), convert_float2(v2));                      \
1664}                                                                         \
1665extern half __attribute__((overloadable)) fn(half3 v1, half3 v2) {        \
1666  return fn(convert_float3(v1), convert_float3(v2));                      \
1667}                                                                         \
1668extern half __attribute__((overloadable)) fn(half4 v1, half4 v2) {        \
1669  return fn(convert_float4(v1), convert_float4(v2));                      \
1670}
1671
1672/* Define f16 functions of the form
1673 *     HN output = fn(HN input1, HN input2)
1674 * where HN is a vector half type.  The functions are defined to call the
1675 * scalar function of the same name.
1676 */
1677#define SCALARIZE_HN_FUNC_HN_HN(fn)                                       \
1678extern half2 __attribute__((overloadable)) fn(half2 v1, half2 v2) {       \
1679  half2 ret;                                                              \
1680  ret.x = fn(v1.x, v2.x);                                                 \
1681  ret.y = fn(v1.y, v2.y);                                                 \
1682  return ret;                                                             \
1683}                                                                         \
1684extern half3 __attribute__((overloadable)) fn(half3 v1, half3 v2) {       \
1685  half3 ret;                                                              \
1686  ret.x = fn(v1.x, v2.x);                                                 \
1687  ret.y = fn(v1.y, v2.y);                                                 \
1688  ret.z = fn(v1.z, v2.z);                                                 \
1689  return ret;                                                             \
1690}                                                                         \
1691extern half4 __attribute__((overloadable)) fn(half4 v1, half4 v2) {       \
1692  half4 ret;                                                              \
1693  ret.x = fn(v1.x, v2.x);                                                 \
1694  ret.y = fn(v1.y, v2.y);                                                 \
1695  ret.z = fn(v1.z, v2.z);                                                 \
1696  ret.w = fn(v1.w, v2.w);                                                 \
1697  return ret;                                                             \
1698}                                                                         \
1699
1700HN_FUNC_HN(acos);
1701HN_FUNC_HN(acosh);
1702HN_FUNC_HN(acospi);
1703HN_FUNC_HN(asin);
1704HN_FUNC_HN(asinh);
1705HN_FUNC_HN(asinpi);
1706HN_FUNC_HN(atan);
1707HN_FUNC_HN(atanh);
1708HN_FUNC_HN(atanpi);
1709HN_FUNC_HN_HN(atan2);
1710HN_FUNC_HN_HN(atan2pi);
1711
1712HN_FUNC_HN(cbrt);
1713HN_FUNC_HN(ceil);
1714
1715// TODO Add copysign
1716
1717HN_FUNC_HN(cos);
1718HN_FUNC_HN(cosh);
1719HN_FUNC_HN(cospi);
1720
1721extern half3 __attribute__((overloadable)) cross(half3 lhs, half3 rhs) {
1722    half3 r;
1723    r.x = lhs.y * rhs.z  - lhs.z * rhs.y;
1724    r.y = lhs.z * rhs.x  - lhs.x * rhs.z;
1725    r.z = lhs.x * rhs.y  - lhs.y * rhs.x;
1726    return r;
1727}
1728
1729extern half4 __attribute__((overloadable)) cross(half4 lhs, half4 rhs) {
1730    half4 r;
1731    r.x = lhs.y * rhs.z  - lhs.z * rhs.y;
1732    r.y = lhs.z * rhs.x  - lhs.x * rhs.z;
1733    r.z = lhs.x * rhs.y  - lhs.y * rhs.x;
1734    r.w = 0.f;
1735    return r;
1736}
1737
1738HN_FUNC_HN(degrees);
1739H_FUNC_HN_HN(distance);
1740H_FUNC_HN_HN(dot);
1741
1742HN_FUNC_HN(erf);
1743HN_FUNC_HN(erfc);
1744HN_FUNC_HN(exp);
1745HN_FUNC_HN(exp10);
1746HN_FUNC_HN(exp2);
1747HN_FUNC_HN(expm1);
1748
1749HN_FUNC_HN(fabs);
1750HN_FUNC_HN_HN(fdim);
1751HN_FUNC_HN(floor);
1752HN_FUNC_HN_HN_HN(fma);
1753HN_FUNC_HN_HN(fmax);
1754HN_FUNC_HN_H(fmax);
1755HN_FUNC_HN_HN(fmin);
1756HN_FUNC_HN_H(fmin);
1757HN_FUNC_HN_HN(fmod);
1758
1759// TODO Add (both variants) of fract
1760// TODO Add frexp
1761
1762HN_FUNC_HN_HN(hypot);
1763
1764// TODO Add ilogb
1765
1766HN_FUNC_HN_IN(ldexp);
1767extern half2 __attribute__((overloadable)) ldexp(half2 v, int exponent) {
1768    return convert_half2(ldexp(convert_float2(v), exponent));
1769}
1770extern half3 __attribute__((overloadable)) ldexp(half3 v, int exponent) {
1771    return convert_half3(ldexp(convert_float3(v), exponent));
1772}
1773extern half4 __attribute__((overloadable)) ldexp(half4 v, int exponent) {
1774    return convert_half4(ldexp(convert_float4(v), exponent));
1775}
1776
1777H_FUNC_HN(length);
1778HN_FUNC_HN(lgamma);
1779
1780extern half __attribute__((overloadable)) lgamma(half h, int *signp) {
1781    return (half) lgamma((float) h, signp);
1782}
1783extern half2 __attribute__((overloadable)) lgamma(half2 v, int2 *signp) {
1784    return convert_half2(lgamma(convert_float2(v), signp));
1785}
1786extern half3 __attribute__((overloadable)) lgamma(half3 v, int3 *signp) {
1787    return convert_half3(lgamma(convert_float3(v), signp));
1788}
1789extern half4 __attribute__((overloadable)) lgamma(half4 v, int4 *signp) {
1790    return convert_half4(lgamma(convert_float4(v), signp));
1791}
1792
1793HN_FUNC_HN(log);
1794HN_FUNC_HN(log10);
1795HN_FUNC_HN(log1p);
1796HN_FUNC_HN(log2);
1797HN_FUNC_HN(logb);
1798
1799HN_FUNC_HN_HN_HN(mad);
1800HN_FUNC_HN_HN(max);
1801HN_FUNC_HN_H(max); // TODO can this be arch-specific similar to _Z3maxDv2_ff?
1802HN_FUNC_HN_HN(min);
1803HN_FUNC_HN_H(min); // TODO can this be arch-specific similar to _Z3minDv2_ff?
1804
1805extern half __attribute__((overloadable)) mix(half start, half stop, half amount) {
1806    return start + (stop - start) * amount;
1807}
1808extern half2 __attribute__((overloadable)) mix(half2 start, half2 stop, half2 amount) {
1809    return start + (stop - start) * amount;
1810}
1811extern half3 __attribute__((overloadable)) mix(half3 start, half3 stop, half3 amount) {
1812    return start + (stop - start) * amount;
1813}
1814extern half4 __attribute__((overloadable)) mix(half4 start, half4 stop, half4 amount) {
1815    return start + (stop - start) * amount;
1816}
1817extern half2 __attribute__((overloadable)) mix(half2 start, half2 stop, half amount) {
1818    return start + (stop - start) * amount;
1819}
1820extern half3 __attribute__((overloadable)) mix(half3 start, half3 stop, half amount) {
1821    return start + (stop - start) * amount;
1822}
1823extern half4 __attribute__((overloadable)) mix(half4 start, half4 stop, half amount) {
1824    return start + (stop - start) * amount;
1825}
1826
1827// TODO Define modf.  Does it make sense to delegate to the float?
1828
1829half __attribute__((overloadable)) nan_half() {
1830  unsigned short nan_short = kHalfPositiveInfinity | 0x0200;
1831  half nan;
1832  SET_HALF_WORD(nan, nan_short);
1833  return nan;
1834}
1835
1836// TODO Add nextafter
1837
1838HN_FUNC_HN(normalize);
1839
1840HN_FUNC_HN_HN(pow);
1841HN_FUNC_HN_IN(pown);
1842HN_FUNC_HN_HN(powr);
1843HN_FUNC_HN(radians);
1844HN_FUNC_HN_HN(remainder);
1845
1846extern half __attribute__((overloadable)) remquo(half n, half d, int *quo) {
1847    return (float) remquo((float) n, (float) d, quo);
1848}
1849extern half2 __attribute__((overloadable)) remquo(half2 n, half2 d, int2 *quo) {
1850    return convert_half2(remquo(convert_float2(d), convert_float2(n), quo));
1851}
1852extern half3 __attribute__((overloadable)) remquo(half3 n, half3 d, int3 *quo) {
1853    return convert_half3(remquo(convert_float3(d), convert_float3(n), quo));
1854}
1855extern half4 __attribute__((overloadable)) remquo(half4 n, half4 d, int4 *quo) {
1856    return convert_half4(remquo(convert_float4(d), convert_float4(n), quo));
1857}
1858
1859HN_FUNC_HN(rint);
1860HN_FUNC_HN_IN(rootn);
1861HN_FUNC_HN(round);
1862HN_FUNC_HN(rsqrt);
1863
1864extern half __attribute__((overloadable)) sign(half h) {
1865    if (h > 0) return (half) 1.f;
1866    if (h < 0) return (half) -1.f;
1867    return h;
1868}
1869extern half2 __attribute__((overloadable)) sign(half2 v) {
1870    half2 ret;
1871    ret.x = sign(v.x);
1872    ret.y = sign(v.y);
1873    return ret;
1874}
1875extern half3 __attribute__((overloadable)) sign(half3 v) {
1876    half3 ret;
1877    ret.x = sign(v.x);
1878    ret.y = sign(v.y);
1879    ret.z = sign(v.z);
1880    return ret;
1881}
1882extern half4 __attribute__((overloadable)) sign(half4 v) {
1883    half4 ret;
1884    ret.x = sign(v.x);
1885    ret.y = sign(v.y);
1886    ret.z = sign(v.z);
1887    ret.w = sign(v.w);
1888    return ret;
1889}
1890
1891HN_FUNC_HN(sin);
1892
1893extern half __attribute__((overloadable)) sincos(half v, half *cosptr) {
1894    *cosptr = cos(v);
1895    return sin(v);
1896}
1897// TODO verify if LLVM eliminates the duplicate convert_float2
1898extern half2 __attribute__((overloadable)) sincos(half2 v, half2 *cosptr) {
1899    *cosptr = cos(v);
1900    return sin(v);
1901}
1902extern half3 __attribute__((overloadable)) sincos(half3 v, half3 *cosptr) {
1903    *cosptr = cos(v);
1904    return sin(v);
1905}
1906extern half4 __attribute__((overloadable)) sincos(half4 v, half4 *cosptr) {
1907    *cosptr = cos(v);
1908    return sin(v);
1909}
1910
1911HN_FUNC_HN(sinh);
1912HN_FUNC_HN(sinpi);
1913HN_FUNC_HN(sqrt);
1914
1915extern half __attribute__((overloadable)) step(half edge, half v) {
1916    return (v < edge) ? 0.f : 1.f;
1917}
1918extern half2 __attribute__((overloadable)) step(half2 edge, half2 v) {
1919    half2 r;
1920    r.x = (v.x < edge.x) ? 0.f : 1.f;
1921    r.y = (v.y < edge.y) ? 0.f : 1.f;
1922    return r;
1923}
1924extern half3 __attribute__((overloadable)) step(half3 edge, half3 v) {
1925    half3 r;
1926    r.x = (v.x < edge.x) ? 0.f : 1.f;
1927    r.y = (v.y < edge.y) ? 0.f : 1.f;
1928    r.z = (v.z < edge.z) ? 0.f : 1.f;
1929    return r;
1930}
1931extern half4 __attribute__((overloadable)) step(half4 edge, half4 v) {
1932    half4 r;
1933    r.x = (v.x < edge.x) ? 0.f : 1.f;
1934    r.y = (v.y < edge.y) ? 0.f : 1.f;
1935    r.z = (v.z < edge.z) ? 0.f : 1.f;
1936    r.w = (v.w < edge.w) ? 0.f : 1.f;
1937    return r;
1938}
1939extern half2 __attribute__((overloadable)) step(half2 edge, half v) {
1940    half2 r;
1941    r.x = (v < edge.x) ? 0.f : 1.f;
1942    r.y = (v < edge.y) ? 0.f : 1.f;
1943    return r;
1944}
1945extern half3 __attribute__((overloadable)) step(half3 edge, half v) {
1946    half3 r;
1947    r.x = (v < edge.x) ? 0.f : 1.f;
1948    r.y = (v < edge.y) ? 0.f : 1.f;
1949    r.z = (v < edge.z) ? 0.f : 1.f;
1950    return r;
1951}
1952extern half4 __attribute__((overloadable)) step(half4 edge, half v) {
1953    half4 r;
1954    r.x = (v < edge.x) ? 0.f : 1.f;
1955    r.y = (v < edge.y) ? 0.f : 1.f;
1956    r.z = (v < edge.z) ? 0.f : 1.f;
1957    r.w = (v < edge.w) ? 0.f : 1.f;
1958    return r;
1959}
1960extern half2 __attribute__((overloadable)) step(half edge, half2 v) {
1961    half2 r;
1962    r.x = (v.x < edge) ? 0.f : 1.f;
1963    r.y = (v.y < edge) ? 0.f : 1.f;
1964    return r;
1965}
1966extern half3 __attribute__((overloadable)) step(half edge, half3 v) {
1967    half3 r;
1968    r.x = (v.x < edge) ? 0.f : 1.f;
1969    r.y = (v.y < edge) ? 0.f : 1.f;
1970    r.z = (v.z < edge) ? 0.f : 1.f;
1971    return r;
1972}
1973extern half4 __attribute__((overloadable)) step(half edge, half4 v) {
1974    half4 r;
1975    r.x = (v.x < edge) ? 0.f : 1.f;
1976    r.y = (v.y < edge) ? 0.f : 1.f;
1977    r.z = (v.z < edge) ? 0.f : 1.f;
1978    r.w = (v.w < edge) ? 0.f : 1.f;
1979    return r;
1980}
1981
1982HN_FUNC_HN(tan);
1983HN_FUNC_HN(tanh);
1984HN_FUNC_HN(tanpi);
1985HN_FUNC_HN(tgamma);
1986HN_FUNC_HN(trunc); // TODO: rethink: needs half-specific implementation?
1987
1988HN_FUNC_HN(native_acos);
1989HN_FUNC_HN(native_acosh);
1990HN_FUNC_HN(native_acospi);
1991HN_FUNC_HN(native_asin);
1992HN_FUNC_HN(native_asinh);
1993HN_FUNC_HN(native_asinpi);
1994HN_FUNC_HN(native_atan);
1995HN_FUNC_HN(native_atanh);
1996HN_FUNC_HN(native_atanpi);
1997HN_FUNC_HN_HN(native_atan2);
1998HN_FUNC_HN_HN(native_atan2pi);
1999
2000HN_FUNC_HN(native_cbrt);
2001HN_FUNC_HN(native_cos);
2002HN_FUNC_HN(native_cosh);
2003HN_FUNC_HN(native_cospi);
2004
2005H_FUNC_HN_HN(native_distance);
2006HN_FUNC_HN_HN(native_divide);
2007
2008HN_FUNC_HN(native_exp);
2009HN_FUNC_HN(native_exp10);
2010HN_FUNC_HN(native_exp2);
2011HN_FUNC_HN(native_expm1);
2012
2013HN_FUNC_HN_HN(native_hypot);
2014H_FUNC_HN(native_length);
2015
2016HN_FUNC_HN(native_log);
2017HN_FUNC_HN(native_log10);
2018HN_FUNC_HN(native_log1p);
2019HN_FUNC_HN(native_log2);
2020
2021HN_FUNC_HN(native_normalize);
2022
2023HN_FUNC_HN_HN(native_powr); // TODO are parameter limits different for half?
2024
2025HN_FUNC_HN(native_recip);
2026HN_FUNC_HN_IN(native_rootn);
2027HN_FUNC_HN(native_rsqrt);
2028
2029HN_FUNC_HN(native_sin);
2030
2031extern half __attribute__((overloadable)) native_sincos(half v, half *cosptr) {
2032    return sincos(v, cosptr);
2033}
2034extern half2 __attribute__((overloadable)) native_sincos(half2 v, half2 *cosptr) {
2035    return sincos(v, cosptr);
2036}
2037extern half3 __attribute__((overloadable)) native_sincos(half3 v, half3 *cosptr) {
2038    return sincos(v, cosptr);
2039}
2040extern half4 __attribute__((overloadable)) native_sincos(half4 v, half4 *cosptr) {
2041    return sincos(v, cosptr);
2042}
2043
2044HN_FUNC_HN(native_sinh);
2045HN_FUNC_HN(native_sinpi);
2046HN_FUNC_HN(native_sqrt);
2047
2048HN_FUNC_HN(native_tan);
2049HN_FUNC_HN(native_tanh);
2050HN_FUNC_HN(native_tanpi);
2051
2052#undef HN_FUNC_HN
2053#undef HN_FUNC_HN_HN
2054#undef HN_FUNC_HN_H
2055#undef HN_FUNC_HN_HN_HN
2056#undef HN_FUNC_HN_IN
2057#undef H_FUNC_HN
2058#undef H_FUNC_HN_HN
2059#undef SCALARIZE_HN_FUNC_HN_HN
2060
2061