summaryrefslogtreecommitdiff
path: root/driver/runtime/rs_cl.c
blob: a79ad2a96be2453907a788f9e238d75ea8618847 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
#include "rs_types.rsh"

extern float2 __attribute__((overloadable)) convert_float2(int2 c);
extern float3 __attribute__((overloadable)) convert_float3(int3 c);
extern float4 __attribute__((overloadable)) convert_float4(int4 c);

extern int2 __attribute__((overloadable)) convert_int2(float2 c);
extern int3 __attribute__((overloadable)) convert_int3(float3 c);
extern int4 __attribute__((overloadable)) convert_int4(float4 c);


extern float __attribute__((overloadable)) fmin(float v, float v2);
extern float2 __attribute__((overloadable)) fmin(float2 v, float v2);
extern float3 __attribute__((overloadable)) fmin(float3 v, float v2);
extern float4 __attribute__((overloadable)) fmin(float4 v, float v2);

extern float __attribute__((overloadable)) fmax(float v, float v2);
extern float2 __attribute__((overloadable)) fmax(float2 v, float v2);
extern float3 __attribute__((overloadable)) fmax(float3 v, float v2);
extern float4 __attribute__((overloadable)) fmax(float4 v, float v2);

// Float ops, 6.11.2

#define FN_FUNC_FN(fnc)                                         \
extern float2 __attribute__((overloadable)) fnc(float2 v) { \
    float2 r;                                                   \
    r.x = fnc(v.x);                                             \
    r.y = fnc(v.y);                                             \
    return r;                                                   \
}                                                               \
extern float3 __attribute__((overloadable)) fnc(float3 v) { \
    float3 r;                                                   \
    r.x = fnc(v.x);                                             \
    r.y = fnc(v.y);                                             \
    r.z = fnc(v.z);                                             \
    return r;                                                   \
}                                                               \
extern float4 __attribute__((overloadable)) fnc(float4 v) { \
    float4 r;                                                   \
    r.x = fnc(v.x);                                             \
    r.y = fnc(v.y);                                             \
    r.z = fnc(v.z);                                             \
    r.w = fnc(v.w);                                             \
    return r;                                                   \
}

#define IN_FUNC_FN(fnc)                                         \
extern int2 __attribute__((overloadable)) fnc(float2 v) {   \
    int2 r;                                                     \
    r.x = fnc(v.x);                                             \
    r.y = fnc(v.y);                                             \
    return r;                                                   \
}                                                               \
extern int3 __attribute__((overloadable)) fnc(float3 v) {   \
    int3 r;                                                     \
    r.x = fnc(v.x);                                             \
    r.y = fnc(v.y);                                             \
    r.z = fnc(v.z);                                             \
    return r;                                                   \
}                                                               \
extern int4 __attribute__((overloadable)) fnc(float4 v) {   \
    int4 r;                                                     \
    r.x = fnc(v.x);                                             \
    r.y = fnc(v.y);                                             \
    r.z = fnc(v.z);                                             \
    r.w = fnc(v.w);                                             \
    return r;                                                   \
}

#define FN_FUNC_FN_FN(fnc)                                                  \
extern float2 __attribute__((overloadable)) fnc(float2 v1, float2 v2) { \
    float2 r;                                                               \
    r.x = fnc(v1.x, v2.x);                                                  \
    r.y = fnc(v1.y, v2.y);                                                  \
    return r;                                                               \
}                                                                           \
extern float3 __attribute__((overloadable)) fnc(float3 v1, float3 v2) { \
    float3 r;                                                               \
    r.x = fnc(v1.x, v2.x);                                                  \
    r.y = fnc(v1.y, v2.y);                                                  \
    r.z = fnc(v1.z, v2.z);                                                  \
    return r;                                                               \
}                                                                           \
extern float4 __attribute__((overloadable)) fnc(float4 v1, float4 v2) { \
    float4 r;                                                               \
    r.x = fnc(v1.x, v2.x);                                                  \
    r.y = fnc(v1.y, v2.y);                                                  \
    r.z = fnc(v1.z, v2.z);                                                  \
    r.w = fnc(v1.w, v2.w);                                                  \
    return r;                                                               \
}

#define FN_FUNC_FN_F(fnc)                                                   \
extern float2 __attribute__((overloadable)) fnc(float2 v1, float v2) {  \
    float2 r;                                                               \
    r.x = fnc(v1.x, v2);                                                    \
    r.y = fnc(v1.y, v2);                                                    \
    return r;                                                               \
}                                                                           \
extern float3 __attribute__((overloadable)) fnc(float3 v1, float v2) {  \
    float3 r;                                                               \
    r.x = fnc(v1.x, v2);                                                    \
    r.y = fnc(v1.y, v2);                                                    \
    r.z = fnc(v1.z, v2);                                                    \
    return r;                                                               \
}                                                                           \
extern float4 __attribute__((overloadable)) fnc(float4 v1, float v2) {  \
    float4 r;                                                               \
    r.x = fnc(v1.x, v2);                                                    \
    r.y = fnc(v1.y, v2);                                                    \
    r.z = fnc(v1.z, v2);                                                    \
    r.w = fnc(v1.w, v2);                                                    \
    return r;                                                               \
}

#define FN_FUNC_FN_IN(fnc)                                                  \
extern float2 __attribute__((overloadable)) fnc(float2 v1, int2 v2) {   \
    float2 r;                                                               \
    r.x = fnc(v1.x, v2.x);                                                  \
    r.y = fnc(v1.y, v2.y);                                                  \
    return r;                                                               \
}                                                                           \
extern float3 __attribute__((overloadable)) fnc(float3 v1, int3 v2) {   \
    float3 r;                                                               \
    r.x = fnc(v1.x, v2.x);                                                  \
    r.y = fnc(v1.y, v2.y);                                                  \
    r.z = fnc(v1.z, v2.z);                                                  \
    return r;                                                               \
}                                                                           \
extern float4 __attribute__((overloadable)) fnc(float4 v1, int4 v2) {   \
    float4 r;                                                               \
    r.x = fnc(v1.x, v2.x);                                                  \
    r.y = fnc(v1.y, v2.y);                                                  \
    r.z = fnc(v1.z, v2.z);                                                  \
    r.w = fnc(v1.w, v2.w);                                                  \
    return r;                                                               \
}

#define FN_FUNC_FN_I(fnc)                                                   \
extern float2 __attribute__((overloadable)) fnc(float2 v1, int v2) {    \
    float2 r;                                                               \
    r.x = fnc(v1.x, v2);                                                    \
    r.y = fnc(v1.y, v2);                                                    \
    return r;                                                               \
}                                                                           \
extern float3 __attribute__((overloadable)) fnc(float3 v1, int v2) {    \
    float3 r;                                                               \
    r.x = fnc(v1.x, v2);                                                    \
    r.y = fnc(v1.y, v2);                                                    \
    r.z = fnc(v1.z, v2);                                                    \
    return r;                                                               \
}                                                                           \
extern float4 __attribute__((overloadable)) fnc(float4 v1, int v2) {    \
    float4 r;                                                               \
    r.x = fnc(v1.x, v2);                                                    \
    r.y = fnc(v1.y, v2);                                                    \
    r.z = fnc(v1.z, v2);                                                    \
    r.w = fnc(v1.w, v2);                                                    \
    return r;                                                               \
}

