assert-h, verify: Fix compilation error with g++ (4.8.5) -std=gnu++11.
[gnulib.git] / m4 / ilogb.m4
blob622d621e5ed253cec9ba3660bc98c53f74864186
1 # ilogb.m4
2 # serial 10
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_ILOGB],
10   AC_REQUIRE([gl_MATH_H_DEFAULTS])
12   dnl Determine ILOGB_LIBM.
13   gl_MATHFUNC([ilogb], [int], [(double)])
14   if test $gl_cv_func_ilogb_no_libm = yes \
15      || test $gl_cv_func_ilogb_in_libm = yes; then
16     saved_LIBS="$LIBS"
17     LIBS="$LIBS $ILOGB_LIBM"
18     gl_FUNC_ILOGB_WORKS
19     LIBS="$saved_LIBS"
20     case "$gl_cv_func_ilogb_works" in
21       *yes) ;;
22       *) REPLACE_ILOGB=1 ;;
23     esac
24   else
25     HAVE_ILOGB=0
26   fi
27   if test $HAVE_ILOGB = 0 || test $REPLACE_ILOGB = 1; then
28     dnl Find libraries needed to link lib/ilogb.c.
29     AC_REQUIRE([gl_FUNC_FREXP])
30     AC_REQUIRE([gl_FUNC_ISNAND])
31     ILOGB_LIBM=
32     dnl Append $FREXP_LIBM to ILOGB_LIBM, avoiding gratuitous duplicates.
33     case " $ILOGB_LIBM " in
34       *" $FREXP_LIBM "*) ;;
35       *) ILOGB_LIBM="$ILOGB_LIBM $FREXP_LIBM" ;;
36     esac
37     dnl Append $ISNAND_LIBM to ILOGB_LIBM, avoiding gratuitous duplicates.
38     case " $ILOGB_LIBM " in
39       *" $ISNAND_LIBM "*) ;;
40       *) ILOGB_LIBM="$ILOGB_LIBM $ISNAND_LIBM" ;;
41     esac
42   fi
43   AC_SUBST([ILOGB_LIBM])
46 dnl Test whether ilogb() works.
47 dnl On OpenBSD 6.7, AIX 5.1, ilogb(0.0) is wrong.
48 dnl On Mac OS X 10.5 in 64-bit mode and on AIX 7.1 in 64-bit mode,
49 dnl ilogb(2^(DBL_MIN_EXP-1)) is wrong.
50 dnl On NetBSD 7.1, OpenBSD 6.7, ilogb(Infinity) is wrong.
51 dnl On NetBSD 7.1, OpenBSD 6.7, ilogb(NaN) is wrong.
52 AC_DEFUN([gl_FUNC_ILOGB_WORKS],
54   AC_REQUIRE([AC_PROG_CC])
55   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
56   AC_CACHE_CHECK([whether ilogb works], [gl_cv_func_ilogb_works],
57     [
58       AC_RUN_IFELSE(
59         [AC_LANG_SOURCE([[
60 #include <float.h>
61 #include <limits.h>
62 #include <math.h>
63 /* Provide FP_ILOGB0, FP_ILOGBNAN, like in math.in.h.  */
64 #if defined FP_ILOGB0 && defined FP_ILOGBNAN
65 # if defined __HAIKU__
66   /* Haiku: match what ilogb() does */
67 #  undef FP_ILOGB0
68 #  undef FP_ILOGBNAN
69 #  define FP_ILOGB0   (- 2147483647 - 1) /* INT_MIN */
70 #  define FP_ILOGBNAN (- 2147483647 - 1) /* INT_MIN */
71 # endif
72 #else
73 # if defined __NetBSD__ || defined __sgi
74   /* NetBSD, IRIX 6.5: match what ilogb() does */
75 #  define FP_ILOGB0   INT_MIN
76 #  define FP_ILOGBNAN INT_MIN
77 # elif defined _AIX
78   /* AIX 5.1: match what ilogb() does in AIX >= 5.2 */
79 #  define FP_ILOGB0   INT_MIN
80 #  define FP_ILOGBNAN INT_MAX
81 # elif defined __sun
82   /* Solaris 9: match what ilogb() does */
83 #  define FP_ILOGB0   (- INT_MAX)
84 #  define FP_ILOGBNAN INT_MAX
85 # endif
86 #endif
87 volatile double x;
88 static double zero;
89 static int dummy (double x) { return 0; }
90 int main (int argc, char *argv[])
92   int (* volatile my_ilogb) (double) = argc ? ilogb : dummy;
93   int result = 0;
94   /* This test fails on OpenBSD 6.7, AIX 5.1.  */
95   {
96     x = 0.0;
97     if (my_ilogb (x) != FP_ILOGB0)
98       result |= 1;
99   }
100   /* This test fails on Mac OS X 10.5 in 64-bit mode and on
101      AIX 7.1 in 64-bit mode.  */
102   {
103     int i;
104     x = 0.5;
105     for (i = DBL_MIN_EXP - 1; i < 0; i++)
106       x = x * 0.5;
107     if (x > 0.0 && my_ilogb (x) != DBL_MIN_EXP - 2)
108       result |= 2;
109   }
110   /* This test fails on NetBSD 7.1, OpenBSD 6.7.  */
111   {
112     x = 1.0 / zero;
113     if (my_ilogb (x) != INT_MAX)
114       result |= 4;
115   }
116   /* This test fails on NetBSD 7.1, OpenBSD 6.7.  */
117   {
118     x = zero / zero;
119     if (my_ilogb (x) != FP_ILOGBNAN)
120       result |= 8;
121   }
122   return result;
124 ]])],
125         [gl_cv_func_ilogb_works=yes],
126         [gl_cv_func_ilogb_works=no],
127         [case "$host_os" in
128            aix* | openbsd* | netbsd* | solaris*)
129               gl_cv_func_ilogb_works="guessing no" ;;
130               # Guess yes on native Windows.
131            mingw* | windows*)
132               gl_cv_func_ilogb_works="guessing yes" ;;
133            *) gl_cv_func_ilogb_works="guessing yes" ;;
134          esac
135         ])
136     ])