-
Notifications
You must be signed in to change notification settings - Fork 529
/
POSIX.xs
3901 lines (3604 loc) · 91 KB
/
POSIX.xs
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
#define PERL_EXT_POSIX
#define PERL_EXT
#ifdef NETWARE
#define _POSIX_
/*
* Ideally this should be somewhere down in the includes
* but putting it in other places is giving compiler errors.
* Also here I am unable to check for HAS_UNAME since it wouldn't have
* yet come into the file at this stage - sgp 18th Oct 2000
*/
#include <sys/utsname.h>
#endif /* NETWARE */
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#define PERLIO_NOT_STDIO 1
#include "perl.h"
#include "XSUB.h"
static int not_here(const char *s);
#if defined(PERL_IMPLICIT_SYS)
# undef signal
# undef open
# undef setmode
# define open PerlLIO_open3
#endif
#include <ctype.h>
#ifdef I_DIRENT /* XXX maybe better to just rely on perl.h? */
#include <dirent.h>
#endif
#include <errno.h>
#ifdef WIN32
#include <sys/errno2.h>
#endif
#include <float.h>
#ifdef I_FENV
#if !(defined(__vax__) && defined(__NetBSD__))
#include <fenv.h>
#endif
#endif
#include <limits.h>
#include <locale.h>
#include <math.h>
#ifdef I_PWD
#include <pwd.h>
#endif
#include <setjmp.h>
#include <signal.h>
#include <stdarg.h>
#include <stddef.h>
#ifdef I_UNISTD
#include <unistd.h>
#endif
#ifdef I_SYS_TIME
# include <sys/time.h>
#endif
#ifdef I_SYS_RESOURCE
# include <sys/resource.h>
#endif
/* Cygwin's stdio.h doesn't make cuserid() visible with -D_GNU_SOURCE,
unlike Linux.
*/
#ifdef __CYGWIN__
# undef HAS_CUSERID
#endif
#if defined(USE_QUADMATH) && defined(I_QUADMATH)
# undef M_E
# undef M_LOG2E
# undef M_LOG10E
# undef M_LN2
# undef M_LN10
# undef M_PI
# undef M_PI_2
# undef M_PI_4
# undef M_1_PI
# undef M_2_PI
# undef M_2_SQRTPI
# undef M_SQRT2
# undef M_SQRT1_2
# define M_E M_Eq
# define M_LOG2E M_LOG2Eq
# define M_LOG10E M_LOG10Eq
# define M_LN2 M_LN2q
# define M_LN10 M_LN10q
# define M_PI M_PIq
# define M_PI_2 M_PI_2q
# define M_PI_4 M_PI_4q
# define M_1_PI M_1_PIq
# define M_2_PI M_2_PIq
# define M_2_SQRTPI M_2_SQRTPIq
# define M_SQRT2 M_SQRT2q
# define M_SQRT1_2 M_SQRT1_2q
#else
# ifdef USE_LONG_DOUBLE
# undef M_E
# undef M_LOG2E
# undef M_LOG10E
# undef M_LN2
# undef M_LN10
# undef M_PI
# undef M_PI_2
# undef M_PI_4
# undef M_1_PI
# undef M_2_PI
# undef M_2_SQRTPI
# undef M_SQRT2
# undef M_SQRT1_2
# define FLOAT_C(c) CAT2(c,L)
# else
# define FLOAT_C(c) (c)
# endif
# ifndef M_E
# define M_E FLOAT_C(2.71828182845904523536028747135266250)
# endif
# ifndef M_LOG2E
# define M_LOG2E FLOAT_C(1.44269504088896340735992468100189214)
# endif
# ifndef M_LOG10E
# define M_LOG10E FLOAT_C(0.434294481903251827651128918916605082)
# endif
# ifndef M_LN2
# define M_LN2 FLOAT_C(0.693147180559945309417232121458176568)
# endif
# ifndef M_LN10
# define M_LN10 FLOAT_C(2.30258509299404568401799145468436421)
# endif
# ifndef M_PI
# define M_PI FLOAT_C(3.14159265358979323846264338327950288)
# endif
# ifndef M_PI_2
# define M_PI_2 FLOAT_C(1.57079632679489661923132169163975144)
# endif
# ifndef M_PI_4
# define M_PI_4 FLOAT_C(0.785398163397448309615660845819875721)
# endif
# ifndef M_1_PI
# define M_1_PI FLOAT_C(0.318309886183790671537767526745028724)
# endif
# ifndef M_2_PI
# define M_2_PI FLOAT_C(0.636619772367581343075535053490057448)
# endif
# ifndef M_2_SQRTPI
# define M_2_SQRTPI FLOAT_C(1.12837916709551257389615890312154517)
# endif
# ifndef M_SQRT2
# define M_SQRT2 FLOAT_C(1.41421356237309504880168872420969808)
# endif
# ifndef M_SQRT1_2
# define M_SQRT1_2 FLOAT_C(0.707106781186547524400844362104849039)
# endif
#endif
#if !defined(INFINITY) && defined(NV_INF)
# define INFINITY NV_INF
#endif
#if !defined(NAN) && defined(NV_NAN)
# define NAN NV_NAN
#endif
#if !defined(Inf) && defined(NV_INF)
# define Inf NV_INF
#endif
#if !defined(NaN) && defined(NV_NAN)
# define NaN NV_NAN
#endif
/* We will have an emulation. */
#ifndef FP_INFINITE
# define FP_INFINITE 0
# define FP_NAN 1
# define FP_NORMAL 2
# define FP_SUBNORMAL 3
# define FP_ZERO 4
#endif
/* We will have an emulation. */
#ifndef FE_TONEAREST
# define FE_TOWARDZERO 0
# define FE_TONEAREST 1
# define FE_UPWARD 2
# define FE_DOWNWARD 3
#endif
/* C89 math.h:
acos asin atan atan2 ceil cos cosh exp fabs floor fmod frexp ldexp
log log10 modf pow sin sinh sqrt tan tanh
* Implemented in core:
atan2 cos exp log pow sin sqrt
* C99 math.h added:
acosh asinh atanh cbrt copysign erf erfc exp2 expm1 fdim fma fmax
fmin fpclassify hypot ilogb isfinite isgreater isgreaterequal isinf
isless islessequal islessgreater isnan isnormal isunordered lgamma
log1p log2 logb lrint lround nan nearbyint nextafter nexttoward remainder
remquo rint round scalbn signbit tgamma trunc
See:
http://pubs.opengroup.org/onlinepubs/009695399/basedefs/math.h.html
* Berkeley/SVID extensions:
j0 j1 jn y0 y1 yn
* Configure already (5.21.5) scans for:
copysign*l* fpclassify isfinite isinf isnan isnan*l* ilogb*l* signbit scalbn*l*
* For floating-point round mode (which matters for e.g. lrint and rint)
fegetround fesetround
*/
/* XXX Constant FP_FAST_FMA (if true, FMA is faster) */
/* XXX Add ldiv(), lldiv()? It's C99, but from stdlib.h, not math.h */
/* XXX Beware old gamma() -- one cannot know whether that is the
* gamma or the log of gamma, that's why the new tgamma and lgamma.
* Though also remember lgamma_r. */
/* Certain AIX releases have the C99 math, but not in long double.
* The <math.h> has them, e.g. __expl128, but no library has them!
*
* Also see the comments in hints/aix.sh about long doubles. */
#if defined(USE_QUADMATH) && defined(I_QUADMATH)
# define c99_acosh acoshq
# define c99_asinh asinhq
# define c99_atanh atanhq
# define c99_cbrt cbrtq
# define c99_copysign copysignq
# define c99_erf erfq
# define c99_erfc erfcq
/* no exp2q */
# define c99_expm1 expm1q
# define c99_fdim fdimq
# define c99_fma fmaq
# define c99_fmax fmaxq
# define c99_fmin fminq
# define c99_hypot hypotq
# define c99_ilogb ilogbq
# define c99_lgamma lgammaq
# define c99_log1p log1pq
# define c99_log2 log2q
/* no logbq */
# if defined(USE_64_BIT_INT) && QUADKIND == QUAD_IS_LONG_LONG
# define c99_lrint llrintq
# define c99_lround llroundq
# else
# define c99_lrint lrintq
# define c99_lround lroundq
# endif
# define c99_nan nanq
# define c99_nearbyint nearbyintq
# define c99_nextafter nextafterq
/* no nexttowardq */
# define c99_remainder remainderq
# define c99_remquo remquoq
# define c99_rint rintq
# define c99_round roundq
# define c99_scalbn scalbnq
/* We already define Perl_signbit to signbitq in perl.h. */
# define c99_tgamma tgammaq
# define c99_trunc truncq
# define bessel_j0 j0q
# define bessel_j1 j1q
# define bessel_jn jnq
# define bessel_y0 y0q
# define bessel_y1 y1q
# define bessel_yn ynq
#elif defined(USE_LONG_DOUBLE) && \
(defined(HAS_FREXPL) || defined(HAS_ILOGBL)) && defined(HAS_SQRTL)
/* Use some of the Configure scans for long double math functions
* as the canary for all the C99 *l variants being defined. */
# define c99_acosh acoshl
# define c99_asinh asinhl
# define c99_atanh atanhl
# define c99_cbrt cbrtl
# define c99_copysign copysignl
# define c99_erf erfl
# define c99_erfc erfcl
# define c99_exp2 exp2l
# define c99_expm1 expm1l
# define c99_fdim fdiml
# define c99_fma fmal
# define c99_fmax fmaxl
# define c99_fmin fminl
# define c99_hypot hypotl
# define c99_ilogb ilogbl
# define c99_lgamma lgammal
# define c99_log1p log1pl
# define c99_log2 log2l
# define c99_logb logbl
# if defined(USE_64_BIT_INT) && QUADKIND == QUAD_IS_LONG_LONG && defined(HAS_LLRINTL)
# define c99_lrint llrintl
# elif defined(HAS_LRINTL)
# define c99_lrint lrintl
# endif
# if defined(USE_64_BIT_INT) && QUADKIND == QUAD_IS_LONG_LONG && defined(HAS_LLROUNDL)
# define c99_lround llroundl
# elif defined(HAS_LROUNDL)
# define c99_lround lroundl
# endif
# define c99_nan nanl
# define c99_nearbyint nearbyintl
# define c99_nextafter nextafterl
# define c99_nexttoward nexttowardl
# define c99_remainder remainderl
# define c99_remquo remquol
# define c99_rint rintl
# define c99_round roundl
# define c99_scalbn scalbnl
/* We already define Perl_signbit in perl.h. */
# define c99_tgamma tgammal
# define c99_trunc truncl
#else
# define c99_acosh acosh
# define c99_asinh asinh
# define c99_atanh atanh
# define c99_cbrt cbrt
# define c99_copysign copysign
# define c99_erf erf
# define c99_erfc erfc
# define c99_exp2 exp2
# define c99_expm1 expm1
# define c99_fdim fdim
# define c99_fma fma
# define c99_fmax fmax
# define c99_fmin fmin
# define c99_hypot hypot
# define c99_ilogb ilogb
# define c99_lgamma lgamma
# define c99_log1p log1p
# define c99_log2 log2
# define c99_logb logb
# if defined(USE_64_BIT_INT) && QUADKIND == QUAD_IS_LONG_LONG && defined(HAS_LLRINT)
# define c99_lrint llrint
# else
# define c99_lrint lrint
# endif
# if defined(USE_64_BIT_INT) && QUADKIND == QUAD_IS_LONG_LONG && defined(HAS_LLROUND)
# define c99_lround llround
# else
# define c99_lround lround
# endif
# define c99_nan nan
# define c99_nearbyint nearbyint
# define c99_nextafter nextafter
# define c99_nexttoward nexttoward
# define c99_remainder remainder
# define c99_remquo remquo
# define c99_rint rint
# define c99_round round
# define c99_scalbn scalbn
/* We already define Perl_signbit in perl.h. */
# define c99_tgamma tgamma
# define c99_trunc trunc
#endif
/* AIX xlc (__IBMC__) really doesn't have the following long double
* math interfaces (no __acoshl128 aka acoshl, etc.), see
* hints/aix.sh. These are in the -lc128 but fail to be found
* during dynamic linking/loading.
*
* XXX1 Better Configure scans
* XXX2 Is this xlc version dependent? */
#if defined(USE_LONG_DOUBLE) && defined(__IBMC__)
# undef c99_acosh
# undef c99_asinh
# undef c99_atanh
# undef c99_cbrt
# undef c99_copysign
# undef c99_exp2
# undef c99_expm1
# undef c99_fdim
# undef c99_fma
# undef c99_fmax
# undef c99_fmin
# undef c99_hypot
# undef c99_ilogb
# undef c99_lrint
# undef c99_lround
# undef c99_log1p
# undef c99_log2
# undef c99_logb
# undef c99_nan
# undef c99_nearbyint
# undef c99_nextafter
# undef c99_nexttoward
# undef c99_remainder
# undef c99_remquo
# undef c99_rint
# undef c99_round
# undef c99_scalbn
# undef c99_tgamma
# undef c99_trunc
#endif
/* The cc with NetBSD 8.0 and 9.0 claims to be a C11 hosted compiler,
* but doesn't define several functions required by C99, let alone C11.
* http://gnats.netbsd.org/53234
*/
#if defined(USE_LONG_DOUBLE) && defined(__NetBSD__) \
&& !defined(NETBSD_HAVE_FIXED_LONG_DOUBLE_MATH)
# undef c99_expm1
# undef c99_lgamma
# undef c99_log1p
# undef c99_log2
# undef c99_nexttoward
# undef c99_remainder
# undef c99_remquo
# undef c99_tgamma
#endif
#ifndef isunordered
# ifdef Perl_isnan
# define isunordered(x, y) (Perl_isnan(x) || Perl_isnan(y))
# elif defined(HAS_UNORDERED)
# define isunordered(x, y) unordered(x, y)
# endif
#endif
/* XXX these isgreater/isnormal/isunordered macros definitions should
* be moved further in the file to be part of the emulations, so that
* platforms can e.g. #undef c99_isunordered and have it work like
* it does for the other interfaces. */
#if !defined(isgreater) && defined(isunordered)
# define isgreater(x, y) (!isunordered((x), (y)) && (x) > (y))
# define isgreaterequal(x, y) (!isunordered((x), (y)) && (x) >= (y))
# define isless(x, y) (!isunordered((x), (y)) && (x) < (y))
# define islessequal(x, y) (!isunordered((x), (y)) && (x) <= (y))
# define islessgreater(x, y) (!isunordered((x), (y)) && \
((x) > (y) || (y) > (x)))
#endif
/* Check both the Configure symbol and the macro-ness (like C99 promises). */
#if defined(HAS_FPCLASSIFY) && defined(fpclassify)
# define c99_fpclassify fpclassify
#endif
/* Like isnormal(), the isfinite(), isinf(), and isnan() are also C99
and also (sizeof-arg-aware) macros, but they are already well taken
care of by Configure et al, and defined in perl.h as
Perl_isfinite(), Perl_isinf(), and Perl_isnan(). */
#ifdef isnormal
# define c99_isnormal isnormal
#endif
#ifdef isgreater /* canary for all the C99 is*<cmp>* macros. */
# define c99_isgreater isgreater
# define c99_isgreaterequal isgreaterequal
# define c99_isless isless
# define c99_islessequal islessequal
# define c99_islessgreater islessgreater
# define c99_isunordered isunordered
#endif
/* The Great Wall of Undef where according to the definedness of HAS_FOO symbols
* the corresponding c99_foo wrappers are undefined. This list doesn't include
* the isfoo() interfaces because they are either type-aware macros, or dealt
* separately, already in perl.h */
#ifndef HAS_ACOSH
# undef c99_acosh
#endif
#ifndef HAS_ASINH
# undef c99_asinh
#endif
#ifndef HAS_ATANH
# undef c99_atanh
#endif
#ifndef HAS_CBRT
# undef c99_cbrt
#endif
#ifndef HAS_COPYSIGN
# undef c99_copysign
#endif
#ifndef HAS_ERF
# undef c99_erf
#endif
#ifndef HAS_ERFC
# undef c99_erfc
#endif
#ifndef HAS_EXP2
# undef c99_exp2
#endif
#ifndef HAS_EXPM1
# undef c99_expm1
#endif
#ifndef HAS_FDIM
# undef c99_fdim
#endif
#ifndef HAS_FMA
# undef c99_fma
#endif
#ifndef HAS_FMAX
# undef c99_fmax
#endif
#ifndef HAS_FMIN
# undef c99_fmin
#endif
#ifndef HAS_FPCLASSIFY
# undef c99_fpclassify
#endif
#ifndef HAS_HYPOT
# undef c99_hypot
#endif
#ifndef HAS_ILOGB
# undef c99_ilogb
#endif
#ifndef HAS_LGAMMA
# undef c99_lgamma
#endif
#ifndef HAS_LOG1P
# undef c99_log1p
#endif
#ifndef HAS_LOG2
# undef c99_log2
#endif
#ifndef HAS_LOGB
# undef c99_logb
#endif
#ifndef HAS_LRINT
# undef c99_lrint
#endif
#ifndef HAS_LROUND
# undef c99_lround
#endif
#ifndef HAS_NAN
# undef c99_nan
#endif
#ifndef HAS_NEARBYINT
# undef c99_nearbyint
#endif
#ifndef HAS_NEXTAFTER
# undef c99_nextafter
#endif
#ifndef HAS_NEXTTOWARD
# undef c99_nexttoward
#endif
#ifndef HAS_REMAINDER
# undef c99_remainder
#endif
#ifndef HAS_REMQUO
# undef c99_remquo
#endif
#ifndef HAS_RINT
# undef c99_rint
#endif
#ifndef HAS_ROUND
# undef c99_round
#endif
#ifndef HAS_SCALBN
# undef c99_scalbn
#endif
#ifndef HAS_TGAMMA
# undef c99_tgamma
#endif
#ifndef HAS_TRUNC
# undef c99_trunc
#endif
#ifdef _MSC_VER
/* Some APIs exist under Win32 with "underbar" names. */
# undef c99_hypot
# undef c99_logb
# undef c99_nextafter
# define c99_hypot _hypot
# define c99_logb _logb
# define c99_nextafter _nextafter
# define bessel_j0 _j0
# define bessel_j1 _j1
# define bessel_jn _jn
# define bessel_y0 _y0
# define bessel_y1 _y1
# define bessel_yn _yn
#endif
/* The Bessel functions: BSD, SVID, XPG4, and POSIX. But not C99. */
#if defined(HAS_J0) && !defined(bessel_j0)
# if defined(USE_LONG_DOUBLE) && defined(HAS_J0L)
# define bessel_j0 j0l
# define bessel_j1 j1l
# define bessel_jn jnl
# define bessel_y0 y0l
# define bessel_y1 y1l
# define bessel_yn ynl
# else
# define bessel_j0 j0
# define bessel_j1 j1
# define bessel_jn jn
# define bessel_y0 y0
# define bessel_y1 y1
# define bessel_yn yn
# endif
#endif
/* Emulations for missing math APIs.
*
* Keep in mind that the point of many of these functions is that
* they, if available, are supposed to give more precise/more
* numerically stable results.
*
* See e.g. http://www.johndcook.com/math_h.html
*/
#ifndef c99_acosh
static NV my_acosh(NV x)
{
return Perl_log(x + Perl_sqrt(x * x - 1));
}
# define c99_acosh my_acosh
#endif
#ifndef c99_asinh
static NV my_asinh(NV x)
{
return Perl_log(x + Perl_sqrt(x * x + 1));
}
# define c99_asinh my_asinh
#endif
#ifndef c99_atanh
static NV my_atanh(NV x)
{
return (Perl_log(1 + x) - Perl_log(1 - x)) / 2;
}
# define c99_atanh my_atanh
#endif
#ifndef c99_cbrt
static NV my_cbrt(NV x)
{
static const NV one_third = (NV)1.0/3;
return x >= 0.0 ? Perl_pow(x, one_third) : -Perl_pow(-x, one_third);
}
# define c99_cbrt my_cbrt
#endif
#ifndef c99_copysign
static NV my_copysign(NV x, NV y)
{
return y >= 0 ? (x < 0 ? -x : x) : (x < 0 ? x : -x);
}
# define c99_copysign my_copysign
#endif
/* XXX cosh (though c89) */
#ifndef c99_erf
static NV my_erf(NV x)
{
/* http://www.johndcook.com/cpp_erf.html -- public domain */
NV a1 = 0.254829592;
NV a2 = -0.284496736;
NV a3 = 1.421413741;
NV a4 = -1.453152027;
NV a5 = 1.061405429;
NV p = 0.3275911;
NV t, y;
int sign = x < 0 ? -1 : 1; /* Save the sign. */
x = PERL_ABS(x);
/* Abramowitz and Stegun formula 7.1.26 */
t = 1.0 / (1.0 + p * x);
y = 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1) * t * Perl_exp(-x*x);
return sign * y;
}
# define c99_erf my_erf
#endif
#ifndef c99_erfc
static NV my_erfc(NV x) {
/* This is not necessarily numerically stable, but better than nothing. */
return 1.0 - c99_erf(x);
}
# define c99_erfc my_erfc
#endif
#ifndef c99_exp2
static NV my_exp2(NV x)
{
return Perl_pow((NV)2.0, x);
}
# define c99_exp2 my_exp2
#endif
#ifndef c99_expm1
static NV my_expm1(NV x)
{
if (PERL_ABS(x) < 1e-5)
/* http://www.johndcook.com/cpp_expm1.html -- public domain.
* Taylor series, the first four terms (the last term quartic). */
/* Probably not enough for long doubles. */
return x * (1.0 + x * (1/2.0 + x * (1/6.0 + x/24.0)));
else
return Perl_exp(x) - 1;
}
# define c99_expm1 my_expm1
#endif
#ifndef c99_fdim
static NV my_fdim(NV x, NV y)
{
#ifdef NV_NAN
return (Perl_isnan(x) || Perl_isnan(y)) ? NV_NAN : (x > y ? x - y : 0);
#else
return (x > y ? x - y : 0);
#endif
}
# define c99_fdim my_fdim
#endif
#ifndef c99_fma
static NV my_fma(NV x, NV y, NV z)
{
return (x * y) + z;
}
# define c99_fma my_fma
#endif
#ifndef c99_fmax
static NV my_fmax(NV x, NV y)
{
#ifdef NV_NAN
if (Perl_isnan(x)) {
return Perl_isnan(y) ? NV_NAN : y;
} else if (Perl_isnan(y)) {
return x;
}
#endif
return x > y ? x : y;
}
# define c99_fmax my_fmax
#endif
#ifndef c99_fmin
static NV my_fmin(NV x, NV y)
{
#ifdef NV_NAN
if (Perl_isnan(x)) {
return Perl_isnan(y) ? NV_NAN : y;
} else if (Perl_isnan(y)) {
return x;
}
#endif
return x < y ? x : y;
}
# define c99_fmin my_fmin
#endif
#ifndef c99_fpclassify
static IV my_fpclassify(NV x)
{
#ifdef Perl_fp_class_inf
if (Perl_fp_class_inf(x)) return FP_INFINITE;
if (Perl_fp_class_nan(x)) return FP_NAN;
if (Perl_fp_class_norm(x)) return FP_NORMAL;
if (Perl_fp_class_denorm(x)) return FP_SUBNORMAL;
if (Perl_fp_class_zero(x)) return FP_ZERO;
# define c99_fpclassify my_fpclassify
#endif
return -1;
}
#endif
#ifndef c99_hypot
static NV my_hypot(NV x, NV y)
{
/* http://en.wikipedia.org/wiki/Hypot */
NV t;
x = PERL_ABS(x); /* Take absolute values. */
if (y == 0)
return x;
#ifdef NV_INF
if (Perl_isnan(y))
return NV_INF;
#endif
y = PERL_ABS(y);
if (x < y) { /* Swap so that y is less. */
t = x;
x = y;
y = t;
}
t = y / x;
return x * Perl_sqrt(1.0 + t * t);
}
# define c99_hypot my_hypot
#endif
#ifndef c99_ilogb
static IV my_ilogb(NV x)
{
return (IV)(Perl_log(x) * M_LOG2E);
}
# define c99_ilogb my_ilogb
#endif
/* tgamma and lgamma emulations based on
* http://www.johndcook.com/cpp_gamma.html,
* code placed in public domain.
*
* Note that these implementations (neither the johndcook originals
* nor these) do NOT set the global signgam variable. This is not
* necessarily a bad thing. */
/* Note that the tgamma() and lgamma() implementations
* here depend on each other. */
#if !defined(HAS_TGAMMA) || !defined(c99_tgamma)
static NV my_tgamma(NV x);
# define c99_tgamma my_tgamma
# define USE_MY_TGAMMA
#endif
#if !defined(HAS_LGAMMA) || !defined(c99_lgamma)
static NV my_lgamma(NV x);
# define c99_lgamma my_lgamma
# define USE_MY_LGAMMA
#endif
#ifdef USE_MY_TGAMMA
static NV my_tgamma(NV x)
{
const NV gamma = 0.577215664901532860606512090; /* Euler's gamma constant. */
#ifdef NV_NAN
if (Perl_isnan(x) || x < 0.0)
return NV_NAN;
#endif
#ifdef NV_INF
if (x == 0.0 || x == NV_INF)
#ifdef DOUBLE_IS_IEEE_FORMAT
return x == -0.0 ? -NV_INF : NV_INF;
#else
return NV_INF;
#endif
#endif
/* The function domain is split into three intervals:
* (0, 0.001), [0.001, 12), and (12, infinity) */
/* First interval: (0, 0.001)
* For small values, 1/tgamma(x) has power series x + gamma x^2,
* so in this range, 1/tgamma(x) = x + gamma x^2 with error on the order of x^3.
* The relative error over this interval is less than 6e-7. */
if (x < 0.001)
return 1.0 / (x * (1.0 + gamma * x));
/* Second interval: [0.001, 12) */
if (x < 12.0) {
double y = x; /* Working copy. */
int n = 0;
/* Numerator coefficients for approximation over the interval (1,2) */
static const NV p[] = {
-1.71618513886549492533811E+0,
2.47656508055759199108314E+1,
-3.79804256470945635097577E+2,
6.29331155312818442661052E+2,
8.66966202790413211295064E+2,
-3.14512729688483675254357E+4,
-3.61444134186911729807069E+4,
6.64561438202405440627855E+4
};
/* Denominator coefficients for approximation over the interval (1, 2) */
static const NV q[] = {
-3.08402300119738975254353E+1,
3.15350626979604161529144E+2,
-1.01515636749021914166146E+3,
-3.10777167157231109440444E+3,
2.25381184209801510330112E+4,
4.75584627752788110767815E+3,
-1.34659959864969306392456E+5,
-1.15132259675553483497211E+5
};
NV num = 0.0;
NV den = 1.0;
NV z;
NV result;
int i;
if (x < 1.0)
y += 1.0;
else {
n = (int)Perl_floor(y) - 1;
y -= n;
}
z = y - 1;
for (i = 0; i < 8; i++) {
num = (num + p[i]) * z;
den = den * z + q[i];
}
result = num / den + 1.0;
if (x < 1.0) {
/* Use the identity tgamma(z) = tgamma(z+1)/z
* The variable "result" now holds tgamma of the original y + 1
* Thus we use y - 1 to get back the original y. */
result /= (y - 1.0);
}
else {
/* Use the identity tgamma(z+n) = z*(z+1)* ... *(z+n-1)*tgamma(z) */
for (i = 0; i < n; i++)
result *= y++;
}
return result;
}
#ifdef NV_INF
/* Third interval: [12, +Inf) */
#if LDBL_MANT_DIG == 113 /* IEEE quad prec */
if (x > 1755.548) {
return NV_INF;
}
#else
if (x > 171.624) {
return NV_INF;
}
#endif
#endif
return Perl_exp(c99_lgamma(x));
}
#endif
#ifdef USE_MY_LGAMMA
static NV my_lgamma(NV x)
{
#ifdef NV_NAN
if (Perl_isnan(x))
return NV_NAN;
#endif
#ifdef NV_INF
if (x <= 0 || x == NV_INF)
return NV_INF;
#endif
if (x == 1.0 || x == 2.0)
return 0;
if (x < 12.0)
return Perl_log(PERL_ABS(c99_tgamma(x)));
/* Abramowitz and Stegun 6.1.41
* Asymptotic series should be good to at least 11 or 12 figures
* For error analysis, see Whittiker and Watson
* A Course in Modern Analysis (1927), page 252 */
{
static const NV c[8] = {
1.0/12.0,
-1.0/360.0,
1.0/1260.0,
-1.0/1680.0,
1.0/1188.0,
-691.0/360360.0,
1.0/156.0,
-3617.0/122400.0
};
NV z = 1.0 / (x * x);
NV sum = c[7];
static const NV half_log_of_two_pi =
0.91893853320467274178032973640562;
NV series;
int i;
for (i = 6; i >= 0; i--) {
sum *= z;
sum += c[i];
}
series = sum / x;
return (x - 0.5) * Perl_log(x) - x + half_log_of_two_pi + series;
}
}
#endif
#ifndef c99_log1p
static NV my_log1p(NV x)
{
/* http://www.johndcook.com/cpp_log_one_plus_x.html -- public domain.