stdopen: Fix compilation error with IRIX cc.
[gnulib.git] / m4 / expl.m4
blob93851a2f51d2bc9b928ae0711da2dcb94956999a
1 # expl.m4 serial 11
2 dnl Copyright (C) 2010-2019 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_EXPL],
9   AC_REQUIRE([gl_MATH_H_DEFAULTS])
10   AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
11   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
13   dnl Persuade glibc <math.h> to declare expl().
14   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
16   EXPL_LIBM=
17   AC_CACHE_CHECK([whether expl() can be used without linking with libm],
18     [gl_cv_func_expl_no_libm],
19     [
20       AC_LINK_IFELSE(
21         [AC_LANG_PROGRAM(
22            [[#ifndef __NO_MATH_INLINES
23              # define __NO_MATH_INLINES 1 /* for glibc */
24              #endif
25              #include <math.h>
26              long double (*funcptr) (long double) = expl;
27              long double x;]],
28            [[return funcptr (x) > 1.5
29                     || expl (x) > 1.5;]])],
30         [gl_cv_func_expl_no_libm=yes],
31         [gl_cv_func_expl_no_libm=no])
32     ])
33   if test $gl_cv_func_expl_no_libm = no; then
34     AC_CACHE_CHECK([whether expl() can be used with libm],
35       [gl_cv_func_expl_in_libm],
36       [
37         save_LIBS="$LIBS"
38         LIBS="$LIBS -lm"
39         AC_LINK_IFELSE(
40           [AC_LANG_PROGRAM(
41              [[#ifndef __NO_MATH_INLINES
42                # define __NO_MATH_INLINES 1 /* for glibc */
43                #endif
44                #include <math.h>
45                long double (*funcptr) (long double) = expl;
46                long double x;]],
47              [[return funcptr (x) > 1.5
48                       || expl (x) > 1.5;]])],
49           [gl_cv_func_expl_in_libm=yes],
50           [gl_cv_func_expl_in_libm=no])
51         LIBS="$save_LIBS"
52       ])
53     if test $gl_cv_func_expl_in_libm = yes; then
54       EXPL_LIBM=-lm
55     fi
56   fi
57   dnl On Haiku 2017 the system's native expl() is just a stub: it returns 0.0
58   dnl and prints "__expl not implemented" for all arguments.
59   dnl On OpenBSD 5.4 the system's native expl() is buggy:
60   dnl it returns 'nan' for small values. Test for this anomaly.
61   if test $gl_cv_func_expl_no_libm = yes \
62      || test $gl_cv_func_expl_in_libm = yes; then
63     AC_CACHE_CHECK([whether expl() breaks with small values],
64       [gl_cv_func_expl_buggy],
65       [save_LIBS="$LIBS"
66        LIBS="$EXPL_LIBM"
67        AC_RUN_IFELSE(
68          [AC_LANG_PROGRAM(
69             [[#include <math.h>]],
70             [[volatile long double x1 = -1.0;
71               volatile long double x2 = -0.8;
72               volatile long double x3 = -0.4;
73               return expl(x1) == 0.0 || isnan(expl(x1))
74                      || isnan(expl(x2)) || isnan(expl(x3));
75             ]])
76          ],
77          [gl_cv_func_expl_buggy=no], [gl_cv_func_expl_buggy=yes],
78          [case $host_os in
79             haiku* | openbsd*)
80                       gl_cv_func_expl_buggy="guessing yes" ;;
81                       # Guess no on native Windows.
82             mingw*)   gl_cv_func_expl_buggy="guessing no" ;;
83             *)        gl_cv_func_expl_buggy="guessing no" ;;
84           esac
85          ])
86        LIBS="$save_LIBS"
87       ])
88     case "$gl_cv_func_expl_buggy" in
89       *yes)
90         gl_cv_func_expl_in_libm=no
91         gl_cv_func_expl_no_libm=no ;;
92     esac
93   fi
94   if test $gl_cv_func_expl_no_libm = yes \
95      || test $gl_cv_func_expl_in_libm = yes; then
96     dnl Also check whether it's declared.
97     dnl Mac OS X 10.3 has expl() in libc but doesn't declare it in <math.h>.
98     AC_CHECK_DECL([expl], , [HAVE_DECL_EXPL=0], [[#include <math.h>]])
99   else
100     HAVE_DECL_EXPL=0
101     HAVE_EXPL=0
102     dnl Find libraries needed to link lib/expl.c.
103     if test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 1; then
104       AC_REQUIRE([gl_FUNC_EXP])
105       EXPL_LIBM="$EXP_LIBM"
106     else
107       AC_REQUIRE([gl_FUNC_ISNANL])
108       AC_REQUIRE([gl_FUNC_ROUNDL])
109       AC_REQUIRE([gl_FUNC_LDEXPL])
110       EXPL_LIBM=
111       dnl Append $ISNANL_LIBM to EXPL_LIBM, avoiding gratuitous duplicates.
112       case " $EXPL_LIBM " in
113         *" $ISNANL_LIBM "*) ;;
114         *) EXPL_LIBM="$EXPL_LIBM $ISNANL_LIBM" ;;
115       esac
116       dnl Append $ROUNDL_LIBM to EXPL_LIBM, avoiding gratuitous duplicates.
117       case " $EXPL_LIBM " in
118         *" $ROUNDL_LIBM "*) ;;
119         *) EXPL_LIBM="$EXPL_LIBM $ROUNDL_LIBM" ;;
120       esac
121       dnl Append $LDEXPL_LIBM to EXPL_LIBM, avoiding gratuitous duplicates.
122       case " $EXPL_LIBM " in
123         *" $LDEXPL_LIBM "*) ;;
124         *) EXPL_LIBM="$EXPL_LIBM $LDEXPL_LIBM" ;;
125       esac
126     fi
127   fi
128   AC_SUBST([EXPL_LIBM])