immutable: Add tests.
[gnulib.git] / m4 / ilogbf.m4
blobb78f97e1438b42f8bb7c7f79cb55e49d4736063b
1 # ilogbf.m4 serial 6
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_ILOGBF],
9   AC_REQUIRE([gl_MATH_H_DEFAULTS])
11   dnl Persuade glibc <math.h> to declare ilogbf().
12   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
14   dnl Determine ILOGBF_LIBM.
15   gl_MATHFUNC([ilogbf], [int], [(float)])
16   if test $gl_cv_func_ilogbf_no_libm = yes \
17      || test $gl_cv_func_ilogbf_in_libm = yes; then
18     save_LIBS="$LIBS"
19     LIBS="$LIBS $ILOGBF_LIBM"
20     gl_FUNC_ILOGBF_WORKS
21     LIBS="$save_LIBS"
22     case "$gl_cv_func_ilogbf_works" in
23       *yes) ;;
24       *) REPLACE_ILOGBF=1 ;;
25     esac
26   else
27     HAVE_ILOGBF=0
28   fi
29   if test $HAVE_ILOGBF = 0 || test $REPLACE_ILOGBF = 1; then
30     dnl Find libraries needed to link lib/ilogbf.c.
31     AC_REQUIRE([gl_FUNC_FREXPF])
32     AC_REQUIRE([gl_FUNC_ISNANF])
33     ILOGBF_LIBM=
34     dnl Append $FREXPF_LIBM to ILOGBF_LIBM, avoiding gratuitous duplicates.
35     case " $ILOGBF_LIBM " in
36       *" $FREXPF_LIBM "*) ;;
37       *) ILOGBF_LIBM="$ILOGBF_LIBM $FREXPF_LIBM" ;;
38     esac
39     dnl Append $ISNANF_LIBM to ILOGBF_LIBM, avoiding gratuitous duplicates.
40     case " $ILOGBF_LIBM " in
41       *" $ISNANF_LIBM "*) ;;
42       *) ILOGBF_LIBM="$ILOGBF_LIBM $ISNANF_LIBM" ;;
43     esac
44   fi
45   AC_SUBST([ILOGBF_LIBM])
48 dnl Test whether ilogbf() works.
49 dnl On OpenBSD 6.7, ilogbf(0.0) is wrong.
50 dnl On NetBSD 7.1, OpenBSD 6.7, ilogbf(Infinity) is wrong.
51 AC_DEFUN([gl_FUNC_ILOGBF_WORKS],
53   AC_REQUIRE([AC_PROG_CC])
54   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
55   AC_CACHE_CHECK([whether ilogbf works], [gl_cv_func_ilogbf_works],
56     [
57       AC_RUN_IFELSE(
58         [AC_LANG_SOURCE([[
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 ilogbf() does */
73 #  define FP_ILOGB0   INT_MIN
74 #  define FP_ILOGBNAN INT_MIN
75 # elif defined _AIX
76   /* AIX 5.1: match what ilogbf() 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 ilogbf() does */
81 #  define FP_ILOGB0   (- INT_MAX)
82 #  define FP_ILOGBNAN INT_MAX
83 # endif
84 #endif
85 volatile float x;
86 static float zero;
87 static int dummy (float x) { return 0; }
88 int main (int argc, char *argv[])
90   int (* volatile my_ilogbf) (float) = argc ? ilogbf : dummy;
91   int result = 0;
92   /* This test fails on OpenBSD 6.7.  */
93   {
94     x = 0.0f;
95     if (my_ilogbf (x) != FP_ILOGB0)
96       result |= 1;
97   }
98   /* This test fails on NetBSD 7.1, OpenBSD 6.7.  */
99   {
100     x = 1.0f / zero;
101     if (my_ilogbf (x) != INT_MAX)
102       result |= 2;
103   }
104   return result;
106 ]])],
107         [gl_cv_func_ilogbf_works=yes],
108         [gl_cv_func_ilogbf_works=no],
109         [case "$host_os" in
110            openbsd* | netbsd*)
111                    gl_cv_func_ilogbf_works="guessing no" ;;
112                    # Guess yes on native Windows.
113            mingw*) gl_cv_func_ilogbf_works="guessing yes" ;;
114            *)      gl_cv_func_ilogbf_works="guessing yes" ;;
115          esac
116         ])
117     ])