utimens: port to NetBSD-10.99.12/amd64
[gnulib.git] / m4 / trunc.m4
blobcd747a48938cee0939997647a125e7be8bf80ef1
1 # trunc.m4
2 # serial 18
3 dnl Copyright (C) 2007, 2010-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
8 AC_DEFUN([gl_FUNC_TRUNC],
10   m4_divert_text([DEFAULTS], [gl_trunc_required=plain])
11   AC_REQUIRE([gl_MATH_H_DEFAULTS])
12   dnl Persuade glibc <math.h> to declare trunc().
13   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
14   dnl Test whether trunc() is declared.
15   AC_CHECK_DECLS([trunc], , , [[#include <math.h>]])
16   if test "$ac_cv_have_decl_trunc" = yes; then
17     dnl Test whether trunc() can be used without libm.
18     TRUNC_LIBM=?
19     AC_LINK_IFELSE(
20       [AC_LANG_PROGRAM(
21          [[#ifndef __NO_MATH_INLINES
22            # define __NO_MATH_INLINES 1 /* for glibc */
23            #endif
24            #include <math.h>
25            double (*funcptr) (double) = trunc;
26            double x;]],
27          [[x = funcptr(x) + trunc(x);]])],
28       [TRUNC_LIBM=])
29     if test "$TRUNC_LIBM" = "?"; then
30       saved_LIBS="$LIBS"
31       LIBS="$LIBS -lm"
32       AC_LINK_IFELSE(
33         [AC_LANG_PROGRAM(
34            [[#ifndef __NO_MATH_INLINES
35              # define __NO_MATH_INLINES 1 /* for glibc */
36              #endif
37              #include <math.h>
38              double (*funcptr) (double) = trunc;
39              double x;]],
40            [[x = funcptr(x) + trunc(x);]])],
41         [TRUNC_LIBM="-lm"])
42       LIBS="$saved_LIBS"
43     fi
44     if test "$TRUNC_LIBM" = "?"; then
45       TRUNC_LIBM=
46     fi
47     m4_ifdef([gl_FUNC_TRUNC_IEEE], [
48       if test $gl_trunc_required = ieee && test $REPLACE_TRUNC = 0; then
49         AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
50         AC_CACHE_CHECK([whether trunc works according to ISO C 99 with IEC 60559],
51           [gl_cv_func_trunc_ieee],
52           [
53             saved_LIBS="$LIBS"
54             LIBS="$LIBS $TRUNC_LIBM"
55             AC_RUN_IFELSE(
56               [AC_LANG_SOURCE([[
57 #ifndef __NO_MATH_INLINES
58 # define __NO_MATH_INLINES 1 /* for glibc */
59 #endif
60 #include <math.h>
61 ]gl_DOUBLE_MINUS_ZERO_CODE[
62 ]gl_DOUBLE_SIGNBIT_CODE[
63 static double dummy (double f) { return 0; }
64 int main (int argc, char *argv[])
66   double (* volatile my_trunc) (double) = argc ? trunc : dummy;
67   /* Test whether trunc (-0.0) is -0.0.  */
68   if (signbitd (minus_zerod) && !signbitd (my_trunc (minus_zerod)))
69     return 1;
70   return 0;
72               ]])],
73               [gl_cv_func_trunc_ieee=yes],
74               [gl_cv_func_trunc_ieee=no],
75               [case "$host_os" in
76                                      # Guess yes on glibc systems.
77                  *-gnu* | gnu*)      gl_cv_func_trunc_ieee="guessing yes" ;;
78                                      # Guess yes on musl systems.
79                  *-musl* | midipix*) gl_cv_func_trunc_ieee="guessing yes" ;;
80                                      # Guess yes on native Windows.
81                  mingw* | windows*)  gl_cv_func_trunc_ieee="guessing yes" ;;
82                                      # If we don't know, obey --enable-cross-guesses.
83                  *)                  gl_cv_func_trunc_ieee="$gl_cross_guess_normal" ;;
84                esac
85               ])
86             LIBS="$saved_LIBS"
87           ])
88         case "$gl_cv_func_trunc_ieee" in
89           *yes) ;;
90           *) REPLACE_TRUNC=1 ;;
91         esac
92       fi
93     ])
94   else
95     HAVE_DECL_TRUNC=0
96   fi
97   if test $HAVE_DECL_TRUNC = 0 || test $REPLACE_TRUNC = 1; then
98     dnl No libraries are needed to link lib/trunc.c.
99     TRUNC_LIBM=
100   fi
101   AC_SUBST([TRUNC_LIBM])