#define FN_FUNC_FN_PFN(fnc)                     \
extern float2 __attribute__((overloadable)) \
        fnc(float2 v1, float2 *v2) {            \
    float2 r;                                   \
    float t[2];                                 \
    r.x = fnc(v1.x, &t[0]);                     \
    r.y = fnc(v1.y, &t[1]);                     \
    v2->x = t[0];                               \
    v2->y = t[1];                               \
    return r;                                   \
}                                               \
extern float3 __attribute__((overloadable)) \
        fnc(float3 v1, float3 *v2) {            \
    float3 r;                                   \
    float t[3];                                 \
    r.x = fnc(v1.x, &t[0]);                     \
    r.y = fnc(v1.y, &t[1]);                     \
    r.z = fnc(v1.z, &t[2]);                     \
    v2->x = t[0];                               \
    v2->y = t[1];                               \
    v2->z = t[2];                               \
    return r;                                   \
}                                               \
extern float4 __attribute__((overloadable)) \
        fnc(float4 v1, float4 *v2) {            \
    float4 r;                                   \
    float t[4];                                 \
    r.x = fnc(v1.x, &t[0]);                     \
    r.y = fnc(v1.y, &t[1]);                     \
    r.z = fnc(v1.z, &t[2]);                     \
    r.w = fnc(v1.w, &t[3]);                     \
    v2->x = t[0];                               \
    v2->y = t[1];                               \
    v2->z = t[2];                               \
    v2->w = t[3];                               \
    return r;                                   \
}

#define FN_FUNC_FN_PIN(fnc)                                                 \
extern float2 __attribute__((overloadable)) fnc(float2 v1, int2 *v2) {  \
    float2 r;                                                               \
    int t[2];                                                               \
    r.x = fnc(v1.x, &t[0]);                                                 \
    r.y = fnc(v1.y, &t[1]);                                                 \
    v2->x = t[0];                                                           \
    v2->y = t[1];                                                           \
    return r;                                                               \
}                                                                           \
extern float3 __attribute__((overloadable)) fnc(float3 v1, int3 *v2) {  \
    float3 r;                                                               \
    int t[3];                                                               \
    r.x = fnc(v1.x, &t[0]);                                                 \
    r.y = fnc(v1.y, &t[1]);                                                 \
    r.z = fnc(v1.z, &t[2]);                                                 \
    v2->x = t[0];                                                           \
    v2->y = t[1];                                                           \
    v2->z = t[2];                                                           \
    return r;                                                               \
}                                                                           \
extern float4 __attribute__((overloadable)) fnc(float4 v1, int4 *v2) {  \
    float4 r;                                                               \
    int t[4];                                                               \
    r.x = fnc(v1.x, &t[0]);                                                 \
    r.y = fnc(v1.y, &t[1]);                                                 \
    r.z = fnc(v1.z, &t[2]);                                                 \
    r.w = fnc(v1.w, &t[3]);                                                 \
    v2->x = t[0];                                                           \
    v2->y = t[1];                                                           \
    v2->z = t[2];                                                           \
    v2->w = t[3];                                                           \
    return r;                                                               \
}

#define FN_FUNC_FN_FN_FN(fnc)                   \
extern float2 __attribute__((overloadable)) \
        fnc(float2 v1, float2 v2, float2 v3) {  \
    float2 r;                                   \
    r.x = fnc(v1.x, v2.x, v3.x);                \
    r.y = fnc(v1.y, v2.y, v3.y);                \
    return r;                                   \
}                                               \
extern float3 __attribute__((overloadable)) \
        fnc(float3 v1, float3 v2, float3 v3) {  \
    float3 r;                                   \
    r.x = fnc(v1.x, v2.x, v3.x);                \
    r.y = fnc(v1.y, v2.y, v3.y);                \
    r.z = fnc(v1.z, v2.z, v3.z);                \
    return r;                                   \
}                                               \
extern float4 __attribute__((overloadable)) \
        fnc(float4 v1, float4 v2, float4 v3) {  \
    float4 r;                                   \
    r.x = fnc(v1.x, v2.x, v3.x);                \
    r.y = fnc(v1.y, v2.y, v3.y);                \
    r.z = fnc(v1.z, v2.z, v3.z);                \
    r.w = fnc(v1.w, v2.w, v3.w);                \
    return r;                                   \
}

#define FN_FUNC_FN_FN_PIN(fnc)                  \
extern float2 __attribute__((overloadable)) \
        fnc(float2 v1, float2 v2, int2 *v3) {   \
    float2 r;                                   \
    int t[2];                                   \
    r.x = fnc(v1.x, v2.x, &t[0]);               \
    r.y = fnc(v1.y, v2.y, &t[1]);               \
    v3->x = t[0];                               \
    v3->y = t[1];                               \
    return r;                                   \
}                                               \
extern float3 __attribute__((overloadable)) \
        fnc(float3 v1, float3 v2, int3 *v3) {   \
    float3 r;                                   \
    int t[3];                                   \
    r.x = fnc(v1.x, v2.x, &t[0]);               \
    r.y = fnc(v1.y, v2.y, &t[1]);               \
    r.z = fnc(v1.z, v2.z, &t[2]);               \
    v3->x = t[0];                               \
    v3->y = t[1];                               \
    v3->z = t[2];                               \
    return r;                                   \
}                                               \
extern float4 __attribute__((overloadable)) \
        fnc(float4 v1, float4 v2, int4 *v3) {   \
    float4 r;                                   \
    int t[4];                                   \
    r.x = fnc(v1.x, v2.x, &t[0]);               \
    r.y = fnc(v1.y, v2.y, &t[1]);               \
    r.z = fnc(v1.z, v2.z, &t[2]);               \
    r.w = fnc(v1.w, v2.w, &t[3]);               \
    v3->x = t[0];                               \
    v3->y = t[1];                               \
    v3->z = t[2];                               \
    v3->w = t[3];                               \
    return r;                                   \
}

static const int iposinf = 0x7f800000;
static const int ineginf = 0xff800000;

static const float posinf() {
    float f = *((float*)&iposinf);
    return f;
}

static const float neginf() {
    float f = *((float*)&ineginf);
    return f;
}

static bool isinf(float f) {
    int i = *((int*)(void*)&f);
    return (i == iposinf) || (i == ineginf);
}

static bool isnan(float f) {
    int i = *((int*)(void*)&f);
    return (((i & 0x7f800000) == 0x7f800000) && (i & 0x007fffff));
}

static bool isposzero(float f) {
    int i = *((int*)(void*)&f);
    return (i == 0x00000000);
}

static bool isnegzero(float f) {
    int i = *((int*)(void*)&f);
    return (i == 0x80000000);
}

static bool iszero(float f) {
    return isposzero(f) || isnegzero(f);
}


extern float __attribute__((overloadable)) acos(float);
FN_FUNC_FN(acos)

extern float __attribute__((overloadable)) acosh(float);
FN_FUNC_FN(acosh)


extern float __attribute__((overloadable)) acospi(float v) {
    return acos(v) / M_PI;
}
FN_FUNC_FN(acospi)

