New module 'frexp'.
[gnulib.git] / m4 / frexp.m4
1 # frexp.m4 serial 1
2 dnl Copyright (C) 2007 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_FREXP],
8 [
9   AC_REQUIRE([gl_MATH_H_DEFAULTS])
10   FREXP_LIBM=
11   AC_CACHE_CHECK([whether frexp() can be used without linking with libm],
12     [gl_cv_func_frexp_no_libm],
13     [
14       AC_TRY_LINK([#include <math.h>
15                    long double x;],
16                   [int e; return frexp (x, &e) > 0;],
17         [gl_cv_func_frexp_no_libm=yes],
18         [gl_cv_func_frexp_no_libm=no])
19     ])
20   if test $gl_cv_func_frexp_no_libm = no; then
21     AC_CACHE_CHECK([whether frexp() can be used with libm],
22       [gl_cv_func_frexp_in_libm],
23       [
24         save_LIBS="$LIBS"
25         LIBS="$LIBS -lm"
26         AC_TRY_LINK([#include <math.h>
27                      long double x;],
28                     [int e; return frexp (x, &e) > 0;],
29           [gl_cv_func_frexp_in_libm=yes],
30           [gl_cv_func_frexp_in_libm=no])
31         LIBS="$save_LIBS"
32       ])
33     if test $gl_cv_func_frexp_in_libm = yes; then
34       FREXP_LIBM=-lm
35     fi
36   fi
37   if test $gl_cv_func_frexp_no_libm = yes \
38      || test $gl_cv_func_frexp_in_libm = yes; then
39     save_LIBS="$LIBS"
40     LIBS="$LIBS $FREXP_LIBM"
41     gl_FUNC_FREXP_WORKS
42     LIBS="$save_LIBS"
43     case "$gl_cv_func_frexp_works" in
44       *yes) gl_func_frexp=yes ;;
45       *)    gl_func_frexp=no; REPLACE_FREXP=1; FREXP_LIBM= ;;
46     esac
47   else
48     gl_func_frexp=no
49   fi
50   if test $gl_func_frexp = yes; then
51     AC_DEFINE([HAVE_FREXP], 1,
52       [Define if the frexp() function is available and works.])
53   else
54     AC_LIBOBJ([frexp])
55   fi
56 ])
57
58 dnl Test whether frexp() works also on denormalized numbers.
59 dnl This test fails e.g. on NetBSD.
60 AC_DEFUN([gl_FUNC_FREXP_WORKS],
61 [
62   AC_REQUIRE([AC_PROG_CC])
63   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
64   AC_CACHE_CHECK([whether frexp works], [gl_cv_func_frexp_works],
65     [
66       AC_TRY_RUN([
67 #include <float.h>
68 #include <math.h>
69 int main()
70 {
71   int i;
72   volatile double x;
73   for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
74     ;
75   if (x > 0.0)
76     {
77       int exp;
78       double y = frexp (x, &exp);
79       /* On machines with IEEE754 arithmetic: x = 1.11254e-308, exp = -1022.
80          On NetBSD: y = 0.75. Correct: y = 0.5.  */
81       if (y != 0.5)
82         return 1;
83     }
84   return 0;
85 }], [gl_cv_func_frexp_works=yes], [gl_cv_func_frexp_works=no],
86       [case "$host_os" in
87          netbsd*) gl_cv_func_frexp_works="guessing no";;
88          *)       gl_cv_func_frexp_works="guessing yes";;
89        esac
90       ])
91     ])
92 ])