1 /* Implementation of various C99 functions
2 Copyright (C) 2004, 2009 Free Software Foundation, Inc.
4 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6 Libgfortran is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public
8 License as published by the Free Software Foundation; either
9 version 3 of the License, or (at your option) any later version.
11 Libgfortran is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
27 #define C99_PROTOS_H WE_DONT_WANT_PROTOS_NOW
28 #include "libgfortran.h"
30 /* IRIX's <math.h> declares a non-C99 compliant implementation of cabs,
31 which takes two floating point arguments instead of a single complex.
32 If <complex.h> is missing this prevents building of c99_functions.c.
33 To work around this we redirect cabs{,f,l} calls to __gfc_cabs{,f,l}. */
35 #if defined(__sgi__) && !defined(HAVE_COMPLEX_H)
39 #define cabs __gfc_cabs
40 #define cabsf __gfc_cabsf
41 #define cabsl __gfc_cabsl
44 /* Tru64's <math.h> declares a non-C99 compliant implementation of cabs,
45 which takes two floating point arguments instead of a single complex.
46 To work around this we redirect cabs{,f,l} calls to __gfc_cabs{,f,l}. */
52 #define cabs __gfc_cabs
53 #define cabsf __gfc_cabsf
54 #define cabsl __gfc_cabsl
57 /* On a C99 system "I" (with I*I = -1) should be defined in complex.h;
58 if not, we define a fallback version here. */
60 # if defined(_Imaginary_I)
61 # define I _Imaginary_I
62 # elif defined(_Complex_I)
69 /* Prototypes are included to silence -Wstrict-prototypes
70 -Wmissing-prototypes. */
73 /* Wrappers for systems without the various C99 single precision Bessel
76 #if defined(HAVE_J0) && ! defined(HAVE_J0F)
83 return (float) j0 ((double) x
);
87 #if defined(HAVE_J1) && !defined(HAVE_J1F)
93 return (float) j1 ((double) x
);
97 #if defined(HAVE_JN) && !defined(HAVE_JNF)
99 float jnf (int, float);
104 return (float) jn (n
, (double) x
);
108 #if defined(HAVE_Y0) && !defined(HAVE_Y0F)
115 return (float) y0 ((double) x
);
119 #if defined(HAVE_Y1) && !defined(HAVE_Y1F)
126 return (float) y1 ((double) x
);
130 #if defined(HAVE_YN) && !defined(HAVE_YNF)
132 float ynf (int, float);
137 return (float) yn (n
, (double) x
);
142 /* Wrappers for systems without the C99 erff() and erfcf() functions. */
144 #if defined(HAVE_ERF) && !defined(HAVE_ERFF)
151 return (float) erf ((double) x
);
155 #if defined(HAVE_ERFC) && !defined(HAVE_ERFCF)
162 return (float) erfc ((double) x
);
169 float acosf (float x
);
174 return (float) acos (x
);
178 #if HAVE_ACOSH && !HAVE_ACOSHF
179 float acoshf (float x
);
184 return (float) acosh ((double) x
);
190 float asinf (float x
);
195 return (float) asin (x
);
199 #if HAVE_ASINH && !HAVE_ASINHF
200 float asinhf (float x
);
205 return (float) asinh ((double) x
);
210 #define HAVE_ATAN2F 1
211 float atan2f (float y
, float x
);
214 atan2f (float y
, float x
)
216 return (float) atan2 (y
, x
);
222 float atanf (float x
);
227 return (float) atan (x
);
231 #if HAVE_ATANH && !HAVE_ATANHF
232 float atanhf (float x
);
237 return (float) atanh ((double) x
);
243 float ceilf (float x
);
248 return (float) ceil (x
);
252 #ifndef HAVE_COPYSIGNF
253 #define HAVE_COPYSIGNF 1
254 float copysignf (float x
, float y
);
257 copysignf (float x
, float y
)
259 return (float) copysign (x
, y
);
265 float cosf (float x
);
270 return (float) cos (x
);
276 float coshf (float x
);
281 return (float) cosh (x
);
287 float expf (float x
);
292 return (float) exp (x
);
298 float fabsf (float x
);
303 return (float) fabs (x
);
308 #define HAVE_FLOORF 1
309 float floorf (float x
);
314 return (float) floor (x
);
320 float fmodf (float x
, float y
);
323 fmodf (float x
, float y
)
325 return (float) fmod (x
, y
);
330 #define HAVE_FREXPF 1
331 float frexpf (float x
, int *exp
);
334 frexpf (float x
, int *exp
)
336 return (float) frexp (x
, exp
);
341 #define HAVE_HYPOTF 1
342 float hypotf (float x
, float y
);
345 hypotf (float x
, float y
)
347 return (float) hypot (x
, y
);
353 float logf (float x
);
358 return (float) log (x
);
363 #define HAVE_LOG10F 1
364 float log10f (float x
);
369 return (float) log10 (x
);
374 #define HAVE_SCALBN 1
375 double scalbn (double x
, int y
);
378 scalbn (double x
, int y
)
380 #if (FLT_RADIX == 2) && defined(HAVE_LDEXP)
383 return x
* pow (FLT_RADIX
, y
);
389 #define HAVE_SCALBNF 1
390 float scalbnf (float x
, int y
);
393 scalbnf (float x
, int y
)
395 return (float) scalbn (x
, y
);
401 float sinf (float x
);
406 return (float) sin (x
);
412 float sinhf (float x
);
417 return (float) sinh (x
);
423 float sqrtf (float x
);
428 return (float) sqrt (x
);
434 float tanf (float x
);
439 return (float) tan (x
);
445 float tanhf (float x
);
450 return (float) tanh (x
);
456 double trunc (double x
);
472 #define HAVE_TRUNCF 1
473 float truncf (float x
);
478 return (float) trunc (x
);
482 #ifndef HAVE_NEXTAFTERF
483 #define HAVE_NEXTAFTERF 1
484 /* This is a portable implementation of nextafterf that is intended to be
485 independent of the floating point format or its in memory representation.
486 This implementation works correctly with denormalized values. */
487 float nextafterf (float x
, float y
);
490 nextafterf (float x
, float y
)
492 /* This variable is marked volatile to avoid excess precision problems
493 on some platforms, including IA-32. */
494 volatile float delta
;
495 float absx
, denorm_min
;
497 if (isnan (x
) || isnan (y
))
502 return x
> 0 ? __FLT_MAX__
: - __FLT_MAX__
;
504 /* absx = fabsf (x); */
505 absx
= (x
< 0.0) ? -x
: x
;
507 /* __FLT_DENORM_MIN__ is non-zero iff the target supports denormals. */
508 if (__FLT_DENORM_MIN__
== 0.0f
)
509 denorm_min
= __FLT_MIN__
;
511 denorm_min
= __FLT_DENORM_MIN__
;
513 if (absx
< __FLT_MIN__
)
520 /* Discard the fraction from x. */
521 frac
= frexpf (absx
, &exp
);
522 delta
= scalbnf (0.5f
, exp
);
524 /* Scale x by the epsilon of the representation. By rights we should
525 have been able to combine this with scalbnf, but some targets don't
526 get that correct with denormals. */
527 delta
*= __FLT_EPSILON__
;
529 /* If we're going to be reducing the absolute value of X, and doing so
530 would reduce the exponent of X, then the delta to be applied is
531 one exponent smaller. */
532 if (frac
== 0.5f
&& (y
< x
) == (x
> 0))
535 /* If that underflows to zero, then we're back to the minimum. */
548 #if !defined(HAVE_POWF) || defined(HAVE_BROKEN_POWF)
552 float powf (float x
, float y
);
555 powf (float x
, float y
)
557 return (float) pow (x
, y
);
561 /* Note that if fpclassify is not defined, then NaN is not handled */
563 /* Algorithm by Steven G. Kargl. */
565 #if !defined(HAVE_ROUNDL)
566 #define HAVE_ROUNDL 1
567 long double roundl (long double x
);
569 #if defined(HAVE_CEILL)
570 /* Round to nearest integral value. If the argument is halfway between two
571 integral values then round away from zero. */
574 roundl (long double x
)
597 /* Poor version of roundl for system that don't have ceill. */
599 roundl (long double x
)
601 if (x
> DBL_MAX
|| x
< -DBL_MAX
)
603 #ifdef HAVE_NEXTAFTERL
604 static long double prechalf
= nexafterl (0.5L, LDBL_MAX
);
606 static long double prechalf
= 0.5L;
608 return (GFC_INTEGER_LARGEST
) (x
+ (x
> 0 ? prechalf
: -prechalf
));
612 return round ((double) x
);
620 /* Round to nearest integral value. If the argument is halfway between two
621 integral values then round away from zero. */
622 double round (double x
);
649 #define HAVE_ROUNDF 1
650 /* Round to nearest integral value. If the argument is halfway between two
651 integral values then round away from zero. */
652 float roundf (float x
);
679 /* lround{f,,l} and llround{f,,l} functions. */
681 #if !defined(HAVE_LROUNDF) && defined(HAVE_ROUNDF)
682 #define HAVE_LROUNDF 1
683 long int lroundf (float x
);
688 return (long int) roundf (x
);
692 #if !defined(HAVE_LROUND) && defined(HAVE_ROUND)
693 #define HAVE_LROUND 1
694 long int lround (double x
);
699 return (long int) round (x
);
703 #if !defined(HAVE_LROUNDL) && defined(HAVE_ROUNDL)
704 #define HAVE_LROUNDL 1
705 long int lroundl (long double x
);
708 lroundl (long double x
)
710 return (long long int) roundl (x
);
714 #if !defined(HAVE_LLROUNDF) && defined(HAVE_ROUNDF)
715 #define HAVE_LLROUNDF 1
716 long long int llroundf (float x
);
721 return (long long int) roundf (x
);
725 #if !defined(HAVE_LLROUND) && defined(HAVE_ROUND)
726 #define HAVE_LLROUND 1
727 long long int llround (double x
);
732 return (long long int) round (x
);
736 #if !defined(HAVE_LLROUNDL) && defined(HAVE_ROUNDL)
737 #define HAVE_LLROUNDL 1
738 long long int llroundl (long double x
);
741 llroundl (long double x
)
743 return (long long int) roundl (x
);
749 #define HAVE_LOG10L 1
750 /* log10 function for long double variables. The version provided here
751 reduces the argument until it fits into a double, then use log10. */
752 long double log10l (long double x
);
755 log10l (long double x
)
757 #if LDBL_MAX_EXP > DBL_MAX_EXP
762 if (x
> 0x1p
16383L) { p2_result
+= 16383; x
/= 0x1p
16383L; }
763 if (x
> 0x1p
8191L) { p2_result
+= 8191; x
/= 0x1p
8191L; }
764 if (x
> 0x1p
4095L) { p2_result
+= 4095; x
/= 0x1p
4095L; }
765 if (x
> 0x1p
2047L) { p2_result
+= 2047; x
/= 0x1p
2047L; }
766 if (x
> 0x1p
1023L) { p2_result
+= 1023; x
/= 0x1p
1023L; }
767 val
= log10 ((double) x
);
768 return (val
+ p2_result
* .30102999566398119521373889472449302L);
771 #if LDBL_MIN_EXP < DBL_MIN_EXP
776 if (x
< 0x1p
-16380L) { p2_result
+= 16380; x
/= 0x1p
-16380L; }
777 if (x
< 0x1p
-8189L) { p2_result
+= 8189; x
/= 0x1p
-8189L; }
778 if (x
< 0x1p
-4093L) { p2_result
+= 4093; x
/= 0x1p
-4093L; }
779 if (x
< 0x1p
-2045L) { p2_result
+= 2045; x
/= 0x1p
-2045L; }
780 if (x
< 0x1p
-1021L) { p2_result
+= 1021; x
/= 0x1p
-1021L; }
781 val
= fabs (log10 ((double) x
));
782 return (- val
- p2_result
* .30102999566398119521373889472449302L);
791 #define HAVE_FLOORL 1
792 long double floorl (long double x
);
795 floorl (long double x
)
797 /* Zero, possibly signed. */
801 /* Large magnitude. */
802 if (x
> DBL_MAX
|| x
< (-DBL_MAX
))
805 /* Small positive values. */
806 if (x
>= 0 && x
< DBL_MIN
)
809 /* Small negative values. */
810 if (x
< 0 && x
> (-DBL_MIN
))
820 long double fmodl (long double x
, long double y
);
823 fmodl (long double x
, long double y
)
828 /* Need to check that the result has the same sign as x and magnitude
829 less than the magnitude of y. */
830 return x
- floorl (x
/ y
) * y
;
835 #if !defined(HAVE_CABSF)
837 float cabsf (float complex z
);
840 cabsf (float complex z
)
842 return hypotf (REALPART (z
), IMAGPART (z
));
846 #if !defined(HAVE_CABS)
848 double cabs (double complex z
);
851 cabs (double complex z
)
853 return hypot (REALPART (z
), IMAGPART (z
));
857 #if !defined(HAVE_CABSL) && defined(HAVE_HYPOTL)
859 long double cabsl (long double complex z
);
862 cabsl (long double complex z
)
864 return hypotl (REALPART (z
), IMAGPART (z
));
869 #if !defined(HAVE_CARGF)
871 float cargf (float complex z
);
874 cargf (float complex z
)
876 return atan2f (IMAGPART (z
), REALPART (z
));
880 #if !defined(HAVE_CARG)
882 double carg (double complex z
);
885 carg (double complex z
)
887 return atan2 (IMAGPART (z
), REALPART (z
));
891 #if !defined(HAVE_CARGL) && defined(HAVE_ATAN2L)
893 long double cargl (long double complex z
);
896 cargl (long double complex z
)
898 return atan2l (IMAGPART (z
), REALPART (z
));
903 /* exp(z) = exp(a)*(cos(b) + i sin(b)) */
904 #if !defined(HAVE_CEXPF)
906 float complex cexpf (float complex z
);
909 cexpf (float complex z
)
916 COMPLEX_ASSIGN (v
, cosf (b
), sinf (b
));
921 #if !defined(HAVE_CEXP)
923 double complex cexp (double complex z
);
926 cexp (double complex z
)
933 COMPLEX_ASSIGN (v
, cos (b
), sin (b
));
938 #if !defined(HAVE_CEXPL) && defined(HAVE_COSL) && defined(HAVE_SINL) && defined(EXPL)
940 long double complex cexpl (long double complex z
);
943 cexpl (long double complex z
)
946 long double complex v
;
950 COMPLEX_ASSIGN (v
, cosl (b
), sinl (b
));
956 /* log(z) = log (cabs(z)) + i*carg(z) */
957 #if !defined(HAVE_CLOGF)
959 float complex clogf (float complex z
);
962 clogf (float complex z
)
966 COMPLEX_ASSIGN (v
, logf (cabsf (z
)), cargf (z
));
971 #if !defined(HAVE_CLOG)
973 double complex clog (double complex z
);
976 clog (double complex z
)
980 COMPLEX_ASSIGN (v
, log (cabs (z
)), carg (z
));
985 #if !defined(HAVE_CLOGL) && defined(HAVE_LOGL) && defined(HAVE_CABSL) && defined(HAVE_CARGL)
987 long double complex clogl (long double complex z
);
990 clogl (long double complex z
)
992 long double complex v
;
994 COMPLEX_ASSIGN (v
, logl (cabsl (z
)), cargl (z
));
1000 /* log10(z) = log10 (cabs(z)) + i*carg(z) */
1001 #if !defined(HAVE_CLOG10F)
1002 #define HAVE_CLOG10F 1
1003 float complex clog10f (float complex z
);
1006 clog10f (float complex z
)
1010 COMPLEX_ASSIGN (v
, log10f (cabsf (z
)), cargf (z
));
1015 #if !defined(HAVE_CLOG10)
1016 #define HAVE_CLOG10 1
1017 double complex clog10 (double complex z
);
1020 clog10 (double complex z
)
1024 COMPLEX_ASSIGN (v
, log10 (cabs (z
)), carg (z
));
1029 #if !defined(HAVE_CLOG10L) && defined(HAVE_LOG10L) && defined(HAVE_CABSL) && defined(HAVE_CARGL)
1030 #define HAVE_CLOG10L 1
1031 long double complex clog10l (long double complex z
);
1034 clog10l (long double complex z
)
1036 long double complex v
;
1038 COMPLEX_ASSIGN (v
, log10l (cabsl (z
)), cargl (z
));
1044 /* pow(base, power) = cexp (power * clog (base)) */
1045 #if !defined(HAVE_CPOWF)
1046 #define HAVE_CPOWF 1
1047 float complex cpowf (float complex base
, float complex power
);
1050 cpowf (float complex base
, float complex power
)
1052 return cexpf (power
* clogf (base
));
1056 #if !defined(HAVE_CPOW)
1058 double complex cpow (double complex base
, double complex power
);
1061 cpow (double complex base
, double complex power
)
1063 return cexp (power
* clog (base
));
1067 #if !defined(HAVE_CPOWL) && defined(HAVE_CEXPL) && defined(HAVE_CLOGL)
1068 #define HAVE_CPOWL 1
1069 long double complex cpowl (long double complex base
, long double complex power
);
1072 cpowl (long double complex base
, long double complex power
)
1074 return cexpl (power
* clogl (base
));
1079 /* sqrt(z). Algorithm pulled from glibc. */
1080 #if !defined(HAVE_CSQRTF)
1081 #define HAVE_CSQRTF 1
1082 float complex csqrtf (float complex z
);
1085 csqrtf (float complex z
)
1096 COMPLEX_ASSIGN (v
, 0, copysignf (sqrtf (-re
), im
));
1100 COMPLEX_ASSIGN (v
, fabsf (sqrtf (re
)), copysignf (0, im
));
1107 r
= sqrtf (0.5 * fabsf (im
));
1109 COMPLEX_ASSIGN (v
, r
, copysignf (r
, im
));
1115 d
= hypotf (re
, im
);
1116 /* Use the identity 2 Re res Im res = Im x
1117 to avoid cancellation error in d +/- Re x. */
1120 r
= sqrtf (0.5 * d
+ 0.5 * re
);
1125 s
= sqrtf (0.5 * d
- 0.5 * re
);
1126 r
= fabsf ((0.5 * im
) / s
);
1129 COMPLEX_ASSIGN (v
, r
, copysignf (s
, im
));
1135 #if !defined(HAVE_CSQRT)
1136 #define HAVE_CSQRT 1
1137 double complex csqrt (double complex z
);
1140 csqrt (double complex z
)
1151 COMPLEX_ASSIGN (v
, 0, copysign (sqrt (-re
), im
));
1155 COMPLEX_ASSIGN (v
, fabs (sqrt (re
)), copysign (0, im
));
1162 r
= sqrt (0.5 * fabs (im
));
1164 COMPLEX_ASSIGN (v
, r
, copysign (r
, im
));
1171 /* Use the identity 2 Re res Im res = Im x
1172 to avoid cancellation error in d +/- Re x. */
1175 r
= sqrt (0.5 * d
+ 0.5 * re
);
1180 s
= sqrt (0.5 * d
- 0.5 * re
);
1181 r
= fabs ((0.5 * im
) / s
);
1184 COMPLEX_ASSIGN (v
, r
, copysign (s
, im
));
1190 #if !defined(HAVE_CSQRTL) && defined(HAVE_COPYSIGNL) && defined(HAVE_SQRTL) && defined(HAVE_FABSL) && defined(HAVE_HYPOTL)
1191 #define HAVE_CSQRTL 1
1192 long double complex csqrtl (long double complex z
);
1195 csqrtl (long double complex z
)
1198 long double complex v
;
1206 COMPLEX_ASSIGN (v
, 0, copysignl (sqrtl (-re
), im
));
1210 COMPLEX_ASSIGN (v
, fabsl (sqrtl (re
)), copysignl (0, im
));
1217 r
= sqrtl (0.5 * fabsl (im
));
1219 COMPLEX_ASSIGN (v
, copysignl (r
, im
), r
);
1223 long double d
, r
, s
;
1225 d
= hypotl (re
, im
);
1226 /* Use the identity 2 Re res Im res = Im x
1227 to avoid cancellation error in d +/- Re x. */
1230 r
= sqrtl (0.5 * d
+ 0.5 * re
);
1235 s
= sqrtl (0.5 * d
- 0.5 * re
);
1236 r
= fabsl ((0.5 * im
) / s
);
1239 COMPLEX_ASSIGN (v
, r
, copysignl (s
, im
));
1246 /* sinh(a + i b) = sinh(a) cos(b) + i cosh(a) sin(b) */
1247 #if !defined(HAVE_CSINHF)
1248 #define HAVE_CSINHF 1
1249 float complex csinhf (float complex a
);
1252 csinhf (float complex a
)
1259 COMPLEX_ASSIGN (v
, sinhf (r
) * cosf (i
), coshf (r
) * sinf (i
));
1264 #if !defined(HAVE_CSINH)
1265 #define HAVE_CSINH 1
1266 double complex csinh (double complex a
);
1269 csinh (double complex a
)
1276 COMPLEX_ASSIGN (v
, sinh (r
) * cos (i
), cosh (r
) * sin (i
));
1281 #if !defined(HAVE_CSINHL) && defined(HAVE_COSL) && defined(HAVE_COSHL) && defined(HAVE_SINL) && defined(HAVE_SINHL)
1282 #define HAVE_CSINHL 1
1283 long double complex csinhl (long double complex a
);
1286 csinhl (long double complex a
)
1289 long double complex v
;
1293 COMPLEX_ASSIGN (v
, sinhl (r
) * cosl (i
), coshl (r
) * sinl (i
));
1299 /* cosh(a + i b) = cosh(a) cos(b) + i sinh(a) sin(b) */
1300 #if !defined(HAVE_CCOSHF)
1301 #define HAVE_CCOSHF 1
1302 float complex ccoshf (float complex a
);
1305 ccoshf (float complex a
)
1312 COMPLEX_ASSIGN (v
, coshf (r
) * cosf (i
), sinhf (r
) * sinf (i
));
1317 #if !defined(HAVE_CCOSH)
1318 #define HAVE_CCOSH 1
1319 double complex ccosh (double complex a
);
1322 ccosh (double complex a
)
1329 COMPLEX_ASSIGN (v
, cosh (r
) * cos (i
), sinh (r
) * sin (i
));
1334 #if !defined(HAVE_CCOSHL) && defined(HAVE_COSL) && defined(HAVE_COSHL) && defined(HAVE_SINL) && defined(HAVE_SINHL)
1335 #define HAVE_CCOSHL 1
1336 long double complex ccoshl (long double complex a
);
1339 ccoshl (long double complex a
)
1342 long double complex v
;
1346 COMPLEX_ASSIGN (v
, coshl (r
) * cosl (i
), sinhl (r
) * sinl (i
));
1352 /* tanh(a + i b) = (tanh(a) + i tan(b)) / (1 + i tanh(a) tan(b)) */
1353 #if !defined(HAVE_CTANHF)
1354 #define HAVE_CTANHF 1
1355 float complex ctanhf (float complex a
);
1358 ctanhf (float complex a
)
1363 rt
= tanhf (REALPART (a
));
1364 it
= tanf (IMAGPART (a
));
1365 COMPLEX_ASSIGN (n
, rt
, it
);
1366 COMPLEX_ASSIGN (d
, 1, rt
* it
);
1372 #if !defined(HAVE_CTANH)
1373 #define HAVE_CTANH 1
1374 double complex ctanh (double complex a
);
1376 ctanh (double complex a
)
1379 double complex n
, d
;
1381 rt
= tanh (REALPART (a
));
1382 it
= tan (IMAGPART (a
));
1383 COMPLEX_ASSIGN (n
, rt
, it
);
1384 COMPLEX_ASSIGN (d
, 1, rt
* it
);
1390 #if !defined(HAVE_CTANHL) && defined(HAVE_TANL) && defined(HAVE_TANHL)
1391 #define HAVE_CTANHL 1
1392 long double complex ctanhl (long double complex a
);
1395 ctanhl (long double complex a
)
1398 long double complex n
, d
;
1400 rt
= tanhl (REALPART (a
));
1401 it
= tanl (IMAGPART (a
));
1402 COMPLEX_ASSIGN (n
, rt
, it
);
1403 COMPLEX_ASSIGN (d
, 1, rt
* it
);
1410 /* sin(a + i b) = sin(a) cosh(b) + i cos(a) sinh(b) */
1411 #if !defined(HAVE_CSINF)
1412 #define HAVE_CSINF 1
1413 float complex csinf (float complex a
);
1416 csinf (float complex a
)
1423 COMPLEX_ASSIGN (v
, sinf (r
) * coshf (i
), cosf (r
) * sinhf (i
));
1428 #if !defined(HAVE_CSIN)
1430 double complex csin (double complex a
);
1433 csin (double complex a
)
1440 COMPLEX_ASSIGN (v
, sin (r
) * cosh (i
), cos (r
) * sinh (i
));
1445 #if !defined(HAVE_CSINL) && defined(HAVE_COSL) && defined(HAVE_COSHL) && defined(HAVE_SINL) && defined(HAVE_SINHL)
1446 #define HAVE_CSINL 1
1447 long double complex csinl (long double complex a
);
1450 csinl (long double complex a
)
1453 long double complex v
;
1457 COMPLEX_ASSIGN (v
, sinl (r
) * coshl (i
), cosl (r
) * sinhl (i
));
1463 /* cos(a + i b) = cos(a) cosh(b) - i sin(a) sinh(b) */
1464 #if !defined(HAVE_CCOSF)
1465 #define HAVE_CCOSF 1
1466 float complex ccosf (float complex a
);
1469 ccosf (float complex a
)
1476 COMPLEX_ASSIGN (v
, cosf (r
) * coshf (i
), - (sinf (r
) * sinhf (i
)));
1481 #if !defined(HAVE_CCOS)
1483 double complex ccos (double complex a
);
1486 ccos (double complex a
)
1493 COMPLEX_ASSIGN (v
, cos (r
) * cosh (i
), - (sin (r
) * sinh (i
)));
1498 #if !defined(HAVE_CCOSL) && defined(HAVE_COSL) && defined(HAVE_COSHL) && defined(HAVE_SINL) && defined(HAVE_SINHL)
1499 #define HAVE_CCOSL 1
1500 long double complex ccosl (long double complex a
);
1503 ccosl (long double complex a
)
1506 long double complex v
;
1510 COMPLEX_ASSIGN (v
, cosl (r
) * coshl (i
), - (sinl (r
) * sinhl (i
)));
1516 /* tan(a + i b) = (tan(a) + i tanh(b)) / (1 - i tan(a) tanh(b)) */
1517 #if !defined(HAVE_CTANF)
1518 #define HAVE_CTANF 1
1519 float complex ctanf (float complex a
);
1522 ctanf (float complex a
)
1527 rt
= tanf (REALPART (a
));
1528 it
= tanhf (IMAGPART (a
));
1529 COMPLEX_ASSIGN (n
, rt
, it
);
1530 COMPLEX_ASSIGN (d
, 1, - (rt
* it
));
1536 #if !defined(HAVE_CTAN)
1538 double complex ctan (double complex a
);
1541 ctan (double complex a
)
1544 double complex n
, d
;
1546 rt
= tan (REALPART (a
));
1547 it
= tanh (IMAGPART (a
));
1548 COMPLEX_ASSIGN (n
, rt
, it
);
1549 COMPLEX_ASSIGN (d
, 1, - (rt
* it
));
1555 #if !defined(HAVE_CTANL) && defined(HAVE_TANL) && defined(HAVE_TANHL)
1556 #define HAVE_CTANL 1
1557 long double complex ctanl (long double complex a
);
1560 ctanl (long double complex a
)
1563 long double complex n
, d
;
1565 rt
= tanl (REALPART (a
));
1566 it
= tanhl (IMAGPART (a
));
1567 COMPLEX_ASSIGN (n
, rt
, it
);
1568 COMPLEX_ASSIGN (d
, 1, - (rt
* it
));
1575 /* Complex ASIN. Returns wrongly NaN for infinite arguments.
1576 Algorithm taken from Abramowitz & Stegun. */
1578 #if !defined(HAVE_CASINF) && defined(HAVE_CLOGF) && defined(HAVE_CSQRTF)
1579 #define HAVE_CASINF 1
1580 complex float casinf (complex float z
);
1583 casinf (complex float z
)
1585 return -I
*clogf (I
*z
+ csqrtf (1.0f
-z
*z
));
1590 #if !defined(HAVE_CASIN) && defined(HAVE_CLOG) && defined(HAVE_CSQRT)
1591 #define HAVE_CASIN 1
1592 complex double casin (complex double z
);
1595 casin (complex double z
)
1597 return -I
*clog (I
*z
+ csqrt (1.0-z
*z
));
1602 #if !defined(HAVE_CASINL) && defined(HAVE_CLOGL) && defined(HAVE_CSQRTL)
1603 #define HAVE_CASINL 1
1604 complex long double casinl (complex long double z
);
1607 casinl (complex long double z
)
1609 return -I
*clogl (I
*z
+ csqrtl (1.0L-z
*z
));
1614 /* Complex ACOS. Returns wrongly NaN for infinite arguments.
1615 Algorithm taken from Abramowitz & Stegun. */
1617 #if !defined(HAVE_CACOSF) && defined(HAVE_CLOGF) && defined(HAVE_CSQRTF)
1618 #define HAVE_CACOSF 1
1619 complex float cacosf (complex float z
);
1622 cacosf (complex float z
)
1624 return -I
*clogf (z
+ I
*csqrtf (1.0f
-z
*z
));
1629 #if !defined(HAVE_CACOS) && defined(HAVE_CLOG) && defined(HAVE_CSQRT)
1630 #define HAVE_CACOS 1
1631 complex double cacos (complex double z
);
1634 cacos (complex double z
)
1636 return -I
*clog (z
+ I
*csqrt (1.0-z
*z
));
1641 #if !defined(HAVE_CACOSL) && defined(HAVE_CLOGL) && defined(HAVE_CSQRTL)
1642 #define HAVE_CACOSL 1
1643 complex long double cacosl (complex long double z
);
1646 cacosl (complex long double z
)
1648 return -I
*clogl (z
+ I
*csqrtl (1.0L-z
*z
));
1653 /* Complex ATAN. Returns wrongly NaN for infinite arguments.
1654 Algorithm taken from Abramowitz & Stegun. */
1656 #if !defined(HAVE_CATANF) && defined(HAVE_CLOGF)
1657 #define HAVE_CACOSF 1
1658 complex float catanf (complex float z
);
1661 catanf (complex float z
)
1663 return I
*clogf ((I
+z
)/(I
-z
))/2.0f
;
1668 #if !defined(HAVE_CATAN) && defined(HAVE_CLOG)
1669 #define HAVE_CACOS 1
1670 complex double catan (complex double z
);
1673 catan (complex double z
)
1675 return I
*clog ((I
+z
)/(I
-z
))/2.0;
1680 #if !defined(HAVE_CATANL) && defined(HAVE_CLOGL)
1681 #define HAVE_CACOSL 1
1682 complex long double catanl (complex long double z
);
1685 catanl (complex long double z
)
1687 return I
*clogl ((I
+z
)/(I
-z
))/2.0L;
1692 /* Complex ASINH. Returns wrongly NaN for infinite arguments.
1693 Algorithm taken from Abramowitz & Stegun. */
1695 #if !defined(HAVE_CASINHF) && defined(HAVE_CLOGF) && defined(HAVE_CSQRTF)
1696 #define HAVE_CASINHF 1
1697 complex float casinhf (complex float z
);
1700 casinhf (complex float z
)
1702 return clogf (z
+ csqrtf (z
*z
+1.0f
));
1707 #if !defined(HAVE_CASINH) && defined(HAVE_CLOG) && defined(HAVE_CSQRT)
1708 #define HAVE_CASINH 1
1709 complex double casinh (complex double z
);
1712 casinh (complex double z
)
1714 return clog (z
+ csqrt (z
*z
+1.0));
1719 #if !defined(HAVE_CASINHL) && defined(HAVE_CLOGL) && defined(HAVE_CSQRTL)
1720 #define HAVE_CASINHL 1
1721 complex long double casinhl (complex long double z
);
1724 casinhl (complex long double z
)
1726 return clogl (z
+ csqrtl (z
*z
+1.0L));
1731 /* Complex ACOSH. Returns wrongly NaN for infinite arguments.
1732 Algorithm taken from Abramowitz & Stegun. */
1734 #if !defined(HAVE_CACOSHF) && defined(HAVE_CLOGF) && defined(HAVE_CSQRTF)
1735 #define HAVE_CACOSHF 1
1736 complex float cacoshf (complex float z
);
1739 cacoshf (complex float z
)
1741 return clogf (z
+ csqrtf (z
-1.0f
) * csqrtf (z
+1.0f
));
1746 #if !defined(HAVE_CACOSH) && defined(HAVE_CLOG) && defined(HAVE_CSQRT)
1747 #define HAVE_CACOSH 1
1748 complex double cacosh (complex double z
);
1751 cacosh (complex double z
)
1753 return clog (z
+ csqrt (z
-1.0) * csqrt (z
+1.0));
1758 #if !defined(HAVE_CACOSHL) && defined(HAVE_CLOGL) && defined(HAVE_CSQRTL)
1759 #define HAVE_CACOSHL 1
1760 complex long double cacoshl (complex long double z
);
1763 cacoshl (complex long double z
)
1765 return clogl (z
+ csqrtl (z
-1.0L) * csqrtl (z
+1.0L));
1770 /* Complex ATANH. Returns wrongly NaN for infinite arguments.
1771 Algorithm taken from Abramowitz & Stegun. */
1773 #if !defined(HAVE_CATANHF) && defined(HAVE_CLOGF)
1774 #define HAVE_CATANHF 1
1775 complex float catanhf (complex float z
);
1778 catanhf (complex float z
)
1780 return clogf ((1.0f
+z
)/(1.0f
-z
))/2.0f
;
1785 #if !defined(HAVE_CATANH) && defined(HAVE_CLOG)
1786 #define HAVE_CATANH 1
1787 complex double catanh (complex double z
);
1790 catanh (complex double z
)
1792 return clog ((1.0+z
)/(1.0-z
))/2.0;
1796 #if !defined(HAVE_CATANHL) && defined(HAVE_CLOGL)
1797 #define HAVE_CATANHL 1
1798 complex long double catanhl (complex long double z
);
1801 catanhl (complex long double z
)
1803 return clogl ((1.0L+z
)/(1.0L-z
))/2.0L;
1808 #if !defined(HAVE_TGAMMA)
1809 #define HAVE_TGAMMA 1
1810 double tgamma (double);
1812 /* Fallback tgamma() function. Uses the algorithm from
1813 http://www.netlib.org/specfun/gamma and references therein. */
1816 #define SQRTPI 0.9189385332046727417803297
1819 #define PI 3.1415926535897932384626434
1825 double fact
, res
, sum
, xden
, xnum
, y
, y1
, ysq
, z
;
1827 static double p
[8] = {
1828 -1.71618513886549492533811e0
, 2.47656508055759199108314e1
,
1829 -3.79804256470945635097577e2
, 6.29331155312818442661052e2
,
1830 8.66966202790413211295064e2
, -3.14512729688483675254357e4
,
1831 -3.61444134186911729807069e4
, 6.64561438202405440627855e4
};
1833 static double q
[8] = {
1834 -3.08402300119738975254353e1
, 3.15350626979604161529144e2
,
1835 -1.01515636749021914166146e3
, -3.10777167157231109440444e3
,
1836 2.25381184209801510330112e4
, 4.75584627752788110767815e3
,
1837 -1.34659959864969306392456e5
, -1.15132259675553483497211e5
};
1839 static double c
[7] = { -1.910444077728e-03,
1840 8.4171387781295e-04, -5.952379913043012e-04,
1841 7.93650793500350248e-04, -2.777777777777681622553e-03,
1842 8.333333333333333331554247e-02, 5.7083835261e-03 };
1844 static const double xminin
= 2.23e-308;
1845 static const double xbig
= 171.624;
1846 static const double xnan
= __builtin_nan ("0x0"), xinf
= __builtin_inf ();
1847 static double eps
= 0;
1850 eps
= nextafter (1., 2.) - 1.;
1857 if (__builtin_isnan (x
))
1868 if (y1
!= trunc (y1
*0.5l)*2)
1870 fact
= -PI
/ sin (PI
*res
);
1874 return x
== 0 ? copysign (xinf
, x
) : xnan
;
1901 for (i
= 0; i
< 8; i
++)
1903 xnum
= (xnum
+ p
[i
]) * z
;
1904 xden
= xden
* z
+ q
[i
];
1907 res
= xnum
/ xden
+ 1;
1912 for (i
= 1; i
<= n
; i
++)
1924 for (i
= 0; i
< 6; i
++)
1925 sum
= sum
/ ysq
+ c
[i
];
1927 sum
= sum
/y
- y
+ SQRTPI
;
1928 sum
= sum
+ (y
- 0.5) * log (y
);
1932 return x
< 0 ? xnan
: xinf
;
1946 #if !defined(HAVE_LGAMMA)
1947 #define HAVE_LGAMMA 1
1948 double lgamma (double);
1950 /* Fallback lgamma() function. Uses the algorithm from
1951 http://www.netlib.org/specfun/algama and references therein,
1952 except for negative arguments (where netlib would return +Inf)
1953 where we use the following identity:
1954 lgamma(y) = log(pi/(|y*sin(pi*y)|)) - lgamma(-y)
1962 #define SQRTPI 0.9189385332046727417803297
1965 #define PI 3.1415926535897932384626434
1967 #define PNT68 0.6796875
1968 #define D1 -0.5772156649015328605195174
1969 #define D2 0.4227843350984671393993777
1970 #define D4 1.791759469228055000094023
1972 static double p1
[8] = {
1973 4.945235359296727046734888e0
, 2.018112620856775083915565e2
,
1974 2.290838373831346393026739e3
, 1.131967205903380828685045e4
,
1975 2.855724635671635335736389e4
, 3.848496228443793359990269e4
,
1976 2.637748787624195437963534e4
, 7.225813979700288197698961e3
};
1977 static double q1
[8] = {
1978 6.748212550303777196073036e1
, 1.113332393857199323513008e3
,
1979 7.738757056935398733233834e3
, 2.763987074403340708898585e4
,
1980 5.499310206226157329794414e4
, 6.161122180066002127833352e4
,
1981 3.635127591501940507276287e4
, 8.785536302431013170870835e3
};
1982 static double p2
[8] = {
1983 4.974607845568932035012064e0
, 5.424138599891070494101986e2
,
1984 1.550693864978364947665077e4
, 1.847932904445632425417223e5
,
1985 1.088204769468828767498470e6
, 3.338152967987029735917223e6
,
1986 5.106661678927352456275255e6
, 3.074109054850539556250927e6
};
1987 static double q2
[8] = {
1988 1.830328399370592604055942e2
, 7.765049321445005871323047e3
,
1989 1.331903827966074194402448e5
, 1.136705821321969608938755e6
,
1990 5.267964117437946917577538e6
, 1.346701454311101692290052e7
,
1991 1.782736530353274213975932e7
, 9.533095591844353613395747e6
};
1992 static double p4
[8] = {
1993 1.474502166059939948905062e4
, 2.426813369486704502836312e6
,
1994 1.214755574045093227939592e8
, 2.663432449630976949898078e9
,
1995 2.940378956634553899906876e10
, 1.702665737765398868392998e11
,
1996 4.926125793377430887588120e11
, 5.606251856223951465078242e11
};
1997 static double q4
[8] = {
1998 2.690530175870899333379843e3
, 6.393885654300092398984238e5
,
1999 4.135599930241388052042842e7
, 1.120872109616147941376570e9
,
2000 1.488613728678813811542398e10
, 1.016803586272438228077304e11
,
2001 3.417476345507377132798597e11
, 4.463158187419713286462081e11
};
2002 static double c
[7] = {
2003 -1.910444077728e-03, 8.4171387781295e-04,
2004 -5.952379913043012e-04, 7.93650793500350248e-04,
2005 -2.777777777777681622553e-03, 8.333333333333333331554247e-02,
2008 static double xbig
= 2.55e305
, xinf
= __builtin_inf (), eps
= 0,
2012 double corr
, res
, xden
, xm1
, xm2
, xm4
, xnum
, ysq
;
2015 eps
= __builtin_nextafter (1., 2.) - 1.;
2017 if ((y
> 0) && (y
<= xbig
))
2031 xm1
= (y
- 0.5) - 0.5;
2034 if ((y
<= 0.5) || (y
>= PNT68
))
2038 for (i
= 0; i
< 8; i
++)
2040 xnum
= xnum
*xm1
+ p1
[i
];
2041 xden
= xden
*xm1
+ q1
[i
];
2043 res
= corr
+ (xm1
* (D1
+ xm1
*(xnum
/xden
)));
2047 xm2
= (y
- 0.5) - 0.5;
2050 for (i
= 0; i
< 8; i
++)
2052 xnum
= xnum
*xm2
+ p2
[i
];
2053 xden
= xden
*xm2
+ q2
[i
];
2055 res
= corr
+ xm2
* (D2
+ xm2
*(xnum
/xden
));
2063 for (i
= 0; i
< 8; i
++)
2065 xnum
= xnum
*xm2
+ p2
[i
];
2066 xden
= xden
*xm2
+ q2
[i
];
2068 res
= xm2
* (D2
+ xm2
*(xnum
/xden
));
2075 for (i
= 0; i
< 8; i
++)
2077 xnum
= xnum
*xm4
+ p4
[i
];
2078 xden
= xden
*xm4
+ q4
[i
];
2080 res
= D4
+ xm4
*(xnum
/xden
);
2089 for (i
= 0; i
< 6; i
++)
2090 res
= res
/ ysq
+ c
[i
];
2094 res
= res
+ SQRTPI
- 0.5*corr
;
2095 res
= res
+ y
*(corr
-1);
2098 else if (y
< 0 && __builtin_floor (y
) != y
)
2100 /* lgamma(y) = log(pi/(|y*sin(pi*y)|)) - lgamma(-y)
2101 For abs(y) very close to zero, we use a series expansion to
2102 the first order in y to avoid overflow. */
2104 res
= -2 * log (fabs (y
)) - lgamma (-y
);
2106 res
= log (PI
/ fabs (y
* sin (PI
* y
))) - lgamma (-y
);
2116 #if defined(HAVE_TGAMMA) && !defined(HAVE_TGAMMAF)
2117 #define HAVE_TGAMMAF 1
2118 float tgammaf (float);
2123 return (float) tgamma ((double) x
);
2127 #if defined(HAVE_LGAMMA) && !defined(HAVE_LGAMMAF)
2128 #define HAVE_LGAMMAF 1
2129 float lgammaf (float);
2134 return (float) lgamma ((double) x
);