extern float __attribute__((overloadable)) asin(float);
FN_FUNC_FN(asin)

extern float __attribute__((overloadable)) asinh(float);
FN_FUNC_FN(asinh)

extern float __attribute__((overloadable)) asinpi(float v) {
    return asin(v) / M_PI;
}
FN_FUNC_FN(asinpi)

extern float __attribute__((overloadable)) atan(float);
FN_FUNC_FN(atan)

extern float __attribute__((overloadable)) atan2(float, float);
FN_FUNC_FN_FN(atan2)

extern float __attribute__((overloadable)) atanh(float);
FN_FUNC_FN(atanh)

extern float __attribute__((overloadable)) atanpi(float v) {
    return atan(v) / M_PI;
}
FN_FUNC_FN(atanpi)


extern float __attribute__((overloadable)) atan2pi(float y, float x) {
    return atan2(y, x) / M_PI;
}
FN_FUNC_FN_FN(atan2pi)

extern float __attribute__((overloadable)) cbrt(float);
FN_FUNC_FN(cbrt)

extern float __attribute__((overloadable)) ceil(float);
FN_FUNC_FN(ceil)

extern float __attribute__((overloadable)) copysign(float, float);
FN_FUNC_FN_FN(copysign)

extern float __attribute__((overloadable)) cos(float);
FN_FUNC_FN(cos)

extern float __attribute__((overloadable)) cosh(float);
FN_FUNC_FN(cosh)

extern float __attribute__((overloadable)) cospi(float v) {
    return cos(v * M_PI);
}
FN_FUNC_FN(cospi)

extern float __attribute__((overloadable)) erfc(float);
FN_FUNC_FN(erfc)

extern float __attribute__((overloadable)) erf(float);
FN_FUNC_FN(erf)

extern float __attribute__((overloadable)) exp(float);
FN_FUNC_FN(exp)

extern float __attribute__((overloadable)) exp2(float);
FN_FUNC_FN(exp2)

extern float __attribute__((overloadable)) pow(float, float);

extern float __attribute__((overloadable)) exp10(float v) {
    return exp2(v * 3.321928095f);
}
FN_FUNC_FN(exp10)

extern float __attribute__((overloadable)) expm1(float);
FN_FUNC_FN(expm1)

extern float __attribute__((overloadable)) fabs(float v) {
    int i = *((int*)(void*)&v) & 0x7fffffff;
    return  *((float*)(void*)&i);
}
FN_FUNC_FN(fabs)

extern float __attribute__((overloadable)) fdim(float, float);
FN_FUNC_FN_FN(fdim)

extern float __attribute__((overloadable)) floor(float);
FN_FUNC_FN(floor)

extern float __attribute__((overloadable)) fma(float, float, float);
FN_FUNC_FN_FN_FN(fma)

extern float __attribute__((overloadable)) fmin(float, float);

extern float __attribute__((overloadable)) fmod(float, float);
FN_FUNC_FN_FN(fmod)

extern float __attribute__((overloadable)) fract(float v, float *iptr) {
    int i = (int)floor(v);
    if (iptr) {
        iptr[0] = i;
    }
    return fmin(v - i, 0x1.fffffep-1f);
}
FN_FUNC_FN_PFN(fract)

extern float __attribute__((overloadable)) frexp(float, int *);
FN_FUNC_FN_PIN(frexp)

extern float __attribute__((overloadable)) hypot(float, float);
FN_FUNC_FN_FN(hypot)

extern int __attribute__((overloadable)) ilogb(float);
IN_FUNC_FN(ilogb)

extern float __attribute__((overloadable)) ldexp(float, int);
FN_FUNC_FN_IN(ldexp)
FN_FUNC_FN_I(ldexp)

extern float __attribute__((overloadable)) lgamma(float);
FN_FUNC_FN(lgamma)
extern float __attribute__((overloadable)) lgamma(float, int*);
FN_FUNC_FN_PIN(lgamma)

extern float __attribute__((overloadable)) log(float);
FN_FUNC_FN(log)

extern float __attribute__((overloadable)) log10(float);
FN_FUNC_FN(log10)


extern float __attribute__((overloadable)) log2(float v) {
    return log10(v) * 3.321928095f;
}
FN_FUNC_FN(log2)

extern float __attribute__((overloadable)) log1p(float);
FN_FUNC_FN(log1p)

extern float __attribute__((overloadable)) logb(float);
FN_FUNC_FN(logb)

extern float __attribute__((overloadable)) mad(float a, float b, float c) {
    return a * b + c;
}
extern float2 __attribute__((overloadable)) mad(float2 a, float2 b, float2 c) {
    return a * b + c;
}
extern float3 __attribute__((overloadable)) mad(float3 a, float3 b, float3 c) {
    return a * b + c;
}
extern float4 __attribute__((overloadable)) mad(float4 a, float4 b, float4 c) {
    return a * b + c;
}

extern float __attribute__((overloadable)) modf(float, float *);
FN_FUNC_FN_PFN(modf);

extern float __attribute__((overloadable)) nan(uint v) {
    float f[1];
    uint32_t *ip = (uint32_t *)f;
    *ip = v | 0x7fc00000;
    return f[0];
}

extern float __attribute__((overloadable)) nextafter(float, float);
FN_FUNC_FN_FN(nextafter)

FN_FUNC_FN_FN(pow)

extern float __attribute__((overloadable)) pown(float v, int p) {
    /* The mantissa of a float has fewer bits than an int (24 effective vs. 31).
     * For very large ints, we'll lose whether the exponent is even or odd, making
     * the selection of a correct sign incorrect.  We correct this.  Use copysign
     * to handle the negative zero case.
     */
    float sign = (p & 0x1) ? copysign(1.f, v) : 1.f;
    float f = pow(v, (float)p);
    return copysign(f, sign);
}
FN_FUNC_FN_IN(pown)

extern float __attribute__((overloadable)) powr(float v, float p) {
    return pow(v, p);
}
extern float2 __attribute__((overloadable)) powr(float2 v, float2 p) {
    return pow(v, p);
}
extern float3 __attribute__((overloadable)) powr(float3 v, float3 p) {
    return pow(v, p);
}
extern float4 __attribute__((overloadable)) powr(float4 v, float4 p) {
    return pow(v, p);
}

extern float __attribute__((overloadable)) remainder(float, float);
FN_FUNC_FN_FN(remainder)

extern float __attribute__((overloadable)) remquo(float, float, int *);
FN_FUNC_FN_FN_PIN(remquo)

extern float __attribute__((overloadable)) rint(float);
FN_FUNC_FN(rint)

extern float __attribute__((overloadable)) rootn(float v, int r) {
    if (r == 0) {
        return posinf();
    }

    if (iszero(v)) {
        if (r < 0) {
            if (r & 1) {
                return copysign(posinf(), v);
            } else {
                return posinf();
            }
        } else {
            if (r & 1) {
                return copysign(0.f, v);
            } else {
                return 0.f;
            }
        }
    }

    if (!isinf(v) && !isnan(v) && (v < 0.f)) {
        if (r & 1) {
            return (-1.f * pow(-1.f * v, 1.f / r));
        } else {
            return nan(0);
        }
    }

    return pow(v, 1.f / r);
}
FN_FUNC_FN_IN(rootn);

