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