rs_cl.c revision 0b0bcff691d047da1d658889866c6a0347850f1c
1#include "rs_types.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.
519     */
520    float sign = (v < 0.0f && (p & 0x1)) ? -1.0 : 1.0;
521    float f = pow(v, (float)p);
522    return copysign(f, sign);
523}
524FN_FUNC_FN_IN(pown)
525
526extern float __attribute__((overloadable)) powr(float v, float p) {
527    return pow(v, p);
528}
529extern float2 __attribute__((overloadable)) powr(float2 v, float2 p) {
530    return pow(v, p);
531}
532extern float3 __attribute__((overloadable)) powr(float3 v, float3 p) {
533    return pow(v, p);
534}
535extern float4 __attribute__((overloadable)) powr(float4 v, float4 p) {
536    return pow(v, p);
537}
538
539extern float __attribute__((overloadable)) remainder(float, float);
540FN_FUNC_FN_FN(remainder)
541
542extern float __attribute__((overloadable)) remquo(float, float, int *);
543FN_FUNC_FN_FN_PIN(remquo)
544
545extern float __attribute__((overloadable)) rint(float);
546FN_FUNC_FN(rint)
547
548extern float __attribute__((overloadable)) rootn(float v, int r) {
549    if (r == 0) {
550        return nan(0);
551    }
552
553    if (iszero(v)) {
554        if (r < 0) {
555            if (r & 1) {
556                return copysign(posinf(), v);
557            } else {
558                return posinf();
559            }
560        } else {
561            if (r & 1) {
562                return copysign(0.f, v);
563            } else {
564                return 0.f;
565            }
566        }
567    }
568
569    if (!isinf(v) && !isnan(v) && (v < 0.f)) {
570        if (r & 1) {
571            return (-1.f * pow(-1.f * v, 1.f / r));
572        } else {
573            return nan(0);
574        }
575    }
576
577    return pow(v, 1.f / r);
578}
579FN_FUNC_FN_IN(rootn);
580
581extern float __attribute__((overloadable)) round(float);
582FN_FUNC_FN(round)
583
584
585extern float __attribute__((overloadable)) sqrt(float);
586extern float __attribute__((overloadable)) rsqrt(float v) {
587    return 1.f / sqrt(v);
588}
589
590#if !defined(__i386__)
591FN_FUNC_FN(sqrt)
592#endif // !defined(__i386__)
593
594FN_FUNC_FN(rsqrt)
595
596extern float __attribute__((overloadable)) sin(float);
597FN_FUNC_FN(sin)
598
599extern float __attribute__((overloadable)) sincos(float v, float *cosptr) {
600    *cosptr = cos(v);
601    return sin(v);
602}
603extern float2 __attribute__((overloadable)) sincos(float2 v, float2 *cosptr) {
604    *cosptr = cos(v);
605    return sin(v);
606}
607extern float3 __attribute__((overloadable)) sincos(float3 v, float3 *cosptr) {
608    *cosptr = cos(v);
609    return sin(v);
610}
611extern float4 __attribute__((overloadable)) sincos(float4 v, float4 *cosptr) {
612    *cosptr = cos(v);
613    return sin(v);
614}
615
616extern float __attribute__((overloadable)) sinh(float);
617FN_FUNC_FN(sinh)
618
619extern float __attribute__((overloadable)) sinpi(float v) {
620    return sin(v * M_PI);
621}
622FN_FUNC_FN(sinpi)
623
624extern float __attribute__((overloadable)) tan(float);
625FN_FUNC_FN(tan)
626
627extern float __attribute__((overloadable)) tanh(float);
628FN_FUNC_FN(tanh)
629
630extern float __attribute__((overloadable)) tanpi(float v) {
631    return tan(v * M_PI);
632}
633FN_FUNC_FN(tanpi)
634
635
636extern float __attribute__((overloadable)) tgamma(float);
637FN_FUNC_FN(tgamma)
638
639extern float __attribute__((overloadable)) trunc(float);
640FN_FUNC_FN(trunc)
641
642// Int ops (partial), 6.11.3
643
644#define XN_FUNC_YN(typeout, fnc, typein)                                \
645extern typeout __attribute__((overloadable)) fnc(typein);               \
646extern typeout##2 __attribute__((overloadable)) fnc(typein##2 v) {  \
647    typeout##2 r;                                                       \
648    r.x = fnc(v.x);                                                     \
649    r.y = fnc(v.y);                                                     \
650    return r;                                                           \
651}                                                                       \
652extern typeout##3 __attribute__((overloadable)) fnc(typein##3 v) {  \
653    typeout##3 r;                                                       \
654    r.x = fnc(v.x);                                                     \
655    r.y = fnc(v.y);                                                     \
656    r.z = fnc(v.z);                                                     \
657    return r;                                                           \
658}                                                                       \
659extern typeout##4 __attribute__((overloadable)) fnc(typein##4 v) {  \
660    typeout##4 r;                                                       \
661    r.x = fnc(v.x);                                                     \
662    r.y = fnc(v.y);                                                     \
663    r.z = fnc(v.z);                                                     \
664    r.w = fnc(v.w);                                                     \
665    return r;                                                           \
666}
667
668
669#define UIN_FUNC_IN(fnc)          \
670XN_FUNC_YN(uchar, fnc, char)      \
671XN_FUNC_YN(ushort, fnc, short)    \
672XN_FUNC_YN(uint, fnc, int)
673
674#define IN_FUNC_IN(fnc)           \
675XN_FUNC_YN(uchar, fnc, uchar)     \
676XN_FUNC_YN(char, fnc, char)       \
677XN_FUNC_YN(ushort, fnc, ushort)   \
678XN_FUNC_YN(short, fnc, short)     \
679XN_FUNC_YN(uint, fnc, uint)       \
680XN_FUNC_YN(int, fnc, int)
681
682
683#define XN_FUNC_XN_XN_BODY(type, fnc, body)         \
684extern type __attribute__((overloadable))       \
685        fnc(type v1, type v2) {                     \
686    return body;                                    \
687}                                                   \
688extern type##2 __attribute__((overloadable))    \
689        fnc(type##2 v1, type##2 v2) {               \
690    type##2 r;                                      \
691    r.x = fnc(v1.x, v2.x);                          \
692    r.y = fnc(v1.y, v2.y);                          \
693    return r;                                       \
694}                                                   \
695extern type##3 __attribute__((overloadable))    \
696        fnc(type##3 v1, type##3 v2) {               \
697    type##3 r;                                      \
698    r.x = fnc(v1.x, v2.x);                          \
699    r.y = fnc(v1.y, v2.y);                          \
700    r.z = fnc(v1.z, v2.z);                          \
701    return r;                                       \
702}                                                   \
703extern type##4 __attribute__((overloadable))    \
704        fnc(type##4 v1, type##4 v2) {               \
705    type##4 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    r.w = fnc(v1.w, v2.w);                          \
710    return r;                                       \
711}
712
713#define IN_FUNC_IN_IN_BODY(fnc, body) \
714XN_FUNC_XN_XN_BODY(uchar, fnc, body)  \
715XN_FUNC_XN_XN_BODY(char, fnc, body)   \
716XN_FUNC_XN_XN_BODY(ushort, fnc, body) \
717XN_FUNC_XN_XN_BODY(short, fnc, body)  \
718XN_FUNC_XN_XN_BODY(uint, fnc, body)   \
719XN_FUNC_XN_XN_BODY(int, fnc, body)    \
720XN_FUNC_XN_XN_BODY(float, fnc, body)
721
722
723/**
724 * abs
725 */
726extern uint32_t __attribute__((overloadable)) abs(int32_t v) {
727    if (v < 0)
728        return -v;
729    return v;
730}
731extern uint16_t __attribute__((overloadable)) abs(int16_t v) {
732    if (v < 0)
733        return -v;
734    return v;
735}
736extern uint8_t __attribute__((overloadable)) abs(int8_t v) {
737    if (v < 0)
738        return -v;
739    return v;
740}
741
742/**
743 * clz
744 * __builtin_clz only accepts a 32-bit unsigned int, so every input will be
745 * expanded to 32 bits. For our smaller data types, we need to subtract off
746 * these unused top bits (that will be always be composed of zeros).
747 */
748extern uint32_t __attribute__((overloadable)) clz(uint32_t v) {
749    return __builtin_clz(v);
750}
751extern uint16_t __attribute__((overloadable)) clz(uint16_t v) {
752    return __builtin_clz(v) - 16;
753}
754extern uint8_t __attribute__((overloadable)) clz(uint8_t v) {
755    return __builtin_clz(v) - 24;
756}
757extern int32_t __attribute__((overloadable)) clz(int32_t v) {
758    return __builtin_clz(v);
759}
760extern int16_t __attribute__((overloadable)) clz(int16_t v) {
761    return __builtin_clz(((uint32_t)v) & 0x0000ffff) - 16;
762}
763extern int8_t __attribute__((overloadable)) clz(int8_t v) {
764    return __builtin_clz(((uint32_t)v) & 0x000000ff) - 24;
765}
766
767
768UIN_FUNC_IN(abs)
769IN_FUNC_IN(clz)
770
771
772// 6.11.4
773
774
775extern float __attribute__((overloadable)) degrees(float radians) {
776    return radians * (180.f / M_PI);
777}
778extern float2 __attribute__((overloadable)) degrees(float2 radians) {
779    return radians * (180.f / M_PI);
780}
781extern float3 __attribute__((overloadable)) degrees(float3 radians) {
782    return radians * (180.f / M_PI);
783}
784extern float4 __attribute__((overloadable)) degrees(float4 radians) {
785    return radians * (180.f / M_PI);
786}
787
788extern float __attribute__((overloadable)) mix(float start, float stop, float amount) {
789    return start + (stop - start) * amount;
790}
791extern float2 __attribute__((overloadable)) mix(float2 start, float2 stop, float2 amount) {
792    return start + (stop - start) * amount;
793}
794extern float3 __attribute__((overloadable)) mix(float3 start, float3 stop, float3 amount) {
795    return start + (stop - start) * amount;
796}
797extern float4 __attribute__((overloadable)) mix(float4 start, float4 stop, float4 amount) {
798    return start + (stop - start) * amount;
799}
800extern float2 __attribute__((overloadable)) mix(float2 start, float2 stop, float amount) {
801    return start + (stop - start) * amount;
802}
803extern float3 __attribute__((overloadable)) mix(float3 start, float3 stop, float amount) {
804    return start + (stop - start) * amount;
805}
806extern float4 __attribute__((overloadable)) mix(float4 start, float4 stop, float amount) {
807    return start + (stop - start) * amount;
808}
809
810extern float __attribute__((overloadable)) radians(float degrees) {
811    return degrees * (M_PI / 180.f);
812}
813extern float2 __attribute__((overloadable)) radians(float2 degrees) {
814    return degrees * (M_PI / 180.f);
815}
816extern float3 __attribute__((overloadable)) radians(float3 degrees) {
817    return degrees * (M_PI / 180.f);
818}
819extern float4 __attribute__((overloadable)) radians(float4 degrees) {
820    return degrees * (M_PI / 180.f);
821}
822
823extern float __attribute__((overloadable)) step(float edge, float v) {
824    return (v < edge) ? 0.f : 1.f;
825}
826extern float2 __attribute__((overloadable)) step(float2 edge, float2 v) {
827    float2 r;
828    r.x = (v.x < edge.x) ? 0.f : 1.f;
829    r.y = (v.y < edge.y) ? 0.f : 1.f;
830    return r;
831}
832extern float3 __attribute__((overloadable)) step(float3 edge, float3 v) {
833    float3 r;
834    r.x = (v.x < edge.x) ? 0.f : 1.f;
835    r.y = (v.y < edge.y) ? 0.f : 1.f;
836    r.z = (v.z < edge.z) ? 0.f : 1.f;
837    return r;
838}
839extern float4 __attribute__((overloadable)) step(float4 edge, float4 v) {
840    float4 r;
841    r.x = (v.x < edge.x) ? 0.f : 1.f;
842    r.y = (v.y < edge.y) ? 0.f : 1.f;
843    r.z = (v.z < edge.z) ? 0.f : 1.f;
844    r.w = (v.w < edge.w) ? 0.f : 1.f;
845    return r;
846}
847extern float2 __attribute__((overloadable)) step(float2 edge, float v) {
848    float2 r;
849    r.x = (v < edge.x) ? 0.f : 1.f;
850    r.y = (v < edge.y) ? 0.f : 1.f;
851    return r;
852}
853extern float3 __attribute__((overloadable)) step(float3 edge, float v) {
854    float3 r;
855    r.x = (v < edge.x) ? 0.f : 1.f;
856    r.y = (v < edge.y) ? 0.f : 1.f;
857    r.z = (v < edge.z) ? 0.f : 1.f;
858    return r;
859}
860extern float4 __attribute__((overloadable)) step(float4 edge, float v) {
861    float4 r;
862    r.x = (v < edge.x) ? 0.f : 1.f;
863    r.y = (v < edge.y) ? 0.f : 1.f;
864    r.z = (v < edge.z) ? 0.f : 1.f;
865    r.w = (v < edge.w) ? 0.f : 1.f;
866    return r;
867}
868extern float2 __attribute__((overloadable)) step(float edge, float2 v) {
869    float2 r;
870    r.x = (v.x < edge) ? 0.f : 1.f;
871    r.y = (v.y < edge) ? 0.f : 1.f;
872    return r;
873}
874extern float3 __attribute__((overloadable)) step(float edge, float3 v) {
875    float3 r;
876    r.x = (v.x < edge) ? 0.f : 1.f;
877    r.y = (v.y < edge) ? 0.f : 1.f;
878    r.z = (v.z < edge) ? 0.f : 1.f;
879    return r;
880}
881extern float4 __attribute__((overloadable)) step(float edge, float4 v) {
882    float4 r;
883    r.x = (v.x < edge) ? 0.f : 1.f;
884    r.y = (v.y < edge) ? 0.f : 1.f;
885    r.z = (v.z < edge) ? 0.f : 1.f;
886    r.w = (v.w < edge) ? 0.f : 1.f;
887    return r;
888}
889
890extern float __attribute__((overloadable)) smoothstep(float, float, float);
891extern float2 __attribute__((overloadable)) smoothstep(float2, float2, float2);
892extern float3 __attribute__((overloadable)) smoothstep(float3, float3, float3);
893extern float4 __attribute__((overloadable)) smoothstep(float4, float4, float4);
894extern float2 __attribute__((overloadable)) smoothstep(float, float, float2);
895extern float3 __attribute__((overloadable)) smoothstep(float, float, float3);
896extern float4 __attribute__((overloadable)) smoothstep(float, float, float4);
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(__i386__)
925
926extern float __attribute__((overloadable)) dot(float lhs, float rhs) {
927    return lhs * rhs;
928}
929extern float __attribute__((overloadable)) dot(float2 lhs, float2 rhs) {
930    return lhs.x*rhs.x + lhs.y*rhs.y;
931}
932extern float __attribute__((overloadable)) dot(float3 lhs, float3 rhs) {
933    return lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z;
934}
935extern float __attribute__((overloadable)) dot(float4 lhs, float4 rhs) {
936    return lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z + lhs.w*rhs.w;
937}
938
939extern float __attribute__((overloadable)) length(float v) {
940    return fabs(v);
941}
942extern float __attribute__((overloadable)) length(float2 v) {
943    return sqrt(v.x*v.x + v.y*v.y);
944}
945extern float __attribute__((overloadable)) length(float3 v) {
946    return sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
947}
948extern float __attribute__((overloadable)) length(float4 v) {
949    return sqrt(v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w);
950}
951
952#else
953
954extern float __attribute__((overloadable)) length(float v);
955extern float __attribute__((overloadable)) length(float2 v);
956extern float __attribute__((overloadable)) length(float3 v);
957extern float __attribute__((overloadable)) length(float4 v);
958
959#endif
960
961extern float __attribute__((overloadable)) distance(float lhs, float rhs) {
962    return length(lhs - rhs);
963}
964extern float __attribute__((overloadable)) distance(float2 lhs, float2 rhs) {
965    return length(lhs - rhs);
966}
967extern float __attribute__((overloadable)) distance(float3 lhs, float3 rhs) {
968    return length(lhs - rhs);
969}
970extern float __attribute__((overloadable)) distance(float4 lhs, float4 rhs) {
971    return length(lhs - rhs);
972}
973
974/* For the normalization functions, vectors of length 0 should simply be
975 * returned (i.e. all the components of that vector are 0).
976 */
977extern float __attribute__((overloadable)) normalize(float v) {
978    if (v == 0.0f) {
979        return 0.0f;
980    } else if (v < 0.0f) {
981        return -1.0f;
982    } else {
983        return 1.0f;
984    }
985}
986extern float2 __attribute__((overloadable)) normalize(float2 v) {
987    float l = length(v);
988    return l == 0.0f ? v : v / l;
989}
990extern float3 __attribute__((overloadable)) normalize(float3 v) {
991    float l = length(v);
992    return l == 0.0f ? v : v / l;
993}
994extern float4 __attribute__((overloadable)) normalize(float4 v) {
995    float l = length(v);
996    return l == 0.0f ? v : v / l;
997}
998
999extern float __attribute__((overloadable)) half_sqrt(float);
1000
1001extern float __attribute__((overloadable)) fast_length(float v) {
1002    return fabs(v);
1003}
1004extern float __attribute__((overloadable)) fast_length(float2 v) {
1005    return half_sqrt(v.x*v.x + v.y*v.y);
1006}
1007extern float __attribute__((overloadable)) fast_length(float3 v) {
1008    return half_sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
1009}
1010extern float __attribute__((overloadable)) fast_length(float4 v) {
1011    return half_sqrt(v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w);
1012}
1013
1014extern float __attribute__((overloadable)) fast_distance(float lhs, float rhs) {
1015    return fast_length(lhs - rhs);
1016}
1017extern float __attribute__((overloadable)) fast_distance(float2 lhs, float2 rhs) {
1018    return fast_length(lhs - rhs);
1019}
1020extern float __attribute__((overloadable)) fast_distance(float3 lhs, float3 rhs) {
1021    return fast_length(lhs - rhs);
1022}
1023extern float __attribute__((overloadable)) fast_distance(float4 lhs, float4 rhs) {
1024    return fast_length(lhs - rhs);
1025}
1026
1027extern float __attribute__((overloadable)) half_rsqrt(float);
1028
1029/* For the normalization functions, vectors of length 0 should simply be
1030 * returned (i.e. all the components of that vector are 0).
1031 */
1032extern float __attribute__((overloadable)) fast_normalize(float v) {
1033    if (v == 0.0f) {
1034        return 0.0f;
1035    } else if (v < 0.0f) {
1036        return -1.0f;
1037    } else {
1038        return 1.0f;
1039    }
1040}
1041// If the length is 0, then rlength should be NaN.
1042extern float2 __attribute__((overloadable)) fast_normalize(float2 v) {
1043    float rlength = half_rsqrt(v.x*v.x + v.y*v.y);
1044    return (rlength == rlength) ? v * rlength : v;
1045}
1046extern float3 __attribute__((overloadable)) fast_normalize(float3 v) {
1047    float rlength = half_rsqrt(v.x*v.x + v.y*v.y + v.z*v.z);
1048    return (rlength == rlength) ? v * rlength : v;
1049}
1050extern float4 __attribute__((overloadable)) fast_normalize(float4 v) {
1051    float rlength = half_rsqrt(v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w);
1052    return (rlength == rlength) ? v * rlength : v;
1053}
1054
1055extern float __attribute__((overloadable)) half_recip(float);
1056
1057/*
1058extern float __attribute__((overloadable)) approx_atan(float x) {
1059    if (x == 0.f)
1060        return 0.f;
1061    if (x < 0.f)
1062        return -1.f * approx_atan(-1.f * x);
1063    if (x > 1.f)
1064        return M_PI_2 - approx_atan(approx_recip(x));
1065    return x * approx_recip(1.f + 0.28f * x*x);
1066}
1067FN_FUNC_FN(approx_atan)
1068*/
1069
1070typedef union
1071{
1072  float fv;
1073  int32_t iv;
1074} ieee_float_shape_type;
1075
1076/* Get a 32 bit int from a float.  */
1077
1078#define GET_FLOAT_WORD(i,d)                 \
1079do {                                \
1080  ieee_float_shape_type gf_u;                   \
1081  gf_u.fv = (d);                     \
1082  (i) = gf_u.iv;                      \
1083} while (0)
1084
1085/* Set a float from a 32 bit int.  */
1086
1087#define SET_FLOAT_WORD(d,i)                 \
1088do {                                \
1089  ieee_float_shape_type sf_u;                   \
1090  sf_u.iv = (i);                      \
1091  (d) = sf_u.fv;                     \
1092} while (0)
1093
1094
1095
1096// Valid -125 to 125
1097extern float __attribute__((overloadable)) native_exp2(float v) {
1098    int32_t iv = (int)v;
1099    int32_t x = iv + (iv >> 31); // ~floor(v)
1100    float r = (v - x);
1101
1102    float fo;
1103    SET_FLOAT_WORD(fo, (x + 127) << 23);
1104
1105    r *= 0.694f; // ~ log(e) / log(2)
1106    float r2 = r*r;
1107    float adj = 1.f + r + (r2 * 0.5f) + (r2*r * 0.166666f) + (r2*r2 * 0.0416666f);
1108    return fo * adj;
1109}
1110
1111extern float2 __attribute__((overloadable)) native_exp2(float2 v) {
1112    int2 iv = convert_int2(v);
1113    int2 x = iv + (iv >> (int2)31);//floor(v);
1114    float2 r = (v - convert_float2(x));
1115
1116    x += 127;
1117
1118    float2 fo = (float2)(x << (int2)23);
1119
1120    r *= 0.694f; // ~ log(e) / log(2)
1121    float2 r2 = r*r;
1122    float2 adj = 1.f + r + (r2 * 0.5f) + (r2*r * 0.166666f) + (r2*r2 * 0.0416666f);
1123    return fo * adj;
1124}
1125
1126extern float4 __attribute__((overloadable)) native_exp2(float4 v) {
1127    int4 iv = convert_int4(v);
1128    int4 x = iv + (iv >> (int4)31);//floor(v);
1129    float4 r = (v - convert_float4(x));
1130
1131    x += 127;
1132
1133    float4 fo = (float4)(x << (int4)23);
1134
1135    r *= 0.694f; // ~ log(e) / log(2)
1136    float4 r2 = r*r;
1137    float4 adj = 1.f + r + (r2 * 0.5f) + (r2*r * 0.166666f) + (r2*r2 * 0.0416666f);
1138    return fo * adj;
1139}
1140
1141extern float3 __attribute__((overloadable)) native_exp2(float3 v) {
1142    float4 t = 1.f;
1143    t.xyz = v;
1144    return native_exp2(t).xyz;
1145}
1146
1147
1148extern float __attribute__((overloadable)) native_exp(float v) {
1149    return native_exp2(v * 1.442695041f);
1150}
1151extern float2 __attribute__((overloadable)) native_exp(float2 v) {
1152    return native_exp2(v * 1.442695041f);
1153}
1154extern float3 __attribute__((overloadable)) native_exp(float3 v) {
1155    return native_exp2(v * 1.442695041f);
1156}
1157extern float4 __attribute__((overloadable)) native_exp(float4 v) {
1158    return native_exp2(v * 1.442695041f);
1159}
1160
1161extern float __attribute__((overloadable)) native_exp10(float v) {
1162    return native_exp2(v * 3.321928095f);
1163}
1164extern float2 __attribute__((overloadable)) native_exp10(float2 v) {
1165    return native_exp2(v * 3.321928095f);
1166}
1167extern float3 __attribute__((overloadable)) native_exp10(float3 v) {
1168    return native_exp2(v * 3.321928095f);
1169}
1170extern float4 __attribute__((overloadable)) native_exp10(float4 v) {
1171    return native_exp2(v * 3.321928095f);
1172}
1173
1174extern float __attribute__((overloadable)) native_log2(float v) {
1175    int32_t ibits;
1176    GET_FLOAT_WORD(ibits, v);
1177
1178    int32_t e = (ibits >> 23) & 0xff;
1179
1180    ibits &= 0x7fffff;
1181    ibits |= 127 << 23;
1182
1183    float ir;
1184    SET_FLOAT_WORD(ir, ibits);
1185
1186    ir -= 1.5f;
1187    float ir2 = ir*ir;
1188    float adj2 = 0.405465108f + // -0.00009f +
1189                 (0.666666667f * ir) -
1190                 (0.222222222f * ir2) +
1191                 (0.098765432f * ir*ir2) -
1192                 (0.049382716f * ir2*ir2) +
1193                 (0.026337449f * ir*ir2*ir2) -
1194                 (0.014631916f * ir2*ir2*ir2);
1195    adj2 *= (1.f / 0.693147181f);
1196
1197    return (float)(e - 127) + adj2;
1198}
1199extern float2 __attribute__((overloadable)) native_log2(float2 v) {
1200    float2 v2 = {native_log2(v.x), native_log2(v.y)};
1201    return v2;
1202}
1203extern float3 __attribute__((overloadable)) native_log2(float3 v) {
1204    float3 v2 = {native_log2(v.x), native_log2(v.y), native_log2(v.z)};
1205    return v2;
1206}
1207extern float4 __attribute__((overloadable)) native_log2(float4 v) {
1208    float4 v2 = {native_log2(v.x), native_log2(v.y), native_log2(v.z), native_log2(v.w)};
1209    return v2;
1210}
1211
1212extern float __attribute__((overloadable)) native_log(float v) {
1213    return native_log2(v) * (1.f / 1.442695041f);
1214}
1215extern float2 __attribute__((overloadable)) native_log(float2 v) {
1216    return native_log2(v) * (1.f / 1.442695041f);
1217}
1218extern float3 __attribute__((overloadable)) native_log(float3 v) {
1219    return native_log2(v) * (1.f / 1.442695041f);
1220}
1221extern float4 __attribute__((overloadable)) native_log(float4 v) {
1222    return native_log2(v) * (1.f / 1.442695041f);
1223}
1224
1225extern float __attribute__((overloadable)) native_log10(float v) {
1226    return native_log2(v) * (1.f / 3.321928095f);
1227}
1228extern float2 __attribute__((overloadable)) native_log10(float2 v) {
1229    return native_log2(v) * (1.f / 3.321928095f);
1230}
1231extern float3 __attribute__((overloadable)) native_log10(float3 v) {
1232    return native_log2(v) * (1.f / 3.321928095f);
1233}
1234extern float4 __attribute__((overloadable)) native_log10(float4 v) {
1235    return native_log2(v) * (1.f / 3.321928095f);
1236}
1237
1238
1239extern float __attribute__((overloadable)) native_powr(float v, float y) {
1240    float v2 = native_log2(v);
1241    v2 = fmax(v2, -125.f);
1242    return native_exp2(v2 * y);
1243}
1244extern float2 __attribute__((overloadable)) native_powr(float2 v, float2 y) {
1245    float2 v2 = native_log2(v);
1246    v2 = fmax(v2, -125.f);
1247    return native_exp2(v2 * y);
1248}
1249extern float3 __attribute__((overloadable)) native_powr(float3 v, float3 y) {
1250    float3 v2 = native_log2(v);
1251    v2 = fmax(v2, -125.f);
1252    return native_exp2(v2 * y);
1253}
1254extern float4 __attribute__((overloadable)) native_powr(float4 v, float4 y) {
1255    float4 v2 = native_log2(v);
1256    v2 = fmax(v2, -125.f);
1257    return native_exp2(v2 * y);
1258}
1259
1260
1261#undef FN_FUNC_FN
1262#undef IN_FUNC_FN
1263#undef FN_FUNC_FN_FN
1264#undef FN_FUNC_FN_F
1265#undef FN_FUNC_FN_IN
1266#undef FN_FUNC_FN_I
1267#undef FN_FUNC_FN_PFN
1268#undef FN_FUNC_FN_PIN
1269#undef FN_FUNC_FN_FN_FN
1270#undef FN_FUNC_FN_FN_PIN
1271#undef XN_FUNC_YN
1272#undef UIN_FUNC_IN
1273#undef IN_FUNC_IN
1274#undef XN_FUNC_XN_XN_BODY
1275#undef IN_FUNC_IN_IN_BODY
1276