extern float __attribute__((overloadable)) round(float);
FN_FUNC_FN(round)


extern float __attribute__((overloadable)) sqrt(float);
extern float __attribute__((overloadable)) rsqrt(float v) {
    return 1.f / sqrt(v);
}

#if !defined(__i386__) && !defined(__x86_64__)
FN_FUNC_FN(sqrt)
#else
extern float2 __attribute__((overloadable)) sqrt(float2);
extern float3 __attribute__((overloadable)) sqrt(float3);
extern float4 __attribute__((overloadable)) sqrt(float4);
#endif // !defined(__i386__) && !defined(__x86_64__)

FN_FUNC_FN(rsqrt)

extern float __attribute__((overloadable)) sin(float);
FN_FUNC_FN(sin)

extern float __attribute__((overloadable)) sincos(float v, float *cosptr) {
    *cosptr = cos(v);
    return sin(v);
}
extern float2 __attribute__((overloadable)) sincos(float2 v, float2 *cosptr) {
    *cosptr = cos(v);
    return sin(v);
}
extern float3 __attribute__((overloadable)) sincos(float3 v, float3 *cosptr) {
    *cosptr = cos(v);
    return sin(v);
}
extern float4 __attribute__((overloadable)) sincos(float4 v, float4 *cosptr) {
    *cosptr = cos(v);
    return sin(v);
}

extern float __attribute__((overloadable)) sinh(float);
FN_FUNC_FN(sinh)

extern float __attribute__((overloadable)) sinpi(float v) {
    return sin(v * M_PI);
}
FN_FUNC_FN(sinpi)

extern float __attribute__((overloadable)) tan(float);
FN_FUNC_FN(tan)

extern float __attribute__((overloadable)) tanh(float);
FN_FUNC_FN(tanh)

extern float __attribute__((overloadable)) tanpi(float v) {
    return tan(v * M_PI);
}
FN_FUNC_FN(tanpi)


extern float __attribute__((overloadable)) tgamma(float);
FN_FUNC_FN(tgamma)

extern float __attribute__((overloadable)) trunc(float);
FN_FUNC_FN(trunc)

// Int ops (partial), 6.11.3

#define XN_FUNC_YN(typeout, fnc, typein)                                \
extern typeout __attribute__((overloadable)) fnc(typein);               \
extern typeout##2 __attribute__((overloadable)) fnc(typein##2 v) {  \
    typeout##2 r;                                                       \
    r.x = fnc(v.x);                                                     \
    r.y = fnc(v.y);                                                     \
    return r;                                                           \
}                                                                       \
extern typeout##3 __attribute__((overloadable)) fnc(typein##3 v) {  \
    typeout##3 r;                                                       \
    r.x = fnc(v.x);                                                     \
    r.y = fnc(v.y);                                                     \
    r.z = fnc(v.z);                                                     \
    return r;                                                           \
}                                                                       \
extern typeout##4 __attribute__((overloadable)) fnc(typein##4 v) {  \
    typeout##4 r;                                                       \
    r.x = fnc(v.x);                                                     \
    r.y = fnc(v.y);                                                     \
    r.z = fnc(v.z);                                                     \
    r.w = fnc(v.w);                                                     \
    return r;                                                           \
}


#define UIN_FUNC_IN(fnc)          \
XN_FUNC_YN(uchar, fnc, char)      \
XN_FUNC_YN(ushort, fnc, short)    \
XN_FUNC_YN(uint, fnc, int)

#define IN_FUNC_IN(fnc)           \
XN_FUNC_YN(uchar, fnc, uchar)     \
XN_FUNC_YN(char, fnc, char)       \
XN_FUNC_YN(ushort, fnc, ushort)   \
XN_FUNC_YN(short, fnc, short)     \
XN_FUNC_YN(uint, fnc, uint)       \
XN_FUNC_YN(int, fnc, int)


#define XN_FUNC_XN_XN_BODY(type, fnc, body)         \
extern type __attribute__((overloadable))       \
        fnc(type v1, type v2) {                     \
    return body;                                    \
}                                                   \
extern type##2 __attribute__((overloadable))    \
        fnc(type##2 v1, type##2 v2) {               \
    type##2 r;                                      \
    r.x = fnc(v1.x, v2.x);                          \
    r.y = fnc(v1.y, v2.y);                          \
    return r;                                       \
}                                                   \
extern type##3 __attribute__((overloadable))    \
        fnc(type##3 v1, type##3 v2) {               \
    type##3 r;                                      \
    r.x = fnc(v1.x, v2.x);                          \
    r.y = fnc(v1.y, v2.y);                          \
    r.z = fnc(v1.z, v2.z);                          \
    return r;                                       \
}                                                   \
extern type##4 __attribute__((overloadable))    \
        fnc(type##4 v1, type##4 v2) {               \
    type##4 r;                                      \
    r.x = fnc(v1.x, v2.x);                          \
    r.y = fnc(v1.y, v2.y);                          \
    r.z = fnc(v1.z, v2.z);                          \
    r.w = fnc(v1.w, v2.w);                          \
    return r;                                       \
}

#define IN_FUNC_IN_IN_BODY(fnc, body) \
XN_FUNC_XN_XN_BODY(uchar, fnc, body)  \
XN_FUNC_XN_XN_BODY(char, fnc, body)   \
XN_FUNC_XN_XN_BODY(ushort, fnc, body) \
XN_FUNC_XN_XN_BODY(short, fnc, body)  \
XN_FUNC_XN_XN_BODY(uint, fnc, body)   \
XN_FUNC_XN_XN_BODY(int, fnc, body)    \
XN_FUNC_XN_XN_BODY(float, fnc, body)


/**
 * abs
 */
extern uint32_t __attribute__((overloadable)) abs(int32_t v) {
    if (v < 0)
        return -v;
    return v;
}
extern uint16_t __attribute__((overloadable)) abs(int16_t v) {
    if (v < 0)
        return -v;
    return v;
}
extern uint8_t __attribute__((overloadable)) abs(int8_t v) {
    if (v < 0)
        return -v;
    return v;
}

/**
 * clz
 * __builtin_clz only accepts a 32-bit unsigned int, so every input will be
 * expanded to 32 bits. For our smaller data types, we need to subtract off
 * these unused top bits (that will be always be composed of zeros).
 */
extern uint32_t __attribute__((overloadable)) clz(uint32_t v) {
    return __builtin_clz(v);
}
extern uint16_t __attribute__((overloadable)) clz(uint16_t v) {
    return __builtin_clz(v) - 16;
}
extern uint8_t __attribute__((overloadable)) clz(uint8_t v) {
    return __builtin_clz(v) - 24;
}
extern int32_t __attribute__((overloadable)) clz(int32_t v) {
    return __builtin_clz(v);
}
extern int16_t __attribute__((overloadable)) clz(int16_t v) {
    return __builtin_clz(((uint32_t)v) & 0x0000ffff) - 16;
}
extern int8_t __attribute__((overloadable)) clz(int8_t v) {
    return __builtin_clz(((uint32_t)v) & 0x000000ff) - 24;
}


