immutable: Add tests.
[gnulib.git] / m4 / remainderl.m4
blob395ad2a88823b42279727846e55793697bc84b6c
1 # remainderl.m4 serial 12
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_REMAINDERL],
9   m4_divert_text([DEFAULTS], [gl_remainderl_required=plain])
10   AC_REQUIRE([gl_MATH_H_DEFAULTS])
11   AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
12   AC_REQUIRE([gl_FUNC_REMAINDER])
14   dnl Persuade glibc <math.h> to declare remainderl().
15   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
17   dnl Test whether remainderl() exists. Assume that remainderl(), if it exists, is
18   dnl defined in the same library as remainder().
19   save_LIBS="$LIBS"
20   LIBS="$LIBS $REMAINDER_LIBM"
21   AC_CACHE_CHECK([for remainderl],
22     [gl_cv_func_remainderl],
23     [
24       AC_LINK_IFELSE(
25         [AC_LANG_PROGRAM(
26            [[#ifndef __NO_MATH_INLINES
27              # define __NO_MATH_INLINES 1 /* for glibc */
28              #endif
29              #include <math.h>
30              extern
31              #ifdef __cplusplus
32              "C"
33              #endif
34              long double remainderl (long double, long double);
35              long double (*funcptr) (long double, long double) = remainderl;
36              long double x;
37              long double y;]],
38            [[return funcptr (x, y) > 1
39                     || remainderl (x, y) > 1;]])],
40         [gl_cv_func_remainderl=yes],
41         [gl_cv_func_remainderl=no])
42     ])
43   LIBS="$save_LIBS"
44   if test $gl_cv_func_remainderl = yes; then
45     HAVE_REMAINDERL=1
46     REMAINDERL_LIBM="$REMAINDER_LIBM"
47     dnl Also check whether it's declared.
48     dnl IRIX 6.5 has remainderl() in libm but doesn't declare it in <math.h>.
49     AC_CHECK_DECLS([remainderl], , [HAVE_DECL_REMAINDERL=0], [[#include <math.h>]])
51     save_LIBS="$LIBS"
52     LIBS="$LIBS $REMAINDERL_LIBM"
53     gl_FUNC_REMAINDERL_WORKS
54     LIBS="$save_LIBS"
55     case "$gl_cv_func_remainderl_works" in
56       *yes) ;;
57       *) REPLACE_REMAINDERL=1 ;;
58     esac
60     m4_ifdef([gl_FUNC_REMAINDERL_IEEE], [
61       if test $gl_remainderl_required = ieee && test $REPLACE_REMAINDERL = 0; then
62         AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
63         AC_CACHE_CHECK([whether remainderl works according to ISO C 99 with IEC 60559],
64           [gl_cv_func_remainderl_ieee],
65           [
66             save_LIBS="$LIBS"
67             LIBS="$LIBS $REMAINDERL_LIBM"
68             AC_RUN_IFELSE(
69               [AC_LANG_SOURCE([[
70 #ifndef __NO_MATH_INLINES
71 # define __NO_MATH_INLINES 1 /* for glibc */
72 #endif
73 #include <math.h>
74 extern
75 #ifdef __cplusplus
76 "C"
77 #endif
78 long double remainderl (long double, long double);
79 /* Compare two numbers with ==.
80    This is a separate function because IRIX 6.5 "cc -O" miscompiles an
81    'x == x' test.  */
82 static int
83 numeric_equal (long double x, long double y)
85   return x == y;
87 static long double dummy (long double x, long double y) { return 0; }
88 int main (int argc, char *argv[])
90   long double (* volatile my_remainderl) (long double, long double) = argc ? remainderl : dummy;
91   long double f;
92   /* Test remainderl(...,0.0L).
93      This test fails on OSF/1 5.1.  */
94   f = my_remainderl (2.0L, 0.0L);
95   if (numeric_equal (f, f))
96     return 1;
97   return 0;
99               ]])],
100               [gl_cv_func_remainderl_ieee=yes],
101               [gl_cv_func_remainderl_ieee=no],
102               [case "$host_os" in
103                                 # Guess yes on glibc systems.
104                  *-gnu* | gnu*) gl_cv_func_remainderl_ieee="guessing yes" ;;
105                                 # Guess yes on musl systems.
106                  *-musl*)       gl_cv_func_remainderl_ieee="guessing yes" ;;
107                                 # Guess yes on native Windows.
108                  mingw*)        gl_cv_func_remainderl_ieee="guessing yes" ;;
109                                 # If we don't know, obey --enable-cross-guesses.
110                  *)             gl_cv_func_remainderl_ieee="$gl_cross_guess_normal" ;;
111                esac
112               ])
113             LIBS="$save_LIBS"
114           ])
115         case "$gl_cv_func_remainderl_ieee" in
116           *yes) ;;
117           *) REPLACE_REMAINDERL=1 ;;
118         esac
119       fi
120     ])
121   else
122     HAVE_REMAINDERL=0
123     HAVE_DECL_REMAINDERL=0
124   fi
125   if test $HAVE_REMAINDERL = 0 || test $REPLACE_REMAINDERL = 1; then
126     dnl Find libraries needed to link lib/remainderl.c.
127     if test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 1; then
128       REMAINDERL_LIBM="$REMAINDER_LIBM"
129     else
130       AC_REQUIRE([gl_FUNC_FABSL])
131       AC_REQUIRE([gl_FUNC_FMODL])
132       AC_REQUIRE([gl_FUNC_ISNANL])
133       REMAINDERL_LIBM=
134       dnl Append $FABSL_LIBM to REMAINDERL_LIBM, avoiding gratuitous duplicates.
135       case " $REMAINDERL_LIBM " in
136         *" $FABSL_LIBM "*) ;;
137         *) REMAINDERL_LIBM="$REMAINDERL_LIBM $FABSL_LIBM" ;;
138       esac
139       dnl Append $FMODL_LIBM to REMAINDERL_LIBM, avoiding gratuitous duplicates.
140       case " $REMAINDERL_LIBM " in
141         *" $FMODL_LIBM "*) ;;
142         *) REMAINDERL_LIBM="$REMAINDERL_LIBM $FMODL_LIBM" ;;
143       esac
144       dnl Append $ISNANL_LIBM to REMAINDERL_LIBM, avoiding gratuitous duplicates.
145       case " $REMAINDERL_LIBM " in
146         *" $ISNANL_LIBM "*) ;;
147         *) REMAINDERL_LIBM="$REMAINDERL_LIBM $ISNANL_LIBM" ;;
148       esac
149     fi
150   fi
151   AC_SUBST([REMAINDERL_LIBM])
154 dnl Test whether remainderl() works.
155 dnl It produces completely wrong values on OpenBSD 5.1/SPARC.
156 AC_DEFUN([gl_FUNC_REMAINDERL_WORKS],
158   AC_REQUIRE([AC_PROG_CC])
159   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
160   AC_CACHE_CHECK([whether remainderl works], [gl_cv_func_remainderl_works],
161     [
162       AC_RUN_IFELSE(
163         [AC_LANG_SOURCE([[
164 #ifndef __NO_MATH_INLINES
165 # define __NO_MATH_INLINES 1 /* for glibc */
166 #endif
167 #include <math.h>
168 extern
169 #ifdef __cplusplus
171 #endif
172 long double remainderl (long double, long double);
173 volatile long double x;
174 volatile long double y;
175 long double z;
176 int main ()
178   /* This test fails on OpenBSD 5.1/SPARC.  */
179   x = 9.245907126L;
180   y = 3.141592654L;
181   z = remainderl (x, y);
182   if (z >= 0.0L)
183     return 1;
184   return 0;
186 ]])],
187         [gl_cv_func_remainderl_works=yes],
188         [gl_cv_func_remainderl_works=no],
189         [case "$host_os" in
190            openbsd*) gl_cv_func_remainderl_works="guessing no" ;;
191                      # Guess yes on native Windows.
192            mingw*)   gl_cv_func_remainderl_works="guessing yes" ;;
193            *)        gl_cv_func_remainderl_works="guessing yes" ;;
194          esac
195         ])
196     ])