1 /* Implementation of various C99 functions
2 Copyright (C) 2004-2013 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 /* On a C99 system "I" (with I*I = -1) should be defined in complex.h;
31 if not, we define a fallback version here. */
33 # if defined(_Imaginary_I)
34 # define I _Imaginary_I
35 # elif defined(_Complex_I)
42 /* Prototypes are included to silence -Wstrict-prototypes
43 -Wmissing-prototypes. */
46 /* Wrappers for systems without the various C99 single precision Bessel
49 #if defined(HAVE_J0) && ! defined(HAVE_J0F)
56 return (float) j0 ((double) x
);
60 #if defined(HAVE_J1) && !defined(HAVE_J1F)
66 return (float) j1 ((double) x
);
70 #if defined(HAVE_JN) && !defined(HAVE_JNF)
72 float jnf (int, float);
77 return (float) jn (n
, (double) x
);
81 #if defined(HAVE_Y0) && !defined(HAVE_Y0F)
88 return (float) y0 ((double) x
);
92 #if defined(HAVE_Y1) && !defined(HAVE_Y1F)
99 return (float) y1 ((double) x
);
103 #if defined(HAVE_YN) && !defined(HAVE_YNF)
105 float ynf (int, float);
110 return (float) yn (n
, (double) x
);
115 /* Wrappers for systems without the C99 erff() and erfcf() functions. */
117 #if defined(HAVE_ERF) && !defined(HAVE_ERFF)
124 return (float) erf ((double) x
);
128 #if defined(HAVE_ERFC) && !defined(HAVE_ERFCF)
135 return (float) erfc ((double) x
);
142 float acosf (float x
);
147 return (float) acos (x
);
151 #if HAVE_ACOSH && !HAVE_ACOSHF
152 float acoshf (float x
);
157 return (float) acosh ((double) x
);
163 float asinf (float x
);
168 return (float) asin (x
);
172 #if HAVE_ASINH && !HAVE_ASINHF
173 float asinhf (float x
);
178 return (float) asinh ((double) x
);
183 #define HAVE_ATAN2F 1
184 float atan2f (float y
, float x
);
187 atan2f (float y
, float x
)
189 return (float) atan2 (y
, x
);
195 float atanf (float x
);
200 return (float) atan (x
);
204 #if HAVE_ATANH && !HAVE_ATANHF
205 float atanhf (float x
);
210 return (float) atanh ((double) x
);
216 float ceilf (float x
);
221 return (float) ceil (x
);
225 #ifndef HAVE_COPYSIGNF
226 #define HAVE_COPYSIGNF 1
227 float copysignf (float x
, float y
);
230 copysignf (float x
, float y
)
232 return (float) copysign (x
, y
);
238 float cosf (float x
);
243 return (float) cos (x
);
249 float coshf (float x
);
254 return (float) cosh (x
);
260 float expf (float x
);
265 return (float) exp (x
);
271 float fabsf (float x
);
276 return (float) fabs (x
);
281 #define HAVE_FLOORF 1
282 float floorf (float x
);
287 return (float) floor (x
);
293 float fmodf (float x
, float y
);
296 fmodf (float x
, float y
)
298 return (float) fmod (x
, y
);
303 #define HAVE_FREXPF 1
304 float frexpf (float x
, int *exp
);
307 frexpf (float x
, int *exp
)
309 return (float) frexp (x
, exp
);
314 #define HAVE_HYPOTF 1
315 float hypotf (float x
, float y
);
318 hypotf (float x
, float y
)
320 return (float) hypot (x
, y
);
326 float logf (float x
);
331 return (float) log (x
);
336 #define HAVE_LOG10F 1
337 float log10f (float x
);
342 return (float) log10 (x
);
347 #define HAVE_SCALBN 1
348 double scalbn (double x
, int y
);
351 scalbn (double x
, int y
)
353 #if (FLT_RADIX == 2) && defined(HAVE_LDEXP)
356 return x
* pow (FLT_RADIX
, y
);
362 #define HAVE_SCALBNF 1
363 float scalbnf (float x
, int y
);
366 scalbnf (float x
, int y
)
368 return (float) scalbn (x
, y
);
374 float sinf (float x
);
379 return (float) sin (x
);
385 float sinhf (float x
);
390 return (float) sinh (x
);
396 float sqrtf (float x
);
401 return (float) sqrt (x
);
407 float tanf (float x
);
412 return (float) tan (x
);
418 float tanhf (float x
);
423 return (float) tanh (x
);
429 double trunc (double x
);
445 #define HAVE_TRUNCF 1
446 float truncf (float x
);
451 return (float) trunc (x
);
455 #ifndef HAVE_NEXTAFTERF
456 #define HAVE_NEXTAFTERF 1
457 /* This is a portable implementation of nextafterf that is intended to be
458 independent of the floating point format or its in memory representation.
459 This implementation works correctly with denormalized values. */
460 float nextafterf (float x
, float y
);
463 nextafterf (float x
, float y
)
465 /* This variable is marked volatile to avoid excess precision problems
466 on some platforms, including IA-32. */
467 volatile float delta
;
468 float absx
, denorm_min
;
470 if (isnan (x
) || isnan (y
))
475 return x
> 0 ? __FLT_MAX__
: - __FLT_MAX__
;
477 /* absx = fabsf (x); */
478 absx
= (x
< 0.0) ? -x
: x
;
480 /* __FLT_DENORM_MIN__ is non-zero iff the target supports denormals. */
481 if (__FLT_DENORM_MIN__
== 0.0f
)
482 denorm_min
= __FLT_MIN__
;
484 denorm_min
= __FLT_DENORM_MIN__
;
486 if (absx
< __FLT_MIN__
)
493 /* Discard the fraction from x. */
494 frac
= frexpf (absx
, &exp
);
495 delta
= scalbnf (0.5f
, exp
);
497 /* Scale x by the epsilon of the representation. By rights we should
498 have been able to combine this with scalbnf, but some targets don't
499 get that correct with denormals. */
500 delta
*= __FLT_EPSILON__
;
502 /* If we're going to be reducing the absolute value of X, and doing so
503 would reduce the exponent of X, then the delta to be applied is
504 one exponent smaller. */
505 if (frac
== 0.5f
&& (y
< x
) == (x
> 0))
508 /* If that underflows to zero, then we're back to the minimum. */
523 float powf (float x
, float y
);
526 powf (float x
, float y
)
528 return (float) pow (x
, y
);
535 /* Round to nearest integral value. If the argument is halfway between two
536 integral values then round away from zero. */
537 double round (double x
);
564 /* Algorithm by Steven G. Kargl. */
566 #if !defined(HAVE_ROUNDL)
567 #define HAVE_ROUNDL 1
568 long double roundl (long double x
);
570 #if defined(HAVE_CEILL)
571 /* Round to nearest integral value. If the argument is halfway between two
572 integral values then round away from zero. */
575 roundl (long double x
)
598 /* Poor version of roundl for system that don't have ceill. */
600 roundl (long double x
)
602 if (x
> DBL_MAX
|| x
< -DBL_MAX
)
604 #ifdef HAVE_NEXTAFTERL
605 long double prechalf
= nextafterl (0.5L, LDBL_MAX
);
607 static long double prechalf
= 0.5L;
609 return (GFC_INTEGER_LARGEST
) (x
+ (x
> 0 ? prechalf
: -prechalf
));
613 return round ((double) x
);
620 #define HAVE_ROUNDF 1
621 /* Round to nearest integral value. If the argument is halfway between two
622 integral values then round away from zero. */
623 float roundf (float x
);
650 /* lround{f,,l} and llround{f,,l} functions. */
652 #if !defined(HAVE_LROUNDF) && defined(HAVE_ROUNDF)
653 #define HAVE_LROUNDF 1
654 long int lroundf (float x
);
659 return (long int) roundf (x
);
663 #if !defined(HAVE_LROUND) && defined(HAVE_ROUND)
664 #define HAVE_LROUND 1
665 long int lround (double x
);
670 return (long int) round (x
);
674 #if !defined(HAVE_LROUNDL) && defined(HAVE_ROUNDL)
675 #define HAVE_LROUNDL 1
676 long int lroundl (long double x
);
679 lroundl (long double x
)
681 return (long long int) roundl (x
);
685 #if !defined(HAVE_LLROUNDF) && defined(HAVE_ROUNDF)
686 #define HAVE_LLROUNDF 1
687 long long int llroundf (float x
);
692 return (long long int) roundf (x
);
696 #if !defined(HAVE_LLROUND) && defined(HAVE_ROUND)
697 #define HAVE_LLROUND 1
698 long long int llround (double x
);
703 return (long long int) round (x
);
707 #if !defined(HAVE_LLROUNDL) && defined(HAVE_ROUNDL)
708 #define HAVE_LLROUNDL 1
709 long long int llroundl (long double x
);
712 llroundl (long double x
)
714 return (long long int) roundl (x
);
720 #define HAVE_LOG10L 1
721 /* log10 function for long double variables. The version provided here
722 reduces the argument until it fits into a double, then use log10. */
723 long double log10l (long double x
);
726 log10l (long double x
)
728 #if LDBL_MAX_EXP > DBL_MAX_EXP
733 if (x
> 0x1p
16383L) { p2_result
+= 16383; x
/= 0x1p
16383L; }
734 if (x
> 0x1p
8191L) { p2_result
+= 8191; x
/= 0x1p
8191L; }
735 if (x
> 0x1p
4095L) { p2_result
+= 4095; x
/= 0x1p
4095L; }
736 if (x
> 0x1p
2047L) { p2_result
+= 2047; x
/= 0x1p
2047L; }
737 if (x
> 0x1p
1023L) { p2_result
+= 1023; x
/= 0x1p
1023L; }
738 val
= log10 ((double) x
);
739 return (val
+ p2_result
* .30102999566398119521373889472449302L);
742 #if LDBL_MIN_EXP < DBL_MIN_EXP
747 if (x
< 0x1p
-16380L) { p2_result
+= 16380; x
/= 0x1p
-16380L; }
748 if (x
< 0x1p
-8189L) { p2_result
+= 8189; x
/= 0x1p
-8189L; }
749 if (x
< 0x1p
-4093L) { p2_result
+= 4093; x
/= 0x1p
-4093L; }
750 if (x
< 0x1p
-2045L) { p2_result
+= 2045; x
/= 0x1p
-2045L; }
751 if (x
< 0x1p
-1021L) { p2_result
+= 1021; x
/= 0x1p
-1021L; }
752 val
= fabs (log10 ((double) x
));
753 return (- val
- p2_result
* .30102999566398119521373889472449302L);
762 #define HAVE_FLOORL 1
763 long double floorl (long double x
);
766 floorl (long double x
)
768 /* Zero, possibly signed. */
772 /* Large magnitude. */
773 if (x
> DBL_MAX
|| x
< (-DBL_MAX
))
776 /* Small positive values. */
777 if (x
>= 0 && x
< DBL_MIN
)
780 /* Small negative values. */
781 if (x
< 0 && x
> (-DBL_MIN
))
791 long double fmodl (long double x
, long double y
);
794 fmodl (long double x
, long double y
)
799 /* Need to check that the result has the same sign as x and magnitude
800 less than the magnitude of y. */
801 return x
- floorl (x
/ y
) * y
;
806 #if !defined(HAVE_CABSF)
808 float cabsf (float complex z
);
811 cabsf (float complex z
)
813 return hypotf (REALPART (z
), IMAGPART (z
));
817 #if !defined(HAVE_CABS)
819 double cabs (double complex z
);
822 cabs (double complex z
)
824 return hypot (REALPART (z
), IMAGPART (z
));
828 #if !defined(HAVE_CABSL) && defined(HAVE_HYPOTL)
830 long double cabsl (long double complex z
);
833 cabsl (long double complex z
)
835 return hypotl (REALPART (z
), IMAGPART (z
));
840 #if !defined(HAVE_CARGF)
842 float cargf (float complex z
);
845 cargf (float complex z
)
847 return atan2f (IMAGPART (z
), REALPART (z
));
851 #if !defined(HAVE_CARG)
853 double carg (double complex z
);
856 carg (double complex z
)
858 return atan2 (IMAGPART (z
), REALPART (z
));
862 #if !defined(HAVE_CARGL) && defined(HAVE_ATAN2L)
864 long double cargl (long double complex z
);
867 cargl (long double complex z
)
869 return atan2l (IMAGPART (z
), REALPART (z
));
874 /* exp(z) = exp(a)*(cos(b) + i sin(b)) */
875 #if !defined(HAVE_CEXPF)
877 float complex cexpf (float complex z
);
880 cexpf (float complex z
)
887 COMPLEX_ASSIGN (v
, cosf (b
), sinf (b
));
892 #if !defined(HAVE_CEXP)
894 double complex cexp (double complex z
);
897 cexp (double complex z
)
904 COMPLEX_ASSIGN (v
, cos (b
), sin (b
));
909 #if !defined(HAVE_CEXPL) && defined(HAVE_COSL) && defined(HAVE_SINL) && defined(EXPL)
911 long double complex cexpl (long double complex z
);
914 cexpl (long double complex z
)
917 long double complex v
;
921 COMPLEX_ASSIGN (v
, cosl (b
), sinl (b
));
927 /* log(z) = log (cabs(z)) + i*carg(z) */
928 #if !defined(HAVE_CLOGF)
930 float complex clogf (float complex z
);
933 clogf (float complex z
)
937 COMPLEX_ASSIGN (v
, logf (cabsf (z
)), cargf (z
));
942 #if !defined(HAVE_CLOG)
944 double complex clog (double complex z
);
947 clog (double complex z
)
951 COMPLEX_ASSIGN (v
, log (cabs (z
)), carg (z
));
956 #if !defined(HAVE_CLOGL) && defined(HAVE_LOGL) && defined(HAVE_CABSL) && defined(HAVE_CARGL)
958 long double complex clogl (long double complex z
);
961 clogl (long double complex z
)
963 long double complex v
;
965 COMPLEX_ASSIGN (v
, logl (cabsl (z
)), cargl (z
));
971 /* log10(z) = log10 (cabs(z)) + i*carg(z) */
972 #if !defined(HAVE_CLOG10F)
973 #define HAVE_CLOG10F 1
974 float complex clog10f (float complex z
);
977 clog10f (float complex z
)
981 COMPLEX_ASSIGN (v
, log10f (cabsf (z
)), cargf (z
));
986 #if !defined(HAVE_CLOG10)
987 #define HAVE_CLOG10 1
988 double complex clog10 (double complex z
);
991 clog10 (double complex z
)
995 COMPLEX_ASSIGN (v
, log10 (cabs (z
)), carg (z
));
1000 #if !defined(HAVE_CLOG10L) && defined(HAVE_LOG10L) && defined(HAVE_CABSL) && defined(HAVE_CARGL)
1001 #define HAVE_CLOG10L 1
1002 long double complex clog10l (long double complex z
);
1005 clog10l (long double complex z
)
1007 long double complex v
;
1009 COMPLEX_ASSIGN (v
, log10l (cabsl (z
)), cargl (z
));
1015 /* pow(base, power) = cexp (power * clog (base)) */
1016 #if !defined(HAVE_CPOWF)
1017 #define HAVE_CPOWF 1
1018 float complex cpowf (float complex base
, float complex power
);
1021 cpowf (float complex base
, float complex power
)
1023 return cexpf (power
* clogf (base
));
1027 #if !defined(HAVE_CPOW)
1029 double complex cpow (double complex base
, double complex power
);
1032 cpow (double complex base
, double complex power
)
1034 return cexp (power
* clog (base
));
1038 #if !defined(HAVE_CPOWL) && defined(HAVE_CEXPL) && defined(HAVE_CLOGL)
1039 #define HAVE_CPOWL 1
1040 long double complex cpowl (long double complex base
, long double complex power
);
1043 cpowl (long double complex base
, long double complex power
)
1045 return cexpl (power
* clogl (base
));
1050 /* sqrt(z). Algorithm pulled from glibc. */
1051 #if !defined(HAVE_CSQRTF)
1052 #define HAVE_CSQRTF 1
1053 float complex csqrtf (float complex z
);
1056 csqrtf (float complex z
)
1067 COMPLEX_ASSIGN (v
, 0, copysignf (sqrtf (-re
), im
));
1071 COMPLEX_ASSIGN (v
, fabsf (sqrtf (re
)), copysignf (0, im
));
1078 r
= sqrtf (0.5 * fabsf (im
));
1080 COMPLEX_ASSIGN (v
, r
, copysignf (r
, im
));
1086 d
= hypotf (re
, im
);
1087 /* Use the identity 2 Re res Im res = Im x
1088 to avoid cancellation error in d +/- Re x. */
1091 r
= sqrtf (0.5 * d
+ 0.5 * re
);
1096 s
= sqrtf (0.5 * d
- 0.5 * re
);
1097 r
= fabsf ((0.5 * im
) / s
);
1100 COMPLEX_ASSIGN (v
, r
, copysignf (s
, im
));
1106 #if !defined(HAVE_CSQRT)
1107 #define HAVE_CSQRT 1
1108 double complex csqrt (double complex z
);
1111 csqrt (double complex z
)
1122 COMPLEX_ASSIGN (v
, 0, copysign (sqrt (-re
), im
));
1126 COMPLEX_ASSIGN (v
, fabs (sqrt (re
)), copysign (0, im
));
1133 r
= sqrt (0.5 * fabs (im
));
1135 COMPLEX_ASSIGN (v
, r
, copysign (r
, im
));
1142 /* Use the identity 2 Re res Im res = Im x
1143 to avoid cancellation error in d +/- Re x. */
1146 r
= sqrt (0.5 * d
+ 0.5 * re
);
1151 s
= sqrt (0.5 * d
- 0.5 * re
);
1152 r
= fabs ((0.5 * im
) / s
);
1155 COMPLEX_ASSIGN (v
, r
, copysign (s
, im
));
1161 #if !defined(HAVE_CSQRTL) && defined(HAVE_COPYSIGNL) && defined(HAVE_SQRTL) && defined(HAVE_FABSL) && defined(HAVE_HYPOTL)
1162 #define HAVE_CSQRTL 1
1163 long double complex csqrtl (long double complex z
);
1166 csqrtl (long double complex z
)
1169 long double complex v
;
1177 COMPLEX_ASSIGN (v
, 0, copysignl (sqrtl (-re
), im
));
1181 COMPLEX_ASSIGN (v
, fabsl (sqrtl (re
)), copysignl (0, im
));
1188 r
= sqrtl (0.5 * fabsl (im
));
1190 COMPLEX_ASSIGN (v
, copysignl (r
, im
), r
);
1194 long double d
, r
, s
;
1196 d
= hypotl (re
, im
);
1197 /* Use the identity 2 Re res Im res = Im x
1198 to avoid cancellation error in d +/- Re x. */
1201 r
= sqrtl (0.5 * d
+ 0.5 * re
);
1206 s
= sqrtl (0.5 * d
- 0.5 * re
);
1207 r
= fabsl ((0.5 * im
) / s
);
1210 COMPLEX_ASSIGN (v
, r
, copysignl (s
, im
));
1217 /* sinh(a + i b) = sinh(a) cos(b) + i cosh(a) sin(b) */
1218 #if !defined(HAVE_CSINHF)
1219 #define HAVE_CSINHF 1
1220 float complex csinhf (float complex a
);
1223 csinhf (float complex a
)
1230 COMPLEX_ASSIGN (v
, sinhf (r
) * cosf (i
), coshf (r
) * sinf (i
));
1235 #if !defined(HAVE_CSINH)
1236 #define HAVE_CSINH 1
1237 double complex csinh (double complex a
);
1240 csinh (double complex a
)
1247 COMPLEX_ASSIGN (v
, sinh (r
) * cos (i
), cosh (r
) * sin (i
));
1252 #if !defined(HAVE_CSINHL) && defined(HAVE_COSL) && defined(HAVE_COSHL) && defined(HAVE_SINL) && defined(HAVE_SINHL)
1253 #define HAVE_CSINHL 1
1254 long double complex csinhl (long double complex a
);
1257 csinhl (long double complex a
)
1260 long double complex v
;
1264 COMPLEX_ASSIGN (v
, sinhl (r
) * cosl (i
), coshl (r
) * sinl (i
));
1270 /* cosh(a + i b) = cosh(a) cos(b) + i sinh(a) sin(b) */
1271 #if !defined(HAVE_CCOSHF)
1272 #define HAVE_CCOSHF 1
1273 float complex ccoshf (float complex a
);
1276 ccoshf (float complex a
)
1283 COMPLEX_ASSIGN (v
, coshf (r
) * cosf (i
), sinhf (r
) * sinf (i
));
1288 #if !defined(HAVE_CCOSH)
1289 #define HAVE_CCOSH 1
1290 double complex ccosh (double complex a
);
1293 ccosh (double complex a
)
1300 COMPLEX_ASSIGN (v
, cosh (r
) * cos (i
), sinh (r
) * sin (i
));
1305 #if !defined(HAVE_CCOSHL) && defined(HAVE_COSL) && defined(HAVE_COSHL) && defined(HAVE_SINL) && defined(HAVE_SINHL)
1306 #define HAVE_CCOSHL 1
1307 long double complex ccoshl (long double complex a
);
1310 ccoshl (long double complex a
)
1313 long double complex v
;
1317 COMPLEX_ASSIGN (v
, coshl (r
) * cosl (i
), sinhl (r
) * sinl (i
));
1323 /* tanh(a + i b) = (tanh(a) + i tan(b)) / (1 + i tanh(a) tan(b)) */
1324 #if !defined(HAVE_CTANHF)
1325 #define HAVE_CTANHF 1
1326 float complex ctanhf (float complex a
);
1329 ctanhf (float complex a
)
1334 rt
= tanhf (REALPART (a
));
1335 it
= tanf (IMAGPART (a
));
1336 COMPLEX_ASSIGN (n
, rt
, it
);
1337 COMPLEX_ASSIGN (d
, 1, rt
* it
);
1343 #if !defined(HAVE_CTANH)
1344 #define HAVE_CTANH 1
1345 double complex ctanh (double complex a
);
1347 ctanh (double complex a
)
1350 double complex n
, d
;
1352 rt
= tanh (REALPART (a
));
1353 it
= tan (IMAGPART (a
));
1354 COMPLEX_ASSIGN (n
, rt
, it
);
1355 COMPLEX_ASSIGN (d
, 1, rt
* it
);
1361 #if !defined(HAVE_CTANHL) && defined(HAVE_TANL) && defined(HAVE_TANHL)
1362 #define HAVE_CTANHL 1
1363 long double complex ctanhl (long double complex a
);
1366 ctanhl (long double complex a
)
1369 long double complex n
, d
;
1371 rt
= tanhl (REALPART (a
));
1372 it
= tanl (IMAGPART (a
));
1373 COMPLEX_ASSIGN (n
, rt
, it
);
1374 COMPLEX_ASSIGN (d
, 1, rt
* it
);
1381 /* sin(a + i b) = sin(a) cosh(b) + i cos(a) sinh(b) */
1382 #if !defined(HAVE_CSINF)
1383 #define HAVE_CSINF 1
1384 float complex csinf (float complex a
);
1387 csinf (float complex a
)
1394 COMPLEX_ASSIGN (v
, sinf (r
) * coshf (i
), cosf (r
) * sinhf (i
));
1399 #if !defined(HAVE_CSIN)
1401 double complex csin (double complex a
);
1404 csin (double complex a
)
1411 COMPLEX_ASSIGN (v
, sin (r
) * cosh (i
), cos (r
) * sinh (i
));
1416 #if !defined(HAVE_CSINL) && defined(HAVE_COSL) && defined(HAVE_COSHL) && defined(HAVE_SINL) && defined(HAVE_SINHL)
1417 #define HAVE_CSINL 1
1418 long double complex csinl (long double complex a
);
1421 csinl (long double complex a
)
1424 long double complex v
;
1428 COMPLEX_ASSIGN (v
, sinl (r
) * coshl (i
), cosl (r
) * sinhl (i
));
1434 /* cos(a + i b) = cos(a) cosh(b) - i sin(a) sinh(b) */
1435 #if !defined(HAVE_CCOSF)
1436 #define HAVE_CCOSF 1
1437 float complex ccosf (float complex a
);
1440 ccosf (float complex a
)
1447 COMPLEX_ASSIGN (v
, cosf (r
) * coshf (i
), - (sinf (r
) * sinhf (i
)));
1452 #if !defined(HAVE_CCOS)
1454 double complex ccos (double complex a
);
1457 ccos (double complex a
)
1464 COMPLEX_ASSIGN (v
, cos (r
) * cosh (i
), - (sin (r
) * sinh (i
)));
1469 #if !defined(HAVE_CCOSL) && defined(HAVE_COSL) && defined(HAVE_COSHL) && defined(HAVE_SINL) && defined(HAVE_SINHL)
1470 #define HAVE_CCOSL 1
1471 long double complex ccosl (long double complex a
);
1474 ccosl (long double complex a
)
1477 long double complex v
;
1481 COMPLEX_ASSIGN (v
, cosl (r
) * coshl (i
), - (sinl (r
) * sinhl (i
)));
1487 /* tan(a + i b) = (tan(a) + i tanh(b)) / (1 - i tan(a) tanh(b)) */
1488 #if !defined(HAVE_CTANF)
1489 #define HAVE_CTANF 1
1490 float complex ctanf (float complex a
);
1493 ctanf (float complex a
)
1498 rt
= tanf (REALPART (a
));
1499 it
= tanhf (IMAGPART (a
));
1500 COMPLEX_ASSIGN (n
, rt
, it
);
1501 COMPLEX_ASSIGN (d
, 1, - (rt
* it
));
1507 #if !defined(HAVE_CTAN)
1509 double complex ctan (double complex a
);
1512 ctan (double complex a
)
1515 double complex n
, d
;
1517 rt
= tan (REALPART (a
));
1518 it
= tanh (IMAGPART (a
));
1519 COMPLEX_ASSIGN (n
, rt
, it
);
1520 COMPLEX_ASSIGN (d
, 1, - (rt
* it
));
1526 #if !defined(HAVE_CTANL) && defined(HAVE_TANL) && defined(HAVE_TANHL)
1527 #define HAVE_CTANL 1
1528 long double complex ctanl (long double complex a
);
1531 ctanl (long double complex a
)
1534 long double complex n
, d
;
1536 rt
= tanl (REALPART (a
));
1537 it
= tanhl (IMAGPART (a
));
1538 COMPLEX_ASSIGN (n
, rt
, it
);
1539 COMPLEX_ASSIGN (d
, 1, - (rt
* it
));
1546 /* Complex ASIN. Returns wrongly NaN for infinite arguments.
1547 Algorithm taken from Abramowitz & Stegun. */
1549 #if !defined(HAVE_CASINF) && defined(HAVE_CLOGF) && defined(HAVE_CSQRTF)
1550 #define HAVE_CASINF 1
1551 complex float casinf (complex float z
);
1554 casinf (complex float z
)
1556 return -I
*clogf (I
*z
+ csqrtf (1.0f
-z
*z
));
1561 #if !defined(HAVE_CASIN) && defined(HAVE_CLOG) && defined(HAVE_CSQRT)
1562 #define HAVE_CASIN 1
1563 complex double casin (complex double z
);
1566 casin (complex double z
)
1568 return -I
*clog (I
*z
+ csqrt (1.0-z
*z
));
1573 #if !defined(HAVE_CASINL) && defined(HAVE_CLOGL) && defined(HAVE_CSQRTL)
1574 #define HAVE_CASINL 1
1575 complex long double casinl (complex long double z
);
1578 casinl (complex long double z
)
1580 return -I
*clogl (I
*z
+ csqrtl (1.0L-z
*z
));
1585 /* Complex ACOS. Returns wrongly NaN for infinite arguments.
1586 Algorithm taken from Abramowitz & Stegun. */
1588 #if !defined(HAVE_CACOSF) && defined(HAVE_CLOGF) && defined(HAVE_CSQRTF)
1589 #define HAVE_CACOSF 1
1590 complex float cacosf (complex float z
);
1593 cacosf (complex float z
)
1595 return -I
*clogf (z
+ I
*csqrtf (1.0f
-z
*z
));
1600 #if !defined(HAVE_CACOS) && defined(HAVE_CLOG) && defined(HAVE_CSQRT)
1601 #define HAVE_CACOS 1
1602 complex double cacos (complex double z
);
1605 cacos (complex double z
)
1607 return -I
*clog (z
+ I
*csqrt (1.0-z
*z
));
1612 #if !defined(HAVE_CACOSL) && defined(HAVE_CLOGL) && defined(HAVE_CSQRTL)
1613 #define HAVE_CACOSL 1
1614 complex long double cacosl (complex long double z
);
1617 cacosl (complex long double z
)
1619 return -I
*clogl (z
+ I
*csqrtl (1.0L-z
*z
));
1624 /* Complex ATAN. Returns wrongly NaN for infinite arguments.
1625 Algorithm taken from Abramowitz & Stegun. */
1627 #if !defined(HAVE_CATANF) && defined(HAVE_CLOGF)
1628 #define HAVE_CACOSF 1
1629 complex float catanf (complex float z
);
1632 catanf (complex float z
)
1634 return I
*clogf ((I
+z
)/(I
-z
))/2.0f
;
1639 #if !defined(HAVE_CATAN) && defined(HAVE_CLOG)
1640 #define HAVE_CACOS 1
1641 complex double catan (complex double z
);
1644 catan (complex double z
)
1646 return I
*clog ((I
+z
)/(I
-z
))/2.0;
1651 #if !defined(HAVE_CATANL) && defined(HAVE_CLOGL)
1652 #define HAVE_CACOSL 1
1653 complex long double catanl (complex long double z
);
1656 catanl (complex long double z
)
1658 return I
*clogl ((I
+z
)/(I
-z
))/2.0L;
1663 /* Complex ASINH. Returns wrongly NaN for infinite arguments.
1664 Algorithm taken from Abramowitz & Stegun. */
1666 #if !defined(HAVE_CASINHF) && defined(HAVE_CLOGF) && defined(HAVE_CSQRTF)
1667 #define HAVE_CASINHF 1
1668 complex float casinhf (complex float z
);
1671 casinhf (complex float z
)
1673 return clogf (z
+ csqrtf (z
*z
+1.0f
));
1678 #if !defined(HAVE_CASINH) && defined(HAVE_CLOG) && defined(HAVE_CSQRT)
1679 #define HAVE_CASINH 1
1680 complex double casinh (complex double z
);
1683 casinh (complex double z
)
1685 return clog (z
+ csqrt (z
*z
+1.0));
1690 #if !defined(HAVE_CASINHL) && defined(HAVE_CLOGL) && defined(HAVE_CSQRTL)
1691 #define HAVE_CASINHL 1
1692 complex long double casinhl (complex long double z
);
1695 casinhl (complex long double z
)
1697 return clogl (z
+ csqrtl (z
*z
+1.0L));
1702 /* Complex ACOSH. Returns wrongly NaN for infinite arguments.
1703 Algorithm taken from Abramowitz & Stegun. */
1705 #if !defined(HAVE_CACOSHF) && defined(HAVE_CLOGF) && defined(HAVE_CSQRTF)
1706 #define HAVE_CACOSHF 1
1707 complex float cacoshf (complex float z
);
1710 cacoshf (complex float z
)
1712 return clogf (z
+ csqrtf (z
-1.0f
) * csqrtf (z
+1.0f
));
1717 #if !defined(HAVE_CACOSH) && defined(HAVE_CLOG) && defined(HAVE_CSQRT)
1718 #define HAVE_CACOSH 1
1719 complex double cacosh (complex double z
);
1722 cacosh (complex double z
)
1724 return clog (z
+ csqrt (z
-1.0) * csqrt (z
+1.0));
1729 #if !defined(HAVE_CACOSHL) && defined(HAVE_CLOGL) && defined(HAVE_CSQRTL)
1730 #define HAVE_CACOSHL 1
1731 complex long double cacoshl (complex long double z
);
1734 cacoshl (complex long double z
)
1736 return clogl (z
+ csqrtl (z
-1.0L) * csqrtl (z
+1.0L));
1741 /* Complex ATANH. Returns wrongly NaN for infinite arguments.
1742 Algorithm taken from Abramowitz & Stegun. */
1744 #if !defined(HAVE_CATANHF) && defined(HAVE_CLOGF)
1745 #define HAVE_CATANHF 1
1746 complex float catanhf (complex float z
);
1749 catanhf (complex float z
)
1751 return clogf ((1.0f
+z
)/(1.0f
-z
))/2.0f
;
1756 #if !defined(HAVE_CATANH) && defined(HAVE_CLOG)
1757 #define HAVE_CATANH 1
1758 complex double catanh (complex double z
);
1761 catanh (complex double z
)
1763 return clog ((1.0+z
)/(1.0-z
))/2.0;
1767 #if !defined(HAVE_CATANHL) && defined(HAVE_CLOGL)
1768 #define HAVE_CATANHL 1
1769 complex long double catanhl (complex long double z
);
1772 catanhl (complex long double z
)
1774 return clogl ((1.0L+z
)/(1.0L-z
))/2.0L;
1779 #if !defined(HAVE_TGAMMA)
1780 #define HAVE_TGAMMA 1
1781 double tgamma (double);
1783 /* Fallback tgamma() function. Uses the algorithm from
1784 http://www.netlib.org/specfun/gamma and references therein. */
1787 #define SQRTPI 0.9189385332046727417803297
1790 #define PI 3.1415926535897932384626434
1796 double fact
, res
, sum
, xden
, xnum
, y
, y1
, ysq
, z
;
1798 static double p
[8] = {
1799 -1.71618513886549492533811e0
, 2.47656508055759199108314e1
,
1800 -3.79804256470945635097577e2
, 6.29331155312818442661052e2
,
1801 8.66966202790413211295064e2
, -3.14512729688483675254357e4
,
1802 -3.61444134186911729807069e4
, 6.64561438202405440627855e4
};
1804 static double q
[8] = {
1805 -3.08402300119738975254353e1
, 3.15350626979604161529144e2
,
1806 -1.01515636749021914166146e3
, -3.10777167157231109440444e3
,
1807 2.25381184209801510330112e4
, 4.75584627752788110767815e3
,
1808 -1.34659959864969306392456e5
, -1.15132259675553483497211e5
};
1810 static double c
[7] = { -1.910444077728e-03,
1811 8.4171387781295e-04, -5.952379913043012e-04,
1812 7.93650793500350248e-04, -2.777777777777681622553e-03,
1813 8.333333333333333331554247e-02, 5.7083835261e-03 };
1815 static const double xminin
= 2.23e-308;
1816 static const double xbig
= 171.624;
1817 static const double xnan
= __builtin_nan ("0x0"), xinf
= __builtin_inf ();
1818 static double eps
= 0;
1821 eps
= nextafter (1., 2.) - 1.;
1839 if (y1
!= trunc (y1
*0.5l)*2)
1841 fact
= -PI
/ sin (PI
*res
);
1845 return x
== 0 ? copysign (xinf
, x
) : xnan
;
1872 for (i
= 0; i
< 8; i
++)
1874 xnum
= (xnum
+ p
[i
]) * z
;
1875 xden
= xden
* z
+ q
[i
];
1878 res
= xnum
/ xden
+ 1;
1883 for (i
= 1; i
<= n
; i
++)
1895 for (i
= 0; i
< 6; i
++)
1896 sum
= sum
/ ysq
+ c
[i
];
1898 sum
= sum
/y
- y
+ SQRTPI
;
1899 sum
= sum
+ (y
- 0.5) * log (y
);
1903 return x
< 0 ? xnan
: xinf
;
1917 #if !defined(HAVE_LGAMMA)
1918 #define HAVE_LGAMMA 1
1919 double lgamma (double);
1921 /* Fallback lgamma() function. Uses the algorithm from
1922 http://www.netlib.org/specfun/algama and references therein,
1923 except for negative arguments (where netlib would return +Inf)
1924 where we use the following identity:
1925 lgamma(y) = log(pi/(|y*sin(pi*y)|)) - lgamma(-y)
1933 #define SQRTPI 0.9189385332046727417803297
1936 #define PI 3.1415926535897932384626434
1938 #define PNT68 0.6796875
1939 #define D1 -0.5772156649015328605195174
1940 #define D2 0.4227843350984671393993777
1941 #define D4 1.791759469228055000094023
1943 static double p1
[8] = {
1944 4.945235359296727046734888e0
, 2.018112620856775083915565e2
,
1945 2.290838373831346393026739e3
, 1.131967205903380828685045e4
,
1946 2.855724635671635335736389e4
, 3.848496228443793359990269e4
,
1947 2.637748787624195437963534e4
, 7.225813979700288197698961e3
};
1948 static double q1
[8] = {
1949 6.748212550303777196073036e1
, 1.113332393857199323513008e3
,
1950 7.738757056935398733233834e3
, 2.763987074403340708898585e4
,
1951 5.499310206226157329794414e4
, 6.161122180066002127833352e4
,
1952 3.635127591501940507276287e4
, 8.785536302431013170870835e3
};
1953 static double p2
[8] = {
1954 4.974607845568932035012064e0
, 5.424138599891070494101986e2
,
1955 1.550693864978364947665077e4
, 1.847932904445632425417223e5
,
1956 1.088204769468828767498470e6
, 3.338152967987029735917223e6
,
1957 5.106661678927352456275255e6
, 3.074109054850539556250927e6
};
1958 static double q2
[8] = {
1959 1.830328399370592604055942e2
, 7.765049321445005871323047e3
,
1960 1.331903827966074194402448e5
, 1.136705821321969608938755e6
,
1961 5.267964117437946917577538e6
, 1.346701454311101692290052e7
,
1962 1.782736530353274213975932e7
, 9.533095591844353613395747e6
};
1963 static double p4
[8] = {
1964 1.474502166059939948905062e4
, 2.426813369486704502836312e6
,
1965 1.214755574045093227939592e8
, 2.663432449630976949898078e9
,
1966 2.940378956634553899906876e10
, 1.702665737765398868392998e11
,
1967 4.926125793377430887588120e11
, 5.606251856223951465078242e11
};
1968 static double q4
[8] = {
1969 2.690530175870899333379843e3
, 6.393885654300092398984238e5
,
1970 4.135599930241388052042842e7
, 1.120872109616147941376570e9
,
1971 1.488613728678813811542398e10
, 1.016803586272438228077304e11
,
1972 3.417476345507377132798597e11
, 4.463158187419713286462081e11
};
1973 static double c
[7] = {
1974 -1.910444077728e-03, 8.4171387781295e-04,
1975 -5.952379913043012e-04, 7.93650793500350248e-04,
1976 -2.777777777777681622553e-03, 8.333333333333333331554247e-02,
1979 static double xbig
= 2.55e305
, xinf
= __builtin_inf (), eps
= 0,
1983 double corr
, res
, xden
, xm1
, xm2
, xm4
, xnum
, ysq
;
1986 eps
= __builtin_nextafter (1., 2.) - 1.;
1988 if ((y
> 0) && (y
<= xbig
))
2002 xm1
= (y
- 0.5) - 0.5;
2005 if ((y
<= 0.5) || (y
>= PNT68
))
2009 for (i
= 0; i
< 8; i
++)
2011 xnum
= xnum
*xm1
+ p1
[i
];
2012 xden
= xden
*xm1
+ q1
[i
];
2014 res
= corr
+ (xm1
* (D1
+ xm1
*(xnum
/xden
)));
2018 xm2
= (y
- 0.5) - 0.5;
2021 for (i
= 0; i
< 8; i
++)
2023 xnum
= xnum
*xm2
+ p2
[i
];
2024 xden
= xden
*xm2
+ q2
[i
];
2026 res
= corr
+ xm2
* (D2
+ xm2
*(xnum
/xden
));
2034 for (i
= 0; i
< 8; i
++)
2036 xnum
= xnum
*xm2
+ p2
[i
];
2037 xden
= xden
*xm2
+ q2
[i
];
2039 res
= xm2
* (D2
+ xm2
*(xnum
/xden
));
2046 for (i
= 0; i
< 8; i
++)
2048 xnum
= xnum
*xm4
+ p4
[i
];
2049 xden
= xden
*xm4
+ q4
[i
];
2051 res
= D4
+ xm4
*(xnum
/xden
);
2060 for (i
= 0; i
< 6; i
++)
2061 res
= res
/ ysq
+ c
[i
];
2065 res
= res
+ SQRTPI
- 0.5*corr
;
2066 res
= res
+ y
*(corr
-1);
2069 else if (y
< 0 && __builtin_floor (y
) != y
)
2071 /* lgamma(y) = log(pi/(|y*sin(pi*y)|)) - lgamma(-y)
2072 For abs(y) very close to zero, we use a series expansion to
2073 the first order in y to avoid overflow. */
2075 res
= -2 * log (fabs (y
)) - lgamma (-y
);
2077 res
= log (PI
/ fabs (y
* sin (PI
* y
))) - lgamma (-y
);
2087 #if defined(HAVE_TGAMMA) && !defined(HAVE_TGAMMAF)
2088 #define HAVE_TGAMMAF 1
2089 float tgammaf (float);
2094 return (float) tgamma ((double) x
);
2098 #if defined(HAVE_LGAMMA) && !defined(HAVE_LGAMMAF)
2099 #define HAVE_LGAMMAF 1
2100 float lgammaf (float);
2105 return (float) lgamma ((double) x
);