havelib: Fix for Solaris 11 OpenIndiana and Solaris 11 OmniOS.
[gnulib.git] / m4 / ilogbl.m4
blob7de37e71aa718108c291ff95b10135be2eb4c0a3
1 # ilogbl.m4 serial 4
2 dnl Copyright (C) 2010-2020 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_ILOGBL],
9   AC_REQUIRE([gl_MATH_H_DEFAULTS])
10   AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
11   AC_REQUIRE([gl_FUNC_ILOGB])
13   dnl Persuade glibc <math.h> to declare ilogbl().
14   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
16   dnl Test whether ilogbl() exists. Assume that ilogbl(), if it exists, is
17   dnl defined in the same library as ilogb().
18   save_LIBS="$LIBS"
19   LIBS="$LIBS $ILOGB_LIBM"
20   AC_CACHE_CHECK([for ilogbl],
21     [gl_cv_func_ilogbl],
22     [
23       AC_LINK_IFELSE(
24         [AC_LANG_PROGRAM(
25            [[#ifndef __NO_MATH_INLINES
26              # define __NO_MATH_INLINES 1 /* for glibc */
27              #endif
28              #include <math.h>
29              int (*funcptr) (long double) = ilogbl;
30              long double x;]],
31            [[return (funcptr (x) % 2) == 0
32                     || (ilogbl (x) % 2) == 0;]])],
33         [gl_cv_func_ilogbl=yes],
34         [gl_cv_func_ilogbl=no])
35     ])
36   LIBS="$save_LIBS"
37   if test $gl_cv_func_ilogbl = yes; then
38     ILOGBL_LIBM="$ILOGB_LIBM"
39     save_LIBS="$LIBS"
40     LIBS="$LIBS $ILOGBL_LIBM"
41     gl_FUNC_ILOGBL_WORKS
42     LIBS="$save_LIBS"
43     case "$gl_cv_func_ilogbl_works" in
44       *yes) ;;
45       *) REPLACE_ILOGBL=1 ;;
46     esac
47   else
48     HAVE_ILOGBL=0
49   fi
50   if test $HAVE_ILOGBL = 0 || test $REPLACE_ILOGBL = 1; then
51     dnl Find libraries needed to link lib/ilogbl.c.
52     if test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 1; then
53       ILOGBL_LIBM="$ILOGB_LIBM"
54     else
55       AC_REQUIRE([gl_FUNC_FREXPL])
56       AC_REQUIRE([gl_FUNC_ISNANL])
57       ILOGBL_LIBM=
58       dnl Append $FREXPL_LIBM to ILOGBL_LIBM, avoiding gratuitous duplicates.
59       case " $ILOGBL_LIBM " in
60         *" $FREXPL_LIBM "*) ;;
61         *) ILOGBL_LIBM="$ILOGBL_LIBM $FREXPL_LIBM" ;;
62       esac
63       dnl Append $ISNANL_LIBM to ILOGBL_LIBM, avoiding gratuitous duplicates.
64       case " $ILOGBL_LIBM " in
65         *" $ISNANL_LIBM "*) ;;
66         *) ILOGBL_LIBM="$ILOGBL_LIBM $ISNANL_LIBM" ;;
67       esac
68     fi
69   fi
70   AC_SUBST([ILOGBL_LIBM])
73 dnl Test whether ilogbl() works.
74 dnl On Cygwin 2.9, ilogb(0.0L) is wrong.
75 dnl On Haiku 2017, it returns i-2 instead of i-1 for values between
76 dnl ca. 2^-16444 and ca. 2^-16382.
77 AC_DEFUN([gl_FUNC_ILOGBL_WORKS],
79   AC_REQUIRE([AC_PROG_CC])
80   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
81   AC_CACHE_CHECK([whether ilogbl works], [gl_cv_func_ilogbl_works],
82     [
83       AC_RUN_IFELSE(
84         [AC_LANG_SOURCE([[
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_MIN_EXP
90 # define LDBL_MIN_EXP    (-16381)
91 #endif
92 #if defined __i386__ && (defined __FreeBSD__ || defined __DragonFly__)
93 # undef LDBL_MIN_EXP
94 # define LDBL_MIN_EXP    (-16381)
95 #endif
96 #if (defined _ARCH_PPC || defined _POWER) && defined _AIX && (LDBL_MANT_DIG == 106) && defined __GNUC__
97 # undef LDBL_MIN_EXP
98 # define LDBL_MIN_EXP DBL_MIN_EXP
99 #endif
100 #if defined __sgi && (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 volatile long double x;
107 static int dummy (long double x) { return 0; }
108 int main (int argc, char *argv[])
110   int (* volatile my_ilogbl) (long double) = argc ? ilogbl : dummy;
111   int result = 0;
112   /* This test fails on Cygwin 2.9.  */
113   {
114     x = 0.0L;
115     if (my_ilogbl (x) != FP_ILOGB0)
116       result |= 1;
117   }
118   /* This test fails on Haiku 2017.  */
119   {
120     int i;
121     for (i = 1, x = (long double)1.0; i >= LDBL_MIN_EXP-100 && x > (long double)0.0; i--, x *= (long double)0.5)
122       if (my_ilogbl (x) != i - 1)
123         {
124           result |= 2;
125           break;
126         }
127   }
128   return result;
130 ]])],
131         [gl_cv_func_ilogbl_works=yes],
132         [gl_cv_func_ilogbl_works=no],
133         [case "$host_os" in
134            haiku*) gl_cv_func_ilogbl_works="guessing no" ;;
135                    # Guess yes on native Windows.
136            mingw*) gl_cv_func_ilogbl_works="guessing yes" ;;
137            *)      gl_cv_func_ilogbl_works="guessing yes" ;;
138          esac
139         ])
140     ])