immutable: Add tests.
[gnulib.git] / m4 / expm1l.m4
blob0eaaf844ba893cbbb78164a5bc394a1716ef6657
1 # expm1l.m4 serial 7
2 dnl Copyright (C) 2010-2021 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
7 AC_DEFUN([gl_FUNC_EXPM1L],
9   AC_REQUIRE([gl_MATH_H_DEFAULTS])
10   AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
12   dnl Persuade glibc <math.h> to declare expm1l().
13   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
15   EXPM1L_LIBM=
16   AC_CACHE_CHECK([whether expm1l() can be used without linking with libm],
17     [gl_cv_func_expm1l_no_libm],
18     [
19       AC_LINK_IFELSE(
20         [AC_LANG_PROGRAM(
21            [[#ifndef __NO_MATH_INLINES
22              # define __NO_MATH_INLINES 1 /* for glibc */
23              #endif
24              #include <math.h>
25              extern
26              #ifdef __cplusplus
27              "C"
28              #endif
29              long double expm1l (long double);
30              long double (* volatile funcptr) (long double) = expm1l;
31              long double x;]],
32            [[return funcptr (x) > 0.5
33                     || expm1l (x) > 0.5;]])],
34         [gl_cv_func_expm1l_no_libm=yes],
35         [gl_cv_func_expm1l_no_libm=no])
36     ])
37   if test $gl_cv_func_expm1l_no_libm = no; then
38     AC_CACHE_CHECK([whether expm1l() can be used with libm],
39       [gl_cv_func_expm1l_in_libm],
40       [
41         save_LIBS="$LIBS"
42         LIBS="$LIBS -lm"
43         AC_LINK_IFELSE(
44           [AC_LANG_PROGRAM(
45              [[#ifndef __NO_MATH_INLINES
46                # define __NO_MATH_INLINES 1 /* for glibc */
47                #endif
48                #include <math.h>
49                extern
50                #ifdef __cplusplus
51                "C"
52                #endif
53                long double expm1l (long double);
54                long double (* volatile funcptr) (long double) = expm1l;
55                long double x;]],
56              [[return funcptr (x) > 0.5
57                       || expm1l (x) > 0.5;]])],
58           [gl_cv_func_expm1l_in_libm=yes],
59           [gl_cv_func_expm1l_in_libm=no])
60         LIBS="$save_LIBS"
61       ])
62     if test $gl_cv_func_expm1l_in_libm = yes; then
63       EXPM1L_LIBM=-lm
64     fi
65   fi
66   if test $gl_cv_func_expm1l_no_libm = yes \
67      || test $gl_cv_func_expm1l_in_libm = yes; then
68     HAVE_EXPM1L=1
69     dnl Also check whether it's declared.
70     dnl IRIX 6.5 has expm1l() in libm but doesn't declare it in <math.h>.
71     AC_CHECK_DECL([expm1l], , [HAVE_DECL_EXPM1L=0], [[#include <math.h>]])
72     if test $REPLACE_EXPM1L = 0; then
73       AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
74       AC_CACHE_CHECK([whether expm1l works],
75         [gl_cv_func_expm1l_works],
76         [
77           save_LIBS="$LIBS"
78           LIBS="$LIBS $EXPM1L_LIBM"
79           AC_RUN_IFELSE(
80             [AC_LANG_SOURCE([[
81 #ifndef __NO_MATH_INLINES
82 # define __NO_MATH_INLINES 1 /* for glibc */
83 #endif
84 #include <float.h>
85 #include <math.h>
86 /* Override the values of <float.h>, like done in float.in.h.  */
87 #if defined __i386__ && (defined __BEOS__ || defined __OpenBSD__)
88 # undef LDBL_MANT_DIG
89 # define LDBL_MANT_DIG   64
90 # undef LDBL_MIN_EXP
91 # define LDBL_MIN_EXP    (-16381)
92 # undef LDBL_MAX_EXP
93 # define LDBL_MAX_EXP    16384
94 #endif
95 #if defined __i386__ && (defined __FreeBSD__ || defined __DragonFly__)
96 # undef LDBL_MANT_DIG
97 # define LDBL_MANT_DIG   64
98 # undef LDBL_MIN_EXP
99 # define LDBL_MIN_EXP    (-16381)
100 # undef LDBL_MAX_EXP
101 # define LDBL_MAX_EXP    16384
102 #endif
103 #if (defined _ARCH_PPC || defined _POWER) && defined _AIX && (LDBL_MANT_DIG == 106) && defined __GNUC__
104 # undef LDBL_MIN_EXP
105 # define LDBL_MIN_EXP DBL_MIN_EXP
106 #endif
107 #if defined __sgi && (LDBL_MANT_DIG >= 106)
108 # undef LDBL_MANT_DIG
109 # define LDBL_MANT_DIG 106
110 # if defined __GNUC__
111 #  undef LDBL_MIN_EXP
112 #  define LDBL_MIN_EXP DBL_MIN_EXP
113 # endif
114 #endif
115 #undef expm1l
116 extern
117 #ifdef __cplusplus
119 #endif
120 long double expm1l (long double);
121 static long double dummy (long double x) { return 0; }
122 int main (int argc, char *argv[])
124   long double (* volatile my_expm1l) (long double) = argc ? expm1l : dummy;
125   int result = 0;
126   /* This test fails on Mac OS X 10.5, NetBSD 8.0.  */
127   {
128     const long double TWO_LDBL_MANT_DIG = /* 2^LDBL_MANT_DIG */
129       (long double) (1U << ((LDBL_MANT_DIG - 1) / 5))
130       * (long double) (1U << ((LDBL_MANT_DIG - 1 + 1) / 5))
131       * (long double) (1U << ((LDBL_MANT_DIG - 1 + 2) / 5))
132       * (long double) (1U << ((LDBL_MANT_DIG - 1 + 3) / 5))
133       * (long double) (1U << ((LDBL_MANT_DIG - 1 + 4) / 5));
134     long double x = 11.358L;
135     long double y = my_expm1l (x);
136     long double z = my_expm1l (- x);
137     long double err = (y + (1.0L + y) * z) * TWO_LDBL_MANT_DIG;
138     if (!(err >= -100.0L && err <= 100.0L))
139       result |= 1;
140   }
141   return result;
143             ]])],
144             [gl_cv_func_expm1l_works=yes],
145             [gl_cv_func_expm1l_works=no],
146             [case "$host_os" in
147                               # Guess yes on glibc systems.
148                *-gnu* | gnu*) gl_cv_func_expm1l_works="guessing yes" ;;
149                               # Guess yes on musl systems.
150                *-musl*)       gl_cv_func_expm1l_works="guessing yes" ;;
151                               # Guess yes on native Windows.
152                mingw*)        gl_cv_func_expm1l_works="guessing yes" ;;
153                               # If we don't know, obey --enable-cross-guesses.
154                *)             gl_cv_func_expm1l_works="$gl_cross_guess_normal" ;;
155              esac
156             ])
157           LIBS="$save_LIBS"
158         ])
159       case "$gl_cv_func_expm1l_works" in
160         *yes) ;;
161         *) REPLACE_EXPM1L=1 ;;
162       esac
163     fi
164   else
165     HAVE_EXPM1L=0
166     HAVE_DECL_EXPM1L=0
167   fi
168   if test $HAVE_EXPM1L = 0 || test $REPLACE_EXPM1L = 1; then
169     dnl Find libraries needed to link lib/expm1l.c.
170     if test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 1; then
171       AC_REQUIRE([gl_FUNC_EXPM1])
172       EXPM1L_LIBM="$EXPM1_LIBM"
173     else
174       AC_REQUIRE([gl_FUNC_ISNANL])
175       AC_REQUIRE([gl_FUNC_EXPL])
176       AC_REQUIRE([gl_FUNC_ROUNDL])
177       AC_REQUIRE([gl_FUNC_LDEXPL])
178       EXPM1L_LIBM=
179       dnl Append $ISNANL_LIBM to EXPM1L_LIBM, avoiding gratuitous duplicates.
180       case " $EXPM1L_LIBM " in
181         *" $ISNANL_LIBM "*) ;;
182         *) EXPM1L_LIBM="$EXPM1L_LIBM $ISNANL_LIBM" ;;
183       esac
184       dnl Append $EXPL_LIBM to EXPM1L_LIBM, avoiding gratuitous duplicates.
185       case " $EXPM1L_LIBM " in
186         *" $EXPL_LIBM "*) ;;
187         *) EXPM1L_LIBM="$EXPM1L_LIBM $EXPL_LIBM" ;;
188       esac
189       dnl Append $ROUNDL_LIBM to EXPM1L_LIBM, avoiding gratuitous duplicates.
190       case " $EXPM1L_LIBM " in
191         *" $ROUNDL_LIBM "*) ;;
192         *) EXPM1L_LIBM="$EXPM1L_LIBM $ROUNDL_LIBM" ;;
193       esac
194       dnl Append $LDEXPL_LIBM to EXPM1L_LIBM, avoiding gratuitous duplicates.
195       case " $EXPM1L_LIBM " in
196         *" $LDEXPL_LIBM "*) ;;
197         *) EXPM1L_LIBM="$EXPM1L_LIBM $LDEXPL_LIBM" ;;
198       esac
199     fi
200   fi
201   AC_SUBST([EXPM1L_LIBM])