8cb15ecbe3d166efc610fe5ffeddfb773169f824
[gnulib.git] / m4 / remainderf.m4
1 # remainderf.m4 serial 5
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              extern
30              #ifdef __cplusplus
31              "C"
32              #endif
33              float remainderf (float, float);
34              float (*funcptr) (float, float) = remainderf;
35              float x;
36              float y;]],
37            [[return funcptr (x, y) > 1
38                     || remainderf (x, y) > 1;]])],
39         [gl_cv_func_remainderf=yes],
40         [gl_cv_func_remainderf=no])
41     ])
42   LIBS="$save_LIBS"
43   if test $gl_cv_func_remainderf = yes; then
44     REMAINDERF_LIBM="$REMAINDER_LIBM"
45
46     save_LIBS="$LIBS"
47     LIBS="$LIBS $REMAINDERF_LIBM"
48     gl_FUNC_REMAINDERF_WORKS
49     LIBS="$save_LIBS"
50     case "$gl_cv_func_remainderf_works" in
51       *yes) ;;
52       *) REPLACE_REMAINDERF=1 ;;
53     esac
54
55     m4_ifdef([gl_FUNC_REMAINDERF_IEEE], [
56       if test $gl_remainderf_required = ieee && test $REPLACE_REMAINDERF = 0; then
57         AC_CACHE_CHECK([whether remainderf works according to ISO C 99 with IEC 60559],
58           [gl_cv_func_remainderf_ieee],
59           [
60             save_LIBS="$LIBS"
61             LIBS="$LIBS $REMAINDERF_LIBM"
62             AC_RUN_IFELSE(
63               [AC_LANG_SOURCE([[
64 #ifndef __NO_MATH_INLINES
65 # define __NO_MATH_INLINES 1 /* for glibc */
66 #endif
67 #include <math.h>
68 extern
69 #ifdef __cplusplus
70 "C"
71 #endif
72 float remainderf (float, float);
73 /* Compare two numbers with ==.
74    This is a separate function because IRIX 6.5 "cc -O" miscompiles an
75    'x == x' test.  */
76 static int
77 numeric_equal (float x, float y)
78 {
79   return x == y;
80 }
81 static float dummy (float x, float y) { return 0; }
82 int main (int argc, char *argv[])
83 {
84   float (*my_remainderf) (float, float) = argc ? remainderf : dummy;
85   float i;
86   float f;
87   /* Test remainderf(...,0.0f).
88      This test fails on OSF/1 5.1.  */
89   f = my_remainderf (2.0f, 0.0f);
90   if (numeric_equal (f, f))
91     return 1;
92   return 0;
93 }
94               ]])],
95               [gl_cv_func_remainderf_ieee=yes],
96               [gl_cv_func_remainderf_ieee=no],
97               [gl_cv_func_remainderf_ieee="guessing no"])
98             LIBS="$save_LIBS"
99           ])
100         case "$gl_cv_func_remainderf_ieee" in
101           *yes) ;;
102           *) REPLACE_REMAINDERF=1 ;;
103         esac
104       fi
105     ])
106   else
107     HAVE_REMAINDERF=0
108   fi
109   if test $HAVE_REMAINDERF = 0 || test $REPLACE_REMAINDERF = 1; then
110     dnl Find libraries needed to link lib/remainderf.c.
111     if test $gl_cv_func_remainder_no_libm = yes \
112        || test $gl_cv_func_remainder_in_libm = yes; then
113       AC_DEFINE([HAVE_REMAINDER], [1],
114         [Define to 1 if the remainder() function is available in libc or libm.])
115       REMAINDERF_LIBM="$REMAINDER_LIBM"
116     else
117       AC_REQUIRE([gl_FUNC_FABSF])
118       AC_REQUIRE([gl_FUNC_FMODF])
119       AC_REQUIRE([gl_FUNC_ISNANF])
120       REMAINDERF_LIBM=
121       dnl Append $FABSF_LIBM to REMAINDERF_LIBM, avoiding gratuitous duplicates.
122       case " $REMAINDERF_LIBM " in
123         *" $FABSF_LIBM "*) ;;
124         *) REMAINDERF_LIBM="$REMAINDERF_LIBM $FABSF_LIBM" ;;
125       esac
126       dnl Append $FMODF_LIBM to REMAINDERF_LIBM, avoiding gratuitous duplicates.
127       case " $REMAINDERF_LIBM " in
128         *" $FMODF_LIBM "*) ;;
129         *) REMAINDERF_LIBM="$REMAINDERF_LIBM $FMODF_LIBM" ;;
130       esac
131       dnl Append $ISNANF_LIBM to REMAINDERF_LIBM, avoiding gratuitous duplicates.
132       case " $REMAINDERF_LIBM " in
133         *" $ISNANF_LIBM "*) ;;
134         *) REMAINDERF_LIBM="$REMAINDERF_LIBM $ISNANF_LIBM" ;;
135       esac
136     fi
137   fi
138   AC_SUBST([REMAINDERF_LIBM])
139 ])
140
141 dnl Test whether remainderf() works.
142 dnl It runs into an endless loop on IRIX 6.5.
143 AC_DEFUN([gl_FUNC_REMAINDERF_WORKS],
144 [
145   AC_REQUIRE([AC_PROG_CC])
146   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
147   AC_CHECK_FUNCS_ONCE([alarm])
148   AC_CACHE_CHECK([whether remainderf works], [gl_cv_func_remainderf_works],
149     [
150       AC_RUN_IFELSE(
151         [AC_LANG_SOURCE([[
152 #include <math.h>
153 #if HAVE_ALARM
154 # include <unistd.h>
155 #endif
156 extern
157 #ifdef __cplusplus
158 "C"
159 #endif
160 float remainderf (float, float);
161 volatile float x;
162 volatile float y;
163 float z;
164 int main ()
165 {
166 #if HAVE_ALARM
167   alarm (5);
168 #endif
169   /* This test fails on IRIX 6.5.  */
170   x = 9.316161e+37f;
171   y = 0.5475547314f;
172   z = remainderf (x, y);
173   return 0;
174 }
175 ]])],
176         [gl_cv_func_remainderf_works=yes],
177         [gl_cv_func_remainderf_works=no],
178         [case "$host_os" in
179            irix*) gl_cv_func_remainderf_works="guessing no";;
180            *)     gl_cv_func_remainderf_works="guessing yes";;
181          esac
182         ])
183     ])
184 ])