UIN_FUNC_IN(abs)
IN_FUNC_IN(clz)


// 6.11.4


extern float __attribute__((overloadable)) degrees(float radians) {
    return radians * (180.f / M_PI);
}
extern float2 __attribute__((overloadable)) degrees(float2 radians) {
    return radians * (180.f / M_PI);
}
extern float3 __attribute__((overloadable)) degrees(float3 radians) {
    return radians * (180.f / M_PI);
}
extern float4 __attribute__((overloadable)) degrees(float4 radians) {
    return radians * (180.f / M_PI);
}

extern float __attribute__((overloadable)) mix(float start, float stop, float amount) {
    return start + (stop - start) * amount;
}
extern float2 __attribute__((overloadable)) mix(float2 start, float2 stop, float2 amount) {
    return start + (stop - start) * amount;
}
extern float3 __attribute__((overloadable)) mix(float3 start, float3 stop, float3 amount) {
    return start + (stop - start) * amount;
}
extern float4 __attribute__((overloadable)) mix(float4 start, float4 stop, float4 amount) {
    return start + (stop - start) * amount;
}
extern float2 __attribute__((overloadable)) mix(float2 start, float2 stop, float amount) {
    return start + (stop - start) * amount;
}
extern float3 __attribute__((overloadable)) mix(float3 start, float3 stop, float amount) {
    return start + (stop - start) * amount;
}
extern float4 __attribute__((overloadable)) mix(float4 start, float4 stop, float amount) {
    return start + (stop - start) * amount;
}

extern float __attribute__((overloadable)) radians(float degrees) {
    return degrees * (M_PI / 180.f);
}
extern float2 __attribute__((overloadable)) radians(float2 degrees) {
    return degrees * (M_PI / 180.f);
}
extern float3 __attribute__((overloadable)) radians(float3 degrees) {
    return degrees * (M_PI / 180.f);
}
extern float4 __attribute__((overloadable)) radians(float4 degrees) {
    return degrees * (M_PI / 180.f);
}

extern float __attribute__((overloadable)) step(float edge, float v) {
    return (v < edge) ? 0.f : 1.f;
}
extern float2 __attribute__((overloadable)) step(float2 edge, float2 v) {
    float2 r;
    r.x = (v.x < edge.x) ? 0.f : 1.f;
    r.y = (v.y < edge.y) ? 0.f : 1.f;
    return r;
}
extern float3 __attribute__((overloadable)) step(float3 edge, float3 v) {
    float3 r;
    r.x = (v.x < edge.x) ? 0.f : 1.f;
    r.y = (v.y < edge.y) ? 0.f : 1.f;
    r.z = (v.z < edge.z) ? 0.f : 1.f;
    return r;
}
extern float4 __attribute__((overloadable)) step(float4 edge, float4 v) {
    float4 r;
    r.x = (v.x < edge.x) ? 0.f : 1.f;
    r.y = (v.y < edge.y) ? 0.f : 1.f;
    r.z = (v.z < edge.z) ? 0.f : 1.f;
    r.w = (v.w < edge.w) ? 0.f : 1.f;
    return r;
}
extern float2 __attribute__((overloadable)) step(float2 edge, float v) {
    float2 r;
    r.x = (v < edge.x) ? 0.f : 1.f;
    r.y = (v < edge.y) ? 0.f : 1.f;
    return r;
}
extern float3 __attribute__((overloadable)) step(float3 edge, float v) {
    float3 r;
    r.x = (v < edge.x) ? 0.f : 1.f;
    r.y = (v < edge.y) ? 0.f : 1.f;
    r.z = (v < edge.z) ? 0.f : 1.f;
    return r;
}
extern float4 __attribute__((overloadable)) step(float4 edge, float v) {
    float4 r;
    r.x = (v < edge.x) ? 0.f : 1.f;
    r.y = (v < edge.y) ? 0.f : 1.f;
    r.z = (v < edge.z) ? 0.f : 1.f;
    r.w = (v < edge.w) ? 0.f : 1.f;
    return r;
}
extern float2 __attribute__((overloadable)) step(float edge, float2 v) {
    float2 r;
    r.x = (v.x < edge) ? 0.f : 1.f;
    r.y = (v.y < edge) ? 0.f : 1.f;
    return r;
}
extern float3 __attribute__((overloadable)) step(float edge, float3 v) {
    float3 r;
    r.x = (v.x < edge) ? 0.f : 1.f;
    r.y = (v.y < edge) ? 0.f : 1.f;
    r.z = (v.z < edge) ? 0.f : 1.f;
    return r;
}
extern float4 __attribute__((overloadable)) step(float edge, float4 v) {
    float4 r;
    r.x = (v.x < edge) ? 0.f : 1.f;
    r.y = (v.y < edge) ? 0.f : 1.f;
    r.z = (v.z < edge) ? 0.f : 1.f;
    r.w = (v.w < edge) ? 0.f : 1.f;
    return r;
}

extern float __attribute__((overloadable)) smoothstep(float, float, float);
extern float2 __attribute__((overloadable)) smoothstep(float2, float2, float2);
extern float3 __attribute__((overloadable)) smoothstep(float3, float3, float3);
extern float4 __attribute__((overloadable)) smoothstep(float4, float4, float4);
extern float2 __attribute__((overloadable)) smoothstep(float, float, float2);
extern float3 __attribute__((overloadable)) smoothstep(float, float, float3);
extern float4 __attribute__((overloadable)) smoothstep(float, float, float4);

extern float __attribute__((overloadable)) sign(float v) {
    if (v > 0) return 1.f;
    if (v < 0) return -1.f;
    return v;
}
FN_FUNC_FN(sign)


// 6.11.5
extern float3 __attribute__((overloadable)) cross(float3 lhs, float3 rhs) {
    float3 r;
    r.x = lhs.y * rhs.z  - lhs.z * rhs.y;
    r.y = lhs.z * rhs.x  - lhs.x * rhs.z;
    r.z = lhs.x * rhs.y  - lhs.y * rhs.x;
    return r;
}

extern float4 __attribute__((overloadable)) cross(float4 lhs, float4 rhs) {
    float4 r;
    r.x = lhs.y * rhs.z  - lhs.z * rhs.y;
    r.y = lhs.z * rhs.x  - lhs.x * rhs.z;
    r.z = lhs.x * rhs.y  - lhs.y * rhs.x;
    r.w = 0.f;
    return r;
}

#if !defined(__i386__) && !defined(__x86_64__)

extern float __attribute__((overloadable)) dot(float lhs, float rhs) {
    return lhs * rhs;
}
extern float __attribute__((overloadable)) dot(float2 lhs, float2 rhs) {
    return lhs.x*rhs.x + lhs.y*rhs.y;
}
extern float __attribute__((overloadable)) dot(float3 lhs, float3 rhs) {
    return lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z;
}
extern float __attribute__((overloadable)) dot(float4 lhs, float4 rhs) {
    return lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z + lhs.w*rhs.w;
}

extern float __attribute__((overloadable)) length(float v) {
    return fabs(v);
}
extern float __attribute__((overloadable)) length(float2 v) {
    return sqrt(v.x*v.x + v.y*v.y);
}
extern float __attribute__((overloadable)) length(float3 v) {
    return sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
}
extern float __attribute__((overloadable)) length(float4 v) {
    return sqrt(v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w);
}

