-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdlib.ispc
1653 lines (1388 loc) · 57.8 KB
/
stdlib.ispc
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
// -*- mode: c++ -*-
/*
Copyright (c) 2010-2011, Intel Corporation
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Intel Corporation nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** @file stdlib.ispc
@brief Portion of the ispc standard library implementation that's in
ispc code
*/
///////////////////////////////////////////////////////////////////////////
// Low level primitives
static inline float floatbits(unsigned int a) {
return __floatbits_varying_int32(a);
}
static inline uniform float floatbits(uniform unsigned int a) {
return __floatbits_uniform_int32(a);
}
static inline float floatbits(int a) {
return __floatbits_varying_int32(a);
}
static inline uniform float floatbits(uniform int a) {
return __floatbits_uniform_int32(a);
}
static inline double doublebits(unsigned int64 a) {
return __doublebits_varying_int64(a);
}
static inline uniform double doublebits(uniform unsigned int64 a) {
return __doublebits_uniform_int64(a);
}
static inline unsigned int intbits(float a) {
return __intbits_varying_float(a);
}
static inline uniform unsigned int intbits(uniform float a) {
return __intbits_uniform_float(a);
}
static inline unsigned int64 intbits(double d) {
return __intbits_varying_double(d);
}
static inline uniform unsigned int64 intbits(uniform double d) {
return __intbits_uniform_double(d);
}
static inline float broadcast(float v, uniform int i) {
return __broadcast_float(v, i);
}
static inline int32 broadcast(int32 v, uniform int i) {
return __broadcast_int32(v, i);
}
static inline double broadcast(double v, uniform int i) {
return __broadcast_double(v, i);
}
static inline int64 broadcast(int64 v, uniform int i) {
return __broadcast_int64(v, i);
}
static inline float rotate(float v, uniform int i) {
return __rotate_float(v, i);
}
static inline int32 rotate(int32 v, uniform int i) {
return __rotate_int32(v, i);
}
static inline double rotate(double v, uniform int i) {
return __rotate_double(v, i);
}
static inline int64 rotate(int64 v, uniform int i) {
return __rotate_int64(v, i);
}
static inline float shuffle(float v, int i) {
return __shuffle_float(v, i);
}
static inline int32 shuffle(int32 v, int i) {
return __shuffle_int32(v, i);
}
static inline double shuffle(double v, int i) {
return __shuffle_double(v, i);
}
static inline int64 shuffle(int64 v, int i) {
return __shuffle_int64(v, i);
}
// x[i]
static inline uniform float extract(float x, uniform int i) {
return __extract(x, i);
}
// x[i] = v
static inline float insert(float x, uniform int i, uniform float v) {
return __insert(x, i, v);
}
static inline uniform int extract(int x, uniform int i) {
return intbits(extract(floatbits(x), i));
}
static inline int insert(int x, uniform int i, uniform int v) {
return intbits(insert(floatbits(x), i, floatbits(v)));
}
static inline uniform unsigned int extract(unsigned int x, uniform int i) {
return intbits(extract(floatbits(x), i));
}
static inline unsigned int insert(unsigned int x, uniform int i, uniform unsigned int v) {
return intbits(insert(floatbits(x), i, floatbits(v)));
}
static inline uniform bool any(bool v) {
// We only care about whether "any" is true for the active program instances,
// so we have to make v with the current program mask.
return __movmsk(v & __mask) != 0;
}
static inline uniform bool all(bool v) {
// As with any(), we need to explicitly mask v with the current program mask
// so we're only looking at the current lanes
bool match = ((v & __mask) == __mask);
return __movmsk(match) == (1 << programCount) - 1;
}
static inline uniform int popcnt(uniform int v) {
return __popcnt(v);
}
static inline int popcnt(int v) {
int r;
uniform int i;
for (i = 0; i < programCount; ++i)
r = insert(r, i, popcnt(extract(v, i)));
return (r & __mask);
}
static inline uniform int popcnt(bool v) {
// As with any() and all(), only count across the active lanes
return __popcnt(__movmsk(v & __mask));
}
static inline uniform int lanemask() {
return __movmsk(__mask);
}
///////////////////////////////////////////////////////////////////////////
// Horizontal ops / reductions
static inline uniform float reduce_add(float x) {
// zero the lanes where the mask is off
return __reduce_add_float(__mask ? x : 0.);
}
static inline uniform float reduce_min(float v) {
// For the lanes where the mask is off, replace the given value with
// infinity, so that it doesn't affect the result.
int iflt_max = 0x7f800000; // infinity
// Must use __floatbits_varying_int32, not floatbits(), since with the
// latter the current mask enters into the returned result...
return __reduce_min_float(__mask ? v : __floatbits_varying_int32(iflt_max));
}
static inline uniform float reduce_max(float v) {
// For the lanes where the mask is off, replace the given value with
// negative infinity, so that it doesn't affect the result.
const uniform int iflt_neg_max = 0xff800000; // -infinity
// Must use __floatbits_varying_int32, not floatbits(), since with the
// latter the current mask enters into the returned result...
return __reduce_max_float(__mask ? v : __floatbits_varying_int32(iflt_neg_max));
}
static inline uniform int reduce_add(int x) {
// Zero out the values for lanes that aren't running
return __reduce_add_int32(x & (int)__mask);
}
static inline uniform int reduce_min(int v) {
// Set values for non-running lanes to the maximum integer value so
// they don't affect the result.
int int_max = 0x7fffffff;
return __reduce_min_int32(__mask ? v : int_max);
}
static inline uniform int reduce_max(int v) {
// Set values for non-running lanes to the minimum integer value so
// they don't affect the result.
int int_min = 0x80000000;
return __reduce_max_int32(__mask ? v : int_min);
}
static inline uniform unsigned int reduce_add(unsigned int x) {
// Set values for non-running lanes to zero so they don't affect the
// result.
return __reduce_add_uint32(x & (int)__mask);
}
static inline uniform unsigned int reduce_min(unsigned int v) {
// Set values for non-running lanes to the maximum unsigned integer
// value so they don't affect the result.
unsigned int uint_max = 0xffffffff;
return __reduce_min_uint32(__mask ? v : uint_max);
}
static inline uniform unsigned int reduce_max(unsigned int v) {
// Set values for non-running lanes to zero so they don't affect the
// result.
return __reduce_max_uint32(__mask ? v : 0);
}
///////////////////////////////////////////////////////////////////////////
// packed load, store
static inline uniform unsigned int packed_load_active(uniform int a[], uniform int start,
reference int vals) {
return __packed_load_active(a, start, vals, __mask);
}
static inline uniform unsigned int packed_store_active(uniform int a[], uniform int start,
int vals) {
return __packed_store_active(a, start, vals, __mask);
}
///////////////////////////////////////////////////////////////////////////
// Load/store from/to 8/16-bit types
static inline unsigned int load_from_int8(uniform int a[], uniform int offset) {
return __load_uint8(a, offset);
}
static inline void store_to_int8(uniform int a[], uniform int offset,
unsigned int val) {
__store_uint8(a, offset, val, __mask);
}
static inline unsigned int load_from_int16(uniform int a[], uniform int offset) {
return __load_uint16(a, offset);
}
static inline void store_to_int16(uniform int a[], uniform int offset,
unsigned int val) {
__store_uint16(a, offset, val, __mask);
}
///////////////////////////////////////////////////////////////////////////
// Math
static inline float abs(float a) {
// Floating-point hack: zeroing the high bit clears the sign
unsigned int i = intbits(a);
i &= 0x7fffffff;
return floatbits(i);
}
static inline uniform float abs(uniform float a) {
uniform unsigned int i = intbits(a);
i &= 0x7fffffff;
return floatbits(i);
}
static inline unsigned int signbits(float x) {
unsigned int i = intbits(x);
return (i & 0x80000000u);
}
static inline uniform unsigned int signbits(uniform float x) {
uniform unsigned int i = intbits(x);
return (i & 0x80000000u);
}
static inline float round(float x) {
return __round_varying_float(x);
}
static inline uniform float round(uniform float x) {
return __round_uniform_float(x);
}
static inline float floor(float x) {
return __floor_varying_float(x);
}
static inline uniform float floor(uniform float x) {
return __floor_uniform_float(x);
}
static inline float ceil(float x) {
return __ceil_varying_float(x);
}
static inline uniform float ceil(uniform float x) {
return __ceil_uniform_float(x);
}
static inline float rcp(float v) {
return __rcp_varying_float(v);
}
static inline uniform float rcp(uniform float v) {
return __rcp_uniform_float(v);
}
static inline float sqrt(float v) {
return __sqrt_varying_float(v);
}
static inline uniform float sqrt(uniform float v) {
return __sqrt_uniform_float(v);
}
static inline float min(float a, float b) {
return __min_varying_float(a, b);
}
static inline uniform float min(uniform float a, uniform float b) {
return __min_uniform_float(a, b);
}
static inline float max(float a, float b) {
return __max_varying_float(a, b);
}
static inline uniform float max(uniform float a, uniform float b) {
return __max_uniform_float(a, b);
}
static inline unsigned int min(unsigned int a, unsigned int b) {
return __min_varying_uint32(a, b);
}
static inline uniform unsigned int min(uniform unsigned int a, uniform unsigned int b) {
return __min_uniform_uint32(a, b);
}
static inline unsigned int max(unsigned int a, unsigned int b) {
return __max_varying_uint32(a, b);
}
static inline uniform unsigned int max(uniform unsigned int a, uniform unsigned int b) {
return __max_uniform_uint32(a, b);
}
static inline int min(int a, int b) {
return __min_varying_int32(a, b);
}
static inline uniform int min(uniform int a, uniform int b) {
return __min_uniform_int32(a, b);
}
static inline int max(int a, int b) {
return __max_varying_int32(a, b);
}
static inline uniform int max(uniform int a, uniform int b) {
return __max_uniform_int32(a, b);
}
static inline float clamp(float v, float low, float high) {
return min(max(v, low), high);
}
static inline uniform float clamp(uniform float v, uniform float low, uniform float high) {
return min(max(v, low), high);
}
static inline unsigned int clamp(unsigned int v, unsigned int low, unsigned int high) {
return min(max(v, low), high);
}
static inline uniform unsigned int clamp(uniform unsigned int v, uniform unsigned int low,
uniform unsigned int high) {
return min(max(v, low), high);
}
static inline int clamp(int v, int low, int high) {
return min(max(v, low), high);
}
static inline uniform int clamp(uniform int v, uniform int low, uniform int high) {
return min(max(v, low), high);
}
///////////////////////////////////////////////////////////////////////////
// Transcendentals
static inline float rsqrt(float v) {
return __rsqrt_varying_float(v);
}
static inline uniform float rsqrt(uniform float v) {
return __rsqrt_uniform_float(v);
}
static inline float ldexp(float x, int n) {
unsigned int ex = 0x7F800000u;
unsigned int ix = intbits(x);
ex &= ix; // extract old exponent;
ix = ix & ~0x7F800000u; // clear exponent
n = (n << 23) + ex;
ix |= n; // insert new exponent
return floatbits(ix);
}
static inline uniform float ldexp(uniform float x, uniform int n) {
uniform unsigned int ex = 0x7F800000u;
uniform unsigned int ix = intbits(x);
ex &= ix; // extract old exponent;
ix = ix & ~0x7F800000u; // clear exponent
n = (n << 23) + ex;
ix |= n; // insert new exponent
return floatbits(ix);
}
static inline float frexp(float x, reference int pw2) {
unsigned int ex = 0x7F800000u; // exponent mask
unsigned int ix = intbits(x);
ex &= ix;
ix &= ~0x7F800000u; // clear exponent
pw2 = (int)(ex >> 23) - 126; // compute exponent
ix |= 0x3F000000u; // insert exponent +1 in x
return floatbits(ix);
}
static inline uniform float frexp(uniform float x, reference uniform int pw2) {
uniform unsigned int ex = 0x7F800000u; // exponent mask
uniform unsigned int ix = intbits(x);
ex &= ix;
ix &= ~0x7F800000u; // clear exponent
pw2 = (uniform int)(ex >> 23) - 126; // compute exponent
ix |= 0x3F000000u; // insert exponent +1 in x
return floatbits(ix);
}
// Most of the transcendental implementations in ispc code here come from
// Solomon Boulos's "syrah": https://github.com/boulos/syrah/
static inline float sin(float x_full) {
if (__math_lib == __math_lib_svml) {
return __svml_sin(x_full);
}
else if (__math_lib == __math_lib_system) {
float ret;
uniform int mask = lanemask();
for (uniform int i = 0; i < programCount; ++i) {
if ((mask & (1 << i)) == 0)
continue;
uniform float r = __stdlib_sin(extract(x_full, i));
ret = insert(ret, i, r);
}
return ret;
}
else if (__math_lib == __math_lib_ispc ||
__math_lib == __math_lib_ispc_fast) {
static const float pi_over_two_vec = 1.57079637050628662109375;
static const float two_over_pi_vec = 0.636619746685028076171875;
float scaled = x_full * two_over_pi_vec;
float k_real = floor(scaled);
int k = (int)k_real;
// Reduced range version of x
float x = x_full - k_real * pi_over_two_vec;
int k_mod4 = k & 3;
bool sin_usecos = (k_mod4 == 1 || k_mod4 == 3);
bool flip_sign = (k_mod4 > 1);
// These coefficients are from sollya with fpminimax(sin(x)/x, [|0, 2,
// 4, 6, 8, 10|], [|single...|], [0;Pi/2]);
static const float sin_c2 = -0.16666667163372039794921875;
static const float sin_c4 = 8.333347737789154052734375e-3;
static const float sin_c6 = -1.9842604524455964565277099609375e-4;
static const float sin_c8 = 2.760012648650445044040679931640625e-6;
static const float sin_c10 = -2.50293279435709337121807038784027099609375e-8;
static const float cos_c2 = -0.5;
static const float cos_c4 = 4.166664183139801025390625e-2;
static const float cos_c6 = -1.388833043165504932403564453125e-3;
static const float cos_c8 = 2.47562347794882953166961669921875e-5;
static const float cos_c10 = -2.59630184018533327616751194000244140625e-7;
float outside = sin_usecos ? 1 : x;
float c2 = sin_usecos ? cos_c2 : sin_c2;
float c4 = sin_usecos ? cos_c4 : sin_c4;
float c6 = sin_usecos ? cos_c6 : sin_c6;
float c8 = sin_usecos ? cos_c8 : sin_c8;
float c10 = sin_usecos ? cos_c10 : sin_c10;
float x2 = x * x;
float formula = x2 * c10 + c8;
formula = x2 * formula + c6;
formula = x2 * formula + c4;
formula = x2 * formula + c2;
formula = x2 * formula + 1;
formula *= outside;
formula = flip_sign ? -formula : formula;
return formula;
}
}
static inline uniform float sin(uniform float x_full) {
if (__math_lib == __math_lib_system ||
__math_lib == __math_lib_svml) {
return __stdlib_sin(x_full);
}
else if (__math_lib == __math_lib_ispc ||
__math_lib == __math_lib_ispc_fast) {
static const uniform float pi_over_two_vec = 1.57079637050628662109375;
static const uniform float two_over_pi_vec = 0.636619746685028076171875;
uniform float scaled = x_full * two_over_pi_vec;
uniform float k_real = floor(scaled);
uniform int k = (int)k_real;
// Reduced range version of x
uniform float x = x_full - k_real * pi_over_two_vec;
uniform int k_mod4 = k & 3;
uniform bool sin_usecos = (k_mod4 == 1 || k_mod4 == 3);
uniform bool flip_sign = (k_mod4 > 1);
// These coefficients are from sollya with fpminimax(sin(x)/x, [|0, 2,
// 4, 6, 8, 10|], [|single...|], [0;Pi/2]);
static const uniform float sin_c2 = -0.16666667163372039794921875;
static const uniform float sin_c4 = 8.333347737789154052734375e-3;
static const uniform float sin_c6 = -1.9842604524455964565277099609375e-4;
static const uniform float sin_c8 = 2.760012648650445044040679931640625e-6;
static const uniform float sin_c10 = -2.50293279435709337121807038784027099609375e-8;
static const uniform float cos_c2 = -0.5;
static const uniform float cos_c4 = 4.166664183139801025390625e-2;
static const uniform float cos_c6 = -1.388833043165504932403564453125e-3;
static const uniform float cos_c8 = 2.47562347794882953166961669921875e-5;
static const uniform float cos_c10 = -2.59630184018533327616751194000244140625e-7;
uniform float outside, c2, c4, c6, c8, c10;
if (sin_usecos) {
outside = 1.;
c2 = cos_c2;
c4 = cos_c4;
c6 = cos_c6;
c8 = cos_c8;
c10 = cos_c10;
}
else {
outside = x;
c2 = sin_c2;
c4 = sin_c4;
c6 = sin_c6;
c8 = sin_c8;
c10 = sin_c10;
}
uniform float x2 = x * x;
uniform float formula = x2 * c10 + c8;
formula = x2 * formula + c6;
formula = x2 * formula + c4;
formula = x2 * formula + c2;
formula = x2 * formula + 1.;
formula *= outside;
formula = flip_sign ? -formula : formula;
return formula;
}
}
static inline float cos(float x_full) {
if (__math_lib == __math_lib_svml) {
return __svml_cos(x_full);
}
else if (__math_lib == __math_lib_system) {
float ret;
uniform int mask = lanemask();
for (uniform int i = 0; i < programCount; ++i) {
if ((mask & (1 << i)) == 0)
continue;
uniform float r = __stdlib_cos(extract(x_full, i));
ret = insert(ret, i, r);
}
return ret;
}
else if (__math_lib == __math_lib_ispc ||
__math_lib == __math_lib_ispc_fast) {
static const float pi_over_two_vec = 1.57079637050628662109375;
static const float two_over_pi_vec = 0.636619746685028076171875;
float scaled = x_full * two_over_pi_vec;
float k_real = floor(scaled);
int k = (int)k_real;
// Reduced range version of x
float x = x_full - k_real * pi_over_two_vec;
int k_mod4 = k & 3;
bool cos_usecos = (k_mod4 == 0 || k_mod4 == 2);
bool flip_sign = (k_mod4 == 1 || k_mod4 == 2);
const float sin_c2 = -0.16666667163372039794921875;
const float sin_c4 = 8.333347737789154052734375e-3;
const float sin_c6 = -1.9842604524455964565277099609375e-4;
const float sin_c8 = 2.760012648650445044040679931640625e-6;
const float sin_c10 = -2.50293279435709337121807038784027099609375e-8;
const float cos_c2 = -0.5;
const float cos_c4 = 4.166664183139801025390625e-2;
const float cos_c6 = -1.388833043165504932403564453125e-3;
const float cos_c8 = 2.47562347794882953166961669921875e-5;
const float cos_c10 = -2.59630184018533327616751194000244140625e-7;
float outside = cos_usecos ? 1. : x;
float c2 = cos_usecos ? cos_c2 : sin_c2;
float c4 = cos_usecos ? cos_c4 : sin_c4;
float c6 = cos_usecos ? cos_c6 : sin_c6;
float c8 = cos_usecos ? cos_c8 : sin_c8;
float c10 = cos_usecos ? cos_c10 : sin_c10;
float x2 = x * x;
float formula = x2 * c10 + c8;
formula = x2 * formula + c6;
formula = x2 * formula + c4;
formula = x2 * formula + c2;
formula = x2 * formula + 1.;
formula *= outside;
formula = flip_sign ? -formula : formula;
return formula;
}
}
static inline uniform float cos(uniform float x_full) {
if (__math_lib == __math_lib_system ||
__math_lib == __math_lib_svml) {
return __stdlib_cos(x_full);
}
else if (__math_lib == __math_lib_ispc ||
__math_lib == __math_lib_ispc_fast) {
static const uniform float pi_over_two_vec = 1.57079637050628662109375;
static const uniform float two_over_pi_vec = 0.636619746685028076171875;
uniform float scaled = x_full * two_over_pi_vec;
uniform float k_real = floor(scaled);
uniform int k = (int)k_real;
// Reduced range version of x
uniform float x = x_full - k_real * pi_over_two_vec;
uniform int k_mod4 = k & 3;
uniform bool cos_usecos = (k_mod4 == 0 || k_mod4 == 2);
uniform bool flip_sign = (k_mod4 == 1 || k_mod4 == 2);
const uniform float sin_c2 = -0.16666667163372039794921875;
const uniform float sin_c4 = 8.333347737789154052734375e-3;
const uniform float sin_c6 = -1.9842604524455964565277099609375e-4;
const uniform float sin_c8 = 2.760012648650445044040679931640625e-6;
const uniform float sin_c10 = -2.50293279435709337121807038784027099609375e-8;
const uniform float cos_c2 = -0.5;
const uniform float cos_c4 = 4.166664183139801025390625e-2;
const uniform float cos_c6 = -1.388833043165504932403564453125e-3;
const uniform float cos_c8 = 2.47562347794882953166961669921875e-5;
const uniform float cos_c10 = -2.59630184018533327616751194000244140625e-7;
uniform float outside, c2, c4, c6, c8, c10;
if (cos_usecos) {
outside = 1.;
c2 = cos_c2;
c4 = cos_c4;
c6 = cos_c6;
c8 = cos_c8;
c10 = cos_c10;
}
else {
outside = x;
c2 = sin_c2;
c4 = sin_c4;
c6 = sin_c6;
c8 = sin_c8;
c10 = sin_c10;
}
uniform float x2 = x * x;
uniform float formula = x2 * c10 + c8;
formula = x2 * formula + c6;
formula = x2 * formula + c4;
formula = x2 * formula + c2;
formula = x2 * formula + 1.;
formula *= outside;
formula = flip_sign ? -formula : formula;
return formula;
}
}
static inline void sincos(float x_full, reference float sin_result, reference float cos_result) {
if (__math_lib == __math_lib_svml) {
__svml_sincos(x_full, sin_result, cos_result);
}
else if (__math_lib == __math_lib_system) {
uniform int mask = lanemask();
for (uniform int i = 0; i < programCount; ++i) {
if ((mask & (1 << i)) == 0)
continue;
uniform float s, c;
__stdlib_sincos(extract(x_full, i), s, c);
sin_result = insert(sin_result, i, s);
cos_result = insert(cos_result, i, c);
}
}
else if (__math_lib == __math_lib_ispc ||
__math_lib == __math_lib_ispc_fast) {
const float pi_over_two_vec = 1.57079637050628662109375;
const float two_over_pi_vec = 0.636619746685028076171875;
float scaled = x_full * two_over_pi_vec;
float k_real = floor(scaled);
int k = (int)k_real;
// Reduced range version of x
float x = x_full - k_real * pi_over_two_vec;
int k_mod4 = k & 3;
bool cos_usecos = (k_mod4 == 0 || k_mod4 == 2);
bool sin_usecos = (k_mod4 == 1 || k_mod4 == 3);
bool sin_flipsign = (k_mod4 > 1);
bool cos_flipsign = (k_mod4 == 1 || k_mod4 == 2);
const float one_vec = 1.;
const float sin_c2 = -0.16666667163372039794921875;
const float sin_c4 = 8.333347737789154052734375e-3;
const float sin_c6 = -1.9842604524455964565277099609375e-4;
const float sin_c8 = 2.760012648650445044040679931640625e-6;
const float sin_c10 = -2.50293279435709337121807038784027099609375e-8;
const float cos_c2 = -0.5;
const float cos_c4 = 4.166664183139801025390625e-2;
const float cos_c6 = -1.388833043165504932403564453125e-3;
const float cos_c8 = 2.47562347794882953166961669921875e-5;
const float cos_c10 = -2.59630184018533327616751194000244140625e-7;
float x2 = x * x;
float sin_formula = x2 * sin_c10 + sin_c8;
float cos_formula = x2 * cos_c10 + cos_c8;
sin_formula = x2 * sin_formula + sin_c6;
cos_formula = x2 * cos_formula + cos_c6;
sin_formula = x2 * sin_formula + sin_c4;
cos_formula = x2 * cos_formula + cos_c4;
sin_formula = x2 * sin_formula + sin_c2;
cos_formula = x2 * cos_formula + cos_c2;
sin_formula = x2 * sin_formula + one_vec;
cos_formula = x2 * cos_formula + one_vec;
sin_formula *= x;
sin_result = sin_usecos ? cos_formula : sin_formula;
cos_result = cos_usecos ? cos_formula : sin_formula;
sin_result = sin_flipsign ? -sin_result : sin_result;
cos_result = cos_flipsign ? -cos_result : cos_result;
}
}
static inline void sincos(uniform float x_full, reference uniform float sin_result,
reference uniform float cos_result) {
if (__math_lib == __math_lib_system ||
__math_lib == __math_lib_svml) {
__stdlib_sincos(x_full, sin_result, cos_result);
}
else if (__math_lib == __math_lib_ispc ||
__math_lib == __math_lib_ispc_fast) {
const uniform float pi_over_two_vec = 1.57079637050628662109375;
const uniform float two_over_pi_vec = 0.636619746685028076171875;
uniform float scaled = x_full * two_over_pi_vec;
uniform float k_real = floor(scaled);
uniform int k = (uniform int)k_real;
// Reduced range version of x
uniform float x = x_full - k_real * pi_over_two_vec;
uniform int k_mod4 = k & 3;
uniform bool cos_usecos = (k_mod4 == 0 || k_mod4 == 2);
uniform bool sin_usecos = (k_mod4 == 1 || k_mod4 == 3);
uniform bool sin_flipsign = (k_mod4 > 1);
uniform bool cos_flipsign = (k_mod4 == 1 || k_mod4 == 2);
const uniform float one_vec = 1.;
const uniform float sin_c2 = -0.16666667163372039794921875;
const uniform float sin_c4 = 8.333347737789154052734375e-3;
const uniform float sin_c6 = -1.9842604524455964565277099609375e-4;
const uniform float sin_c8 = 2.760012648650445044040679931640625e-6;
const uniform float sin_c10 = -2.50293279435709337121807038784027099609375e-8;
const uniform float cos_c2 = -0.5;
const uniform float cos_c4 = 4.166664183139801025390625e-2;
const uniform float cos_c6 = -1.388833043165504932403564453125e-3;
const uniform float cos_c8 = 2.47562347794882953166961669921875e-5;
const uniform float cos_c10 = -2.59630184018533327616751194000244140625e-7;
uniform float x2 = x * x;
uniform float sin_formula = x2 * sin_c10 + sin_c8;
uniform float cos_formula = x2 * cos_c10 + cos_c8;
sin_formula = x2 * sin_formula + sin_c6;
cos_formula = x2 * cos_formula + cos_c6;
sin_formula = x2 * sin_formula + sin_c4;
cos_formula = x2 * cos_formula + cos_c4;
sin_formula = x2 * sin_formula + sin_c2;
cos_formula = x2 * cos_formula + cos_c2;
sin_formula = x2 * sin_formula + one_vec;
cos_formula = x2 * cos_formula + one_vec;
sin_formula *= x;
sin_result = sin_usecos ? cos_formula : sin_formula;
cos_result = cos_usecos ? cos_formula : sin_formula;
sin_result = sin_flipsign ? -sin_result : sin_result;
cos_result = cos_flipsign ? -cos_result : cos_result;
}
}
static inline float tan(float x_full) {
if (__math_lib == __math_lib_svml) {
return __svml_tan(x_full);
}
else if (__math_lib == __math_lib_system) {
float ret;
uniform int mask = lanemask();
for (uniform int i = 0; i < programCount; ++i) {
if ((mask & (1 << i)) == 0)
continue;
uniform float r = __stdlib_tan(extract(x_full, i));
ret = insert(ret, i, r);
}
return ret;
}
else if (__math_lib == __math_lib_ispc ||
__math_lib == __math_lib_ispc_fast) {
const float pi_over_four_vec = 0.785398185253143310546875;
const float four_over_pi_vec = 1.27323949337005615234375;
bool x_lt_0 = x_full < 0.;
float y = x_lt_0 ? -x_full : x_full;
float scaled = y * four_over_pi_vec;
float k_real = floor(scaled);
int k = (int)k_real;
float x = y - k_real * pi_over_four_vec;
// if k & 1, x -= Pi/4
bool need_offset = (k & 1) != 0;
x = need_offset ? x - pi_over_four_vec : x;
// if k & 3 == (0 or 3) let z = tan_In...(y) otherwise z = -cot_In0To...
int k_mod4 = k & 3;
bool use_cotan = (k_mod4 == 1) || (k_mod4 == 2);
const float one_vec = 1.0;
const float tan_c2 = 0.33333075046539306640625;
const float tan_c4 = 0.13339905440807342529296875;
const float tan_c6 = 5.3348250687122344970703125e-2;
const float tan_c8 = 2.46033705770969390869140625e-2;
const float tan_c10 = 2.892402000725269317626953125e-3;
const float tan_c12 = 9.500005282461643218994140625e-3;
const float cot_c2 = -0.3333333432674407958984375;
const float cot_c4 = -2.222204394638538360595703125e-2;
const float cot_c6 = -2.11752182804048061370849609375e-3;
const float cot_c8 = -2.0846328698098659515380859375e-4;
const float cot_c10 = -2.548247357481159269809722900390625e-5;
const float cot_c12 = -3.5257363606433500535786151885986328125e-7;
float x2 = x * x;
float z;
cif (use_cotan) {
float cot_val = x2 * cot_c12 + cot_c10;
cot_val = x2 * cot_val + cot_c8;
cot_val = x2 * cot_val + cot_c6;
cot_val = x2 * cot_val + cot_c4;
cot_val = x2 * cot_val + cot_c2;
cot_val = x2 * cot_val + one_vec;
// The equation is for x * cot(x) but we need -x * cot(x) for the tan part.
cot_val /= -x;
z = cot_val;
} else {
float tan_val = x2 * tan_c12 + tan_c10;
tan_val = x2 * tan_val + tan_c8;
tan_val = x2 * tan_val + tan_c6;
tan_val = x2 * tan_val + tan_c4;
tan_val = x2 * tan_val + tan_c2;
tan_val = x2 * tan_val + one_vec;
// Equation was for tan(x)/x
tan_val *= x;
z = tan_val;
}
return x_lt_0 ? -z : z;
}
}
static inline uniform float tan(uniform float x_full) {
if (__math_lib == __math_lib_system ||
__math_lib == __math_lib_svml) {
return __stdlib_tan(x_full);
}
else if (__math_lib == __math_lib_ispc ||
__math_lib == __math_lib_ispc_fast) {
const uniform float pi_over_four_vec = 0.785398185253143310546875;
const uniform float four_over_pi_vec = 1.27323949337005615234375;
uniform bool x_lt_0 = x_full < 0.;
uniform float y = x_lt_0 ? -x_full : x_full;
uniform float scaled = y * four_over_pi_vec;
uniform float k_real = floor(scaled);
uniform int k = (int)k_real;
uniform float x = y - k_real * pi_over_four_vec;
// if k & 1, x -= Pi/4
uniform bool need_offset = (k & 1) != 0;
x = need_offset ? x - pi_over_four_vec : x;
// if k & 3 == (0 or 3) let z = tan_In...(y) otherwise z = -cot_In0To...
uniform int k_mod4 = k & 3;
uniform bool use_cotan = (k_mod4 == 1) || (k_mod4 == 2);
const uniform float one_vec = 1.0;
const uniform float tan_c2 = 0.33333075046539306640625;
const uniform float tan_c4 = 0.13339905440807342529296875;
const uniform float tan_c6 = 5.3348250687122344970703125e-2;
const uniform float tan_c8 = 2.46033705770969390869140625e-2;
const uniform float tan_c10 = 2.892402000725269317626953125e-3;
const uniform float tan_c12 = 9.500005282461643218994140625e-3;
const uniform float cot_c2 = -0.3333333432674407958984375;
const uniform float cot_c4 = -2.222204394638538360595703125e-2;
const uniform float cot_c6 = -2.11752182804048061370849609375e-3;
const uniform float cot_c8 = -2.0846328698098659515380859375e-4;
const uniform float cot_c10 = -2.548247357481159269809722900390625e-5;
const uniform float cot_c12 = -3.5257363606433500535786151885986328125e-7;
uniform float x2 = x * x;
uniform float z;
if (use_cotan) {
uniform float cot_val = x2 * cot_c12 + cot_c10;