sys_socket tests: Improve tests for macro definitions.
[gnulib.git] / m4 / expl.m4
bloba6fd526696241aef421c077f58186798f8a60f8d
1 # expl.m4
2 # serial 22
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_EXPL],
10   AC_REQUIRE([gl_MATH_H_DEFAULTS])
11   AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
12   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
14   dnl Persuade glibc <math.h> to declare expl().
15   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
17   EXPL_LIBM=
18   AC_CACHE_CHECK([whether expl() can be used without linking with libm],
19     [gl_cv_func_expl_no_libm],
20     [
21       AC_LINK_IFELSE(
22         [AC_LANG_PROGRAM(
23            [[#ifndef __NO_MATH_INLINES
24              # define __NO_MATH_INLINES 1 /* for glibc */
25              #endif
26              #include <math.h>
27              long double (* volatile funcptr) (long double) = expl;
28              long double x;]],
29            [[return funcptr (x) > 1.5
30                     || expl (x) > 1.5;]])],
31         [gl_cv_func_expl_no_libm=yes],
32         [gl_cv_func_expl_no_libm=no])
33     ])
34   if test $gl_cv_func_expl_no_libm = no; then
35     AC_CACHE_CHECK([whether expl() can be used with libm],
36       [gl_cv_func_expl_in_libm],
37       [
38         saved_LIBS="$LIBS"
39         LIBS="$LIBS -lm"
40         AC_LINK_IFELSE(
41           [AC_LANG_PROGRAM(
42              [[#ifndef __NO_MATH_INLINES
43                # define __NO_MATH_INLINES 1 /* for glibc */
44                #endif
45                #include <math.h>
46                long double (* volatile funcptr) (long double) = expl;
47                long double x;]],
48              [[return funcptr (x) > 1.5
49                       || expl (x) > 1.5;]])],
50           [gl_cv_func_expl_in_libm=yes],
51           [gl_cv_func_expl_in_libm=no])
52         LIBS="$saved_LIBS"
53       ])
54     if test $gl_cv_func_expl_in_libm = yes; then
55       EXPL_LIBM=-lm
56     fi
57   fi
58   if test $gl_cv_func_expl_no_libm = yes \
59      || test $gl_cv_func_expl_in_libm = yes; then
60     dnl Also check whether it's declared.
61     dnl Mac OS X 10.3 has expl() in libc but doesn't declare it in <math.h>.
62     AC_CHECK_DECL([expl], , [HAVE_DECL_EXPL=0], [[#include <math.h>]])
63     if test $REPLACE_EXPL = 0; then
64       AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
65       AC_CACHE_CHECK([whether expl works],
66         [gl_cv_func_expl_works],
67         [
68           saved_LIBS="$LIBS"
69           LIBS="$LIBS $EXPL_LIBM"
70           AC_RUN_IFELSE(
71             [AC_LANG_SOURCE([[
72 #ifndef __NO_MATH_INLINES
73 # define __NO_MATH_INLINES 1 /* for glibc */
74 #endif
75 #include <float.h>
76 #include <math.h>
77 /* Override the values of <float.h>, like done in float.in.h.  */
78 #if defined __i386__ && (defined __BEOS__ || defined __OpenBSD__)
79 # undef LDBL_MANT_DIG
80 # define LDBL_MANT_DIG   64
81 # undef LDBL_MIN_EXP
82 # define LDBL_MIN_EXP    (-16381)
83 # undef LDBL_MAX_EXP
84 # define LDBL_MAX_EXP    16384
85 #endif
86 #if defined __i386__ && (defined __FreeBSD__ || defined __DragonFly__)
87 # undef LDBL_MANT_DIG
88 # define LDBL_MANT_DIG   64
89 # undef LDBL_MIN_EXP
90 # define LDBL_MIN_EXP    (-16381)
91 # undef LDBL_MAX_EXP
92 # define LDBL_MAX_EXP    16384
93 #endif
94 #if (defined _ARCH_PPC || defined _POWER) && defined _AIX && (LDBL_MANT_DIG == 106) && defined __GNUC__
95 # undef LDBL_MIN_EXP
96 # define LDBL_MIN_EXP DBL_MIN_EXP
97 #endif
98 #if defined __sgi && (LDBL_MANT_DIG >= 106)
99 # undef LDBL_MANT_DIG
100 # define LDBL_MANT_DIG 106
101 # if defined __GNUC__
102 #  undef LDBL_MIN_EXP
103 #  define LDBL_MIN_EXP DBL_MIN_EXP
104 # endif
105 #endif
106 #undef expl
107 extern
108 #ifdef __cplusplus
110 #endif
111 long double expl (long double);
112 static long double dummy (long double x) { return 0; }
113 int main (int argc, char *argv[])
115   long double (* volatile my_expl) (long double) = argc ? expl : dummy;
116   int result = 0;
117   /* On Haiku 2017 the system's native expl() is just a stub: it returns 0.0
118      and prints "__expl not implemented" for all arguments.  */
119   {
120     volatile long double x1 = -1.0;
121     if (expl (x1) == 0.0)
122       result |= 1;
123   }
124   /* On OpenBSD 5.4 the system's native expl() is buggy:
125      it returns 'nan' for small values.  */
126   {
127     volatile long double x1 = -1.0;
128     volatile long double x2 = -0.8;
129     volatile long double x3 = -0.4;
130     if (isnan (expl (x1)) || isnan (expl (x2)) || isnan (expl (x3)))
131       result |= 2;
132   }
133   /* This test fails on musl 1.2.2/arm64, musl 1.2.2/s390x, NetBSD 9.3.  */
134   {
135     const long double TWO_LDBL_MANT_DIG = /* 2^LDBL_MANT_DIG */
136       (long double) (1U << ((LDBL_MANT_DIG - 1) / 5))
137       * (long double) (1U << ((LDBL_MANT_DIG - 1 + 1) / 5))
138       * (long double) (1U << ((LDBL_MANT_DIG - 1 + 2) / 5))
139       * (long double) (1U << ((LDBL_MANT_DIG - 1 + 3) / 5))
140       * (long double) (1U << ((LDBL_MANT_DIG - 1 + 4) / 5));
141     long double x = 11.358L;
142     long double err = (my_expl (x) * my_expl (- x) - 1.0L) * TWO_LDBL_MANT_DIG;
143     if (!(err >= -100.0L && err <= 100.0L))
144       result |= 4;
145   }
146   return result;
148             ]])],
149             [gl_cv_func_expl_works=yes],
150             [gl_cv_func_expl_works=no],
151             [case "$host_os" in
152                                    # Guess yes on glibc systems.
153                *-gnu* | gnu*)      gl_cv_func_expl_works="guessing yes" ;;
154                                    # Guess no on musl systems.
155                *-musl* | midipix*) gl_cv_func_expl_works="guessing no" ;;
156                                    # Guess yes on native Windows.
157                mingw* | windows*)  gl_cv_func_expl_works="guessing yes" ;;
158                                    # If we don't know, obey --enable-cross-guesses.
159                *)                  gl_cv_func_expl_works="$gl_cross_guess_normal" ;;
160              esac
161             ])
162           LIBS="$saved_LIBS"
163         ])
164       case "$gl_cv_func_expl_works" in
165         *yes) ;;
166         *) REPLACE_EXPL=1 ;;
167       esac
168     fi
169   else
170     HAVE_DECL_EXPL=0
171     HAVE_EXPL=0
172   fi
173   if test $HAVE_EXPL = 0 || test $REPLACE_EXPL = 1; then
174     dnl Find libraries needed to link lib/expl.c.
175     if test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 1; then
176       AC_REQUIRE([gl_FUNC_EXP])
177       EXPL_LIBM="$EXP_LIBM"
178     else
179       AC_REQUIRE([gl_FUNC_ISNANL])
180       AC_REQUIRE([gl_FUNC_ROUNDL])
181       AC_REQUIRE([gl_FUNC_LDEXPL])
182       EXPL_LIBM=
183       dnl Append $ISNANL_LIBM to EXPL_LIBM, avoiding gratuitous duplicates.
184       case " $EXPL_LIBM " in
185         *" $ISNANL_LIBM "*) ;;
186         *) EXPL_LIBM="$EXPL_LIBM $ISNANL_LIBM" ;;
187       esac
188       dnl Append $ROUNDL_LIBM to EXPL_LIBM, avoiding gratuitous duplicates.
189       case " $EXPL_LIBM " in
190         *" $ROUNDL_LIBM "*) ;;
191         *) EXPL_LIBM="$EXPL_LIBM $ROUNDL_LIBM" ;;
192       esac
193       dnl Append $LDEXPL_LIBM to EXPL_LIBM, avoiding gratuitous duplicates.
194       case " $EXPL_LIBM " in
195         *" $LDEXPL_LIBM "*) ;;
196         *) EXPL_LIBM="$EXPL_LIBM $LDEXPL_LIBM" ;;
197       esac
198     fi
199   fi
200   AC_SUBST([EXPL_LIBM])