#else

extern float __attribute__((overloadable)) length(float v);
extern float __attribute__((overloadable)) length(float2 v);
extern float __attribute__((overloadable)) length(float3 v);
extern float __attribute__((overloadable)) length(float4 v);

#endif // !defined(__i386__) && !defined(__x86_64__)

extern float __attribute__((overloadable)) distance(float lhs, float rhs) {
    return length(lhs - rhs);
}
extern float __attribute__((overloadable)) distance(float2 lhs, float2 rhs) {
    return length(lhs - rhs);
}
extern float __attribute__((overloadable)) distance(float3 lhs, float3 rhs) {
    return length(lhs - rhs);
}
extern float __attribute__((overloadable)) distance(float4 lhs, float4 rhs) {
    return length(lhs - rhs);
}

/* For the normalization functions, vectors of length 0 should simply be
 * returned (i.e. all the components of that vector are 0).
 */
extern float __attribute__((overloadable)) normalize(float v) {
    if (v == 0.0f) {
        return 0.0f;
    } else if (v < 0.0f) {
        return -1.0f;
    } else {
        return 1.0f;
    }
}
extern float2 __attribute__((overloadable)) normalize(float2 v) {
    float l = length(v);
    return l == 0.0f ? v : v / l;
}
extern float3 __attribute__((overloadable)) normalize(float3 v) {
    float l = length(v);
    return l == 0.0f ? v : v / l;
}
extern float4 __attribute__((overloadable)) normalize(float4 v) {
    float l = length(v);
    return l == 0.0f ? v : v / l;
}

extern float __attribute__((overloadable)) half_sqrt(float v) {
    return sqrt(v);
}
FN_FUNC_FN(half_sqrt)

extern float __attribute__((overloadable)) fast_length(float v) {
    return fabs(v);
}
extern float __attribute__((overloadable)) fast_length(float2 v) {
    return half_sqrt(v.x*v.x + v.y*v.y);
}
extern float __attribute__((overloadable)) fast_length(float3 v) {
    return half_sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
}
extern float __attribute__((overloadable)) fast_length(float4 v) {
    return half_sqrt(v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w);
}

extern float __attribute__((overloadable)) fast_distance(float lhs, float rhs) {
    return fast_length(lhs - rhs);
}
extern float __attribute__((overloadable)) fast_distance(float2 lhs, float2 rhs) {
    return fast_length(lhs - rhs);
}
extern float __attribute__((overloadable)) fast_distance(float3 lhs, float3 rhs) {
    return fast_length(lhs - rhs);
}
extern float __attribute__((overloadable)) fast_distance(float4 lhs, float4 rhs) {
    return fast_length(lhs - rhs);
}

extern float __attribute__((overloadable)) half_rsqrt(float);

/* For the normalization functions, vectors of length 0 should simply be
 * returned (i.e. all the components of that vector are 0).
 */
extern float __attribute__((overloadable)) fast_normalize(float v) {
    if (v == 0.0f) {
        return 0.0f;
    } else if (v < 0.0f) {
        return -1.0f;
    } else {
        return 1.0f;
    }
}
// If the length is 0, then rlength should be NaN.
extern float2 __attribute__((overloadable)) fast_normalize(float2 v) {
    float rlength = half_rsqrt(v.x*v.x + v.y*v.y);
    return (rlength == rlength) ? v * rlength : v;
}
extern float3 __attribute__((overloadable)) fast_normalize(float3 v) {
    float rlength = half_rsqrt(v.x*v.x + v.y*v.y + v.z*v.z);
    return (rlength == rlength) ? v * rlength : v;
}
extern float4 __attribute__((overloadable)) fast_normalize(float4 v) {
    float rlength = half_rsqrt(v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w);
    return (rlength == rlength) ? v * rlength : v;
}

extern float __attribute__((overloadable)) half_recip(float v) {
    return 1.f / v;
}

/*
extern float __attribute__((overloadable)) approx_atan(float x) {
    if (x == 0.f)
        return 0.f;
    if (x < 0.f)
        return -1.f * approx_atan(-1.f * x);
    if (x > 1.f)
        return M_PI_2 - approx_atan(approx_recip(x));
    return x * approx_recip(1.f + 0.28f * x*x);
}
FN_FUNC_FN(approx_atan)
*/

typedef union
{
  float fv;
  int32_t iv;
} ieee_float_shape_type;

/* Get a 32 bit int from a float.  */

#define GET_FLOAT_WORD(i,d)                 \
do {                                \
  ieee_float_shape_type gf_u;                   \
  gf_u.fv = (d);                     \
  (i) = gf_u.iv;                      \
} while (0)

/* Set a float from a 32 bit int.  */

#define SET_FLOAT_WORD(d,i)                 \
do {                                \
  ieee_float_shape_type sf_u;                   \
  sf_u.iv = (i);                      \
  (d) = sf_u.fv;                     \
} while (0)



// Valid -125 to 125
extern float __attribute__((overloadable)) native_exp2(float v) {
    int32_t iv = (int)v;
    int32_t x = iv + (iv >> 31); // ~floor(v)
    float r = (v - x);

    float fo;
    SET_FLOAT_WORD(fo, (x + 127) << 23);

    r *= 0.694f; // ~ log(e) / log(2)
    float r2 = r*r;
    float adj = 1.f + r + (r2 * 0.5f) + (r2*r * 0.166666f) + (r2*r2 * 0.0416666f);
    return fo * adj;
}

extern float2 __attribute__((overloadable)) native_exp2(float2 v) {
    int2 iv = convert_int2(v);
    int2 x = iv + (iv >> (int2)31);//floor(v);
    float2 r = (v - convert_float2(x));

    x += 127;

    float2 fo = (float2)(x << (int2)23);

    r *= 0.694f; // ~ log(e) / log(2)
    float2 r2 = r*r;
    float2 adj = 1.f + r + (r2 * 0.5f) + (r2*r * 0.166666f) + (r2*r2 * 0.0416666f);
    return fo * adj;
}

extern float4 __attribute__((overloadable)) native_exp2(float4 v) {
    int4 iv = convert_int4(v);
    int4 x = iv + (iv >> (int4)31);//floor(v);
    float4 r = (v - convert_float4(x));

    x += 127;

    float4 fo = (float4)(x << (int4)23);

    r *= 0.694f; // ~ log(e) / log(2)
    float4 r2 = r*r;
    float4 adj = 1.f + r + (r2 * 0.5f) + (r2*r * 0.166666f) + (r2*r2 * 0.0416666f);
    return fo * adj;
}

extern float3 __attribute__((overloadable)) native_exp2(float3 v) {
    float4 t = 1.f;
    t.xyz = v;
    return native_exp2(t).xyz;
}


extern float __attribute__((overloadable)) native_exp(float v) {
    return native_exp2(v * 1.442695041f);
}
extern float2 __attribute__((overloadable)) native_exp(float2 v) {
    return native_exp2(v * 1.442695041f);
}
extern float3 __attribute__((overloadable)) native_exp(float3 v) {
    return native_exp2(v * 1.442695041f);
}
extern float4 __attribute__((overloadable)) native_exp(float4 v) {
    return native_exp2(v * 1.442695041f);
}

