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_LOG2L],
10 AC_REQUIRE([gl_MATH_H_DEFAULTS])
11 AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
12 AC_REQUIRE([gl_FUNC_LOG2])
14 dnl Persuade glibc <math.h> to declare log2l().
15 AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
17 dnl Test whether log2l() exists. Assume that log2l(), if it exists, is
18 dnl defined in the same library as log2().
20 LIBS="$LIBS $LOG2_LIBM"
21 gl_CHECK_FUNCS_ANDROID([log2l], [[#include <math.h>]])
23 if test $ac_cv_func_log2l = yes; then
24 LOG2L_LIBM="$LOG2_LIBM"
26 dnl Also check whether it's declared.
27 dnl IRIX 6.5 has log2l() in libm but doesn't declare it in <math.h>.
28 AC_CHECK_DECL([log2l], , [HAVE_DECL_LOG2L=0], [[#include <math.h>]])
31 LIBS="$LIBS $LOG2L_LIBM"
34 case "$gl_cv_func_log2l_works" in
41 case "$gl_cv_onwards_func_log2l" in
42 future*) REPLACE_LOG2L=1 ;;
45 if test $HAVE_LOG2L = 0 || test $REPLACE_LOG2L = 1; then
46 dnl Find libraries needed to link lib/log2l.c.
47 if test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 1; then
48 LOG2L_LIBM="$LOG2_LIBM"
50 AC_REQUIRE([gl_FUNC_ISNANL])
51 AC_REQUIRE([gl_FUNC_FREXPL])
52 AC_REQUIRE([gl_FUNC_LOGL])
54 dnl Append $ISNANL_LIBM to LOG2L_LIBM, avoiding gratuitous duplicates.
55 case " $LOG2L_LIBM " in
56 *" $ISNANL_LIBM "*) ;;
57 *) LOG2L_LIBM="$LOG2L_LIBM $ISNANL_LIBM" ;;
59 dnl Append $FREXPL_LIBM to LOG2L_LIBM, avoiding gratuitous duplicates.
60 case " $LOG2L_LIBM " in
61 *" $FREXPL_LIBM "*) ;;
62 *) LOG2L_LIBM="$LOG2L_LIBM $FREXPL_LIBM" ;;
64 dnl Append $LOGL_LIBM to LOG2L_LIBM, avoiding gratuitous duplicates.
65 case " $LOG2L_LIBM " in
67 *) LOG2L_LIBM="$LOG2L_LIBM $LOGL_LIBM" ;;
71 AC_SUBST([LOG2L_LIBM])
74 dnl Test whether log2l() works.
75 dnl On OSF/1 5.1, log2l(-0.0) is NaN.
76 dnl On musl 1.2.2/{arm64,s390x}, the result is accurate to only 16 digits.
77 dnl On musl 1.2.2/{arm64,s390x} and NetBSD 10.0, the result is Infinity for
78 dnl some large finite arguments.
79 AC_DEFUN([gl_FUNC_LOG2L_WORKS],
81 AC_REQUIRE([AC_PROG_CC])
82 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
83 AC_CACHE_CHECK([whether log2l works], [gl_cv_func_log2l_works],
87 #ifndef __NO_MATH_INLINES
88 # define __NO_MATH_INLINES 1 /* for glibc */
92 /* Override the values of <float.h>, like done in float.in.h. */
93 #if defined __i386__ && (defined __BEOS__ || defined __OpenBSD__)
95 # define LDBL_MANT_DIG 64
97 # define LDBL_MIN_EXP (-16381)
99 # define LDBL_MAX_EXP 16384
101 #if defined __i386__ && (defined __FreeBSD__ || defined __DragonFly__)
102 # undef LDBL_MANT_DIG
103 # define LDBL_MANT_DIG 64
105 # define LDBL_MIN_EXP (-16381)
107 # define LDBL_MAX_EXP 16384
109 #if (defined _ARCH_PPC || defined _POWER) && defined _AIX && (LDBL_MANT_DIG == 106) && defined __GNUC__
111 # define LDBL_MIN_EXP DBL_MIN_EXP
113 #if defined __sgi && (LDBL_MANT_DIG >= 106)
114 # undef LDBL_MANT_DIG
115 # define LDBL_MANT_DIG 106
116 # if defined __GNUC__
118 # define LDBL_MIN_EXP DBL_MIN_EXP
121 #ifndef log2l /* for AIX */
126 long double log2l (long double);
128 static long double dummy (long double x) { return 0; }
129 volatile long double gx;
130 volatile long double gy;
131 int main (int argc, char *argv[])
133 long double (* volatile my_log2l) (long double) = argc ? log2l : dummy;
135 /* This test fails on OSF/1 5.1. */
139 if (!(gy + gy == gy))
142 /* This test fails on musl 1.2.2/arm64, musl 1.2.2/s390x. */
144 const long double TWO_LDBL_MANT_DIG = /* 2^LDBL_MANT_DIG */
145 (long double) (1U << ((LDBL_MANT_DIG - 1) / 5))
146 * (long double) (1U << ((LDBL_MANT_DIG - 1 + 1) / 5))
147 * (long double) (1U << ((LDBL_MANT_DIG - 1 + 2) / 5))
148 * (long double) (1U << ((LDBL_MANT_DIG - 1 + 3) / 5))
149 * (long double) (1U << ((LDBL_MANT_DIG - 1 + 4) / 5));
150 long double x = 11.358L;
151 long double y = my_log2l (x);
152 long double z = my_log2l (1.0L / x);
153 long double err = (y + z) * TWO_LDBL_MANT_DIG;
154 if (!(err >= -10000.0L && err <= 10000.0L))
157 /* This test fails on musl 1.2.2/arm64, musl 1.2.2/s390x, NetBSD 10.0. */
158 if (DBL_MAX_EXP < LDBL_MAX_EXP)
160 long double x = ldexpl (1.0L, DBL_MAX_EXP); /* finite! */
161 long double y = my_log2l (x);
162 if (y > 0 && y + y == y) /* infinite? */
168 [gl_cv_func_log2l_works=yes],
169 [gl_cv_func_log2l_works=no],
171 # Guess yes on glibc systems.
172 *-gnu* | gnu*) gl_cv_func_log2l_works="guessing yes" ;;
173 # Guess no on musl systems.
174 *-musl* | midipix*) gl_cv_func_log2l_works="guessing no" ;;
175 # Guess no on NetBSD and OSF/1.
176 netbsd* | osf*) gl_cv_func_log2l_works="guessing no" ;;
177 # Guess yes on native Windows.
178 mingw* | windows*) gl_cv_func_log2l_works="guessing yes" ;;
179 # If we don't know, obey --enable-cross-guesses.
180 *) gl_cv_func_log2l_works="$gl_cross_guess_normal" ;;