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])
18 AC_CACHE_CHECK([whether expl() can be used without linking with libm],
19 [gl_cv_func_expl_no_libm],
23 [[#ifndef __NO_MATH_INLINES
24 # define __NO_MATH_INLINES 1 /* for glibc */
27 long double (* volatile funcptr) (long double) = expl;
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])
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],
42 [[#ifndef __NO_MATH_INLINES
43 # define __NO_MATH_INLINES 1 /* for glibc */
46 long double (* volatile funcptr) (long double) = expl;
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])
54 if test $gl_cv_func_expl_in_libm = yes; then
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],
69 LIBS="$LIBS $EXPL_LIBM"
72 #ifndef __NO_MATH_INLINES
73 # define __NO_MATH_INLINES 1 /* for glibc */
77 /* Override the values of <float.h>, like done in float.in.h. */
78 #if defined __i386__ && (defined __BEOS__ || defined __OpenBSD__)
80 # define LDBL_MANT_DIG 64
82 # define LDBL_MIN_EXP (-16381)
84 # define LDBL_MAX_EXP 16384
86 #if defined __i386__ && (defined __FreeBSD__ || defined __DragonFly__)
88 # define LDBL_MANT_DIG 64
90 # define LDBL_MIN_EXP (-16381)
92 # define LDBL_MAX_EXP 16384
94 #if (defined _ARCH_PPC || defined _POWER) && defined _AIX && (LDBL_MANT_DIG == 106) && defined __GNUC__
96 # define LDBL_MIN_EXP DBL_MIN_EXP
98 #if defined __sgi && (LDBL_MANT_DIG >= 106)
100 # define LDBL_MANT_DIG 106
101 # if defined __GNUC__
103 # define LDBL_MIN_EXP DBL_MIN_EXP
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;
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. */
120 volatile long double x1 = -1.0;
121 if (expl (x1) == 0.0)
124 /* On OpenBSD 5.4 the system's native expl() is buggy:
125 it returns 'nan' for small values. */
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)))
133 /* This test fails on musl 1.2.2/arm64, musl 1.2.2/s390x, NetBSD 9.3. */
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))
149 [gl_cv_func_expl_works=yes],
150 [gl_cv_func_expl_works=no],
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" ;;
164 case "$gl_cv_func_expl_works" in
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"
179 AC_REQUIRE([gl_FUNC_ISNANL])
180 AC_REQUIRE([gl_FUNC_ROUNDL])
181 AC_REQUIRE([gl_FUNC_LDEXPL])
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" ;;
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" ;;
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" ;;
200 AC_SUBST([EXPL_LIBM])