extern float __attribute__((overloadable)) native_exp10(float v) {
    return native_exp2(v * 3.321928095f);
}
extern float2 __attribute__((overloadable)) native_exp10(float2 v) {
    return native_exp2(v * 3.321928095f);
}
extern float3 __attribute__((overloadable)) native_exp10(float3 v) {
    return native_exp2(v * 3.321928095f);
}
extern float4 __attribute__((overloadable)) native_exp10(float4 v) {
    return native_exp2(v * 3.321928095f);
}

extern float __attribute__((overloadable)) native_log2(float v) {
    int32_t ibits;
    GET_FLOAT_WORD(ibits, v);

    int32_t e = (ibits >> 23) & 0xff;

    ibits &= 0x7fffff;
    ibits |= 127 << 23;

    float ir;
    SET_FLOAT_WORD(ir, ibits);
    ir -= 1.5f;
    float ir2 = ir*ir;
    float adj2 = (0.405465108f / 0.693147181f) +
                 ((0.666666667f / 0.693147181f) * ir) -
                 ((0.222222222f / 0.693147181f) * ir2) +
                 ((0.098765432f / 0.693147181f) * ir*ir2) -
                 ((0.049382716f / 0.693147181f) * ir2*ir2) +
                 ((0.026337449f / 0.693147181f) * ir*ir2*ir2) -
                 ((0.014631916f / 0.693147181f) * ir2*ir2*ir2);
    return (float)(e - 127) + adj2;
}
extern float2 __attribute__((overloadable)) native_log2(float2 v) {
    float2 v2 = {native_log2(v.x), native_log2(v.y)};
    return v2;
}
extern float3 __attribute__((overloadable)) native_log2(float3 v) {
    float3 v2 = {native_log2(v.x), native_log2(v.y), native_log2(v.z)};
    return v2;
}
extern float4 __attribute__((overloadable)) native_log2(float4 v) {
    float4 v2 = {native_log2(v.x), native_log2(v.y), native_log2(v.z), native_log2(v.w)};
    return v2;
}

extern float __attribute__((overloadable)) native_log(float v) {
    return native_log2(v) * (1.f / 1.442695041f);
}
extern float2 __attribute__((overloadable)) native_log(float2 v) {
    return native_log2(v) * (1.f / 1.442695041f);
}
extern float3 __attribute__((overloadable)) native_log(float3 v) {
    return native_log2(v) * (1.f / 1.442695041f);
}
extern float4 __attribute__((overloadable)) native_log(float4 v) {
    return native_log2(v) * (1.f / 1.442695041f);
}

extern float __attribute__((overloadable)) native_log10(float v) {
    return native_log2(v) * (1.f / 3.321928095f);
}
extern float2 __attribute__((overloadable)) native_log10(float2 v) {
    return native_log2(v) * (1.f / 3.321928095f);
}
extern float3 __attribute__((overloadable)) native_log10(float3 v) {
    return native_log2(v) * (1.f / 3.321928095f);
}
extern float4 __attribute__((overloadable)) native_log10(float4 v) {
    return native_log2(v) * (1.f / 3.321928095f);
}


extern float __attribute__((overloadable)) native_powr(float v, float y) {
    float v2 = native_log2(v);
    v2 = fmax(v2 * y, -125.f);
    return native_exp2(v2);
}
extern float2 __attribute__((overloadable)) native_powr(float2 v, float2 y) {
    float2 v2 = native_log2(v);
    v2 = fmax(v2 * y, -125.f);
    return native_exp2(v2);
}
extern float3 __attribute__((overloadable)) native_powr(float3 v, float3 y) {
    float3 v2 = native_log2(v);
    v2 = fmax(v2 * y, -125.f);
    return native_exp2(v2);
}
extern float4 __attribute__((overloadable)) native_powr(float4 v, float4 y) {
    float4 v2 = native_log2(v);
    v2 = fmax(v2 * y, -125.f);
    return native_exp2(v2);
}

extern double __attribute__((overloadable)) min(double v1, double v2) {
    return v1 < v2 ? v1 : v2;
}

extern double2 __attribute__((overloadable)) min(double2 v1, double2 v2) {
    double2 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    return r;
}

extern double3 __attribute__((overloadable)) min(double3 v1, double3 v2) {
    double3 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    r.z = v1.z < v2.z ? v1.z : v2.z;
    return r;
}

extern double4 __attribute__((overloadable)) min(double4 v1, double4 v2) {
    double4 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    r.z = v1.z < v2.z ? v1.z : v2.z;
    r.w = v1.w < v2.w ? v1.w : v2.w;
    return r;
}

extern long __attribute__((overloadable)) min(long v1, long v2) {
    return v1 < v2 ? v1 : v2;
}
extern long2 __attribute__((overloadable)) min(long2 v1, long2 v2) {
    long2 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    return r;
}
extern long3 __attribute__((overloadable)) min(long3 v1, long3 v2) {
    long3 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    r.z = v1.z < v2.z ? v1.z : v2.z;
    return r;
}
extern long4 __attribute__((overloadable)) min(long4 v1, long4 v2) {
    long4 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    r.z = v1.z < v2.z ? v1.z : v2.z;
    r.w = v1.w < v2.w ? v1.w : v2.w;
    return r;
}

extern ulong __attribute__((overloadable)) min(ulong v1, ulong v2) {
    return v1 < v2 ? v1 : v2;
}
extern ulong2 __attribute__((overloadable)) min(ulong2 v1, ulong2 v2) {
    ulong2 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    return r;
}
extern ulong3 __attribute__((overloadable)) min(ulong3 v1, ulong3 v2) {
    ulong3 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    r.z = v1.z < v2.z ? v1.z : v2.z;
    return r;
}
extern ulong4 __attribute__((overloadable)) min(ulong4 v1, ulong4 v2) {
    ulong4 r;
    r.x = v1.x < v2.x ? v1.x : v2.x;
    r.y = v1.y < v2.y ? v1.y : v2.y;
    r.z = v1.z < v2.z ? v1.z : v2.z;
    r.w = v1.w < v2.w ? v1.w : v2.w;
    return r;
}

extern double __attribute__((overloadable)) max(double v1, double v2) {
    return v1 > v2 ? v1 : v2;
}

extern double2 __attribute__((overloadable)) max(double2 v1, double2 v2) {
    double2 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    return r;
}

extern double3 __attribute__((overloadable)) max(double3 v1, double3 v2) {
    double3 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    r.z = v1.z > v2.z ? v1.z : v2.z;
    return r;
}

extern double4 __attribute__((overloadable)) max(double4 v1, double4 v2) {
    double4 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    r.z = v1.z > v2.z ? v1.z : v2.z;
    r.w = v1.w > v2.w ? v1.w : v2.w;
    return r;
}

extern long __attribute__((overloadable)) max(long v1, long v2) {
    return v1 > v2 ? v1 : v2;
}
extern long2 __attribute__((overloadable)) max(long2 v1, long2 v2) {
    long2 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    return r;
}
extern long3 __attribute__((overloadable)) max(long3 v1, long3 v2) {
    long3 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    r.z = v1.z > v2.z ? v1.z : v2.z;
    return r;
}
extern long4 __attribute__((overloadable)) max(long4 v1, long4 v2) {
    long4 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    r.z = v1.z > v2.z ? v1.z : v2.z;
    r.w = v1.w > v2.w ? v1.w : v2.w;
    return r;
}

