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 /* Prototypes to silence -Wstrict-prototypes -Wmissing-prototypes. */
59 float cabsf(float complex);
60 double cabs(double complex);
61 long double cabsl(long double complex);
63 float cargf(float complex);
64 double carg(double complex);
65 long double cargl(long double complex);
67 float complex clog10f(float complex);
68 double complex clog10(double complex);
69 long double complex clog10l(long double complex);
72 /* Wrappers for systems without the various C99 single precision Bessel
75 #if defined(HAVE_J0) && ! defined(HAVE_J0F)
77 extern float j0f (float);
82 return (float) j0 ((double) x);
86 #if defined(HAVE_J1) && !defined(HAVE_J1F)
88 extern float j1f (float);
92 return (float) j1 ((double) x);
96 #if defined(HAVE_JN) && !defined(HAVE_JNF)
98 extern float jnf (int, float);
103 return (float) jn (n, (double) x);
107 #if defined(HAVE_Y0) && !defined(HAVE_Y0F)
109 extern float y0f (float);
114 return (float) y0 ((double) x);
118 #if defined(HAVE_Y1) && !defined(HAVE_Y1F)
120 extern float y1f (float);
125 return (float) y1 ((double) x);
129 #if defined(HAVE_YN) && !defined(HAVE_YNF)
131 extern float ynf (int, float);
136 return (float) yn (n, (double) x);
141 /* Wrappers for systems without the C99 erff() and erfcf() functions. */
143 #if defined(HAVE_ERF) && !defined(HAVE_ERFF)
145 extern float erff (float);
150 return (float) erf ((double) x);
154 #if defined(HAVE_ERFC) && !defined(HAVE_ERFCF)
156 extern float erfcf (float);
161 return (float) erfc ((double) x);
171 return (float) acos(x);
175 #if HAVE_ACOSH && !HAVE_ACOSHF
179 return (float) acosh ((double) x);
188 return (float) asin(x);
192 #if HAVE_ASINH && !HAVE_ASINHF
196 return (float) asinh ((double) x);
201 #define HAVE_ATAN2F 1
203 atan2f(float y, float x)
205 return (float) atan2(y, x);
214 return (float) atan(x);
218 #if HAVE_ATANH && !HAVE_ATANHF
222 return (float) atanh ((double) x);
231 return (float) ceil(x);
235 #ifndef HAVE_COPYSIGNF
236 #define HAVE_COPYSIGNF 1
238 copysignf(float x, float y)
240 return (float) copysign(x, y);
249 return (float) cos(x);
258 return (float) cosh(x);
267 return (float) exp(x);
276 return (float) fabs(x);
281 #define HAVE_FLOORF 1
285 return (float) floor(x);
292 fmodf (float x, float y)
294 return (float) fmod (x, y);
299 #define HAVE_FREXPF 1
301 frexpf(float x, int *exp)
303 return (float) frexp(x, exp);
308 #define HAVE_HYPOTF 1
310 hypotf(float x, float y)
312 return (float) hypot(x, y);
321 return (float) log(x);
326 #define HAVE_LOG10F 1
330 return (float) log10(x);
335 #define HAVE_SCALBN 1
337 scalbn(double x, int y)
339 #if (FLT_RADIX == 2) && defined(HAVE_LDEXP)
342 return x * pow(FLT_RADIX, y);
348 #define HAVE_SCALBNF 1
350 scalbnf(float x, int y)
352 return (float) scalbn(x, y);
361 return (float) sin(x);
370 return (float) sinh(x);
379 return (float) sqrt(x);
388 return (float) tan(x);
397 return (float) tanh(x);
417 #define HAVE_TRUNCF 1
421 return (float) trunc (x);
425 #ifndef HAVE_NEXTAFTERF
426 #define HAVE_NEXTAFTERF 1
427 /* This is a portable implementation of nextafterf that is intended to be
428 independent of the floating point format or its in memory representation.
429 This implementation works correctly with denormalized values. */
431 nextafterf(float x, float y)
433 /* This variable is marked volatile to avoid excess precision problems
434 on some platforms, including IA-32. */
435 volatile float delta;
436 float absx, denorm_min;
438 if (isnan(x) || isnan(y))
443 return x > 0 ? __FLT_MAX__ : - __FLT_MAX__;
445 /* absx = fabsf (x); */
446 absx = (x < 0.0) ? -x : x;
448 /* __FLT_DENORM_MIN__ is non-zero iff the target supports denormals. */
449 if (__FLT_DENORM_MIN__ == 0.0f)
450 denorm_min = __FLT_MIN__;
452 denorm_min = __FLT_DENORM_MIN__;
454 if (absx < __FLT_MIN__)
461 /* Discard the fraction from x. */
462 frac = frexpf (absx, &exp);
463 delta = scalbnf (0.5f, exp);
465 /* Scale x by the epsilon of the representation. By rights we should
466 have been able to combine this with scalbnf, but some targets don't
467 get that correct with denormals. */
468 delta *= __FLT_EPSILON__;
470 /* If we're going to be reducing the absolute value of X, and doing so
471 would reduce the exponent of X, then the delta to be applied is
472 one exponent smaller. */
473 if (frac == 0.5f && (y < x) == (x > 0))
476 /* If that underflows to zero, then we're back to the minimum. */
489 #if !defined(HAVE_POWF) || defined(HAVE_BROKEN_POWF)
494 powf(float x, float y)
496 return (float) pow(x, y);
500 /* Note that if fpclassify is not defined, then NaN is not handled */
502 /* Algorithm by Steven G. Kargl. */
504 #if !defined(HAVE_ROUNDL)
505 #define HAVE_ROUNDL 1
506 #if defined(HAVE_CEILL)
507 /* Round to nearest integral value. If the argument is halfway between two
508 integral values then round away from zero. */
511 roundl(long double x)
534 /* Poor version of roundl for system that don't have ceill. */
536 roundl(long double x)
538 if (x > DBL_MAX || x < -DBL_MAX)
540 #ifdef HAVE_NEXTAFTERL
541 static long double prechalf = nexafterl (0.5L, LDBL_MAX);
543 static long double prechalf = 0.5L;
545 return (GFC_INTEGER_LARGEST) (x + (x > 0 ? prechalf : -prechalf));
549 return round((double) x);
557 /* Round to nearest integral value. If the argument is halfway between two
558 integral values then round away from zero. */
585 #define HAVE_ROUNDF 1
586 /* Round to nearest integral value. If the argument is halfway between two
587 integral values then round away from zero. */
614 /* lround{f,,l} and llround{f,,l} functions. */
616 #if !defined(HAVE_LROUNDF) && defined(HAVE_ROUNDF)
617 #define HAVE_LROUNDF 1
621 return (long int) roundf (x);
625 #if !defined(HAVE_LROUND) && defined(HAVE_ROUND)
626 #define HAVE_LROUND 1
630 return (long int) round (x);
634 #if !defined(HAVE_LROUNDL) && defined(HAVE_ROUNDL)
635 #define HAVE_LROUNDL 1
637 lroundl (long double x)
639 return (long long int) roundl (x);
643 #if !defined(HAVE_LLROUNDF) && defined(HAVE_ROUNDF)
644 #define HAVE_LLROUNDF 1
648 return (long long int) roundf (x);
652 #if !defined(HAVE_LLROUND) && defined(HAVE_ROUND)
653 #define HAVE_LLROUND 1
657 return (long long int) round (x);
661 #if !defined(HAVE_LLROUNDL) && defined(HAVE_ROUNDL)
662 #define HAVE_LLROUNDL 1
664 llroundl (long double x)
666 return (long long int) roundl (x);
672 #define HAVE_LOG10L 1
673 /* log10 function for long double variables. The version provided here
674 reduces the argument until it fits into a double, then use log10. */
676 log10l(long double x)
678 #if LDBL_MAX_EXP > DBL_MAX_EXP
683 if (x > 0x1p16383L) { p2_result += 16383; x /= 0x1p16383L; }
684 if (x > 0x1p8191L) { p2_result += 8191; x /= 0x1p8191L; }
685 if (x > 0x1p4095L) { p2_result += 4095; x /= 0x1p4095L; }
686 if (x > 0x1p2047L) { p2_result += 2047; x /= 0x1p2047L; }
687 if (x > 0x1p1023L) { p2_result += 1023; x /= 0x1p1023L; }
688 val = log10 ((double) x);
689 return (val + p2_result * .30102999566398119521373889472449302L);
692 #if LDBL_MIN_EXP < DBL_MIN_EXP
697 if (x < 0x1p-16380L) { p2_result += 16380; x /= 0x1p-16380L; }
698 if (x < 0x1p-8189L) { p2_result += 8189; x /= 0x1p-8189L; }
699 if (x < 0x1p-4093L) { p2_result += 4093; x /= 0x1p-4093L; }
700 if (x < 0x1p-2045L) { p2_result += 2045; x /= 0x1p-2045L; }
701 if (x < 0x1p-1021L) { p2_result += 1021; x /= 0x1p-1021L; }
702 val = fabs(log10 ((double) x));
703 return (- val - p2_result * .30102999566398119521373889472449302L);
712 #define HAVE_FLOORL 1
714 floorl (long double x)
716 /* Zero, possibly signed. */
720 /* Large magnitude. */
721 if (x > DBL_MAX || x < (-DBL_MAX))
724 /* Small positive values. */
725 if (x >= 0 && x < DBL_MIN)
728 /* Small negative values. */
729 if (x < 0 && x > (-DBL_MIN))
740 fmodl (long double x, long double y)
745 /* Need to check that the result has the same sign as x and magnitude
746 less than the magnitude of y. */
747 return x - floorl (x / y) * y;
752 #if !defined(HAVE_CABSF)
755 cabsf (float complex z)
757 return hypotf (REALPART (z), IMAGPART (z));
761 #if !defined(HAVE_CABS)
764 cabs (double complex z)
766 return hypot (REALPART (z), IMAGPART (z));
770 #if !defined(HAVE_CABSL) && defined(HAVE_HYPOTL)
773 cabsl (long double complex z)
775 return hypotl (REALPART (z), IMAGPART (z));
780 #if !defined(HAVE_CARGF)
783 cargf (float complex z)
785 return atan2f (IMAGPART (z), REALPART (z));
789 #if !defined(HAVE_CARG)
792 carg (double complex z)
794 return atan2 (IMAGPART (z), REALPART (z));
798 #if !defined(HAVE_CARGL) && defined(HAVE_ATAN2L)
801 cargl (long double complex z)
803 return atan2l (IMAGPART (z), REALPART (z));
808 /* exp(z) = exp(a)*(cos(b) + i sin(b)) */
809 #if !defined(HAVE_CEXPF)
812 cexpf (float complex z)
819 COMPLEX_ASSIGN (v, cosf (b), sinf (b));
824 #if !defined(HAVE_CEXP)
827 cexp (double complex z)
834 COMPLEX_ASSIGN (v, cos (b), sin (b));
839 #if !defined(HAVE_CEXPL) && defined(HAVE_COSL) && defined(HAVE_SINL) && defined(EXPL)
842 cexpl (long double complex z)
845 long double complex v;
849 COMPLEX_ASSIGN (v, cosl (b), sinl (b));
855 /* log(z) = log (cabs(z)) + i*carg(z) */
856 #if !defined(HAVE_CLOGF)
859 clogf (float complex z)
863 COMPLEX_ASSIGN (v, logf (cabsf (z)), cargf (z));
868 #if !defined(HAVE_CLOG)
871 clog (double complex z)
875 COMPLEX_ASSIGN (v, log (cabs (z)), carg (z));
880 #if !defined(HAVE_CLOGL) && defined(HAVE_LOGL) && defined(HAVE_CABSL) && defined(HAVE_CARGL)
883 clogl (long double complex z)
885 long double complex v;
887 COMPLEX_ASSIGN (v, logl (cabsl (z)), cargl (z));
893 /* log10(z) = log10 (cabs(z)) + i*carg(z) */
894 #if !defined(HAVE_CLOG10F)
895 #define HAVE_CLOG10F 1
897 clog10f (float complex z)
901 COMPLEX_ASSIGN (v, log10f (cabsf (z)), cargf (z));
906 #if !defined(HAVE_CLOG10)
907 #define HAVE_CLOG10 1
909 clog10 (double complex z)
913 COMPLEX_ASSIGN (v, log10 (cabs (z)), carg (z));
918 #if !defined(HAVE_CLOG10L) && defined(HAVE_LOG10L) && defined(HAVE_CABSL) && defined(HAVE_CARGL)
919 #define HAVE_CLOG10L 1
921 clog10l (long double complex z)
923 long double complex v;
925 COMPLEX_ASSIGN (v, log10l (cabsl (z)), cargl (z));
931 /* pow(base, power) = cexp (power * clog (base)) */
932 #if !defined(HAVE_CPOWF)
935 cpowf (float complex base, float complex power)
937 return cexpf (power * clogf (base));
941 #if !defined(HAVE_CPOW)
944 cpow (double complex base, double complex power)
946 return cexp (power * clog (base));
950 #if !defined(HAVE_CPOWL) && defined(HAVE_CEXPL) && defined(HAVE_CLOGL)
953 cpowl (long double complex base, long double complex power)
955 return cexpl (power * clogl (base));
960 /* sqrt(z). Algorithm pulled from glibc. */
961 #if !defined(HAVE_CSQRTF)
962 #define HAVE_CSQRTF 1
964 csqrtf (float complex z)
975 COMPLEX_ASSIGN (v, 0, copysignf (sqrtf (-re), im));
979 COMPLEX_ASSIGN (v, fabsf (sqrtf (re)), copysignf (0, im));
986 r = sqrtf (0.5 * fabsf (im));
988 COMPLEX_ASSIGN (v, r, copysignf (r, im));
995 /* Use the identity 2 Re res Im res = Im x
996 to avoid cancellation error in d +/- Re x. */
999 r = sqrtf (0.5 * d + 0.5 * re);
1004 s = sqrtf (0.5 * d - 0.5 * re);
1005 r = fabsf ((0.5 * im) / s);
1008 COMPLEX_ASSIGN (v, r, copysignf (s, im));
1014 #if !defined(HAVE_CSQRT)
1015 #define HAVE_CSQRT 1
1017 csqrt (double complex z)
1028 COMPLEX_ASSIGN (v, 0, copysign (sqrt (-re), im));
1032 COMPLEX_ASSIGN (v, fabs (sqrt (re)), copysign (0, im));
1039 r = sqrt (0.5 * fabs (im));
1041 COMPLEX_ASSIGN (v, r, copysign (r, im));
1048 /* Use the identity 2 Re res Im res = Im x
1049 to avoid cancellation error in d +/- Re x. */
1052 r = sqrt (0.5 * d + 0.5 * re);
1057 s = sqrt (0.5 * d - 0.5 * re);
1058 r = fabs ((0.5 * im) / s);
1061 COMPLEX_ASSIGN (v, r, copysign (s, im));
1067 #if !defined(HAVE_CSQRTL) && defined(HAVE_COPYSIGNL) && defined(HAVE_SQRTL) && defined(HAVE_FABSL) && defined(HAVE_HYPOTL)
1068 #define HAVE_CSQRTL 1
1070 csqrtl (long double complex z)
1073 long double complex v;
1081 COMPLEX_ASSIGN (v, 0, copysignl (sqrtl (-re), im));
1085 COMPLEX_ASSIGN (v, fabsl (sqrtl (re)), copysignl (0, im));
1092 r = sqrtl (0.5 * fabsl (im));
1094 COMPLEX_ASSIGN (v, copysignl (r, im), r);
1098 long double d, r, s;
1100 d = hypotl (re, im);
1101 /* Use the identity 2 Re res Im res = Im x
1102 to avoid cancellation error in d +/- Re x. */
1105 r = sqrtl (0.5 * d + 0.5 * re);
1110 s = sqrtl (0.5 * d - 0.5 * re);
1111 r = fabsl ((0.5 * im) / s);
1114 COMPLEX_ASSIGN (v, r, copysignl (s, im));
1121 /* sinh(a + i b) = sinh(a) cos(b) + i cosh(a) sin(b) */
1122 #if !defined(HAVE_CSINHF)
1123 #define HAVE_CSINHF 1
1125 csinhf (float complex a)
1132 COMPLEX_ASSIGN (v, sinhf (r) * cosf (i), coshf (r) * sinf (i));
1137 #if !defined(HAVE_CSINH)
1138 #define HAVE_CSINH 1
1140 csinh (double complex a)
1147 COMPLEX_ASSIGN (v, sinh (r) * cos (i), cosh (r) * sin (i));
1152 #if !defined(HAVE_CSINHL) && defined(HAVE_COSL) && defined(HAVE_COSHL) && defined(HAVE_SINL) && defined(HAVE_SINHL)
1153 #define HAVE_CSINHL 1
1155 csinhl (long double complex a)
1158 long double complex v;
1162 COMPLEX_ASSIGN (v, sinhl (r) * cosl (i), coshl (r) * sinl (i));
1168 /* cosh(a + i b) = cosh(a) cos(b) + i sinh(a) sin(b) */
1169 #if !defined(HAVE_CCOSHF)
1170 #define HAVE_CCOSHF 1
1172 ccoshf (float complex a)
1179 COMPLEX_ASSIGN (v, coshf (r) * cosf (i), sinhf (r) * sinf (i));
1184 #if !defined(HAVE_CCOSH)
1185 #define HAVE_CCOSH 1
1187 ccosh (double complex a)
1194 COMPLEX_ASSIGN (v, cosh (r) * cos (i), sinh (r) * sin (i));
1199 #if !defined(HAVE_CCOSHL) && defined(HAVE_COSL) && defined(HAVE_COSHL) && defined(HAVE_SINL) && defined(HAVE_SINHL)
1200 #define HAVE_CCOSHL 1
1202 ccoshl (long double complex a)
1205 long double complex v;
1209 COMPLEX_ASSIGN (v, coshl (r) * cosl (i), sinhl (r) * sinl (i));
1215 /* tanh(a + i b) = (tanh(a) + i tan(b)) / (1 + i tanh(a) tan(b)) */
1216 #if !defined(HAVE_CTANHF)
1217 #define HAVE_CTANHF 1
1219 ctanhf (float complex a)
1224 rt = tanhf (REALPART (a));
1225 it = tanf (IMAGPART (a));
1226 COMPLEX_ASSIGN (n, rt, it);
1227 COMPLEX_ASSIGN (d, 1, rt * it);
1233 #if !defined(HAVE_CTANH)
1234 #define HAVE_CTANH 1
1236 ctanh (double complex a)
1239 double complex n, d;
1241 rt = tanh (REALPART (a));
1242 it = tan (IMAGPART (a));
1243 COMPLEX_ASSIGN (n, rt, it);
1244 COMPLEX_ASSIGN (d, 1, rt * it);
1250 #if !defined(HAVE_CTANHL) && defined(HAVE_TANL) && defined(HAVE_TANHL)
1251 #define HAVE_CTANHL 1
1253 ctanhl (long double complex a)
1256 long double complex n, d;
1258 rt = tanhl (REALPART (a));
1259 it = tanl (IMAGPART (a));
1260 COMPLEX_ASSIGN (n, rt, it);
1261 COMPLEX_ASSIGN (d, 1, rt * it);
1268 /* sin(a + i b) = sin(a) cosh(b) + i cos(a) sinh(b) */
1269 #if !defined(HAVE_CSINF)
1270 #define HAVE_CSINF 1
1272 csinf (float complex a)
1279 COMPLEX_ASSIGN (v, sinf (r) * coshf (i), cosf (r) * sinhf (i));
1284 #if !defined(HAVE_CSIN)
1287 csin (double complex a)
1294 COMPLEX_ASSIGN (v, sin (r) * cosh (i), cos (r) * sinh (i));
1299 #if !defined(HAVE_CSINL) && defined(HAVE_COSL) && defined(HAVE_COSHL) && defined(HAVE_SINL) && defined(HAVE_SINHL)
1300 #define HAVE_CSINL 1
1302 csinl (long double complex a)
1305 long double complex v;
1309 COMPLEX_ASSIGN (v, sinl (r) * coshl (i), cosl (r) * sinhl (i));
1315 /* cos(a + i b) = cos(a) cosh(b) - i sin(a) sinh(b) */
1316 #if !defined(HAVE_CCOSF)
1317 #define HAVE_CCOSF 1
1319 ccosf (float complex a)
1326 COMPLEX_ASSIGN (v, cosf (r) * coshf (i), - (sinf (r) * sinhf (i)));
1331 #if !defined(HAVE_CCOS)
1334 ccos (double complex a)
1341 COMPLEX_ASSIGN (v, cos (r) * cosh (i), - (sin (r) * sinh (i)));
1346 #if !defined(HAVE_CCOSL) && defined(HAVE_COSL) && defined(HAVE_COSHL) && defined(HAVE_SINL) && defined(HAVE_SINHL)
1347 #define HAVE_CCOSL 1
1349 ccosl (long double complex a)
1352 long double complex v;
1356 COMPLEX_ASSIGN (v, cosl (r) * coshl (i), - (sinl (r) * sinhl (i)));
1362 /* tan(a + i b) = (tan(a) + i tanh(b)) / (1 - i tan(a) tanh(b)) */
1363 #if !defined(HAVE_CTANF)
1364 #define HAVE_CTANF 1
1366 ctanf (float complex a)
1371 rt = tanf (REALPART (a));
1372 it = tanhf (IMAGPART (a));
1373 COMPLEX_ASSIGN (n, rt, it);
1374 COMPLEX_ASSIGN (d, 1, - (rt * it));
1380 #if !defined(HAVE_CTAN)
1383 ctan (double complex a)
1386 double complex n, d;
1388 rt = tan (REALPART (a));
1389 it = tanh (IMAGPART (a));
1390 COMPLEX_ASSIGN (n, rt, it);
1391 COMPLEX_ASSIGN (d, 1, - (rt * it));
1397 #if !defined(HAVE_CTANL) && defined(HAVE_TANL) && defined(HAVE_TANHL)
1398 #define HAVE_CTANL 1
1400 ctanl (long double complex a)
1403 long double complex n, d;
1405 rt = tanl (REALPART (a));
1406 it = tanhl (IMAGPART (a));
1407 COMPLEX_ASSIGN (n, rt, it);
1408 COMPLEX_ASSIGN (d, 1, - (rt * it));
1415 /* Complex ASIN. Returns wrongly NaN for infinite arguments.
1416 Algorithm taken from Abramowitz & Stegun. */
1418 #if !defined(HAVE_CASINF) && defined(HAVE_CLOGF) && defined(HAVE_CSQRTF)
1419 #define HAVE_CASINF 1
1421 casinf (complex float z)
1423 return -I*clogf (I*z + csqrtf (1.0f-z*z));
1428 #if !defined(HAVE_CASIN) && defined(HAVE_CLOG) && defined(HAVE_CSQRT)
1429 #define HAVE_CASIN 1
1431 casin (complex double z)
1433 return -I*clog (I*z + csqrt (1.0-z*z));
1438 #if !defined(HAVE_CASINL) && defined(HAVE_CLOGL) && defined(HAVE_CSQRTL)
1439 #define HAVE_CASINL 1
1441 casinl (complex long double z)
1443 return -I*clogl (I*z + csqrtl (1.0L-z*z));
1448 /* Complex ACOS. Returns wrongly NaN for infinite arguments.
1449 Algorithm taken from Abramowitz & Stegun. */
1451 #if !defined(HAVE_CACOSF) && defined(HAVE_CLOGF) && defined(HAVE_CSQRTF)
1452 #define HAVE_CACOSF 1
1454 cacosf (complex float z)
1456 return -I*clogf (z + I*csqrtf(1.0f-z*z));
1462 #if !defined(HAVE_CACOS) && defined(HAVE_CLOG) && defined(HAVE_CSQRT)
1463 #define HAVE_CACOS 1
1464 cacos (complex double z)
1466 return -I*clog (z + I*csqrt (1.0-z*z));
1471 #if !defined(HAVE_CACOSL) && defined(HAVE_CLOGL) && defined(HAVE_CSQRTL)
1472 #define HAVE_CACOSL 1
1474 cacosl (complex long double z)
1476 return -I*clogl (z + I*csqrtl (1.0L-z*z));
1481 /* Complex ATAN. Returns wrongly NaN for infinite arguments.
1482 Algorithm taken from Abramowitz & Stegun. */
1484 #if !defined(HAVE_CATANF) && defined(HAVE_CLOGF)
1485 #define HAVE_CACOSF 1
1487 catanf (complex float z)
1489 return I*clogf ((I+z)/(I-z))/2.0f;
1494 #if !defined(HAVE_CATAN) && defined(HAVE_CLOG)
1495 #define HAVE_CACOS 1
1497 catan (complex double z)
1499 return I*clog ((I+z)/(I-z))/2.0;
1504 #if !defined(HAVE_CATANL) && defined(HAVE_CLOGL)
1505 #define HAVE_CACOSL 1
1507 catanl (complex long double z)
1509 return I*clogl ((I+z)/(I-z))/2.0L;
1514 /* Complex ASINH. Returns wrongly NaN for infinite arguments.
1515 Algorithm taken from Abramowitz & Stegun. */
1517 #if !defined(HAVE_CASINHF) && defined(HAVE_CLOGF) && defined(HAVE_CSQRTF)
1518 #define HAVE_CASINHF 1
1520 casinhf (complex float z)
1522 return clogf (z + csqrtf (z*z+1.0f));
1527 #if !defined(HAVE_CASINH) && defined(HAVE_CLOG) && defined(HAVE_CSQRT)
1528 #define HAVE_CASINH 1
1530 casinh (complex double z)
1532 return clog (z + csqrt (z*z+1.0));
1537 #if !defined(HAVE_CASINHL) && defined(HAVE_CLOGL) && defined(HAVE_CSQRTL)
1538 #define HAVE_CASINHL 1
1540 casinhl (complex long double z)
1542 return clogl (z + csqrtl (z*z+1.0L));
1547 /* Complex ACOSH. Returns wrongly NaN for infinite arguments.
1548 Algorithm taken from Abramowitz & Stegun. */
1550 #if !defined(HAVE_CACOSHF) && defined(HAVE_CLOGF) && defined(HAVE_CSQRTF)
1551 #define HAVE_CACOSHF 1
1553 cacoshf (complex float z)
1555 return clogf (z + csqrtf (z-1.0f) * csqrtf (z+1.0f));
1560 #if !defined(HAVE_CACOSH) && defined(HAVE_CLOG) && defined(HAVE_CSQRT)
1561 #define HAVE_CACOSH 1
1563 cacosh (complex double z)
1565 return clog (z + csqrt (z-1.0) * csqrt (z+1.0));
1570 #if !defined(HAVE_CACOSHL) && defined(HAVE_CLOGL) && defined(HAVE_CSQRTL)
1571 #define HAVE_CACOSHL 1
1573 cacoshl (complex long double z)
1575 return clogl (z + csqrtl (z-1.0L) * csqrtl (z+1.0L));
1580 /* Complex ATANH. Returns wrongly NaN for infinite arguments.
1581 Algorithm taken from Abramowitz & Stegun. */
1583 #if !defined(HAVE_CATANHF) && defined(HAVE_CLOGF)
1584 #define HAVE_CATANHF 1
1586 catanhf (complex float z)
1588 return clogf ((1.0f+z)/(1.0f-z))/2.0f;
1593 #if !defined(HAVE_CATANH) && defined(HAVE_CLOG)
1594 #define HAVE_CATANH 1
1596 catanh (complex double z)
1598 return clog ((1.0+z)/(1.0-z))/2.0;
1602 #if !defined(HAVE_CATANHL) && defined(HAVE_CLOGL)
1603 #define HAVE_CATANHL 1
1605 catanhl (complex long double z)
1607 return clogl ((1.0L+z)/(1.0L-z))/2.0L;
1612 #if !defined(HAVE_TGAMMA)
1613 #define HAVE_TGAMMA 1
1615 extern double tgamma (double);
1617 /* Fallback tgamma() function. Uses the algorithm from
1618 http://www.netlib.org/specfun/gamma and references therein. */
1621 #define SQRTPI 0.9189385332046727417803297
1624 #define PI 3.1415926535897932384626434
1630 double fact, res, sum, xden, xnum, y, y1, ysq, z;
1632 static double p[8] = {
1633 -1.71618513886549492533811e0, 2.47656508055759199108314e1,
1634 -3.79804256470945635097577e2, 6.29331155312818442661052e2,
1635 8.66966202790413211295064e2, -3.14512729688483675254357e4,
1636 -3.61444134186911729807069e4, 6.64561438202405440627855e4 };
1638 static double q[8] = {
1639 -3.08402300119738975254353e1, 3.15350626979604161529144e2,
1640 -1.01515636749021914166146e3, -3.10777167157231109440444e3,
1641 2.25381184209801510330112e4, 4.75584627752788110767815e3,
1642 -1.34659959864969306392456e5, -1.15132259675553483497211e5 };
1644 static double c[7] = { -1.910444077728e-03,
1645 8.4171387781295e-04, -5.952379913043012e-04,
1646 7.93650793500350248e-04, -2.777777777777681622553e-03,
1647 8.333333333333333331554247e-02, 5.7083835261e-03 };
1649 static const double xminin = 2.23e-308;
1650 static const double xbig = 171.624;
1651 static const double xnan = __builtin_nan ("0x0"), xinf = __builtin_inf ();
1652 static double eps = 0;
1655 eps = nextafter(1., 2.) - 1.;
1662 if (__builtin_isnan (x))
1673 if (y1 != trunc(y1*0.5l)*2)
1675 fact = -PI / sin(PI*res);
1679 return x == 0 ? copysign (xinf, x) : xnan;
1706 for (i = 0; i < 8; i++)
1708 xnum = (xnum + p[i]) * z;
1709 xden = xden * z + q[i];
1712 res = xnum / xden + 1;
1717 for (i = 1; i <= n; i++)
1729 for (i = 0; i < 6; i++)
1730 sum = sum / ysq + c[i];
1732 sum = sum/y - y + SQRTPI;
1733 sum = sum + (y - 0.5) * log(y);
1737 return x < 0 ? xnan : xinf;
1751 #if !defined(HAVE_LGAMMA)
1752 #define HAVE_LGAMMA 1
1754 extern double lgamma (double);
1756 /* Fallback lgamma() function. Uses the algorithm from
1757 http://www.netlib.org/specfun/algama and references therein,
1758 except for negative arguments (where netlib would return +Inf)
1759 where we use the following identity:
1760 lgamma(y) = log(pi/(|y*sin(pi*y)|)) - lgamma(-y)
1768 #define SQRTPI 0.9189385332046727417803297
1771 #define PI 3.1415926535897932384626434
1773 #define PNT68 0.6796875
1774 #define D1 -0.5772156649015328605195174
1775 #define D2 0.4227843350984671393993777
1776 #define D4 1.791759469228055000094023
1778 static double p1[8] = {
1779 4.945235359296727046734888e0, 2.018112620856775083915565e2,
1780 2.290838373831346393026739e3, 1.131967205903380828685045e4,
1781 2.855724635671635335736389e4, 3.848496228443793359990269e4,
1782 2.637748787624195437963534e4, 7.225813979700288197698961e3 };
1783 static double q1[8] = {
1784 6.748212550303777196073036e1, 1.113332393857199323513008e3,
1785 7.738757056935398733233834e3, 2.763987074403340708898585e4,
1786 5.499310206226157329794414e4, 6.161122180066002127833352e4,
1787 3.635127591501940507276287e4, 8.785536302431013170870835e3 };
1788 static double p2[8] = {
1789 4.974607845568932035012064e0, 5.424138599891070494101986e2,
1790 1.550693864978364947665077e4, 1.847932904445632425417223e5,
1791 1.088204769468828767498470e6, 3.338152967987029735917223e6,
1792 5.106661678927352456275255e6, 3.074109054850539556250927e6 };
1793 static double q2[8] = {
1794 1.830328399370592604055942e2, 7.765049321445005871323047e3,
1795 1.331903827966074194402448e5, 1.136705821321969608938755e6,
1796 5.267964117437946917577538e6, 1.346701454311101692290052e7,
1797 1.782736530353274213975932e7, 9.533095591844353613395747e6 };
1798 static double p4[8] = {
1799 1.474502166059939948905062e4, 2.426813369486704502836312e6,
1800 1.214755574045093227939592e8, 2.663432449630976949898078e9,
1801 2.940378956634553899906876e10, 1.702665737765398868392998e11,
1802 4.926125793377430887588120e11, 5.606251856223951465078242e11 };
1803 static double q4[8] = {
1804 2.690530175870899333379843e3, 6.393885654300092398984238e5,
1805 4.135599930241388052042842e7, 1.120872109616147941376570e9,
1806 1.488613728678813811542398e10, 1.016803586272438228077304e11,
1807 3.417476345507377132798597e11, 4.463158187419713286462081e11 };
1808 static double c[7] = {
1809 -1.910444077728e-03, 8.4171387781295e-04,
1810 -5.952379913043012e-04, 7.93650793500350248e-04,
1811 -2.777777777777681622553e-03, 8.333333333333333331554247e-02,
1814 static double xbig = 2.55e305, xinf = __builtin_inf (), eps = 0,
1818 double corr, res, xden, xm1, xm2, xm4, xnum, ysq;
1821 eps = __builtin_nextafter(1., 2.) - 1.;
1823 if ((y > 0) && (y <= xbig))
1837 xm1 = (y - 0.5) - 0.5;
1840 if ((y <= 0.5) || (y >= PNT68))
1844 for (i = 0; i < 8; i++)
1846 xnum = xnum*xm1 + p1[i];
1847 xden = xden*xm1 + q1[i];
1849 res = corr + (xm1 * (D1 + xm1*(xnum/xden)));
1853 xm2 = (y - 0.5) - 0.5;
1856 for (i = 0; i < 8; i++)
1858 xnum = xnum*xm2 + p2[i];
1859 xden = xden*xm2 + q2[i];
1861 res = corr + xm2 * (D2 + xm2*(xnum/xden));
1869 for (i = 0; i < 8; i++)
1871 xnum = xnum*xm2 + p2[i];
1872 xden = xden*xm2 + q2[i];
1874 res = xm2 * (D2 + xm2*(xnum/xden));
1881 for (i = 0; i < 8; i++)
1883 xnum = xnum*xm4 + p4[i];
1884 xden = xden*xm4 + q4[i];
1886 res = D4 + xm4*(xnum/xden);
1895 for (i = 0; i < 6; i++)
1896 res = res / ysq + c[i];
1900 res = res + SQRTPI - 0.5*corr;
1901 res = res + y*(corr-1);
1904 else if (y < 0 && __builtin_floor (y) != y)
1906 /* lgamma(y) = log(pi/(|y*sin(pi*y)|)) - lgamma(-y)
1907 For abs(y) very close to zero, we use a series expansion to
1908 the first order in y to avoid overflow. */
1910 res = -2 * log (fabs (y)) - lgamma (-y);
1912 res = log (PI / fabs (y * sin (PI * y))) - lgamma (-y);
1922 #if defined(HAVE_TGAMMA) && !defined(HAVE_TGAMMAF)
1923 #define HAVE_TGAMMAF 1
1924 extern float tgammaf (float);
1929 return (float) tgamma ((double) x);
1933 #if defined(HAVE_LGAMMA) && !defined(HAVE_LGAMMAF)
1934 #define HAVE_LGAMMAF 1
1935 extern float lgammaf (float);
1940 return (float) lgamma ((double) x);