-
Notifications
You must be signed in to change notification settings - Fork 132
/
sleefsp.c
2436 lines (1853 loc) · 63.5 KB
/
sleefsp.c
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
// Copyright Naoki Shibata and contributors 2010 - 2021.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Always use -ffp-contract=off option to compile SLEEF.
#include <stdio.h>
#include <assert.h>
#include <stdint.h>
#include <limits.h>
#include <float.h>
#ifndef ENABLE_BUILTIN_MATH
#include <math.h>
#define SQRTF sqrtf
#else
#define SQRTF __builtin_sqrtf
#endif
#include "misc.h"
extern const float Sleef_rempitabsp[];
#ifdef DORENAME
#include "rename.h"
#endif
#if defined(_MSC_VER) && !defined (__clang__)
#pragma fp_contract (off)
#else
#pragma STDC FP_CONTRACT OFF
#endif
#define MLA mlaf
#define C2V(x) (x)
#include "estrin.h"
static INLINE CONST int32_t floatToRawIntBits(float d) {
int32_t ret;
memcpy(&ret, &d, sizeof(ret));
return ret;
}
static INLINE CONST float intBitsToFloat(int32_t i) {
float ret;
memcpy(&ret, &i, sizeof(ret));
return ret;
}
static INLINE CONST float fabsfk(float x) {
return intBitsToFloat(0x7fffffffL & floatToRawIntBits(x));
}
static INLINE CONST float mulsignf(float x, float y) {
return intBitsToFloat(floatToRawIntBits(x) ^ (floatToRawIntBits(y) & 0x80000000U));
}
static INLINE CONST float copysignfk(float x, float y) {
return intBitsToFloat((floatToRawIntBits(x) & ~0x80000000U) ^ (floatToRawIntBits(y) & 0x80000000U));
}
static INLINE CONST float signf(float d) { return mulsignf(1, d); }
static INLINE CONST float mlaf(float x, float y, float z) { return x * y + z; }
static INLINE CONST float rintfk(float x) { return x < 0 ? (int)(x - 0.5f) : (int)(x + 0.5f); }
static INLINE CONST int ceilfk(float x) { return (int)x + (x < 0 ? 0 : 1); }
static INLINE CONST float fminfk(float x, float y) { return x < y ? x : y; }
static INLINE CONST float fmaxfk(float x, float y) { return x > y ? x : y; }
static INLINE CONST int xisintf(float x) { return (x == (int)x); }
static INLINE CONST int xsignbitf(double d) { return (floatToRawIntBits(d) & floatToRawIntBits(-0.0)) == floatToRawIntBits(-0.0); }
static INLINE CONST int xisnanf(float x) { return x != x; }
static INLINE CONST int xisinff(float x) { return x == SLEEF_INFINITYf || x == -SLEEF_INFINITYf; }
static INLINE CONST int xisminff(float x) { return x == -SLEEF_INFINITYf; }
static INLINE CONST int xispinff(float x) { return x == SLEEF_INFINITYf; }
static INLINE CONST int xisnegzerof(float x) { return floatToRawIntBits(x) == floatToRawIntBits(-0.0); }
static INLINE CONST int xisnumberf(float x) { return !xisinff(x) && !xisnanf(x); }
static INLINE CONST int ilogbkf(float d) {
int m = d < 5.421010862427522E-20f;
d = m ? 1.8446744073709552E19f * d : d;
int q = (floatToRawIntBits(d) >> 23) & 0xff;
q = m ? q - (64 + 0x7f) : q - 0x7f;
return q;
}
// vilogb2kf is similar to ilogbkf, but the argument has to be a
// normalized FP value.
static INLINE CONST int ilogb2kf(float d) {
return ((floatToRawIntBits(d) >> 23) & 0xff) - 0x7f;
}
EXPORT CONST int xilogbf(float d) {
int e = ilogbkf(fabsfk(d));
e = d == 0.0f ? SLEEF_FP_ILOGB0 : e;
e = xisnanf(d) ? SLEEF_FP_ILOGBNAN : e;
e = xisinff(d) ? INT_MAX : e;
return e;
}
static INLINE CONST float pow2if(int q) {
return intBitsToFloat(((int32_t)(q + 0x7f)) << 23);
}
static INLINE CONST float ldexpkf(float x, int q) {
float u;
int m;
m = q >> 31;
m = (((m + q) >> 6) - m) << 4;
q = q - (m << 2);
m += 127;
m = m < 0 ? 0 : m;
m = m > 255 ? 255 : m;
u = intBitsToFloat(((int32_t)m) << 23);
x = x * u * u * u * u;
u = intBitsToFloat(((int32_t)(q + 0x7f)) << 23);
return x * u;
}
static INLINE CONST float ldexp2kf(float d, int e) { // faster than ldexpkf, short reach
return d * pow2if(e >> 1) * pow2if(e - (e >> 1));
}
static INLINE CONST float ldexp3kf(float d, int e) { // very fast, no denormal
return intBitsToFloat(floatToRawIntBits(d) + (e << 23));
}
//
#ifndef NDEBUG
static int checkfp(float x) {
if (xisinff(x) || xisnanf(x)) return 1;
return 0;
}
#endif
static INLINE CONST float upperf(float d) {
return intBitsToFloat(floatToRawIntBits(d) & 0xfffff000);
}
static INLINE CONST Sleef_float2 df(float h, float l) {
Sleef_float2 ret;
ret.x = h; ret.y = l;
return ret;
}
static INLINE CONST Sleef_float2 dfx(double d) {
Sleef_float2 ret;
ret.x = d; ret.y = d - ret.x;
return ret;
}
static INLINE CONST Sleef_float2 dfnormalize_f2_f2(Sleef_float2 t) {
Sleef_float2 s;
s.x = t.x + t.y;
s.y = t.x - s.x + t.y;
return s;
}
static INLINE CONST Sleef_float2 dfscale_f2_f2_f(Sleef_float2 d, float s) {
Sleef_float2 r;
r.x = d.x * s;
r.y = d.y * s;
return r;
}
static INLINE CONST Sleef_float2 dfneg_f2_f2(Sleef_float2 d) {
Sleef_float2 r;
r.x = -d.x;
r.y = -d.y;
return r;
}
static INLINE CONST Sleef_float2 dfabs_f2_f2(Sleef_float2 x) {
return df(x.x < 0 ? -x.x : x.x, x.x < 0 ? -x.y : x.y);
}
static INLINE CONST Sleef_float2 dfadd_f2_f_f(float x, float y) {
// |x| >= |y|
Sleef_float2 r;
#ifndef NDEBUG
if (!(checkfp(x) || checkfp(y) || fabsfk(x) >= fabsfk(y))) fprintf(stderr, "[dfadd_f2_f_f : %g, %g]", x, y);
#endif
r.x = x + y;
r.y = x - r.x + y;
return r;
}
static INLINE CONST Sleef_float2 dfadd2_f2_f_f(float x, float y) {
Sleef_float2 r;
r.x = x + y;
float v = r.x - x;
r.y = (x - (r.x - v)) + (y - v);
return r;
}
static INLINE CONST Sleef_float2 dfadd_f2_f2_f(Sleef_float2 x, float y) {
// |x| >= |y|
Sleef_float2 r;
#ifndef NDEBUG
if (!(checkfp(x.x) || checkfp(y) || fabsfk(x.x) >= fabsfk(y))) fprintf(stderr, "[dfadd_f2_f2_f : %g %g]", x.x, y);
#endif
r.x = x.x + y;
r.y = x.x - r.x + y + x.y;
return r;
}
static INLINE CONST Sleef_float2 dfadd_f2_f_f2(float x, Sleef_float2 y) {
// |x| >= |y|
Sleef_float2 r;
#ifndef NDEBUG
if (!(checkfp(x) || checkfp(y.x) || fabsfk(x) >= fabsfk(y.x))) {
fprintf(stderr, "[dfadd_f2_f_f2 : %g %g]\n", x, y.x);
fflush(stderr);
}
#endif
r.x = x + y.x;
r.y = x - r.x + y.x + y.y;
return r;
}
static INLINE CONST Sleef_float2 dfadd2_f2_f2_f(Sleef_float2 x, float y) {
// |x| >= |y|
Sleef_float2 r;
r.x = x.x + y;
float v = r.x - x.x;
r.y = (x.x - (r.x - v)) + (y - v);
r.y += x.y;
return r;
}
static INLINE CONST Sleef_float2 dfadd2_f2_f_f2(float x, Sleef_float2 y) {
Sleef_float2 r;
r.x = x + y.x;
float v = r.x - x;
r.y = (x - (r.x - v)) + (y.x - v) + y.y;
return r;
}
static INLINE CONST Sleef_float2 dfadd_f2_f2_f2(Sleef_float2 x, Sleef_float2 y) {
// |x| >= |y|
Sleef_float2 r;
#ifndef NDEBUG
if (!(checkfp(x.x) || checkfp(y.x) || fabsfk(x.x) >= fabsfk(y.x))) fprintf(stderr, "[dfadd_f2_f2_f2 : %g %g]", x.x, y.x);
#endif
r.x = x.x + y.x;
r.y = x.x - r.x + y.x + x.y + y.y;
return r;
}
static INLINE CONST Sleef_float2 dfadd2_f2_f2_f2(Sleef_float2 x, Sleef_float2 y) {
Sleef_float2 r;
r.x = x.x + y.x;
float v = r.x - x.x;
r.y = (x.x - (r.x - v)) + (y.x - v);
r.y += x.y + y.y;
return r;
}
static INLINE CONST Sleef_float2 dfsub_f2_f2_f2(Sleef_float2 x, Sleef_float2 y) {
// |x| >= |y|
Sleef_float2 r;
#ifndef NDEBUG
if (!(checkfp(x.x) || checkfp(y.x) || fabsfk(x.x) >= fabsfk(y.x))) fprintf(stderr, "[dfsub_f2_f2_f2 : %g %g]", x.x, y.x);
#endif
r.x = x.x - y.x;
r.y = x.x - r.x - y.x + x.y - y.y;
return r;
}
static INLINE CONST Sleef_float2 dfdiv_f2_f2_f2(Sleef_float2 n, Sleef_float2 d) {
float t = 1.0f / d.x;
float dh = upperf(d.x), dl = d.x - dh;
float th = upperf(t ), tl = t - th;
float nhh = upperf(n.x), nhl = n.x - nhh;
Sleef_float2 q;
q.x = n.x * t;
float u = -q.x + nhh * th + nhh * tl + nhl * th + nhl * tl +
q.x * (1 - dh * th - dh * tl - dl * th - dl * tl);
q.y = t * (n.y - q.x * d.y) + u;
return q;
}
static INLINE CONST Sleef_float2 dfmul_f2_f_f(float x, float y) {
float xh = upperf(x), xl = x - xh;
float yh = upperf(y), yl = y - yh;
Sleef_float2 r;
r.x = x * y;
r.y = xh * yh - r.x + xl * yh + xh * yl + xl * yl;
return r;
}
static INLINE CONST Sleef_float2 dfmul_f2_f2_f(Sleef_float2 x, float y) {
float xh = upperf(x.x), xl = x.x - xh;
float yh = upperf(y ), yl = y - yh;
Sleef_float2 r;
r.x = x.x * y;
r.y = xh * yh - r.x + xl * yh + xh * yl + xl * yl + x.y * y;
return r;
}
static INLINE CONST Sleef_float2 dfmul_f2_f2_f2(Sleef_float2 x, Sleef_float2 y) {
float xh = upperf(x.x), xl = x.x - xh;
float yh = upperf(y.x), yl = y.x - yh;
Sleef_float2 r;
r.x = x.x * y.x;
r.y = xh * yh - r.x + xl * yh + xh * yl + xl * yl + x.x * y.y + x.y * y.x;
return r;
}
static INLINE CONST float dfmul_f_f2_f2(Sleef_float2 x, Sleef_float2 y) {
float xh = upperf(x.x), xl = x.x - xh;
float yh = upperf(y.x), yl = y.x - yh;
return x.y * yh + xh * y.y + xl * yl + xh * yl + xl * yh + xh * yh;
}
static INLINE CONST Sleef_float2 dfsqu_f2_f2(Sleef_float2 x) {
float xh = upperf(x.x), xl = x.x - xh;
Sleef_float2 r;
r.x = x.x * x.x;
r.y = xh * xh - r.x + (xh + xh) * xl + xl * xl + x.x * (x.y + x.y);
return r;
}
static INLINE CONST float dfsqu_f_f2(Sleef_float2 x) {
float xh = upperf(x.x), xl = x.x - xh;
return xh * x.y + xh * x.y + xl * xl + (xh * xl + xh * xl) + xh * xh;
}
static INLINE CONST Sleef_float2 dfrec_f2_f(float d) {
float t = 1.0f / d;
float dh = upperf(d), dl = d - dh;
float th = upperf(t), tl = t - th;
Sleef_float2 q;
q.x = t;
q.y = t * (1 - dh * th - dh * tl - dl * th - dl * tl);
return q;
}
static INLINE CONST Sleef_float2 dfrec_f2_f2(Sleef_float2 d) {
float t = 1.0f / d.x;
float dh = upperf(d.x), dl = d.x - dh;
float th = upperf(t ), tl = t - th;
Sleef_float2 q;
q.x = t;
q.y = t * (1 - dh * th - dh * tl - dl * th - dl * tl - d.y * t);
return q;
}
static INLINE CONST Sleef_float2 dfsqrt_f2_f2(Sleef_float2 d) {
float t = SQRTF(d.x + d.y);
return dfscale_f2_f2_f(dfmul_f2_f2_f2(dfadd2_f2_f2_f2(d, dfmul_f2_f_f(t, t)), dfrec_f2_f(t)), 0.5f);
}
static INLINE CONST Sleef_float2 dfsqrt_f2_f(float d) {
float t = SQRTF(d);
return dfscale_f2_f2_f(dfmul_f2_f2_f2(dfadd2_f2_f_f2(d, dfmul_f2_f_f(t, t)), dfrec_f2_f(t)), 0.5);
}
//
typedef struct {
float d;
int32_t i;
} fi_t;
typedef struct {
Sleef_float2 df;
int32_t i;
} dfi_t;
static CONST fi_t rempisubf(float x) {
fi_t ret;
float fr = x - (float)(INT64_C(1) << 10) * (int32_t)(x * (1.0f / (INT64_C(1) << 10)));
ret.i = ((7 & ((x > 0 ? 4 : 3) + (int32_t)(fr * 8))) - 3) >> 1;
fr = fr - 0.25f * (int32_t)(fr * 4 + mulsignf(0.5f, x));
fr = fabsfk(fr) > 0.125f ? (fr - mulsignf(0.5f, x)) : fr;
fr = fabsfk(fr) > 1e+10f ? 0 : fr;
if (fabsfk(x) == 0.12499999254941940308f) { fr = x; ret.i = 0; }
ret.d = fr;
return ret;
}
static CONST dfi_t rempif(float a) {
Sleef_float2 x, y;
fi_t di;
int ex = ilogb2kf(a) - 25, q = ex > (90 - 25) ? -64 : 0;
a = ldexp3kf(a, q);
if (ex < 0) ex = 0;
ex *= 4;
x = dfmul_f2_f_f(a, Sleef_rempitabsp[ex]);
di = rempisubf(x.x);
q = di.i;
x.x = di.d;
x = dfnormalize_f2_f2(x);
y = dfmul_f2_f_f(a, Sleef_rempitabsp[ex+1]);
x = dfadd2_f2_f2_f2(x, y);
di = rempisubf(x.x);
q += di.i;
x.x = di.d;
x = dfnormalize_f2_f2(x);
y = dfmul_f2_f2_f(df(Sleef_rempitabsp[ex+2], Sleef_rempitabsp[ex+3]), a);
x = dfadd2_f2_f2_f2(x, y);
x = dfnormalize_f2_f2(x);
x = dfmul_f2_f2_f2(x, df(3.1415927410125732422f*2, -8.7422776573475857731e-08f*2));
dfi_t ret = { fabsfk(a) < 0.7f ? df(a, 0) : x, q };
return ret;
}
EXPORT CONST float xsinf(float d) {
int q;
float u, s, t = d;
if (fabsfk(d) < TRIGRANGEMAX2f) {
q = (int)rintfk(d * (float)M_1_PI);
d = mlaf(q, -PI_A2f, d);
d = mlaf(q, -PI_B2f, d);
d = mlaf(q, -PI_C2f, d);
} else if (fabsfk(d) < TRIGRANGEMAXf) {
q = (int)rintfk(d * (float)M_1_PI);
d = mlaf(q, -PI_Af, d);
d = mlaf(q, -PI_Bf, d);
d = mlaf(q, -PI_Cf, d);
d = mlaf(q, -PI_Df, d);
} else {
dfi_t dfi = rempif(t);
q = ((dfi.i & 3) * 2 + (dfi.df.x > 0) + 1) >> 2;
if ((dfi.i & 1) != 0) {
dfi.df = dfadd2_f2_f2_f2(dfi.df, df(mulsignf(3.1415927410125732422f*-0.5, dfi.df.x),
mulsignf(-8.7422776573475857731e-08f*-0.5, dfi.df.x)));
}
d = dfi.df.x + dfi.df.y;
if (xisinff(t) || xisnanf(t)) d = SLEEF_NANf;
}
s = d * d;
if ((q & 1) != 0) d = -d;
u = 2.6083159809786593541503e-06f;
u = mlaf(u, s, -0.0001981069071916863322258f);
u = mlaf(u, s, 0.00833307858556509017944336f);
u = mlaf(u, s, -0.166666597127914428710938f);
u = mlaf(s, u * d, d);
if (xisnegzerof(t)) u = -0.0f;
return u;
}
EXPORT CONST float xsinf_u1(float d) {
int q;
float u;
Sleef_float2 s, t, x;
if (fabsfk(d) < TRIGRANGEMAX2f) {
q = (int)rintfk(d * (float)M_1_PI);
u = mlaf(q, -PI_A2f, d);
s = dfadd2_f2_f_f(u, q * (-PI_B2f));
s = dfadd_f2_f2_f(s, q * (-PI_C2f));
} else {
dfi_t dfi = rempif(d);
q = ((dfi.i & 3) * 2 + (dfi.df.x > 0) + 1) >> 2;
if ((dfi.i & 1) != 0) {
dfi.df = dfadd2_f2_f2_f2(dfi.df, df(mulsignf(3.1415927410125732422f*-0.5, dfi.df.x),
mulsignf(-8.7422776573475857731e-08f*-0.5, dfi.df.x)));
}
s = dfnormalize_f2_f2(dfi.df);
if (xisinff(d) || xisnanf(d)) s.x = SLEEF_NANf;
}
t = s;
s = dfsqu_f2_f2(s);
u = 2.6083159809786593541503e-06f;
u = mlaf(u, s.x, -0.0001981069071916863322258f);
u = mlaf(u, s.x, 0.00833307858556509017944336f);
x = dfadd_f2_f_f2(1, dfmul_f2_f2_f2(dfadd_f2_f_f(-0.166666597127914428710938f, u * s.x), s));
u = dfmul_f_f2_f2(t, x);
if ((q & 1) != 0) u = -u;
if (xisnegzerof(d)) u = d;
return u;
}
EXPORT CONST float xcosf(float d) {
int q;
float u, s, t = d;
if (fabsfk(d) < TRIGRANGEMAX2f) {
q = 1 + 2*(int)rintfk(d * (float)M_1_PI - 0.5f);
d = mlaf(q, -PI_A2f*0.5f, d);
d = mlaf(q, -PI_B2f*0.5f, d);
d = mlaf(q, -PI_C2f*0.5f, d);
} else if (fabsfk(d) < TRIGRANGEMAXf) {
q = 1 + 2*(int)rintfk(d * (float)M_1_PI - 0.5f);
d = mlaf(q, -PI_Af*0.5f, d);
d = mlaf(q, -PI_Bf*0.5f, d);
d = mlaf(q, -PI_Cf*0.5f, d);
d = mlaf(q, -PI_Df*0.5f, d);
} else {
dfi_t dfi = rempif(t);
q = ((dfi.i & 3) * 2 + (dfi.df.x > 0) + 7) >> 1;
if ((dfi.i & 1) == 0) {
dfi.df = dfadd2_f2_f2_f2(dfi.df, df(mulsignf(3.1415927410125732422f*-0.5, dfi.df.x > 0 ? 1 : -1),
mulsignf(-8.7422776573475857731e-08f*-0.5, dfi.df.x > 0 ? 1 : -1)));
}
d = dfi.df.x + dfi.df.y;
if (xisinff(t) || xisnanf(t)) d = SLEEF_NANf;
}
s = d * d;
if ((q & 2) == 0) d = -d;
u = 2.6083159809786593541503e-06f;
u = mlaf(u, s, -0.0001981069071916863322258f);
u = mlaf(u, s, 0.00833307858556509017944336f);
u = mlaf(u, s, -0.166666597127914428710938f);
u = mlaf(s, u * d, d);
return u;
}
EXPORT CONST float xcosf_u1(float d) {
float u;
Sleef_float2 s, t, x;
int q;
if (fabsfk(d) < TRIGRANGEMAX2f) {
d = fabsfk(d);
float dq = mlaf(rintfk(d * (float)M_1_PI - 0.5f), 2, 1);
q = (int)dq;
s = dfadd2_f2_f_f (d, dq * (-PI_A2f*0.5f));
s = dfadd2_f2_f2_f(s, dq * (-PI_B2f*0.5f));
s = dfadd2_f2_f2_f(s, dq * (-PI_C2f*0.5f));
} else {
dfi_t dfi = rempif(d);
q = ((dfi.i & 3) * 2 + (dfi.df.x > 0) + 7) >> 1;
if ((dfi.i & 1) == 0) {
dfi.df = dfadd2_f2_f2_f2(dfi.df, df(mulsignf(3.1415927410125732422f*-0.5, dfi.df.x > 0 ? 1 : -1),
mulsignf(-8.7422776573475857731e-08f*-0.5, dfi.df.x > 0 ? 1 : -1)));
}
s = dfnormalize_f2_f2(dfi.df);
if (xisinff(d) || xisnanf(d)) s.x = SLEEF_NANf;
}
t = s;
s = dfsqu_f2_f2(s);
u = 2.6083159809786593541503e-06f;
u = mlaf(u, s.x, -0.0001981069071916863322258f);
u = mlaf(u, s.x, 0.00833307858556509017944336f);
x = dfadd_f2_f_f2(1, dfmul_f2_f2_f2(dfadd_f2_f_f(-0.166666597127914428710938f, u * s.x), s));
u = dfmul_f_f2_f2(t, x);
if ((((int)q) & 2) == 0) u = -u;
return u;
}
EXPORT CONST float xfastsinf_u3500(float d) {
int q;
float u, s, t = d;
q = rintfk(d * (float)M_1_PI);
d = mlaf(q, -(float)M_PI, d);
s = d * d;
u = -0.1881748176e-3;
u = mlaf(u, s, +0.8323502727e-2);
u = mlaf(u, s, -0.1666651368e+0);
u = mlaf(s * d, u, d);
if ((q & 1) != 0) u = -u;
if (UNLIKELY(fabsfk(t) > 30.0f)) return xsinf(t);
return u;
}
EXPORT CONST float xfastcosf_u3500(float d) {
int q;
float u, s, t = d;
q = rintfk(mlaf(d, (float)M_1_PI, -0.5f));
d = mlaf(q, -(float)M_PI, d - (float)M_PI*0.5f);
s = d * d;
u = -0.1881748176e-3;
u = mlaf(u, s, +0.8323502727e-2);
u = mlaf(u, s, -0.1666651368e+0);
u = mlaf(s * d, u, d);
if ((q & 1) == 0) u = -u;
if (UNLIKELY(fabsfk(t) > 30.0f)) return xcosf(t);
return u;
}
EXPORT CONST Sleef_float2 xsincosf(float d) {
int q;
float u, s, t;
Sleef_float2 r;
s = d;
if (fabsfk(d) < TRIGRANGEMAX2f) {
q = (int)rintfk(d * ((float)(2 * M_1_PI)));
s = mlaf(q, -PI_A2f*0.5f, s);
s = mlaf(q, -PI_B2f*0.5f, s);
s = mlaf(q, -PI_C2f*0.5f, s);
} else if (fabsfk(d) < TRIGRANGEMAXf) {
q = (int)rintfk(d * ((float)(2 * M_1_PI)));
s = mlaf(q, -PI_Af*0.5f, s);
s = mlaf(q, -PI_Bf*0.5f, s);
s = mlaf(q, -PI_Cf*0.5f, s);
s = mlaf(q, -PI_Df*0.5f, s);
} else {
dfi_t dfi = rempif(d);
q = dfi.i;
s = dfi.df.x + dfi.df.y;
if (xisinff(d) || xisnanf(d)) s = SLEEF_NANf;
}
t = s;
s = s * s;
u = -0.000195169282960705459117889f;
u = mlaf(u, s, 0.00833215750753879547119141f);
u = mlaf(u, s, -0.166666537523269653320312f);
u = u * s * t;
r.x = t + u;
if (xisnegzerof(d)) r.x = -0.0f;
u = -2.71811842367242206819355e-07f;
u = mlaf(u, s, 2.47990446951007470488548e-05f);
u = mlaf(u, s, -0.00138888787478208541870117f);
u = mlaf(u, s, 0.0416666641831398010253906f);
u = mlaf(u, s, -0.5f);
r.y = u * s + 1;
if ((q & 1) != 0) { s = r.y; r.y = r.x; r.x = s; }
if ((q & 2) != 0) { r.x = -r.x; }
if (((q+1) & 2) != 0) { r.y = -r.y; }
return r;
}
EXPORT CONST Sleef_float2 xsincosf_u1(float d) {
int q;
float u;
Sleef_float2 r, s, t, x;
if (fabsfk(d) < TRIGRANGEMAX2f) {
q = (int)rintfk(d * (float)(2 * M_1_PI));
u = mlaf(q, -PI_A2f*0.5f, d);
s = dfadd2_f2_f_f(u, q * (-PI_B2f*0.5f));
s = dfadd_f2_f2_f(s, q * (-PI_C2f*0.5f));
} else {
dfi_t dfi = rempif(d);
q = dfi.i;
s = dfi.df;
if (xisinff(d) || xisnanf(d)) s.x = SLEEF_NANf;
}
t = s;
s.x = dfsqu_f_f2(s);
u = -0.000195169282960705459117889f;
u = mlaf(u, s.x, 0.00833215750753879547119141f);
u = mlaf(u, s.x, -0.166666537523269653320312f);
u *= s.x * t.x;
x = dfadd_f2_f2_f(t, u);
r.x = x.x + x.y;
if (xisnegzerof(d)) r.x = -0.0f;
u = -2.71811842367242206819355e-07f;
u = mlaf(u, s.x, 2.47990446951007470488548e-05f);
u = mlaf(u, s.x, -0.00138888787478208541870117f);
u = mlaf(u, s.x, 0.0416666641831398010253906f);
u = mlaf(u, s.x, -0.5f);
x = dfadd_f2_f_f2(1, dfmul_f2_f_f(s.x, u));
r.y = x.x + x.y;
if ((q & 1) != 0) { u = r.y; r.y = r.x; r.x = u; }
if ((q & 2) != 0) { r.x = -r.x; }
if (((q+1) & 2) != 0) { r.y = -r.y; }
return r;
}
EXPORT CONST Sleef_float2 xsincospif_u05(float d) {
float u, s, t;
Sleef_float2 r, x, s2;
u = d * 4;
int q = ceilfk(u) & ~(int)1;
s = u - (float)q;
t = s;
s = s * s;
s2 = dfmul_f2_f_f(t, t);
//
u = +0.3093842054e-6;
u = mlaf(u, s, -0.3657307388e-4);
u = mlaf(u, s, +0.2490393585e-2);
x = dfadd2_f2_f_f2(u * s, df(-0.080745510756969451904, -1.3373665339076936258e-09));
x = dfadd2_f2_f2_f2(dfmul_f2_f2_f2(s2, x), df(0.78539818525314331055, -2.1857338617566484855e-08));
x = dfmul_f2_f2_f(x, t);
r.x = x.x + x.y;
if (xisnegzerof(d)) r.x = -0.0f;
u = -0.2430611801e-7;
u = mlaf(u, s, +0.3590577080e-5);
u = mlaf(u, s, -0.3259917721e-3);
x = dfadd2_f2_f_f2(u * s, df(0.015854343771934509277, 4.4940051354032242811e-10));
x = dfadd2_f2_f2_f2(dfmul_f2_f2_f2(s2, x), df(-0.30842512845993041992, -9.0728339030733922277e-09));
x = dfadd2_f2_f2_f(dfmul_f2_f2_f2(x, s2), 1);
r.y = x.x + x.y;
if ((q & 2) != 0) { s = r.y; r.y = r.x; r.x = s; }
if ((q & 4) != 0) { r.x = -r.x; }
if (((q+2) & 4) != 0) { r.y = -r.y; }
if (fabsfk(d) > 1e+7f) { r.x = 0; r.y = 1; }
if (xisinff(d)) { r.x = r.y = SLEEF_NANf; }
return r;
}
EXPORT CONST Sleef_float2 xsincospif_u35(float d) {
float u, s, t;
Sleef_float2 r;
u = d * 4;
int q = ceilfk(u) & ~(int)1;
s = u - (float)q;
t = s;
s = s * s;
//
u = -0.3600925265e-4;
u = mlaf(u, s, +0.2490088111e-2);
u = mlaf(u, s, -0.8074551076e-1);
u = mlaf(u, s, +0.7853981853e+0);
r.x = u * t;
u = +0.3539815225e-5;
u = mlaf(u, s, -0.3259574005e-3);
u = mlaf(u, s, +0.1585431583e-1);
u = mlaf(u, s, -0.3084251285e+0);
u = mlaf(u, s, 1);
r.y = u;
if ((q & 2) != 0) { s = r.y; r.y = r.x; r.x = s; }
if ((q & 4) != 0) { r.x = -r.x; }
if (((q+2) & 4) != 0) { r.y = -r.y; }
if (fabsfk(d) > 1e+7f) { r.x = 0; r.y = 1; }
if (xisinff(d)) { r.x = r.y = SLEEF_NANf; }
return r;
}
EXPORT CONST float xtanf(float d) {
int q;
float u, s, x;
x = d;
if (fabsfk(d) < TRIGRANGEMAX2f*0.5f) {
q = (int)rintfk(d * (float)(2 * M_1_PI));
x = mlaf(q, -PI_A2f*0.5f, x);
x = mlaf(q, -PI_B2f*0.5f, x);
x = mlaf(q, -PI_C2f*0.5f, x);
} else if (fabsfk(d) < TRIGRANGEMAXf) {
q = (int)rintfk(d * (float)(2 * M_1_PI));
x = mlaf(q, -PI_Af*0.5f, x);
x = mlaf(q, -PI_Bf*0.5f, x);
x = mlaf(q, -PI_Cf*0.5f, x);
x = mlaf(q, -PI_Df*0.5f, x);
} else {
dfi_t dfi = rempif(d);
q = dfi.i;
x = dfi.df.x + dfi.df.y;
if (xisinff(d) || xisnanf(d)) x = SLEEF_NANf;
}
s = x * x;
if ((q & 1) != 0) x = -x;
float s2 = s * s, s4 = s2 * s2;
u = POLY6(s, s2, s4,
0.00927245803177356719970703f,
0.00331984995864331722259521f,
0.0242998078465461730957031f,
0.0534495301544666290283203f,
0.133383005857467651367188f,
0.333331853151321411132812f);
u = mlaf(s, u * x, x);
if ((q & 1) != 0) u = 1.0f / u;
return u;
}
EXPORT CONST float xtanf_u1(float d) {
int q;
float u;
Sleef_float2 s, t, x;
if (fabsfk(d) < TRIGRANGEMAX2f) {
q = (int)rintfk(d * (float)(2 * M_1_PI));
u = mlaf(q, -PI_A2f*0.5f, d);
s = dfadd2_f2_f_f(u, q * (-PI_B2f*0.5f));
s = dfadd_f2_f2_f(s, q * (-PI_C2f*0.5f));
} else {
dfi_t dfi = rempif(d);
q = dfi.i;
s = dfi.df;
if (xisinff(d) || xisnanf(d)) s.x = SLEEF_NANf;
}
if ((q & 1) != 0) s = dfneg_f2_f2(s);
t = s;
s = dfsqu_f2_f2(s);
s = dfnormalize_f2_f2(s);
u = 0.00446636462584137916564941f;
u = mlaf(u, s.x, -8.3920182078145444393158e-05f);
u = mlaf(u, s.x, 0.0109639242291450500488281f);
u = mlaf(u, s.x, 0.0212360303848981857299805f);
u = mlaf(u, s.x, 0.0540687143802642822265625f);
x = dfadd_f2_f_f(0.133325666189193725585938f, u * s.x);
x = dfadd_f2_f_f2(1, dfmul_f2_f2_f2(dfadd_f2_f_f2(0.33333361148834228515625f, dfmul_f2_f2_f2(s, x)), s));
x = dfmul_f2_f2_f2(t, x);
if ((q & 1) != 0) x = dfrec_f2_f2(x);
u = x.x + x.y;
if (xisnegzerof(d)) u = -0.0f;
return u;
}
EXPORT CONST float xatanf(float s) {
float t, u;
int q = 0;
if (signf(s) == -1) { s = -s; q = 2; }
if (s > 1) { s = 1.0f / s; q |= 1; }
t = s * s;
float t2 = t * t, t4 = t2 * t2;
u = POLY8(t, t2, t4,
0.00282363896258175373077393f,
-0.0159569028764963150024414f,
0.0425049886107444763183594f,
-0.0748900920152664184570312f,
0.106347933411598205566406f,
-0.142027363181114196777344f,
0.199926957488059997558594f,
-0.333331018686294555664062f);
t = s + s * (t * u);
if ((q & 1) != 0) t = 1.570796326794896557998982f - t;
if ((q & 2) != 0) t = -t;
return t;
}
static INLINE CONST float atan2kf(float y, float x) {
float s, t, u;
int q = 0;
if (x < 0) { x = -x; q = -2; }
if (y > x) { t = x; x = y; y = -t; q += 1; }
s = y / x;
t = s * s;
float t2 = t * t, t4 = t2 * t2;
u = POLY8(t, t2, t4,
0.00282363896258175373077393f,
-0.0159569028764963150024414f,
0.0425049886107444763183594f,
-0.0748900920152664184570312f,
0.106347933411598205566406f,
-0.142027363181114196777344f,
0.199926957488059997558594f,
-0.333331018686294555664062f);
t = u * t * s + s;
t = q * (float)(M_PI/2) + t;
return t;
}
EXPORT CONST float xatan2f(float y, float x) {
float r = atan2kf(fabsfk(y), x);
r = mulsignf(r, x);
if (xisinff(x) || x == 0) r = M_PIf/2 - (xisinff(x) ? (signf(x) * (float)(M_PI /2)) : 0);
if (xisinff(y) ) r = M_PIf/2 - (xisinff(x) ? (signf(x) * (float)(M_PI*1/4)) : 0);
if ( y == 0) r = (signf(x) == -1 ? M_PIf : 0);
return xisnanf(x) || xisnanf(y) ? SLEEF_NANf : mulsignf(r, y);
}
EXPORT CONST float xasinf(float d) {
int o = fabsfk(d) < 0.5f;
float x2 = o ? (d*d) : ((1-fabsfk(d))*0.5f), x = o ? fabsfk(d) : SQRTF(x2), u;