New module 'trunc-ieee'.
[gnulib.git] / m4 / trunc.m4
1 # trunc.m4 serial 5
2 dnl Copyright (C) 2007, 2010 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_TRUNC],
8 [
9   m4_divert_text([DEFAULTS], [gl_trunc_required=plain])
10   AC_REQUIRE([gl_MATH_H_DEFAULTS])
11   dnl Persuade glibc <math.h> to declare trunc().
12   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
13   dnl Test whether trunc() is declared.
14   AC_CHECK_DECLS([trunc], , , [#include <math.h>])
15   if test "$ac_cv_have_decl_trunc" = yes; then
16     dnl Test whether trunc() can be used without libm.
17     TRUNC_LIBM=?
18     AC_LINK_IFELSE(
19       [AC_LANG_PROGRAM(
20          [[#ifndef __NO_MATH_INLINES
21            # define __NO_MATH_INLINES 1 /* for glibc */
22            #endif
23            #include <math.h>
24            double x;]],
25          [[x = trunc(x);]])],
26       [TRUNC_LIBM=])
27     if test "$TRUNC_LIBM" = "?"; then
28       save_LIBS="$LIBS"
29       LIBS="$LIBS -lm"
30       AC_LINK_IFELSE(
31         [AC_LANG_PROGRAM(
32            [[#ifndef __NO_MATH_INLINES
33              # define __NO_MATH_INLINES 1 /* for glibc */
34              #endif
35              #include <math.h>
36              double x;]],
37            [[x = trunc(x);]])],
38         [TRUNC_LIBM="-lm"])
39       LIBS="$save_LIBS"
40     fi
41     if test "$TRUNC_LIBM" = "?"; then
42       TRUNC_LIBM=
43     fi
44     m4_ifdef([gl_FUNC_TRUNC_IEEE], [
45       if test $gl_trunc_required = ieee && test $REPLACE_TRUNC = 0; then
46         AC_CACHE_CHECK([whether trunc works according to ISO C 99 with IEC 60559],
47           [gl_cv_func_trunc_ieee],
48           [
49             save_LIBS="$LIBS"
50             LIBS="$LIBS $TRUNC_LIBM"
51             AC_RUN_IFELSE(
52               [AC_LANG_SOURCE([[
53 #ifndef __NO_MATH_INLINES
54 # define __NO_MATH_INLINES 1 /* for glibc */
55 #endif
56 #include <math.h>
57 ]gl_DOUBLE_MINUS_ZERO_CODE[
58 ]gl_DOUBLE_SIGNBIT_CODE[
59 int main()
60 {
61   /* Test whether trunc (-0.0) is -0.0.  */
62   if (signbitd (minus_zerod) && !signbitd (trunc (minus_zerod)))
63     return 1;
64   return 0;
65 }
66               ]])],
67               [gl_cv_func_trunc_ieee=yes],
68               [gl_cv_func_trunc_ieee=no],
69               [gl_cv_func_trunc_ieee="guessing no"])
70             LIBS="$save_LIBS"
71           ])
72         case "$gl_cv_func_trunc_ieee" in
73           *yes) ;;
74           *) REPLACE_TRUNC=1 ;;
75         esac
76       fi
77     ])
78   else
79     HAVE_DECL_TRUNC=0
80   fi
81   if test $HAVE_DECL_TRUNC = 0 || test $REPLACE_TRUNC = 1; then
82     AC_LIBOBJ([trunc])
83     TRUNC_LIBM=
84   fi
85   AC_SUBST([TRUNC_LIBM])
86 ])