1# frexpl.m4 serial 22
2dnl Copyright (C) 2007-2020 Free Software Foundation, Inc.
3dnl This file is free software; the Free Software Foundation
4dnl gives unlimited permission to copy and/or distribute it,
5dnl with or without modifications, as long as this notice is preserved.
6
7AC_DEFUN([gl_FUNC_FREXPL],
8[
9  AC_REQUIRE([gl_MATH_H_DEFAULTS])
10  AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
11
12  dnl Persuade glibc <math.h> to declare frexpl().
13  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
14
15  dnl Check whether it's declared.
16  dnl Mac OS X 10.3 has frexpl() in libc but doesn't declare it in <math.h>.
17  AC_CHECK_DECL([frexpl], , [HAVE_DECL_FREXPL=0], [[#include <math.h>]])
18
19  FREXPL_LIBM=
20  if test $HAVE_DECL_FREXPL = 1; then
21    gl_CHECK_FREXPL_NO_LIBM
22    if test $gl_cv_func_frexpl_no_libm = no; then
23      AC_CACHE_CHECK([whether frexpl() can be used with libm],
24        [gl_cv_func_frexpl_in_libm],
25        [
26          save_LIBS="$LIBS"
27          LIBS="$LIBS -lm"
28          AC_LINK_IFELSE(
29            [AC_LANG_PROGRAM(
30               [[#include <math.h>
31                 long double x;]],
32               [[int e; return frexpl (x, &e) > 0;]])],
33            [gl_cv_func_frexpl_in_libm=yes],
34            [gl_cv_func_frexpl_in_libm=no])
35          LIBS="$save_LIBS"
36        ])
37      if test $gl_cv_func_frexpl_in_libm = yes; then
38        FREXPL_LIBM=-lm
39      fi
40    fi
41    if test $gl_cv_func_frexpl_no_libm = yes \
42       || test $gl_cv_func_frexpl_in_libm = yes; then
43      save_LIBS="$LIBS"
44      LIBS="$LIBS $FREXPL_LIBM"
45      gl_FUNC_FREXPL_WORKS
46      LIBS="$save_LIBS"
47      case "$gl_cv_func_frexpl_works" in
48        *yes) gl_func_frexpl=yes ;;
49        *)    gl_func_frexpl=no; REPLACE_FREXPL=1 ;;
50      esac
51    else
52      gl_func_frexpl=no
53    fi
54    if test $gl_func_frexpl = yes; then
55      AC_DEFINE([HAVE_FREXPL], [1],
56        [Define if the frexpl() function is available.])
57    fi
58  fi
59  if test $HAVE_DECL_FREXPL = 0 || test $gl_func_frexpl = no; then
60    dnl Find libraries needed to link lib/frexpl.c.
61    if test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 1; then
62      AC_REQUIRE([gl_FUNC_FREXP])
63      FREXPL_LIBM="$FREXP_LIBM"
64    else
65      FREXPL_LIBM=
66    fi
67  fi
68  AC_SUBST([FREXPL_LIBM])
69])
70
71AC_DEFUN([gl_FUNC_FREXPL_NO_LIBM],
72[
73  AC_REQUIRE([gl_MATH_H_DEFAULTS])
74  AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
75  dnl Check whether it's declared.
76  dnl Mac OS X 10.3 has frexpl() in libc but doesn't declare it in <math.h>.
77  AC_CHECK_DECL([frexpl], , [HAVE_DECL_FREXPL=0], [[#include <math.h>]])
78  if test $HAVE_DECL_FREXPL = 1; then
79    gl_CHECK_FREXPL_NO_LIBM
80    if test $gl_cv_func_frexpl_no_libm = yes; then
81      gl_FUNC_FREXPL_WORKS
82      case "$gl_cv_func_frexpl_works" in
83        *yes) gl_func_frexpl_no_libm=yes ;;
84        *)    gl_func_frexpl_no_libm=no; REPLACE_FREXPL=1 ;;
85      esac
86    else
87      gl_func_frexpl_no_libm=no
88      dnl Set REPLACE_FREXPL here because the system may have frexpl in libm.
89      REPLACE_FREXPL=1
90    fi
91    if test $gl_func_frexpl_no_libm = yes; then
92      AC_DEFINE([HAVE_FREXPL_IN_LIBC], [1],
93        [Define if the frexpl() function is available in libc.])
94    fi
95  fi
96])
97
98dnl Test whether frexpl() can be used without linking with libm.
99dnl Set gl_cv_func_frexpl_no_libm to 'yes' or 'no' accordingly.
100AC_DEFUN([gl_CHECK_FREXPL_NO_LIBM],
101[
102  AC_CACHE_CHECK([whether frexpl() can be used without linking with libm],
103    [gl_cv_func_frexpl_no_libm],
104    [
105      AC_LINK_IFELSE(
106        [AC_LANG_PROGRAM(
107           [[#include <math.h>
108             long double x;]],
109           [[int e; return frexpl (x, &e) > 0;]])],
110        [gl_cv_func_frexpl_no_libm=yes],
111        [gl_cv_func_frexpl_no_libm=no])
112    ])
113])
114
115dnl Test whether frexpl() works on finite numbers (this fails on
116dnl Mac OS X 10.4/PowerPC, on AIX 5.1, and on BeOS), on denormalized numbers
117dnl (this fails on Mac OS X 10.5/i386), and also on infinite numbers (this
118dnl fails e.g. on IRIX 6.5 and mingw).
119AC_DEFUN([gl_FUNC_FREXPL_WORKS],
120[
121  AC_REQUIRE([AC_PROG_CC])
122  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
123  AC_CACHE_CHECK([whether frexpl works], [gl_cv_func_frexpl_works],
124    [
125      AC_RUN_IFELSE(
126        [AC_LANG_SOURCE([[
127#include <float.h>
128#include <math.h>
129/* Override the values of <float.h>, like done in float.in.h.  */
130#if defined __i386__ && (defined __BEOS__ || defined __OpenBSD__)
131# undef LDBL_MIN_EXP
132# define LDBL_MIN_EXP    (-16381)
133#endif
134#if defined __i386__ && (defined __FreeBSD__ || defined __DragonFly__)
135# undef LDBL_MIN_EXP
136# define LDBL_MIN_EXP    (-16381)
137#endif
138#if (defined _ARCH_PPC || defined _POWER) && defined _AIX && (LDBL_MANT_DIG == 106) && defined __GNUC__
139# undef LDBL_MIN_EXP
140# define LDBL_MIN_EXP DBL_MIN_EXP
141#endif
142#if defined __sgi && (LDBL_MANT_DIG >= 106)
143# if defined __GNUC__
144#  undef LDBL_MIN_EXP
145#  define LDBL_MIN_EXP DBL_MIN_EXP
146# endif
147#endif
148extern
149#ifdef __cplusplus
150"C"
151#endif
152long double frexpl (long double, int *);
153long double zero = 0.0L;
154int main()
155{
156  int result = 0;
157  volatile long double x;
158  /* Test on finite numbers that fails on AIX 5.1.  */
159  x = 16.0L;
160  {
161    int exp = -9999;
162    frexpl (x, &exp);
163    if (exp != 5)
164      result |= 1;
165  }
166  /* Test on finite numbers that fails on Mac OS X 10.4, because its frexpl
167     function returns an invalid (incorrectly normalized) value: it returns
168               y = { 0x3fe028f5, 0xc28f5c28, 0x3c9eb851, 0xeb851eb8 }
169     but the correct result is
170          0.505L = { 0x3fe028f5, 0xc28f5c29, 0xbc547ae1, 0x47ae1480 }  */
171  x = 1.01L;
172  {
173    int exp = -9999;
174    long double y = frexpl (x, &exp);
175    if (!(exp == 1 && y == 0.505L))
176      result |= 2;
177  }
178  /* Test on large finite numbers.  This fails on BeOS at i = 16322, while
179     LDBL_MAX_EXP = 16384.
180     In the loop end test, we test x against Infinity, rather than comparing
181     i with LDBL_MAX_EXP, because BeOS <float.h> has a wrong LDBL_MAX_EXP.  */
182  {
183    int i;
184    for (i = 1, x = 1.0L; x != x + x; i++, x *= 2.0L)
185      {
186        int exp = -9999;
187        frexpl (x, &exp);
188        if (exp != i)
189          {
190            result |= 4;
191            break;
192          }
193      }
194  }
195  /* Test on denormalized numbers.  */
196  {
197    int i;
198    for (i = 1, x = 1.0L; i >= LDBL_MIN_EXP; i--, x *= 0.5L)
199      ;
200    if (x > 0.0L)
201      {
202        int exp;
203        long double y = frexpl (x, &exp);
204        /* On machines with IEEE854 arithmetic: x = 1.68105e-4932,
205           exp = -16382, y = 0.5.  On Mac OS X 10.5: exp = -16384, y = 0.5.  */
206        if (exp != LDBL_MIN_EXP - 1)
207          result |= 8;
208      }
209  }
210  /* Test on infinite numbers.  */
211  /* The Microsoft MSVC 14 compiler chokes on the expression 1.0 / 0.0.  */
212  x = 1.0L / zero;
213  {
214    int exp;
215    long double y = frexpl (x, &exp);
216    if (y != x)
217      result |= 16;
218  }
219  return result;
220}]])],
221        [gl_cv_func_frexpl_works=yes],
222        [gl_cv_func_frexpl_works=no],
223        [
224changequote(,)dnl
225         case "$host_os" in
226           aix | aix[3-6]* | beos* | darwin* | irix* | mingw* | pw*)
227              gl_cv_func_frexpl_works="guessing no";;
228           *) gl_cv_func_frexpl_works="guessing yes";;
229         esac
230changequote([,])dnl
231        ])
232    ])
233])
234