File: sleef_builtin.c

package info (click to toggle)
pocl 7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 29,768 kB
  • sloc: lisp: 151,669; ansic: 135,425; cpp: 65,801; python: 1,846; sh: 1,084; ruby: 255; pascal: 231; tcl: 180; makefile: 174; asm: 81; java: 72; xml: 49
file content (938 lines) | stat: -rw-r--r-- 12,755 bytes parent folder | download | duplicates (6)
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
/* OpenCL built-in library: SLEEF C fallback using libm / compiler builtins

   Copyright (c) 2017 Michal Babej / Tampere University of Technology

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   THE SOFTWARE.
*/

#define _GNU_SOURCE
#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdint.h>

#include "sleef.h"
#include "sleef_cl.h"
#include "rename.h"

//##################################################################

static int64_t
doubleToRawLongBits (double d)
{
  union
  {
    double f;
    int64_t i;
  } tmp;
  tmp.f = d;
  return tmp.i;
}

static double
longBitsToDouble (int64_t i)
{
  union
  {
    double f;
    int64_t i;
  } tmp;
  tmp.i = i;
  return tmp.f;
}

static double
fabsk (double x)
{
  return longBitsToDouble (0x7fffffffffffffffLL & doubleToRawLongBits (x));
}

static double
mulsign (double x, double y)
{
  return longBitsToDouble (doubleToRawLongBits (x)
                           ^ (doubleToRawLongBits (y) & (1LL << 63)));
}

//##################################################################

#define INFINITYf ((float)INFINITY)
#define NANf ((float)NAN)

static int32_t
floatToRawIntBits (float d)
{
  union
  {
    float f;
    int32_t i;
  } tmp;
  tmp.f = d;
  return tmp.i;
}

static float
intBitsToFloat (int32_t i)
{
  union
  {
    float f;
    int32_t i;
  } tmp;
  tmp.i = i;
  return tmp.f;
}

static float
fabsfk (float x)
{
  return intBitsToFloat (0x7fffffffL & floatToRawIntBits (x));
}

static float
mulsignf (float x, float y)
{
  return intBitsToFloat (floatToRawIntBits (x)
                         ^ (floatToRawIntBits (y) & (1 << 31)));
}

//##################################################################


double
xsin (double x)
{
  return __builtin_sin (x);
}
double
xcos (double x)
{
  return __builtin_cos (x);
}
double
xtan (double x)
{
  return __builtin_tan (x);
}
double
xasin (double x)
{
  return __builtin_asin (x);
}
double
xacos (double x)
{
  return __builtin_acos (x);
}
double
xatan (double x)
{
  return __builtin_atan (x);
}
double
xatan2 (double x, double y)
{
  return __builtin_atan2 (x, y);
}
double
xlog (double x)
{
  return __builtin_log (x);
}
double
xcbrt (double x)
{
  return __builtin_cbrt (x);
}

double
xsin_u1 (double x)
{
  return __builtin_sin (x);
}
double
xcos_u1 (double x)
{
  return __builtin_cos (x);
}
double
xtan_u1 (double x)
{
  return __builtin_tan (x);
}
double
xasin_u1 (double x)
{
  return __builtin_asin (x);
}
double
xacos_u1 (double x)
{
  return __builtin_acos (x);
}
double
xatan_u1 (double x)
{
  return __builtin_atan (x);
}
double
xatan2_u1 (double x, double y)
{
  return __builtin_atan2 (x, y);
}
double
xlog_u1 (double x)
{
  return __builtin_log (x);
}
double
xcbrt_u1 (double x)
{
  return __builtin_cbrt (x);
}

double
xexp (double x)
{
  return __builtin_exp (x);
}

double
xpow (double x, double y)
{
  return __builtin_pow (x, y);
}

double
xpown (double x, int y)
{
  return __builtin_pow (x, (double)y);
}

double
xpowr (double x, double y)
{
  if (x < 0.0)
    return NAN;
  if (isnan(y))
    return y;
  double res = __builtin_pow (x, y);
  return res;
}

double
xsinh (double x)
{
  return __builtin_sinh (x);
}

double
xcosh (double x)
{
  return __builtin_cosh (x);
}
double
xtanh (double x)
{
  return __builtin_tanh (x);
}

double
xasinh (double x)
{
  return __builtin_asinh (x);
}
double
xacosh (double x)
{
  return __builtin_acosh (x);
}
double
xatanh (double x)
{
  return __builtin_atanh (x);
}

double
xexp2 (double x)
{
  return __builtin_exp2 (x);
}
double
xexp10 (double x)
{
  return exp10 (x);
}

double
xexpm1 (double x)
{
  return __builtin_expm1 (x);
}

double
xlog10 (double x)
{
  return __builtin_log10 (x);
}
double
xlog1p (double x)
{
  return __builtin_log1p (x);
}

double
xsinpi_u05 (double x)
{
  return __builtin_sin (x * (double)M_PI);
}
double
xcospi_u05 (double x)
{
  return __builtin_cos (x * (double)M_PI);
}

Sleef_double2
xsincos (double x)
{
  Sleef_double2 tmp;
  sincos (x, &tmp.x, &tmp.y);
  return tmp;
}

