immutable: Add tests.
[gnulib.git] / m4 / truncl.m4
blob24e337942cd2a5fc6e999853967e415566a6babe
1 # truncl.m4 serial 17
2 dnl Copyright (C) 2007-2008, 2010-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_TRUNCL],
9   m4_divert_text([DEFAULTS], [gl_truncl_required=plain])
10   AC_REQUIRE([gl_MATH_H_DEFAULTS])
11   AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
12   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
14   dnl Persuade glibc <math.h> to declare truncl().
15   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
17   dnl Test whether truncl() is declared.
18   AC_CHECK_DECLS([truncl], , , [[#include <math.h>]])
19   if test "$ac_cv_have_decl_truncl" = yes; then
20     dnl Test whether truncl() can be used without libm.
21     TRUNCL_LIBM=?
22     AC_LINK_IFELSE(
23       [AC_LANG_PROGRAM(
24          [[#ifndef __NO_MATH_INLINES
25            # define __NO_MATH_INLINES 1 /* for glibc */
26            #endif
27            #include <math.h>
28            long double (*funcptr) (long double) = truncl;
29            long double x;]],
30          [[x = funcptr(x) + truncl(x);]])],
31       [TRUNCL_LIBM=])
32     if test "$TRUNCL_LIBM" = "?"; then
33       save_LIBS="$LIBS"
34       LIBS="$LIBS -lm"
35       AC_LINK_IFELSE(
36         [AC_LANG_PROGRAM(
37            [[#ifndef __NO_MATH_INLINES
38              # define __NO_MATH_INLINES 1 /* for glibc */
39              #endif
40              #include <math.h>
41              long double (*funcptr) (long double) = truncl;
42              long double x;]],
43            [[x = funcptr(x) + truncl(x);]])],
44         [TRUNCL_LIBM="-lm"])
45       LIBS="$save_LIBS"
46     fi
47     if test "$TRUNCL_LIBM" = "?"; then
48       TRUNCL_LIBM=
49     fi
50     dnl Test whether truncl() works. It crashes on OSF/1 4.0d.
51     save_LIBS="$LIBS"
52     LIBS="$LIBS $TRUNCL_LIBM"
53     AC_CACHE_CHECK([whether truncl works], [gl_cv_func_truncl_works],
54       [
55         AC_RUN_IFELSE(
56           [AC_LANG_SOURCE([[
57 #include <math.h>
58 long double x;
59 int main()
61   x = truncl (0.0L);
62   return 0;
63 }]])],
64           [gl_cv_func_truncl_works=yes],
65           [gl_cv_func_truncl_works=no],
66           [case "$host_os" in
67              osf4*)  gl_cv_func_truncl_works="guessing no" ;;
68                      # Guess yes on native Windows.
69              mingw*) gl_cv_func_truncl_works="guessing yes" ;;
70              *)      gl_cv_func_truncl_works="guessing yes" ;;
71            esac
72           ])
73       ])
74     LIBS="$save_LIBS"
75     case "$gl_cv_func_truncl_works" in
76       *yes) ;;
77       *) REPLACE_TRUNCL=1 ;;
78     esac
79     m4_ifdef([gl_FUNC_TRUNCL_IEEE], [
80       if test $gl_truncl_required = ieee && test $REPLACE_TRUNCL = 0; then
81         AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
82         AC_CACHE_CHECK([whether truncl works according to ISO C 99 with IEC 60559],
83           [gl_cv_func_truncl_ieee],
84           [
85             save_LIBS="$LIBS"
86             LIBS="$LIBS $TRUNCL_LIBM"
87             AC_RUN_IFELSE(
88               [AC_LANG_SOURCE([[
89 #ifndef __NO_MATH_INLINES
90 # define __NO_MATH_INLINES 1 /* for glibc */
91 #endif
92 #include <math.h>
93 ]gl_LONG_DOUBLE_MINUS_ZERO_CODE[
94 ]gl_LONG_DOUBLE_SIGNBIT_CODE[
95 static long double dummy (long double f) { return 0; }
96 int main (int argc, char *argv[])
98   long double (* volatile my_truncl) (long double) = argc ? truncl : dummy;
99   /* Test whether truncl (-0.3L) is -0.0L.  */
100   if (signbitl (minus_zerol) && !signbitl (my_truncl (-0.3L)))
101     return 1;
102   return 0;
104               ]])],
105               [gl_cv_func_truncl_ieee=yes],
106               [gl_cv_func_truncl_ieee=no],
107               [case "$host_os" in
108                                 # Guess yes on glibc systems.
109                  *-gnu* | gnu*) gl_cv_func_truncl_ieee="guessing yes" ;;
110                                 # Guess yes on musl systems.
111                  *-musl*)       gl_cv_func_truncl_ieee="guessing yes" ;;
112                                 # Guess yes on native Windows.
113                  mingw*)        gl_cv_func_truncl_ieee="guessing yes" ;;
114                                 # If we don't know, obey --enable-cross-guesses.
115                  *)             gl_cv_func_truncl_ieee="$gl_cross_guess_normal" ;;
116                esac
117               ])
118             LIBS="$save_LIBS"
119           ])
120         case "$gl_cv_func_truncl_ieee" in
121           *yes) ;;
122           *) REPLACE_TRUNCL=1 ;;
123         esac
124       fi
125     ])
126   else
127     HAVE_DECL_TRUNCL=0
128   fi
129   if test $HAVE_DECL_TRUNCL = 0 || test $REPLACE_TRUNCL = 1; then
130     dnl Find libraries needed to link lib/truncl.c.
131     if test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 1; then
132       AC_REQUIRE([gl_FUNC_TRUNC])
133       TRUNCL_LIBM="$TRUNC_LIBM"
134     else
135       TRUNCL_LIBM=
136     fi
137   fi
138   AC_SUBST([TRUNCL_LIBM])