Bump to m4 1.4.19
[platform/upstream/m4.git] / m4 / signbit.m4
1 # signbit.m4 serial 20
2 dnl Copyright (C) 2007-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.
6
7 AC_DEFUN([gl_SIGNBIT],
8 [
9   AC_REQUIRE([gl_MATH_H_DEFAULTS])
10   AC_REQUIRE([AC_CANONICAL_HOST])
11   AC_CACHE_CHECK([for signbit macro], [gl_cv_func_signbit],
12     [
13       AC_RUN_IFELSE(
14         [AC_LANG_SOURCE([[
15 #include <math.h>
16 /* If signbit is defined as a function, don't use it, since calling it for
17    'float' or 'long double' arguments would involve conversions.
18    If signbit is not declared at all but exists as a library function, don't
19    use it, since the prototype may not match.
20    If signbit is not declared at all but exists as a compiler built-in, don't
21    use it, since it's preferable to use __builtin_signbit* (no warnings,
22    no conversions).  */
23 #ifndef signbit
24 # error "signbit should be a macro"
25 #endif
26 #include <string.h>
27 ]gl_SIGNBIT_TEST_PROGRAM
28 ])],
29         [gl_cv_func_signbit=yes],
30         [gl_cv_func_signbit=no],
31         [case "$host_os" in
32                           # Guess yes on glibc systems.
33            *-gnu* | gnu*) gl_cv_func_signbit="guessing yes" ;;
34                           # Guess yes on musl systems.
35            *-musl*)       gl_cv_func_signbit="guessing yes" ;;
36                           # Guess yes on native Windows.
37            mingw*)        gl_cv_func_signbit="guessing yes" ;;
38                           # If we don't know, obey --enable-cross-guesses.
39            *)             gl_cv_func_signbit="$gl_cross_guess_normal" ;;
40          esac
41         ])
42     ])
43   dnl GCC >= 4.0 and clang provide three built-ins for signbit.
44   dnl They can be used without warnings, also in C++, regardless of <math.h>.
45   dnl But they may expand to calls to functions, which may or may not be in
46   dnl libc.
47   AC_CACHE_CHECK([for signbit compiler built-ins],
48     [gl_cv_func_signbit_builtins],
49     [
50       AC_RUN_IFELSE(
51         [AC_LANG_SOURCE([[
52 #if (__GNUC__ >= 4) || (__clang_major__ >= 4)
53 # define signbit(x) \
54    (sizeof (x) == sizeof (long double) ? __builtin_signbitl (x) : \
55     sizeof (x) == sizeof (double) ? __builtin_signbit (x) : \
56     __builtin_signbitf (x))
57 #else
58 # error "signbit should be three compiler built-ins"
59 #endif
60 #include <string.h>
61 ]gl_SIGNBIT_TEST_PROGRAM
62 ])],
63         [gl_cv_func_signbit_builtins=yes],
64         [gl_cv_func_signbit_builtins=no],
65         [case "$host_os" in
66                           # Guess yes on glibc systems.
67            *-gnu* | gnu*) gl_cv_func_signbit_builtins="guessing yes" ;;
68                           # Guess yes on musl systems.
69            *-musl*)       gl_cv_func_signbit_builtins="guessing yes" ;;
70                           # Guess yes on mingw, no on MSVC.
71            mingw*)        if test -n "$GCC"; then
72                             gl_cv_func_signbit_builtins="guessing yes"
73                           else
74                             gl_cv_func_signbit_builtins="guessing no"
75                           fi
76                           ;;
77                           # If we don't know, obey --enable-cross-guesses.
78            *)             gl_cv_func_signbit_builtins="$gl_cross_guess_normal" ;;
79          esac
80         ])
81     ])
82   dnl Use the compiler built-ins whenever possible, because they are more
83   dnl efficient than the system library functions (if they exist).
84   case "$gl_cv_func_signbit_builtins" in
85     *yes)
86       REPLACE_SIGNBIT_USING_BUILTINS=1
87       ;;
88     *)
89       case "$gl_cv_func_signbit" in
90         *yes) ;;
91         *)
92           dnl REPLACE_SIGNBIT=1 makes sure the signbit[fdl] functions get built.
93           REPLACE_SIGNBIT=1
94           ;;
95       esac
96       ;;
97   esac
98   dnl On Solaris 10, with CC in C++ mode, signbit is not available although
99   dnl is with cc in C mode. This cannot be worked around by defining
100   dnl _XOPEN_SOURCE=600, because the latter does not work in C++ mode on
101   dnl Solaris 11.0. Therefore use the replacement functions on Solaris.
102   case "$host_os" in
103     solaris*)
104       REPLACE_SIGNBIT=1
105       ;;
106   esac
107   if test $REPLACE_SIGNBIT = 1; then
108     gl_FLOAT_SIGN_LOCATION
109     gl_DOUBLE_SIGN_LOCATION
110     gl_LONG_DOUBLE_SIGN_LOCATION
111     if test "$gl_cv_cc_float_signbit" = unknown; then
112       dnl Test whether copysignf() is declared.
113       AC_CHECK_DECLS([copysignf], , , [[#include <math.h>]])
114       if test "$ac_cv_have_decl_copysignf" = yes; then
115         dnl Test whether copysignf() can be used without libm.
116         AC_CACHE_CHECK([whether copysignf can be used without linking with libm],
117           [gl_cv_func_copysignf_no_libm],
118           [
119             AC_LINK_IFELSE(
120               [AC_LANG_PROGRAM(
121                  [[#include <math.h>
122                    float x, y;]],
123                  [[return copysignf (x, y) < 0;]])],
124               [gl_cv_func_copysignf_no_libm=yes],
125               [gl_cv_func_copysignf_no_libm=no])
126           ])
127         if test $gl_cv_func_copysignf_no_libm = yes; then
128           AC_DEFINE([HAVE_COPYSIGNF_IN_LIBC], [1],
129             [Define if the copysignf function is declared in <math.h> and available in libc.])
130         fi
131       fi
132     fi
133     if test "$gl_cv_cc_double_signbit" = unknown; then
134       dnl Test whether copysign() is declared.
135       AC_CHECK_DECLS([copysign], , , [[#include <math.h>]])
136       if test "$ac_cv_have_decl_copysign" = yes; then
137         dnl Test whether copysign() can be used without libm.
138         AC_CACHE_CHECK([whether copysign can be used without linking with libm],
139           [gl_cv_func_copysign_no_libm],
140           [
141             AC_LINK_IFELSE(
142               [AC_LANG_PROGRAM(
143                  [[#include <math.h>
144                    double x, y;]],
145                  [[return copysign (x, y) < 0;]])],
146               [gl_cv_func_copysign_no_libm=yes],
147               [gl_cv_func_copysign_no_libm=no])
148           ])
149         if test $gl_cv_func_copysign_no_libm = yes; then
150           AC_DEFINE([HAVE_COPYSIGN_IN_LIBC], [1],
151             [Define if the copysign function is declared in <math.h> and available in libc.])
152         fi
153       fi
154     fi
155     if test "$gl_cv_cc_long_double_signbit" = unknown; then
156       dnl Test whether copysignl() is declared.
157       AC_CHECK_DECLS([copysignl], , , [[#include <math.h>]])
158       if test "$ac_cv_have_decl_copysignl" = yes; then
159         dnl Test whether copysignl() can be used without libm.
160         AC_CACHE_CHECK([whether copysignl can be used without linking with libm],
161           [gl_cv_func_copysignl_no_libm],
162           [
163             AC_LINK_IFELSE(
164               [AC_LANG_PROGRAM(
165                  [[#include <math.h>
166                    long double x, y;]],
167                  [[return copysignl (x, y) < 0;]])],
168               [gl_cv_func_copysignl_no_libm=yes],
169               [gl_cv_func_copysignl_no_libm=no])
170           ])
171         if test $gl_cv_func_copysignl_no_libm = yes; then
172           AC_DEFINE([HAVE_COPYSIGNL_IN_LIBC], [1],
173             [Define if the copysignl function is declared in <math.h> and available in libc.])
174         fi
175       fi
176     fi
177   fi
178 ])
179
180 AC_DEFUN([gl_SIGNBIT_TEST_PROGRAM], [[
181 /* Global variables.
182    Needed because GCC 4 constant-folds __builtin_signbitl (literal)
183    but cannot constant-fold            __builtin_signbitl (variable).  */
184 float vf;
185 double vd;
186 long double vl;
187 int main ()
188 {
189 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
190    So we use -p0f and -p0d instead.  */
191 float p0f = 0.0f;
192 float m0f = -p0f;
193 double p0d = 0.0;
194 double m0d = -p0d;
195 /* On HP-UX 10.20, negating 0.0L does not yield -0.0L.
196    So we use another constant expression instead.
197    But that expression does not work on other platforms, such as when
198    cross-compiling to PowerPC on Mac OS X 10.5.  */
199 long double p0l = 0.0L;
200 #if defined __hpux || defined __sgi
201 long double m0l = -LDBL_MIN * LDBL_MIN;
202 #else
203 long double m0l = -p0l;
204 #endif
205   int result = 0;
206   if (signbit (vf)) /* link check */
207     vf++;
208   {
209     float plus_inf = 1.0f / p0f;
210     float minus_inf = -1.0f / p0f;
211     if (!(!signbit (255.0f)
212           && signbit (-255.0f)
213           && !signbit (p0f)
214           && (memcmp (&m0f, &p0f, sizeof (float)) == 0 || signbit (m0f))
215           && !signbit (plus_inf)
216           && signbit (minus_inf)))
217       result |= 1;
218   }
219   if (signbit (vd)) /* link check */
220     vd++;
221   {
222     double plus_inf = 1.0 / p0d;
223     double minus_inf = -1.0 / p0d;
224     if (!(!signbit (255.0)
225           && signbit (-255.0)
226           && !signbit (p0d)
227           && (memcmp (&m0d, &p0d, sizeof (double)) == 0 || signbit (m0d))
228           && !signbit (plus_inf)
229           && signbit (minus_inf)))
230       result |= 2;
231   }
232   if (signbit (vl)) /* link check */
233     vl++;
234   {
235     long double plus_inf = 1.0L / p0l;
236     long double minus_inf = -1.0L / p0l;
237     if (signbit (255.0L))
238       result |= 4;
239     if (!signbit (-255.0L))
240       result |= 4;
241     if (signbit (p0l))
242       result |= 8;
243     if (!(memcmp (&m0l, &p0l, sizeof (long double)) == 0 || signbit (m0l)))
244       result |= 16;
245     if (signbit (plus_inf))
246       result |= 32;
247     if (!signbit (minus_inf))
248       result |= 64;
249   }
250   return result;
251 }
252 ]])
253
254 AC_DEFUN([gl_FLOAT_SIGN_LOCATION],
255 [
256   gl_FLOATTYPE_SIGN_LOCATION([float], [gl_cv_cc_float_signbit], [f], [FLT])
257 ])
258
259 AC_DEFUN([gl_DOUBLE_SIGN_LOCATION],
260 [
261   gl_FLOATTYPE_SIGN_LOCATION([double], [gl_cv_cc_double_signbit], [], [DBL])
262 ])
263
264 AC_DEFUN([gl_LONG_DOUBLE_SIGN_LOCATION],
265 [
266   gl_FLOATTYPE_SIGN_LOCATION([long double], [gl_cv_cc_long_double_signbit], [L], [LDBL])
267 ])
268
269 AC_DEFUN([gl_FLOATTYPE_SIGN_LOCATION],
270 [
271   AC_CACHE_CHECK([where to find the sign bit in a '$1'],
272     [$2],
273     [
274       AC_RUN_IFELSE(
275         [AC_LANG_SOURCE([[
276 #include <stddef.h>
277 #include <stdio.h>
278 #define NWORDS \
279   ((sizeof ($1) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
280 typedef union { $1 value; unsigned int word[NWORDS]; }
281         memory_float;
282 static memory_float plus = { 1.0$3 };
283 static memory_float minus = { -1.0$3 };
284 int main ()
285 {
286   size_t j, k, i;
287   unsigned int m;
288   FILE *fp = fopen ("conftest.out", "w");
289   if (fp == NULL)
290     return 1;
291   /* Find the different bit.  */
292   k = 0; m = 0;
293   for (j = 0; j < NWORDS; j++)
294     {
295       unsigned int x = plus.word[j] ^ minus.word[j];
296       if ((x & (x - 1)) || (x && m))
297         {
298           /* More than one bit difference.  */
299           fprintf (fp, "unknown");
300           fclose (fp);
301           return 2;
302         }
303       if (x)
304         {
305           k = j;
306           m = x;
307         }
308     }
309   if (m == 0)
310     {
311       /* No difference.  */
312       fprintf (fp, "unknown");
313       fclose (fp);
314       return 3;
315     }
316   /* Now m = plus.word[k] ^ ~minus.word[k].  */
317   if (plus.word[k] & ~minus.word[k])
318     {
319       /* Oh? The sign bit is set in the positive and cleared in the negative
320          numbers?  */
321       fprintf (fp, "unknown");
322       fclose (fp);
323       return 4;
324     }
325   for (i = 0; ; i++)
326     if ((m >> i) & 1)
327       break;
328   fprintf (fp, "word %d bit %d", (int) k, (int) i);
329   if (fclose (fp) != 0)
330     return 5;
331   return 0;
332 }
333         ]])],
334         [$2=`cat conftest.out`],
335         [$2="unknown"],
336         [
337           dnl When cross-compiling, we don't know. It depends on the
338           dnl ABI and compiler version. There are too many cases.
339           $2="unknown"
340         ])
341       rm -f conftest.out
342     ])
343   case "$]$2[" in
344     word*bit*)
345       word=`echo "$]$2[" | sed -e 's/word //' -e 's/ bit.*//'`
346       bit=`echo "$]$2[" | sed -e 's/word.*bit //'`
347       AC_DEFINE_UNQUOTED([$4][_SIGNBIT_WORD], [$word],
348         [Define as the word index where to find the sign of '$1'.])
349       AC_DEFINE_UNQUOTED([$4][_SIGNBIT_BIT], [$bit],
350         [Define as the bit index in the word where to find the sign of '$1'.])
351       ;;
352   esac
353 ])
354
355 # Expands to code that defines a function signbitf(float).
356 # It extracts the sign bit of a non-NaN value.
357 AC_DEFUN([gl_FLOAT_SIGNBIT_CODE],
358 [
359   gl_FLOATTYPE_SIGNBIT_CODE([float], [f], [f])
360 ])
361
362 # Expands to code that defines a function signbitd(double).
363 # It extracts the sign bit of a non-NaN value.
364 AC_DEFUN([gl_DOUBLE_SIGNBIT_CODE],
365 [
366   gl_FLOATTYPE_SIGNBIT_CODE([double], [d], [])
367 ])
368
369 # Expands to code that defines a function signbitl(long double).
370 # It extracts the sign bit of a non-NaN value.
371 AC_DEFUN([gl_LONG_DOUBLE_SIGNBIT_CODE],
372 [
373   gl_FLOATTYPE_SIGNBIT_CODE([long double], [l], [L])
374 ])
375
376 AC_DEFUN([gl_FLOATTYPE_SIGNBIT_CODE],
377 [[
378 static int
379 signbit$2 ($1 value)
380 {
381   typedef union { $1 f; unsigned char b[sizeof ($1)]; } float_union;
382   static float_union plus_one = { 1.0$3 };   /* unused bits are zero here */
383   static float_union minus_one = { -1.0$3 }; /* unused bits are zero here */
384   /* Compute the sign bit mask as the XOR of plus_one and minus_one.  */
385   float_union u;
386   unsigned int i;
387   u.f = value;
388   for (i = 0; i < sizeof ($1); i++)
389     if (u.b[i] & (plus_one.b[i] ^ minus_one.b[i]))
390       return 1;
391   return 0;
392 }
393 ]])