Sleef_double2
xsincos_u1 (double x)
{
  return xsincos (x);
}

double
xldexp (double x, int k)
{
  return __builtin_ldexp (x, k);
}

int
xilogb (double x)
{
  return __builtin_ilogb (x);
}

double
xfma (double x, double y, double z)
{
  return __builtin_fma (x, y, z);
}

double
xsqrt_u05 (double x)
{
  return __builtin_sqrt (x);
}

double
xsqrt_u35 (double x)
{
  return __builtin_sqrt (x);
}

double
xhypot_u05 (double x, double y)
{
  return __builtin_hypot (x, y);
}

double
xhypot_u35 (double x, double y)
{
  return __builtin_hypot (x, y);
}

double
xfabs (double x)
{
  return __builtin_fabs (x);
}

double
xcopysign (double x, double y)
{
  return __builtin_copysign (x, y);
}
double
xfmax (double x, double y)
{
  return __builtin_fmax (x, y);
}
double
xfmin (double x, double y)
{
  return __builtin_fmin (x, y);
}

double
xfdim (double x, double y)
{
  return __builtin_fdim (x, y);
}
double
xtrunc (double x)
{
  return __builtin_trunc (x);
}
double
xfloor (double x)
{
  return __builtin_floor (x);
}

double
xceil (double x)
{
  return __builtin_ceil (x);
}
double
xround (double x)
{
  return __builtin_round (x);
}
double
xrint (double x)
{
  return __builtin_rint (x);
}

double
xnextafter (double x, double y)
{
  return __builtin_nextafter (x, y);
}

double
xfrfrexp (double x)
{
  union
  {
    double f;
    uint64_t u;
  } cx;

  if (__builtin_isnan (x))
    return x;

  if (fabsk (x) < DBL_MIN)
    x *= (1ULL << 63);

  cx.f = x;
  cx.u &= ~0x7ff0000000000000ULL;
  cx.u |= 0x3fe0000000000000ULL;

  if (__builtin_isinf (x))
    cx.f = mulsign (INFINITY, x);
  if (x == 0)
    cx.f = x;

  return cx.f;
}

int
xexpfrexp (double x)
{
  union
  {
    double f;
    uint64_t u;
  } cx;

  int ret = 0;

  if (fabsk (x) < DBL_MIN)
    {
      x *= (1ULL << 63);
      ret = -63;
    }

  cx.f = x;
  ret += (int32_t) (((cx.u >> 52) & 0x7ff)) - 0x3fe;

  if (x == 0 || __builtin_isnan (x) || __builtin_isinf (x))
    ret = 0;

  return ret;
}

double
xfmod (double x, double y)
{
  return __builtin_fmod (x, y);
}

Sleef_double2
xmodf (double x)
{
  Sleef_double2 res;
  double tmp;
  res.x = __builtin_modf (x, &tmp);
  res.y = tmp;
  return res;
}

double
xlgamma_u1 (double x)
{
  return __builtin_lgamma (x);
}
Sleef_double2
xlgamma_r_u1 (double x)
{
  Sleef_double2 ret;
  int sign;
  ret.x = lgamma_r (x, &sign);
  ret.y = (sign > 0 ? 1.0 : -1.0);
  return ret;
}
double
xtgamma_u1 (double x)
{
  return __builtin_tgamma (x);
}
double
xerf_u1 (double x)
{
  return __builtin_erf (x);
}
double
xerfc_u15 (double x)
{
  return __builtin_erfc (x);
}

// *********************************************************************
// *********************************************************************
// *********************************************************************
// *********************************************************************

float
xsinf (float x)
{
  return __builtin_sinf (x);
}
float
xcosf (float x)
{
  return __builtin_cosf (x);
}
float
xtanf (float x)
{
  return __builtin_tanf (x);
}
float
xasinf (float x)
{
  return __builtin_asinf (x);
}
float
xacosf (float x)
{
  return __builtin_acosf (x);
}
float
xatanf (float x)
{
  return __builtin_atanf (x);
}
float
xatan2f (float x, float y)
{
  return __builtin_atan2f (x, y);
}
float
xlogf (float x)
{
  return __builtin_logf (x);
}
float
xcbrtf (float x)
{
  return __builtin_cbrtf (x);
}

float
xsinf_u1 (float x)
{
  return __builtin_sinf (x);
}
float
xcosf_u1 (float x)
{
  return __builtin_cosf (x);
}
float
xtanf_u1 (float x)
{
  return __builtin_tanf (x);
}
float
xasinf_u1 (float x)
{
  return __builtin_asinf (x);
}
float
xacosf_u1 (float x)
{
  return __builtin_acosf (x);
}
float
xatanf_u1 (float x)
{
  return __builtin_atanf (x);
}
float
xatan2f_u1 (float x, float y)
{
  return __builtin_atan2f (x, y);
}
float
xlogf_u1 (float x)
{
  return __builtin_logf (x);
}
float
xcbrtf_u1 (float x)
{
  return __builtin_cbrtf (x);
}

float
xexpf (float x)
{
  return __builtin_expf (x);
}

