1 /* Implementation of various C99 functions
2 Copyright (C) 2004 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 2 of the License, or (at your option) any later version.
11 In addition to the permissions in the GNU General Public License, the
12 Free Software Foundation gives you unlimited permission to link the
13 compiled version of this file into combinations with other programs,
14 and to distribute those combinations without any restriction coming
15 from the use of this file. (The General Public License restrictions
16 do apply in other respects; for example, they cover modification of
17 the file, and distribution when not linked into a combine
20 Libgfortran is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public
26 License along with libgfortran; see the file COPYING. If not,
27 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
28 Boston, MA 02110-1301, USA. */
31 #include <sys/types.h>
35 #define C99_PROTOS_H WE_DONT_WANT_PROTOS_NOW
36 #include "libgfortran.h"
44 return (float) acos(x);
53 return (float) asin(x);
60 atan2f(float y, float x)
62 return (float) atan2(y, x);
71 return (float) atan(x);
80 return (float) ceil(x);
84 #ifndef HAVE_COPYSIGNF
85 #define HAVE_COPYSIGNF
87 copysignf(float x, float y)
89 return (float) copysign(x, y);
98 return (float) cos(x);
107 return (float) cosh(x);
116 return (float) exp(x);
125 return (float) fabs(x);
134 return (float) floor(x);
141 frexpf(float x, int *exp)
143 return (float) frexp(x, exp);
150 hypotf(float x, float y)
152 return (float) hypot(x, y);
161 return (float) log(x);
170 return (float) log10(x);
177 scalbn(double x, int y)
179 return x * pow(FLT_RADIX, y);
186 scalbnf(float x, int y)
188 return (float) scalbn(x, y);
197 return (float) sin(x);
206 return (float) sinh(x);
215 return (float) sqrt(x);
224 return (float) tan(x);
233 return (float) tanh(x);
257 return (float) trunc (x);
261 #ifndef HAVE_NEXTAFTERF
262 #define HAVE_NEXTAFTERF
263 /* This is a portable implementation of nextafterf that is intended to be
264 independent of the floating point format or its in memory representation.
265 This implementation works correctly with denormalized values. */
267 nextafterf(float x, float y)
269 /* This variable is marked volatile to avoid excess precision problems
270 on some platforms, including IA-32. */
271 volatile float delta;
272 float absx, denorm_min;
274 if (isnan(x) || isnan(y))
279 return x > 0 ? __FLT_MAX__ : - __FLT_MAX__;
281 /* absx = fabsf (x); */
282 absx = (x < 0.0) ? -x : x;
284 /* __FLT_DENORM_MIN__ is non-zero iff the target supports denormals. */
285 if (__FLT_DENORM_MIN__ == 0.0f)
286 denorm_min = __FLT_MIN__;
288 denorm_min = __FLT_DENORM_MIN__;
290 if (absx < __FLT_MIN__)
297 /* Discard the fraction from x. */
298 frac = frexpf (absx, &exp);
299 delta = scalbnf (0.5f, exp);
301 /* Scale x by the epsilon of the representation. By rights we should
302 have been able to combine this with scalbnf, but some targets don't
303 get that correct with denormals. */
304 delta *= __FLT_EPSILON__;
306 /* If we're going to be reducing the absolute value of X, and doing so
307 would reduce the exponent of X, then the delta to be applied is
308 one exponent smaller. */
309 if (frac == 0.5f && (y < x) == (x > 0))
312 /* If that underflows to zero, then we're back to the minimum. */
328 powf(float x, float y)
330 return (float) pow(x, y);
334 /* Note that if fpclassify is not defined, then NaN is not handled */
336 /* Algorithm by Steven G. Kargl. */
340 /* Round to nearest integral value. If the argument is halfway between two
341 integral values then round away from zero. */
347 #if defined(fpclassify)
350 if (i == FP_INFINITE || i == FP_NAN)
373 /* Round to nearest integral value. If the argument is halfway between two
374 integral values then round away from zero. */
380 #if defined(fpclassify)
384 if (i == FP_INFINITE || i == FP_NAN)
407 /* log10 function for long double variables. The version provided here
408 reduces the argument until it fits into a double, then use log10. */
410 log10l(long double x)
412 #if LDBL_MAX_EXP > DBL_MAX_EXP
417 if (x > 0x1p16383L) { p2_result += 16383; x /= 0x1p16383L; }
418 if (x > 0x1p8191L) { p2_result += 8191; x /= 0x1p8191L; }
419 if (x > 0x1p4095L) { p2_result += 4095; x /= 0x1p4095L; }
420 if (x > 0x1p2047L) { p2_result += 2047; x /= 0x1p2047L; }
421 if (x > 0x1p1023L) { p2_result += 1023; x /= 0x1p1023L; }
422 val = log10 ((double) x);
423 return (val + p2_result * .30102999566398119521373889472449302L);
426 #if LDBL_MIN_EXP < DBL_MIN_EXP
431 if (x < 0x1p-16380L) { p2_result += 16380; x /= 0x1p-16380L; }
432 if (x < 0x1p-8189L) { p2_result += 8189; x /= 0x1p-8189L; }
433 if (x < 0x1p-4093L) { p2_result += 4093; x /= 0x1p-4093L; }
434 if (x < 0x1p-2045L) { p2_result += 2045; x /= 0x1p-2045L; }
435 if (x < 0x1p-1021L) { p2_result += 1021; x /= 0x1p-1021L; }
436 val = fabs(log10 ((double) x));
437 return (- val - p2_result * .30102999566398119521373889472449302L);
445 #if !defined(HAVE_CABSF)
448 cabsf (float complex z)
450 return hypotf (REALPART (z), IMAGPART (z));
454 #if !defined(HAVE_CABS)
457 cabs (double complex z)
459 return hypot (REALPART (z), IMAGPART (z));
463 #if !defined(HAVE_CABSL) && defined(HAVE_HYPOTL)
466 cabsl (long double complex z)
468 return hypotl (REALPART (z), IMAGPART (z));
473 #if !defined(HAVE_CARGF)
476 cargf (float complex z)
478 return atan2f (IMAGPART (z), REALPART (z));
482 #if !defined(HAVE_CARG)
485 carg (double complex z)
487 return atan2 (IMAGPART (z), REALPART (z));
491 #if !defined(HAVE_CARGL) && defined(HAVE_ATAN2L)
494 cargl (long double complex z)
496 return atan2l (IMAGPART (z), REALPART (z));
501 /* exp(z) = exp(a)*(cos(b) + i sin(b)) */
502 #if !defined(HAVE_CEXPF)
505 cexpf (float complex z)
512 COMPLEX_ASSIGN (v, cosf (b), sinf (b));
517 #if !defined(HAVE_CEXP)
520 cexp (double complex z)
527 COMPLEX_ASSIGN (v, cos (b), sin (b));
532 #if !defined(HAVE_CEXPL) && defined(HAVE_COSL) && defined(HAVE_SINL) && defined(EXPL)
535 cexpl (long double complex z)
538 long double complex v;
542 COMPLEX_ASSIGN (v, cosl (b), sinl (b));
548 /* log(z) = log (cabs(z)) + i*carg(z) */
549 #if !defined(HAVE_CLOGF)
552 clogf (float complex z)
556 COMPLEX_ASSIGN (v, logf (cabsf (z)), cargf (z));
561 #if !defined(HAVE_CLOG)
564 clog (double complex z)
568 COMPLEX_ASSIGN (v, log (cabs (z)), carg (z));
573 #if !defined(HAVE_CLOGL) && defined(HAVE_LOGL) && defined(HAVE_CABSL) && defined(HAVE_CARGL)
576 clogl (long double complex z)
578 long double complex v;
580 COMPLEX_ASSIGN (v, logl (cabsl (z)), cargl (z));
586 /* log10(z) = log10 (cabs(z)) + i*carg(z) */
587 #if !defined(HAVE_CLOG10F)
590 clog10f (float complex z)
594 COMPLEX_ASSIGN (v, log10f (cabsf (z)), cargf (z));
599 #if !defined(HAVE_CLOG10)
602 clog10 (double complex z)
606 COMPLEX_ASSIGN (v, log10 (cabs (z)), carg (z));
611 #if !defined(HAVE_CLOG10L) && defined(HAVE_LOG10L) && defined(HAVE_CABSL) && defined(HAVE_CARGL)
614 clog10l (long double complex z)
616 long double complex v;
618 COMPLEX_ASSIGN (v, log10l (cabsl (z)), cargl (z));
624 /* pow(base, power) = cexp (power * clog (base)) */
625 #if !defined(HAVE_CPOWF)
628 cpowf (float complex base, float complex power)
630 return cexpf (power * clogf (base));
634 #if !defined(HAVE_CPOW)
637 cpow (double complex base, double complex power)
639 return cexp (power * clog (base));
643 #if !defined(HAVE_CPOWL) && defined(HAVE_CEXPL) && defined(HAVE_CLOGL)
646 cpowl (long double complex base, long double complex power)
648 return cexpl (power * clogl (base));
653 /* sqrt(z). Algorithm pulled from glibc. */
654 #if !defined(HAVE_CSQRTF)
657 csqrtf (float complex z)
668 COMPLEX_ASSIGN (v, 0, copysignf (sqrtf (-re), im));
672 COMPLEX_ASSIGN (v, fabsf (sqrtf (re)), copysignf (0, im));
679 r = sqrtf (0.5 * fabsf (im));
681 COMPLEX_ASSIGN (v, copysignf (r, im), r);
688 /* Use the identity 2 Re res Im res = Im x
689 to avoid cancellation error in d +/- Re x. */
692 r = sqrtf (0.5 * d + 0.5 * re);
697 s = sqrtf (0.5 * d - 0.5 * re);
698 r = fabsf ((0.5 * im) / s);
701 COMPLEX_ASSIGN (v, r, copysignf (s, im));
707 #if !defined(HAVE_CSQRT)
710 csqrt (double complex z)
721 COMPLEX_ASSIGN (v, 0, copysign (sqrt (-re), im));
725 COMPLEX_ASSIGN (v, fabs (sqrt (re)), copysign (0, im));
732 r = sqrt (0.5 * fabs (im));
734 COMPLEX_ASSIGN (v, copysign (r, im), r);
741 /* Use the identity 2 Re res Im res = Im x
742 to avoid cancellation error in d +/- Re x. */
745 r = sqrt (0.5 * d + 0.5 * re);
750 s = sqrt (0.5 * d - 0.5 * re);
751 r = fabs ((0.5 * im) / s);
754 COMPLEX_ASSIGN (v, r, copysign (s, im));
760 #if !defined(HAVE_CSQRTL) && defined(HAVE_COPYSIGNL) && defined(HAVE_SQRTL) && defined(HAVE_FABSL) && defined(HAVE_HYPOTL)
763 csqrtl (long double complex z)
766 long double complex v;
774 COMPLEX_ASSIGN (v, 0, copysignl (sqrtl (-re), im));
778 COMPLEX_ASSIGN (v, fabsl (sqrtl (re)), copysignl (0, im));
785 r = sqrtl (0.5 * fabsl (im));
787 COMPLEX_ASSIGN (v, copysignl (r, im), r);
794 /* Use the identity 2 Re res Im res = Im x
795 to avoid cancellation error in d +/- Re x. */
798 r = sqrtl (0.5 * d + 0.5 * re);
803 s = sqrtl (0.5 * d - 0.5 * re);
804 r = fabsl ((0.5 * im) / s);
807 COMPLEX_ASSIGN (v, r, copysignl (s, im));
814 /* sinh(a + i b) = sinh(a) cos(b) + i cosh(a) sin(b) */
815 #if !defined(HAVE_CSINHF)
818 csinhf (float complex a)
825 COMPLEX_ASSIGN (v, sinhf (r) * cosf (i), coshf (r) * sinf (i));
830 #if !defined(HAVE_CSINH)
833 csinh (double complex a)
840 COMPLEX_ASSIGN (v, sinh (r) * cos (i), cosh (r) * sin (i));
845 #if !defined(HAVE_CSINHL) && defined(HAVE_COSL) && defined(HAVE_COSHL) && defined(HAVE_SINL) && defined(HAVE_SINHL)
848 csinhl (long double complex a)
851 long double complex v;
855 COMPLEX_ASSIGN (v, sinhl (r) * cosl (i), coshl (r) * sinl (i));
861 /* cosh(a + i b) = cosh(a) cos(b) - i sinh(a) sin(b) */
862 #if !defined(HAVE_CCOSHF)
865 ccoshf (float complex a)
872 COMPLEX_ASSIGN (v, coshf (r) * cosf (i), - (sinhf (r) * sinf (i)));
877 #if !defined(HAVE_CCOSH)
880 ccosh (double complex a)
887 COMPLEX_ASSIGN (v, cosh (r) * cos (i), - (sinh (r) * sin (i)));
892 #if !defined(HAVE_CCOSHL) && defined(HAVE_COSL) && defined(HAVE_COSHL) && defined(HAVE_SINL) && defined(HAVE_SINHL)
895 ccoshl (long double complex a)
898 long double complex v;
902 COMPLEX_ASSIGN (v, coshl (r) * cosl (i), - (sinhl (r) * sinl (i)));
908 /* tanh(a + i b) = (tanh(a) + i tan(b)) / (1 - i tanh(a) tan(b)) */
909 #if !defined(HAVE_CTANHF)
912 ctanhf (float complex a)
917 rt = tanhf (REALPART (a));
918 it = tanf (IMAGPART (a));
919 COMPLEX_ASSIGN (n, rt, it);
920 COMPLEX_ASSIGN (d, 1, - (rt * it));
926 #if !defined(HAVE_CTANH)
929 ctanh (double complex a)
934 rt = tanh (REALPART (a));
935 it = tan (IMAGPART (a));
936 COMPLEX_ASSIGN (n, rt, it);
937 COMPLEX_ASSIGN (d, 1, - (rt * it));
943 #if !defined(HAVE_CTANHL) && defined(HAVE_TANL) && defined(HAVE_TANHL)
946 ctanhl (long double complex a)
949 long double complex n, d;
951 rt = tanhl (REALPART (a));
952 it = tanl (IMAGPART (a));
953 COMPLEX_ASSIGN (n, rt, it);
954 COMPLEX_ASSIGN (d, 1, - (rt * it));
961 /* sin(a + i b) = sin(a) cosh(b) + i cos(a) sinh(b) */
962 #if !defined(HAVE_CSINF)
965 csinf (float complex a)
972 COMPLEX_ASSIGN (v, sinf (r) * coshf (i), cosf (r) * sinhf (i));
977 #if !defined(HAVE_CSIN)
980 csin (double complex a)
987 COMPLEX_ASSIGN (v, sin (r) * cosh (i), cos (r) * sinh (i));
992 #if !defined(HAVE_CSINL) && defined(HAVE_COSL) && defined(HAVE_COSHL) && defined(HAVE_SINL) && defined(HAVE_SINHL)
995 csinl (long double complex a)
998 long double complex v;
1002 COMPLEX_ASSIGN (v, sinl (r) * coshl (i), cosl (r) * sinhl (i));
1008 /* cos(a + i b) = cos(a) cosh(b) - i sin(a) sinh(b) */
1009 #if !defined(HAVE_CCOSF)
1012 ccosf (float complex a)
1019 COMPLEX_ASSIGN (v, cosf (r) * coshf (i), - (sinf (r) * sinhf (i)));
1024 #if !defined(HAVE_CCOS)
1027 ccos (double complex a)
1034 COMPLEX_ASSIGN (v, cos (r) * cosh (i), - (sin (r) * sinh (i)));
1039 #if !defined(HAVE_CCOSL) && defined(HAVE_COSL) && defined(HAVE_COSHL) && defined(HAVE_SINL) && defined(HAVE_SINHL)
1042 ccosl (long double complex a)
1045 long double complex v;
1049 COMPLEX_ASSIGN (v, cosl (r) * coshl (i), - (sinl (r) * sinhl (i)));
1055 /* tan(a + i b) = (tan(a) + i tanh(b)) / (1 - i tan(a) tanh(b)) */
1056 #if !defined(HAVE_CTANF)
1059 ctanf (float complex a)
1064 rt = tanf (REALPART (a));
1065 it = tanhf (IMAGPART (a));
1066 COMPLEX_ASSIGN (n, rt, it);
1067 COMPLEX_ASSIGN (d, 1, - (rt * it));
1073 #if !defined(HAVE_CTAN)
1076 ctan (double complex a)
1079 double complex n, d;
1081 rt = tan (REALPART (a));
1082 it = tanh (IMAGPART (a));
1083 COMPLEX_ASSIGN (n, rt, it);
1084 COMPLEX_ASSIGN (d, 1, - (rt * it));
1090 #if !defined(HAVE_CTANL) && defined(HAVE_TANL) && defined(HAVE_TANHL)
1093 ctanl (long double complex a)
1096 long double complex n, d;
1098 rt = tanl (REALPART (a));
1099 it = tanhl (IMAGPART (a));
1100 COMPLEX_ASSIGN (n, rt, it);
1101 COMPLEX_ASSIGN (d, 1, - (rt * it));