immutable: Add tests.
[gnulib.git] / m4 / logb.m4
blobd181b1ff269862ebf1131803dd94cb6a5ee68c96
1 # logb.m4 serial 9
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_LOGB],
9   AC_REQUIRE([gl_MATH_H_DEFAULTS])
11   dnl Persuade glibc <math.h> to declare logb().
12   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
14   dnl Test whether logb() is declared.
15   AC_CHECK_DECLS([logb], , , [[#include <math.h>]])
16   if test "$ac_cv_have_decl_logb" != yes; then
17     HAVE_DECL_LOGB=0
18   fi
19   dnl Test whether logb() can be used without libm.
20   LOGB_LIBM=?
21   AC_LINK_IFELSE(
22     [AC_LANG_PROGRAM(
23        [[#ifndef __NO_MATH_INLINES
24          # define __NO_MATH_INLINES 1 /* for glibc */
25          #endif
26          #include <math.h>
27          extern
28          #ifdef __cplusplus
29          "C"
30          #endif
31          double logb (double x);
32          double x;]],
33        [[x = logb(x);]])],
34     [LOGB_LIBM=])
35   if test "$LOGB_LIBM" = "?"; then
36     save_LIBS="$LIBS"
37     LIBS="$LIBS -lm"
38     AC_LINK_IFELSE(
39       [AC_LANG_PROGRAM(
40          [[#ifndef __NO_MATH_INLINES
41            # define __NO_MATH_INLINES 1 /* for glibc */
42            #endif
43            #include <math.h>
44            extern
45            #ifdef __cplusplus
46            "C"
47            #endif
48            double logb (double x);
49            double x;]],
50          [[x = logb(x);]])],
51       [LOGB_LIBM="-lm"])
52     LIBS="$save_LIBS"
53   fi
54   if test "$LOGB_LIBM" != "?"; then
55     HAVE_LOGB=1
56     save_LIBS="$LIBS"
57     LIBS="$LIBS $LOGB_LIBM"
58     gl_FUNC_LOGB_WORKS
59     LIBS="$save_LIBS"
60     case "$gl_cv_func_logb_works" in
61       *yes) ;;
62       *) REPLACE_LOGB=1 ;;
63     esac
64   else
65     HAVE_LOGB=0
66   fi
67   if test $HAVE_LOGB = 0 || test $REPLACE_LOGB = 1; then
68     dnl Find libraries needed to link lib/logb.c.
69     AC_REQUIRE([gl_FUNC_FREXP])
70     AC_REQUIRE([gl_FUNC_ISNAND])
71     LOGB_LIBM=
72     dnl Append $FREXP_LIBM to LOGB_LIBM, avoiding gratuitous duplicates.
73     case " $LOGB_LIBM " in
74       *" $FREXP_LIBM "*) ;;
75       *) LOGB_LIBM="$LOGB_LIBM $FREXP_LIBM" ;;
76     esac
77     dnl Append $ISNAND_LIBM to LOGB_LIBM, avoiding gratuitous duplicates.
78     case " $LOGB_LIBM " in
79       *" $ISNAND_LIBM "*) ;;
80       *) LOGB_LIBM="$LOGB_LIBM $ISNAND_LIBM" ;;
81     esac
82   fi
83   AC_SUBST([LOGB_LIBM])
86 dnl Test whether logb() works.
87 dnl On glibc 2.11/ppc, glibc 2.7/sparc, glibc 2.7/hppa, Solaris 10/SPARC,
88 dnl Solaris 11.4/x86_64, Cygwin 1.5.x, the return value for subnormal
89 dnl (denormalized) arguments is too large.
90 dnl On glibc 2.17/ppc likewise but only for negative subnormal (denormalized)
91 dnl arguments.
92 AC_DEFUN([gl_FUNC_LOGB_WORKS],
94   AC_REQUIRE([AC_PROG_CC])
95   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
96   AC_CACHE_CHECK([whether logb works], [gl_cv_func_logb_works],
97     [
98       AC_RUN_IFELSE(
99         [AC_LANG_SOURCE([[
100 #include <float.h>
101 #include <math.h>
102 extern
103 #ifdef __cplusplus
105 #endif
106 double logb (double);
107 volatile double x;
108 int main ()
110   int result = 0;
111   /* This test fails on glibc 2.11/ppc, glibc 2.7/sparc, glibc 2.7/hppa,
112      Solaris 10/SPARC, Solaris 11.4/x86_64, Cygwin 1.5.x.  */
113   {
114     int i;
115     for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
116       ;
117     /* Here i = DBL_MIN_EXP - 1. Either x = 2^(i-1) is subnormal or x = 0.0.  */
118     if (x > 0.0 && !(logb (x) == (double)(i - 1)))
119       result |= 1;
120   }
121   /* This test fails on glibc 2.17/ppc.  */
122   {
123     int i;
124     for (i = 1, x = -1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
125       ;
126     /* Here i = DBL_MIN_EXP - 1. Either x = -2^(i-1) is subnormal or x = -0.0.  */
127     if (x < 0.0 && !(logb (x) == (double)(i - 1)))
128       result |= 2;
129   }
130   return result;
132 ]])],
133         [gl_cv_func_logb_works=yes],
134         [gl_cv_func_logb_works=no],
135         [case "$host_os" in
136            *gnu* | solaris* | cygwin*) gl_cv_func_logb_works="guessing no" ;;
137            mingw*) # Guess yes on MSVC, no on mingw.
138              AC_EGREP_CPP([Known], [
139 #ifdef _MSC_VER
140  Known
141 #endif
142                ],
143                [gl_cv_func_logb_works="guessing yes"],
144                [gl_cv_func_logb_works="guessing no"])
145              ;;
146            *) gl_cv_func_logb_works="guessing yes" ;;
147          esac
148         ])
149     ])