float
xpowf (float x, float y)
{

  return (float) __builtin_pow ((double)x, (double)y);
}

float
xpownf (float x, int y)
{
  return (float) __builtin_pow ((double)x, (double)y);
}

float
xpowrf (float x, float y)
{
  if (x < 0.0f)
    return NAN;
  float res = (float) __builtin_pow ((double)x, (double)y);
  return res;
}

float
xsinhf (float x)
{
  return __builtin_sinhf (x);
}
float
xcoshf (float x)
{
  return __builtin_coshf (x);
}
float
xtanhf (float x)
{
  return __builtin_tanhf (x);
}

float
xasinhf (float x)
{
  return __builtin_asinhf (x);
}
float
xacoshf (float x)
{
  return __builtin_acoshf (x);
}
float
xatanhf (float x)
{
  return __builtin_atanhf (x);
}

float
xexp2f (float x)
{
  return __builtin_exp2f (x);
}

float
xexp10f (float x)
{
  return exp10f (x);
}

float
xexpm1f (float x)
{
  return __builtin_expm1f (x);
}

float
xlog10f (float x)
{
  return __builtin_log10f (x);
}
float
xlog1pf (float x)
{
  return __builtin_log1pf (x);
}

float
xsinpif_u05 (float x)
{
  return __builtin_sinf (x * (float)M_PI);
}
float
xcospif_u05 (float x)
{
  return __builtin_cosf (x * (float)M_PI);
}

Sleef_float2
xsincosf (float x)
{
  Sleef_float2 tmp;
  sincosf (x, &tmp.x, &tmp.y);
  return tmp;
}

Sleef_float2
xsincosf_u1 (float x)
{
  return xsincosf (x);
}

float
xsqrtf_u05 (float x)
{
  return __builtin_sqrtf (x);
}

float
xsqrtf_u35 (float x)
{
  return __builtin_sqrtf (x);
}

float
xhypotf_u05 (float x, float y)
{
  return __builtin_hypotf (x, y);
}

float
xhypotf_u35 (float x, float y)
{
  return __builtin_hypotf (x, y);
}

float
xldexpf (float x, int k)
{
  return __builtin_ldexpf (x, k);
}

int
xilogbf (float x)
{
  return __builtin_ilogbf (x);
}

float
xfmaf (float x, float y, float z)
{
  return __builtin_fmaf (x, y, z);
}

float
xfabsf (float x)
{
  return __builtin_fabsf (x);
}

float
xcopysignf (float x, float y)
{
  return __builtin_copysignf (x, y);
}
float
xfmaxf (float x, float y)
{
  return __builtin_fmaxf (x, y);
}
float
xfminf (float x, float y)
{
  return __builtin_fminf (x, y);
}

float
xfdimf (float x, float y)
{
  return __builtin_fdimf (x, y);
}
float
xtruncf (float x)
{
  return __builtin_truncf (x);
}
float
xfloorf (float x)
{
  return __builtin_floorf (x);
}

float
xceilf (float x)
{
  return __builtin_ceilf (x);
}
float
xroundf (float x)
{
  return __builtin_roundf (x);
}
float
xrintf (float x)
{
  return __builtin_rintf (x);
}

float
xnextafterf (float x, float y)
{
  return __builtin_nextafterf (x, y);
}


float
xfrfrexpf (float x)
{
  union
  {
    float f;
    int32_t u;
  } cx;

  if (__builtin_isnan (x))
    return x;

  if (fabsfk (x) < FLT_MIN)
    x *= (1 << 30);

  cx.f = x;
  cx.u &= ~0x7f800000U;
  cx.u |= 0x3f000000U;

  if (__builtin_isinf (x))
    cx.f = mulsignf (INFINITYf, x);
  if (x == 0)
    cx.f = x;

  return cx.f;
}

int
xexpfrexpf (float x)
{
  union
  {
    float f;
    uint32_t u;
  } cx;

  int ret = 0;

  if (fabsfk (x) < FLT_MIN)
    {
      x *= (1 << 30);
      ret = -30;
    }

  cx.f = x;
  ret += (int32_t) (((cx.u >> 23) & 0xff)) - 0x7e;

  if (x == 0 || __builtin_isnan (x) || __builtin_isinf (x))
    ret = 0;

  return ret;
}

float
xfmodf (float x, float y)
{
  return __builtin_fmodf (x, y);
}

Sleef_float2
xmodff (float x)
{
  Sleef_float2 res;
  float tmp;
  res.x = __builtin_modff (x, &tmp);
  res.y = tmp;
  return res;
}

float
xlgammaf_u1 (float x)
{
  return __builtin_lgammaf (x);
}
Sleef_float2
xlgamma_rf_u1 (float x)
{
  Sleef_float2 ret;
  int sign;
  ret.x = lgammaf_r (x, &sign);
  ret.y = (sign > 0 ? 1.0f : -1.0f);
  return ret;
}

float
xtgammaf_u1 (float x)
{
  return __builtin_tgammaf (x);
}
float
xerff_u1 (float x)
{
  return __builtin_erff (x);
}
float
xerfcf_u15 (float x)
{
  return __builtin_erfcf (x);
}