maint: update copyright
[gnulib.git] / m4 / logb.m4
1 # logb.m4 serial 6
2 dnl Copyright (C) 2010-2014 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.
6
7 AC_DEFUN([gl_FUNC_LOGB],
8 [
9   AC_REQUIRE([gl_MATH_H_DEFAULTS])
10
11   dnl Persuade glibc <math.h> to declare logb().
12   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
13
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])
84 ])
85
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 Cygwin 1.5.x, the return value for subnormal (denormalized) arguments is
89 dnl too large.
90 AC_DEFUN([gl_FUNC_LOGB_WORKS],
91 [
92   AC_REQUIRE([AC_PROG_CC])
93   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
94   AC_CACHE_CHECK([whether logb works], [gl_cv_func_logb_works],
95     [
96       AC_RUN_IFELSE(
97         [AC_LANG_SOURCE([[
98 #include <float.h>
99 #include <math.h>
100 extern
101 #ifdef __cplusplus
102 "C"
103 #endif
104 double logb (double);
105 volatile double x;
106 int main ()
107 {
108   int i;
109   for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
110     ;
111   /* Here i = DBL_MIN_EXP - 1. Either x = 2^(i-1) is subnormal or x = 0.0.  */
112   if (x > 0.0 && !(logb (x) == (double)(i - 1)))
113     return 1;
114   return 0;
115 }
116 ]])],
117         [gl_cv_func_logb_works=yes],
118         [gl_cv_func_logb_works=no],
119         [case "$host_os" in
120            *gnu* | solaris* | cygwin*) gl_cv_func_logb_works="guessing no";;
121            *)                          gl_cv_func_logb_works="guessing yes";;
122          esac
123         ])
124     ])
125 ])