immutable: Add tests.
[gnulib.git] / m4 / remainderf.m4
blobd75327d4af06ed9e51d55e85b73552357d698dba
1 # remainderf.m4 serial 13
2 dnl Copyright (C) 2012-2021 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.
7 AC_DEFUN([gl_FUNC_REMAINDERF],
9   m4_divert_text([DEFAULTS], [gl_remainderf_required=plain])
10   AC_REQUIRE([gl_MATH_H_DEFAULTS])
11   AC_REQUIRE([gl_FUNC_REMAINDER])
13   dnl Persuade glibc <math.h> to declare remainderf().
14   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
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"
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
55     m4_ifdef([gl_FUNC_REMAINDERF_IEEE], [
56       if test $gl_remainderf_required = ieee && test $REPLACE_REMAINDERF = 0; then
57         AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
58         AC_CACHE_CHECK([whether remainderf works according to ISO C 99 with IEC 60559],
59           [gl_cv_func_remainderf_ieee],
60           [
61             save_LIBS="$LIBS"
62             LIBS="$LIBS $REMAINDERF_LIBM"
63             AC_RUN_IFELSE(
64               [AC_LANG_SOURCE([[
65 #ifndef __NO_MATH_INLINES
66 # define __NO_MATH_INLINES 1 /* for glibc */
67 #endif
68 #include <math.h>
69 extern
70 #ifdef __cplusplus
71 "C"
72 #endif
73 float remainderf (float, float);
74 /* Compare two numbers with ==.
75    This is a separate function because IRIX 6.5 "cc -O" miscompiles an
76    'x == x' test.  */
77 static int
78 numeric_equal (float x, float y)
80   return x == y;
82 static float dummy (float x, float y) { return 0; }
83 int main (int argc, char *argv[])
85   float (* volatile my_remainderf) (float, float) = argc ? remainderf : dummy;
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;
94               ]])],
95               [gl_cv_func_remainderf_ieee=yes],
96               [gl_cv_func_remainderf_ieee=no],
97               [case "$host_os" in
98                                 # Guess yes on glibc systems.
99                  *-gnu* | gnu*) gl_cv_func_remainderf_ieee="guessing yes" ;;
100                                 # Guess yes on musl systems.
101                  *-musl*)       gl_cv_func_remainderf_ieee="guessing yes" ;;
102                                 # Guess yes on native Windows.
103                  mingw*)        gl_cv_func_remainderf_ieee="guessing yes" ;;
104                                 # If we don't know, obey --enable-cross-guesses.
105                  *)             gl_cv_func_remainderf_ieee="$gl_cross_guess_normal" ;;
106                esac
107               ])
108             LIBS="$save_LIBS"
109           ])
110         case "$gl_cv_func_remainderf_ieee" in
111           *yes) ;;
112           *) REPLACE_REMAINDERF=1 ;;
113         esac
114       fi
115     ])
116   else
117     HAVE_REMAINDERF=0
118   fi
119   if test $HAVE_REMAINDERF = 0 || test $REPLACE_REMAINDERF = 1; then
120     dnl Find libraries needed to link lib/remainderf.c.
121     if test $gl_cv_func_remainder_no_libm = yes \
122        || test $gl_cv_func_remainder_in_libm = yes; then
123       AC_DEFINE([HAVE_REMAINDER], [1],
124         [Define to 1 if the remainder() function is available in libc or libm.])
125       REMAINDERF_LIBM="$REMAINDER_LIBM"
126     else
127       AC_REQUIRE([gl_FUNC_FABSF])
128       AC_REQUIRE([gl_FUNC_FMODF])
129       AC_REQUIRE([gl_FUNC_ISNANF])
130       REMAINDERF_LIBM=
131       dnl Append $FABSF_LIBM to REMAINDERF_LIBM, avoiding gratuitous duplicates.
132       case " $REMAINDERF_LIBM " in
133         *" $FABSF_LIBM "*) ;;
134         *) REMAINDERF_LIBM="$REMAINDERF_LIBM $FABSF_LIBM" ;;
135       esac
136       dnl Append $FMODF_LIBM to REMAINDERF_LIBM, avoiding gratuitous duplicates.
137       case " $REMAINDERF_LIBM " in
138         *" $FMODF_LIBM "*) ;;
139         *) REMAINDERF_LIBM="$REMAINDERF_LIBM $FMODF_LIBM" ;;
140       esac
141       dnl Append $ISNANF_LIBM to REMAINDERF_LIBM, avoiding gratuitous duplicates.
142       case " $REMAINDERF_LIBM " in
143         *" $ISNANF_LIBM "*) ;;
144         *) REMAINDERF_LIBM="$REMAINDERF_LIBM $ISNANF_LIBM" ;;
145       esac
146     fi
147   fi
148   AC_SUBST([REMAINDERF_LIBM])
151 dnl Test whether remainderf() works.
152 dnl It runs into an endless loop on IRIX 6.5.
153 AC_DEFUN([gl_FUNC_REMAINDERF_WORKS],
155   AC_REQUIRE([AC_PROG_CC])
156   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
157   AC_CHECK_DECLS_ONCE([alarm])
158   AC_CACHE_CHECK([whether remainderf works], [gl_cv_func_remainderf_works],
159     [
160       AC_RUN_IFELSE(
161         [AC_LANG_SOURCE([[
162 #include <math.h>
163 #if HAVE_DECL_ALARM
164 # include <signal.h>
165 # include <unistd.h>
166 #endif
167 extern
168 #ifdef __cplusplus
170 #endif
171 float remainderf (float, float);
172 volatile float x;
173 volatile float y;
174 float z;
175 int main ()
177 #if HAVE_DECL_ALARM
178   signal (SIGALRM, SIG_DFL);
179   alarm (5);
180 #endif
181   /* This test fails on IRIX 6.5.  */
182   x = 9.316161e+37f;
183   y = 0.5475547314f;
184   z = remainderf (x, y);
185   return 0;
187 ]])],
188         [gl_cv_func_remainderf_works=yes],
189         [gl_cv_func_remainderf_works=no],
190         [case "$host_os" in
191            irix*)  gl_cv_func_remainderf_works="guessing no" ;;
192                    # Guess yes on native Windows.
193            mingw*) gl_cv_func_remainderf_works="guessing yes" ;;
194            *)      gl_cv_func_remainderf_works="guessing yes" ;;
195          esac
196         ])
197     ])