math: Ensure declarations of math functions.
[gnulib.git] / m4 / fmodl.m4
1 # fmodl.m4 serial 4
2 dnl Copyright (C) 2011-2012 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_FMODL],
8 [
9   m4_divert_text([DEFAULTS], [gl_fmodl_required=plain])
10   AC_REQUIRE([gl_MATH_H_DEFAULTS])
11   AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
12   AC_REQUIRE([gl_FUNC_FMOD])
13
14   dnl Persuade glibc <math.h> to declare fmodl().
15   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
16
17   dnl Test whether fmodl() is declared.
18   dnl AIX 5.1 has fmodl() in libc but doesn't declare it in <math.h>, and the
19   dnl function produces NaN results for finite values.
20   AC_CHECK_DECL([fmodl],
21     [ac_cv_have_decl_fmodl=yes], [ac_cv_have_decl_fmodl=no],
22     [[#include <math.h>]])
23
24   dnl Test whether fmodl() exists. Assume that fmodl(), if it exists, is
25   dnl defined in the same library as fmod().
26   save_LIBS="$LIBS"
27   LIBS="$LIBS $FMOD_LIBM"
28   AC_CHECK_FUNCS([fmodl])
29   LIBS="$save_LIBS"
30   if test $ac_cv_func_fmodl = yes; then
31     FMODL_LIBM="$FMOD_LIBM"
32     if test $ac_cv_have_decl_fmodl = no; then
33       REPLACE_FMODL=1
34     fi
35     m4_ifdef([gl_FUNC_FMODL_IEEE], [
36       if test $gl_fmodl_required = ieee && test $REPLACE_FMODL = 0; then
37         AC_CACHE_CHECK([whether fmodl works according to ISO C 99 with IEC 60559],
38           [gl_cv_func_fmodl_ieee],
39           [
40             save_LIBS="$LIBS"
41             LIBS="$LIBS $FMODL_LIBM"
42             AC_RUN_IFELSE(
43               [AC_LANG_SOURCE([[
44 #ifndef __NO_MATH_INLINES
45 # define __NO_MATH_INLINES 1 /* for glibc */
46 #endif
47 #include <math.h>
48 /* Compare two numbers with ==.
49    This is a separate function because IRIX 6.5 "cc -O" miscompiles an
50    'x == x' test.  */
51 static int
52 numeric_equal (long double x, long double y)
53 {
54   return x == y;
55 }
56 static long double dummy (long double x, long double y) { return 0; }
57 int main (int argc, char *argv[])
58 {
59   long double (*my_fmodl) (long double, long double) = argc ? fmodl : dummy;
60   long double f;
61   /* Test fmodl(...,0.0L).
62      This test fails on OSF/1 5.1.  */
63   f = my_fmodl (2.0L, 0.0L);
64   if (numeric_equal (f, f))
65     return 1;
66   return 0;
67 }
68               ]])],
69               [gl_cv_func_fmodl_ieee=yes],
70               [gl_cv_func_fmodl_ieee=no],
71               [gl_cv_func_fmodl_ieee="guessing no"])
72             LIBS="$save_LIBS"
73           ])
74         case "$gl_cv_func_fmodl_ieee" in
75           *yes) ;;
76           *) REPLACE_FMODL=1 ;;
77         esac
78       fi
79     ])
80   else
81     HAVE_FMODL=0
82   fi
83   if test $HAVE_FMODL = 0 || test $REPLACE_FMODL = 1; then
84     dnl Find libraries needed to link lib/fmodl.c.
85     if test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 1; then
86       FMODL_LIBM="$FMOD_LIBM"
87     else
88       AC_REQUIRE([gl_FUNC_FABSL])
89       AC_REQUIRE([gl_FUNC_FREXPL])
90       AC_REQUIRE([gl_FUNC_TRUNCL])
91       AC_REQUIRE([gl_FUNC_LDEXPL])
92       AC_REQUIRE([gl_FUNC_ISNANL])
93       FMODL_LIBM=
94       dnl Append $FABSL_LIBM to FMODL_LIBM, avoiding gratuitous duplicates.
95       case " $FMODL_LIBM " in
96         *" $FABSL_LIBM "*) ;;
97         *) FMODL_LIBM="$FMODL_LIBM $FABSL_LIBM" ;;
98       esac
99       dnl Append $FREXPL_LIBM to FMODL_LIBM, avoiding gratuitous duplicates.
100       case " $FMODL_LIBM " in
101         *" $FREXPL_LIBM "*) ;;
102         *) FMODL_LIBM="$FMODL_LIBM $FREXPL_LIBM" ;;
103       esac
104       dnl Append $TRUNCL_LIBM to FMODL_LIBM, avoiding gratuitous duplicates.
105       case " $FMODL_LIBM " in
106         *" $TRUNCL_LIBM "*) ;;
107         *) FMODL_LIBM="$FMODL_LIBM $TRUNCL_LIBM" ;;
108       esac
109       dnl Append $LDEXPL_LIBM to FMODL_LIBM, avoiding gratuitous duplicates.
110       case " $FMODL_LIBM " in
111         *" $LDEXPL_LIBM "*) ;;
112         *) FMODL_LIBM="$FMODL_LIBM $LDEXPL_LIBM" ;;
113       esac
114       dnl Append $ISNANL_LIBM to FMODL_LIBM, avoiding gratuitous duplicates.
115       case " $FMODL_LIBM " in
116         *" $ISNANL_LIBM "*) ;;
117         *) FMODL_LIBM="$FMODL_LIBM $ISNANL_LIBM" ;;
118       esac
119     fi
120   fi
121   AC_SUBST([FMODL_LIBM])
122 ])