math: Ensure declarations of math functions.
[gnulib.git] / m4 / remainderf.m4
1 # remainderf.m4 serial 4
2 dnl Copyright (C) 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_REMAINDERF],
8 [
9   m4_divert_text([DEFAULTS], [gl_remainderf_required=plain])
10   AC_REQUIRE([gl_MATH_H_DEFAULTS])
11   AC_REQUIRE([gl_FUNC_REMAINDER])
12
13   dnl Persuade glibc <math.h> to declare remainderf().
14   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
15
16   dnl Test whether remainderf() exists. Assume that remainderf(), if it exists, is
17   dnl defined in the same library as remainder().
18   save_LIBS="$LIBS"
19   LIBS="$LIBS $REMAINDER_LIBM"
20   AC_CACHE_CHECK([for remainderf],
21     [gl_cv_func_remainderf],
22     [
23       AC_LINK_IFELSE(
24         [AC_LANG_PROGRAM(
25            [[#ifndef __NO_MATH_INLINES
26              # define __NO_MATH_INLINES 1 /* for glibc */
27              #endif
28              #include <math.h>
29              float (*funcptr) (float, float) = remainderf;
30              float x;
31              float y;]],
32            [[return funcptr (x, y) > 1
33                     || remainderf (x, y) > 1;]])],
34         [gl_cv_func_remainderf=yes],
35         [gl_cv_func_remainderf=no])
36     ])
37   LIBS="$save_LIBS"
38   if test $gl_cv_func_remainderf = yes; then
39     REMAINDERF_LIBM="$REMAINDER_LIBM"
40     m4_ifdef([gl_FUNC_REMAINDERF_IEEE], [
41       if test $gl_remainderf_required = ieee && test $REPLACE_REMAINDERF = 0; then
42         AC_CACHE_CHECK([whether remainderf works according to ISO C 99 with IEC 60559],
43           [gl_cv_func_remainderf_ieee],
44           [
45             save_LIBS="$LIBS"
46             LIBS="$LIBS $REMAINDERF_LIBM"
47             AC_RUN_IFELSE(
48               [AC_LANG_SOURCE([[
49 #ifndef __NO_MATH_INLINES
50 # define __NO_MATH_INLINES 1 /* for glibc */
51 #endif
52 #include <math.h>
53 /* Compare two numbers with ==.
54    This is a separate function because IRIX 6.5 "cc -O" miscompiles an
55    'x == x' test.  */
56 static int
57 numeric_equal (float x, float y)
58 {
59   return x == y;
60 }
61 static float dummy (float x, float y) { return 0; }
62 int main (int argc, char *argv[])
63 {
64   float (*my_remainderf) (float, float) = argc ? remainderf : dummy;
65   float i;
66   float f;
67   /* Test remainderf(...,0.0f).
68      This test fails on OSF/1 5.1.  */
69   f = my_remainderf (2.0f, 0.0f);
70   if (numeric_equal (f, f))
71     return 1;
72   return 0;
73 }
74               ]])],
75               [gl_cv_func_remainderf_ieee=yes],
76               [gl_cv_func_remainderf_ieee=no],
77               [gl_cv_func_remainderf_ieee="guessing no"])
78             LIBS="$save_LIBS"
79           ])
80         case "$gl_cv_func_remainderf_ieee" in
81           *yes) ;;
82           *) REPLACE_REMAINDERF=1 ;;
83         esac
84       fi
85     ])
86   else
87     HAVE_REMAINDERF=0
88   fi
89   if test $HAVE_REMAINDERF = 0 || test $REPLACE_REMAINDERF = 1; then
90     dnl Find libraries needed to link lib/remainderf.c.
91     if test $gl_cv_func_remainder_no_libm = yes \
92        || test $gl_cv_func_remainder_in_libm = yes; then
93       AC_DEFINE([HAVE_REMAINDER], [1],
94         [Define to 1 if the remainder() function is available in libc or libm.])
95       REMAINDERF_LIBM="$REMAINDER_LIBM"
96     else
97       AC_REQUIRE([gl_FUNC_FABSF])
98       AC_REQUIRE([gl_FUNC_FMODF])
99       AC_REQUIRE([gl_FUNC_ISNANF])
100       REMAINDERF_LIBM=
101       dnl Append $FABSF_LIBM to REMAINDERF_LIBM, avoiding gratuitous duplicates.
102       case " $REMAINDERF_LIBM " in
103         *" $FABSF_LIBM "*) ;;
104         *) REMAINDERF_LIBM="$REMAINDERF_LIBM $FABSF_LIBM" ;;
105       esac
106       dnl Append $FMODF_LIBM to REMAINDERF_LIBM, avoiding gratuitous duplicates.
107       case " $REMAINDERF_LIBM " in
108         *" $FMODF_LIBM "*) ;;
109         *) REMAINDERF_LIBM="$REMAINDERF_LIBM $FMODF_LIBM" ;;
110       esac
111       dnl Append $ISNANF_LIBM to REMAINDERF_LIBM, avoiding gratuitous duplicates.
112       case " $REMAINDERF_LIBM " in
113         *" $ISNANF_LIBM "*) ;;
114         *) REMAINDERF_LIBM="$REMAINDERF_LIBM $ISNANF_LIBM" ;;
115       esac
116     fi
117   fi
118   AC_SUBST([REMAINDERF_LIBM])
119 ])