immutable: Add tests.
[gnulib.git] / m4 / truncf.m4
blobb25662aaaa94e34583fbb007a5b1d31449884782
1 # truncf.m4 serial 15
2 dnl Copyright (C) 2007, 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_TRUNCF],
9   m4_divert_text([DEFAULTS], [gl_truncf_required=plain])
10   AC_REQUIRE([gl_MATH_H_DEFAULTS])
11   dnl Persuade glibc <math.h> to declare truncf().
12   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
13   dnl Test whether truncf() is declared.
14   AC_CHECK_DECLS([truncf], , , [[#include <math.h>]])
15   if test "$ac_cv_have_decl_truncf" = yes; then
16     dnl Test whether truncf() can be used without libm.
17     TRUNCF_LIBM=?
18     AC_LINK_IFELSE(
19       [AC_LANG_PROGRAM(
20          [[#ifndef __NO_MATH_INLINES
21            # define __NO_MATH_INLINES 1 /* for glibc */
22            #endif
23            #include <math.h>
24            float (*funcptr) (float) = truncf;
25            float x;]],
26          [[x = funcptr(x) + truncf(x);]])],
27       [TRUNCF_LIBM=])
28     if test "$TRUNCF_LIBM" = "?"; then
29       save_LIBS="$LIBS"
30       LIBS="$LIBS -lm"
31       AC_LINK_IFELSE(
32         [AC_LANG_PROGRAM(
33            [[#ifndef __NO_MATH_INLINES
34              # define __NO_MATH_INLINES 1 /* for glibc */
35              #endif
36              #include <math.h>
37              float (*funcptr) (float) = truncf;
38              float x;]],
39            [[x = funcptr(x) + truncf(x);]])],
40         [TRUNCF_LIBM="-lm"])
41       LIBS="$save_LIBS"
42     fi
43     if test "$TRUNCF_LIBM" = "?"; then
44       TRUNCF_LIBM=
45     fi
46     m4_ifdef([gl_FUNC_TRUNCF_IEEE], [
47       if test $gl_truncf_required = ieee && test $REPLACE_TRUNCF = 0; then
48         AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
49         AC_CACHE_CHECK([whether truncf works according to ISO C 99 with IEC 60559],
50           [gl_cv_func_truncf_ieee],
51           [
52             save_LIBS="$LIBS"
53             LIBS="$LIBS $TRUNCF_LIBM"
54             AC_RUN_IFELSE(
55               [AC_LANG_SOURCE([[
56 #ifndef __NO_MATH_INLINES
57 # define __NO_MATH_INLINES 1 /* for glibc */
58 #endif
59 #include <math.h>
60 ]gl_FLOAT_MINUS_ZERO_CODE[
61 ]gl_FLOAT_SIGNBIT_CODE[
62 static float dummy (float f) { return 0; }
63 int main (int argc, char *argv[])
65   float (* volatile my_truncf) (float) = argc ? truncf : dummy;
66   /* Test whether truncf (-0.0f) is -0.0f.  */
67   if (signbitf (minus_zerof) && !signbitf (my_truncf (minus_zerof)))
68     return 1;
69   return 0;
71               ]])],
72               [gl_cv_func_truncf_ieee=yes],
73               [gl_cv_func_truncf_ieee=no],
74               [case "$host_os" in
75                                 # Guess yes on glibc systems.
76                  *-gnu* | gnu*) gl_cv_func_truncf_ieee="guessing yes" ;;
77                                 # Guess yes on musl systems.
78                  *-musl*)       gl_cv_func_truncf_ieee="guessing yes" ;;
79                                 # Guess yes on native Windows.
80                  mingw*)        gl_cv_func_truncf_ieee="guessing yes" ;;
81                                 # If we don't know, obey --enable-cross-guesses.
82                  *)             gl_cv_func_truncf_ieee="$gl_cross_guess_normal" ;;
83                esac
84               ])
85             LIBS="$save_LIBS"
86           ])
87         case "$gl_cv_func_truncf_ieee" in
88           *yes) ;;
89           *) REPLACE_TRUNCF=1 ;;
90         esac
91       fi
92     ])
93   else
94     HAVE_DECL_TRUNCF=0
95   fi
96   if test $HAVE_DECL_TRUNCF = 0 || test $REPLACE_TRUNCF = 1; then
97     dnl No libraries are needed to link lib/truncf.c.
98     TRUNCF_LIBM=
99   fi
100   AC_SUBST([TRUNCF_LIBM])