roundl: Simplify for platforms where 'long double' == 'double'.
[gnulib.git] / m4 / roundl.m4
1 # roundl.m4 serial 13
2 dnl Copyright (C) 2007, 2009-2011 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_ROUNDL],
8 [
9   m4_divert_text([DEFAULTS], [gl_roundl_required=plain])
10   AC_REQUIRE([gl_MATH_H_DEFAULTS])
11   AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
12
13   dnl Persuade glibc <math.h> to declare roundl().
14   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
15
16   AC_CHECK_DECLS([roundl], , , [[#include <math.h>]])
17   if test "$ac_cv_have_decl_roundl" = yes; then
18     gl_CHECK_MATH_LIB([ROUNDL_LIBM], [x = roundl (x);])
19     if test "$ROUNDL_LIBM" = missing; then
20       REPLACE_ROUNDL=1
21     fi
22     m4_ifdef([gl_FUNC_ROUNDL_IEEE], [
23       if test $gl_roundl_required = ieee && test $REPLACE_ROUNDL = 0; then
24         AC_CACHE_CHECK([whether roundl works according to ISO C 99 with IEC 60559],
25           [gl_cv_func_roundl_ieee],
26           [
27             save_LIBS="$LIBS"
28             LIBS="$LIBS $ROUNDL_LIBM"
29             AC_RUN_IFELSE(
30               [AC_LANG_SOURCE([[
31 #ifndef __NO_MATH_INLINES
32 # define __NO_MATH_INLINES 1 /* for glibc */
33 #endif
34 #include <math.h>
35 ]gl_LONG_DOUBLE_MINUS_ZERO_CODE[
36 ]gl_LONG_DOUBLE_SIGNBIT_CODE[
37 static long double dummy (long double f) { return 0; }
38 int main (int argc, char *argv[])
39 {
40   long double (*my_roundl) (long double) = argc ? roundl : dummy;
41   int result = 0;
42   /* Test whether roundl (-0.0L) is -0.0L.  */
43   if (signbitl (minus_zerol) && !signbitl (my_roundl (minus_zerol)))
44     result |= 1;
45   /* Test whether roundl (-0.3L) is -0.0L.  */
46   if (signbitl (-0.3L) && !signbitl (my_roundl (-0.3L)))
47     result |= 2;
48   return result;
49 }
50               ]])],
51               [gl_cv_func_roundl_ieee=yes],
52               [gl_cv_func_roundl_ieee=no],
53               [gl_cv_func_roundl_ieee="guessing no"])
54             LIBS="$save_LIBS"
55           ])
56         case "$gl_cv_func_roundl_ieee" in
57           *yes) ;;
58           *) REPLACE_ROUNDL=1 ;;
59         esac
60       fi
61     ])
62   else
63     HAVE_DECL_ROUNDL=0
64   fi
65   if test $HAVE_DECL_ROUNDL = 0 || test $REPLACE_ROUNDL = 1; then
66     dnl Find libraries needed to link lib/roundl.c.
67     if test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 1; then
68       AC_REQUIRE([gl_FUNC_ROUND])
69       ROUNDL_LIBM="$ROUND_LIBM"
70     else
71       AC_CHECK_DECLS([ceill, floorl], , , [[#include <math.h>]])
72       if test "$ac_cv_have_decl_floorl" = yes \
73          && test "$ac_cv_have_decl_ceill" = yes; then
74         gl_FUNC_FLOORL_LIBS
75         gl_FUNC_CEILL_LIBS
76         if test "$FLOORL_LIBM" != '?' && test "$CEILL_LIBM" != '?'; then
77           AC_DEFINE([HAVE_FLOORL_AND_CEILL], [1],
78             [Define if the both the floorl() and ceill() functions exist.])
79           ROUNDL_LIBM=
80           dnl Append $FLOORL_LIBM to ROUNDL_LIBM, avoiding gratuitous duplicates.
81           case " $ROUNDL_LIBM " in
82             *" $FLOORL_LIBM "*) ;;
83             *) ROUNDL_LIBM="$ROUNDL_LIBM $FLOORL_LIBM" ;;
84           esac
85           dnl Append $CEILL_LIBM to ROUNDL_LIBM, avoiding gratuitous duplicates.
86           case " $ROUNDL_LIBM " in
87             *" $CEILL_LIBM "*) ;;
88             *) ROUNDL_LIBM="$ROUNDL_LIBM $CEILL_LIBM" ;;
89           esac
90         else
91           ROUNDL_LIBM=
92         fi
93       else
94         ROUNDL_LIBM=
95       fi
96     fi
97   fi
98   AC_SUBST([ROUNDL_LIBM])
99 ])