extern ulong __attribute__((overloadable)) max(ulong v1, ulong v2) {
    return v1 > v2 ? v1 : v2;
}
extern ulong2 __attribute__((overloadable)) max(ulong2 v1, ulong2 v2) {
    ulong2 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    return r;
}
extern ulong3 __attribute__((overloadable)) max(ulong3 v1, ulong3 v2) {
    ulong3 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    r.z = v1.z > v2.z ? v1.z : v2.z;
    return r;
}
extern ulong4 __attribute__((overloadable)) max(ulong4 v1, ulong4 v2) {
    ulong4 r;
    r.x = v1.x > v2.x ? v1.x : v2.x;
    r.y = v1.y > v2.y ? v1.y : v2.y;
    r.z = v1.z > v2.z ? v1.z : v2.z;
    r.w = v1.w > v2.w ? v1.w : v2.w;
    return r;
}

#define THUNK_NATIVE_F(fn) \
    float __attribute__((overloadable)) native_##fn(float v) { return fn(v);} \
    float2 __attribute__((overloadable)) native_##fn(float2 v) { return fn(v);} \
    float3 __attribute__((overloadable)) native_##fn(float3 v) { return fn(v);} \
    float4 __attribute__((overloadable)) native_##fn(float4 v) { return fn(v);}

#define THUNK_NATIVE_F_F(fn) \
    float __attribute__((overloadable)) native_##fn(float v1, float v2) { return fn(v1, v2);} \
    float2 __attribute__((overloadable)) native_##fn(float2 v1, float2 v2) { return fn(v1, v2);} \
    float3 __attribute__((overloadable)) native_##fn(float3 v1, float3 v2) { return fn(v1, v2);} \
    float4 __attribute__((overloadable)) native_##fn(float4 v1, float4 v2) { return fn(v1, v2);}

#define THUNK_NATIVE_F_FP(fn) \
    float __attribute__((overloadable)) native_##fn(float v1, float *v2) { return fn(v1, v2);} \
    float2 __attribute__((overloadable)) native_##fn(float2 v1, float2 *v2) { return fn(v1, v2);} \
    float3 __attribute__((overloadable)) native_##fn(float3 v1, float3 *v2) { return fn(v1, v2);} \
    float4 __attribute__((overloadable)) native_##fn(float4 v1, float4 *v2) { return fn(v1, v2);}

#define THUNK_NATIVE_F_I(fn) \
    float __attribute__((overloadable)) native_##fn(float v1, int v2) { return fn(v1, v2);} \
    float2 __attribute__((overloadable)) native_##fn(float2 v1, int2 v2) { return fn(v1, v2);} \
    float3 __attribute__((overloadable)) native_##fn(float3 v1, int3 v2) { return fn(v1, v2);} \
    float4 __attribute__((overloadable)) native_##fn(float4 v1, int4 v2) { return fn(v1, v2);}

THUNK_NATIVE_F(acos)
THUNK_NATIVE_F(acosh)
THUNK_NATIVE_F(acospi)
THUNK_NATIVE_F(asin)
THUNK_NATIVE_F(asinh)
THUNK_NATIVE_F(asinpi)
THUNK_NATIVE_F(atan)
THUNK_NATIVE_F_F(atan2)
THUNK_NATIVE_F(atanh)
THUNK_NATIVE_F(atanpi)
THUNK_NATIVE_F_F(atan2pi)
THUNK_NATIVE_F(cbrt)
THUNK_NATIVE_F(cos)
THUNK_NATIVE_F(cosh)
THUNK_NATIVE_F(cospi)
THUNK_NATIVE_F(expm1)
THUNK_NATIVE_F_F(hypot)
THUNK_NATIVE_F(log1p)
THUNK_NATIVE_F_I(rootn)
THUNK_NATIVE_F(rsqrt)
THUNK_NATIVE_F(sqrt)
THUNK_NATIVE_F(sin)
THUNK_NATIVE_F_FP(sincos)
THUNK_NATIVE_F(sinh)
THUNK_NATIVE_F(sinpi)
THUNK_NATIVE_F(tan)
THUNK_NATIVE_F(tanh)
THUNK_NATIVE_F(tanpi)

#undef THUNK_NATIVE_F
#undef THUNK_NATIVE_F_F
#undef THUNK_NATIVE_F_I
#undef THUNK_NATIVE_F_FP

float __attribute__((overloadable)) native_normalize(float v) { return fast_normalize(v);}
float2 __attribute__((overloadable)) native_normalize(float2 v) { return fast_normalize(v);}
float3 __attribute__((overloadable)) native_normalize(float3 v) { return fast_normalize(v);}
float4 __attribute__((overloadable)) native_normalize(float4 v) { return fast_normalize(v);}

float __attribute__((overloadable)) native_distance(float v1, float v2) { return fast_distance(v1, v2);}
float __attribute__((overloadable)) native_distance(float2 v1, float2 v2) { return fast_distance(v1, v2);}
float __attribute__((overloadable)) native_distance(float3 v1, float3 v2) { return fast_distance(v1, v2);}
float __attribute__((overloadable)) native_distance(float4 v1, float4 v2) { return fast_distance(v1, v2);}

float __attribute__((overloadable)) native_length(float v) { return fast_length(v);}
float __attribute__((overloadable)) native_length(float2 v) { return fast_length(v);}
float __attribute__((overloadable)) native_length(float3 v) { return fast_length(v);}
float __attribute__((overloadable)) native_length(float4 v) { return fast_length(v);}

float __attribute__((overloadable)) native_divide(float v1, float v2) { return v1 / v2;}
float2 __attribute__((overloadable)) native_divide(float2 v1, float2 v2) { return v1 / v2;}
float3 __attribute__((overloadable)) native_divide(float3 v1, float3 v2) { return v1 / v2;}
float4 __attribute__((overloadable)) native_divide(float4 v1, float4 v2) { return v1 / v2;}

float __attribute__((overloadable)) native_recip(float v) { return 1.f / v;}
float2 __attribute__((overloadable)) native_recip(float2 v) { return ((float2)1.f) / v;}
float3 __attribute__((overloadable)) native_recip(float3 v) { return ((float3)1.f) / v;}
float4 __attribute__((overloadable)) native_recip(float4 v) { return ((float4)1.f) / v;}





#undef FN_FUNC_FN
#undef IN_FUNC_FN
#undef FN_FUNC_FN_FN
#undef FN_FUNC_FN_F
#undef FN_FUNC_FN_IN
#undef FN_FUNC_FN_I
#undef FN_FUNC_FN_PFN
#undef FN_FUNC_FN_PIN
#undef FN_FUNC_FN_FN_FN
#undef FN_FUNC_FN_FN_PIN
#undef XN_FUNC_YN
#undef UIN_FUNC_IN
#undef IN_FUNC_IN
#undef XN_FUNC_XN_XN_BODY
#undef IN_FUNC_IN_IN_BODY