*printf* tests: Remove unused includes.
[gnulib.git] / m4 / remainderl.m4
blob85ed1ecda75922cff4f142763b7f4c689cd6f799
1 # remainderl.m4
2 # serial 17
3 dnl Copyright (C) 2012-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
8 AC_DEFUN([gl_FUNC_REMAINDERL],
10   m4_divert_text([DEFAULTS], [gl_remainderl_required=plain])
11   AC_REQUIRE([gl_MATH_H_DEFAULTS])
12   AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
13   AC_REQUIRE([gl_FUNC_REMAINDER])
15   dnl Persuade glibc <math.h> to declare remainderl().
16   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
18   dnl Test whether remainderl() exists. Assume that remainderl(), if it exists, is
19   dnl defined in the same library as remainder().
20   saved_LIBS="$LIBS"
21   LIBS="$LIBS $REMAINDER_LIBM"
22   AC_CACHE_CHECK([for remainderl],
23     [gl_cv_func_remainderl],
24     [
25       AC_LINK_IFELSE(
26         [AC_LANG_PROGRAM(
27            [[#ifndef __NO_MATH_INLINES
28              # define __NO_MATH_INLINES 1 /* for glibc */
29              #endif
30              #include <math.h>
31              extern
32              #ifdef __cplusplus
33              "C"
34              #endif
35              long double remainderl (long double, long double);
36              long double (*funcptr) (long double, long double) = remainderl;
37              long double x;
38              long double y;]],
39            [[return funcptr (x, y) > 1
40                     || remainderl (x, y) > 1;]])],
41         [gl_cv_func_remainderl=yes],
42         [gl_cv_func_remainderl=no])
43     ])
44   LIBS="$saved_LIBS"
45   if test $gl_cv_func_remainderl = yes; then
46     HAVE_REMAINDERL=1
47     REMAINDERL_LIBM="$REMAINDER_LIBM"
48     dnl Also check whether it's declared.
49     dnl IRIX 6.5 has remainderl() in libm but doesn't declare it in <math.h>.
50     AC_CHECK_DECLS([remainderl], , [HAVE_DECL_REMAINDERL=0], [[#include <math.h>]])
52     saved_LIBS="$LIBS"
53     LIBS="$LIBS $REMAINDERL_LIBM"
54     gl_FUNC_REMAINDERL_WORKS
55     LIBS="$saved_LIBS"
56     case "$gl_cv_func_remainderl_works" in
57       *yes) ;;
58       *) REPLACE_REMAINDERL=1 ;;
59     esac
61     m4_ifdef([gl_FUNC_REMAINDERL_IEEE], [
62       if test $gl_remainderl_required = ieee && test $REPLACE_REMAINDERL = 0; then
63         AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
64         AC_CACHE_CHECK([whether remainderl works according to ISO C 99 with IEC 60559],
65           [gl_cv_func_remainderl_ieee],
66           [
67             saved_LIBS="$LIBS"
68             LIBS="$LIBS $REMAINDERL_LIBM"
69             AC_RUN_IFELSE(
70               [AC_LANG_SOURCE([[
71 #ifndef __NO_MATH_INLINES
72 # define __NO_MATH_INLINES 1 /* for glibc */
73 #endif
74 #include <math.h>
75 extern
76 #ifdef __cplusplus
77 "C"
78 #endif
79 long double remainderl (long double, long double);
80 /* Compare two numbers with ==.
81    This is a separate function because IRIX 6.5 "cc -O" miscompiles an
82    'x == x' test.  */
83 static int
84 numeric_equal (long double x, long double y)
86   return x == y;
88 static long double dummy (long double x, long double y) { return 0; }
89 int main (int argc, char *argv[])
91   long double (* volatile my_remainderl) (long double, long double) = argc ? remainderl : dummy;
92   long double f;
93   /* Test remainderl(...,0.0L).
94      This test fails on OSF/1 5.1.  */
95   f = my_remainderl (2.0L, 0.0L);
96   if (numeric_equal (f, f))
97     return 1;
98   return 0;
100               ]])],
101               [gl_cv_func_remainderl_ieee=yes],
102               [gl_cv_func_remainderl_ieee=no],
103               [case "$host_os" in
104                                      # Guess yes on glibc systems.
105                  *-gnu* | gnu*)      gl_cv_func_remainderl_ieee="guessing yes" ;;
106                                      # Guess yes on musl systems.
107                  *-musl* | midipix*) gl_cv_func_remainderl_ieee="guessing yes" ;;
108                                      # Guess yes on native Windows.
109                  mingw* | windows*)  gl_cv_func_remainderl_ieee="guessing yes" ;;
110                                      # If we don't know, obey --enable-cross-guesses.
111                  *)                  gl_cv_func_remainderl_ieee="$gl_cross_guess_normal" ;;
112                esac
113               ])
114             LIBS="$saved_LIBS"
115           ])
116         case "$gl_cv_func_remainderl_ieee" in
117           *yes) ;;
118           *) REPLACE_REMAINDERL=1 ;;
119         esac
120       fi
121     ])
122   else
123     HAVE_REMAINDERL=0
124     HAVE_DECL_REMAINDERL=0
125   fi
126   if test $HAVE_REMAINDERL = 0 || test $REPLACE_REMAINDERL = 1; then
127     dnl Find libraries needed to link lib/remainderl.c.
128     if test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 1; then
129       REMAINDERL_LIBM="$REMAINDER_LIBM"
130     else
131       AC_REQUIRE([gl_FUNC_FABSL])
132       AC_REQUIRE([gl_FUNC_FMODL])
133       AC_REQUIRE([gl_FUNC_ISNANL])
134       REMAINDERL_LIBM=
135       dnl Append $FABSL_LIBM to REMAINDERL_LIBM, avoiding gratuitous duplicates.
136       case " $REMAINDERL_LIBM " in
137         *" $FABSL_LIBM "*) ;;
138         *) REMAINDERL_LIBM="$REMAINDERL_LIBM $FABSL_LIBM" ;;
139       esac
140       dnl Append $FMODL_LIBM to REMAINDERL_LIBM, avoiding gratuitous duplicates.
141       case " $REMAINDERL_LIBM " in
142         *" $FMODL_LIBM "*) ;;
143         *) REMAINDERL_LIBM="$REMAINDERL_LIBM $FMODL_LIBM" ;;
144       esac
145       dnl Append $ISNANL_LIBM to REMAINDERL_LIBM, avoiding gratuitous duplicates.
146       case " $REMAINDERL_LIBM " in
147         *" $ISNANL_LIBM "*) ;;
148         *) REMAINDERL_LIBM="$REMAINDERL_LIBM $ISNANL_LIBM" ;;
149       esac
150     fi
151   fi
152   AC_SUBST([REMAINDERL_LIBM])
155 dnl Test whether remainderl() works.
156 dnl It produces completely wrong values on OpenBSD 5.1/SPARC.
157 dnl On musl 1.2.2/{arm64,s390x} and NetBSD 10.0, the result is accurate to only
158 dnl 16 digits.
159 AC_DEFUN([gl_FUNC_REMAINDERL_WORKS],
161   AC_REQUIRE([AC_PROG_CC])
162   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
163   AC_CACHE_CHECK([whether remainderl works], [gl_cv_func_remainderl_works],
164     [
165       AC_RUN_IFELSE(
166         [AC_LANG_SOURCE([[
167 #ifndef __NO_MATH_INLINES
168 # define __NO_MATH_INLINES 1 /* for glibc */
169 #endif
170 #include <float.h>
171 #include <math.h>
172 /* Override the values of <float.h>, like done in float.in.h.  */
173 #if defined __i386__ && (defined __BEOS__ || defined __OpenBSD__)
174 # undef LDBL_MANT_DIG
175 # define LDBL_MANT_DIG   64
176 # undef LDBL_MIN_EXP
177 # define LDBL_MIN_EXP    (-16381)
178 # undef LDBL_MAX_EXP
179 # define LDBL_MAX_EXP    16384
180 #endif
181 #if defined __i386__ && (defined __FreeBSD__ || defined __DragonFly__)
182 # undef LDBL_MANT_DIG
183 # define LDBL_MANT_DIG   64
184 # undef LDBL_MIN_EXP
185 # define LDBL_MIN_EXP    (-16381)
186 # undef LDBL_MAX_EXP
187 # define LDBL_MAX_EXP    16384
188 #endif
189 #if (defined _ARCH_PPC || defined _POWER) && defined _AIX && (LDBL_MANT_DIG == 106) && defined __GNUC__
190 # undef LDBL_MIN_EXP
191 # define LDBL_MIN_EXP DBL_MIN_EXP
192 #endif
193 #if defined __sgi && (LDBL_MANT_DIG >= 106)
194 # undef LDBL_MANT_DIG
195 # define LDBL_MANT_DIG 106
196 # if defined __GNUC__
197 #  undef LDBL_MIN_EXP
198 #  define LDBL_MIN_EXP DBL_MIN_EXP
199 # endif
200 #endif
201 extern
202 #ifdef __cplusplus
204 #endif
205 long double remainderl (long double, long double);
206 static long double dummy (long double x, long double y) { return 0; }
207 volatile long double gx;
208 volatile long double gy;
209 long double gz;
210 int main (int argc, char *argv[])
212   long double (* volatile my_remainderl) (long double, long double) =
213     argc ? remainderl : dummy;
214   int result = 0;
215   /* This test fails on OpenBSD 5.1/SPARC.  */
216   {
217     gx = 9.245907126L;
218     gy = 3.141592654L;
219     gz = remainderl (gx, gy);
220     if (gz >= 0.0L)
221       result |= 1;
222   }
223   /* This test fails on musl 1.2.2/arm64, musl 1.2.2/s390x, NetBSD 10.0.  */
224   {
225     const long double TWO_LDBL_MANT_DIG = /* 2^LDBL_MANT_DIG */
226       (long double) (1U << ((LDBL_MANT_DIG - 1) / 5))
227       * (long double) (1U << ((LDBL_MANT_DIG - 1 + 1) / 5))
228       * (long double) (1U << ((LDBL_MANT_DIG - 1 + 2) / 5))
229       * (long double) (1U << ((LDBL_MANT_DIG - 1 + 3) / 5))
230       * (long double) (1U << ((LDBL_MANT_DIG - 1 + 4) / 5));
231     long double x = 11.357996098166760874793827872740874983168L;
232     long double y = 0.486497838502717923110029188864352615388L;
233     long double z = my_remainderl (x, y) - (x - 23 * y);
234     long double err = z * TWO_LDBL_MANT_DIG;
235     if (!(err >= -32.0L && err <= 32.0L))
236       result |= 2;
237   }
238   return result;
240 ]])],
241         [gl_cv_func_remainderl_works=yes],
242         [gl_cv_func_remainderl_works=no],
243         [case "$host_os" in
244                                # Guess yes on glibc systems.
245            *-gnu* | gnu*)      gl_cv_func_remainderl_works="guessing yes" ;;
246                                # Guess no on musl systems.
247            *-musl* | midipix*) gl_cv_func_remainderl_works="guessing no" ;;
248                                # Guess no on NetBSD and OpenBSD.
249            netbsd* | openbsd*) gl_cv_func_remainderl_works="guessing no" ;;
250                                # Guess yes on native Windows.
251            mingw* | windows*)  gl_cv_func_remainderl_works="guessing yes" ;;
252                                # If we don't know, obey --enable-cross-guesses.
253            *)                  gl_cv_func_remainderl_works="$gl_cross_guess_normal" ;;
254          esac
255         ])
256     ])