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