usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / m4 / mathfunc.m4
blobd9a37e215a459673da57518717a4e0e527258ec9
1 # mathfunc.m4
2 # serial 18
3 dnl Copyright (C) 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 # gl_MATHFUNC(FUNC, RETTYPE, PARAMTYPES [, INCLUDES] [, EXTRA-CODE])
9 # ------------------------------------------------------------------
10 # tests whether the function FUNC is available in libc or libm.
11 # RETTYPE is the return type. PARAMTYPES is a parameter list, with parentheses.
12 # It sets FUNC_LIBM to empty or "-lm" accordingly.
14 AC_DEFUN([gl_MATHFUNC],
16   dnl We need the RETTYPE and PARAMTYPES in order to force linking with the
17   dnl function.
18   dnl 1) With gcc >= 4.3 on glibc/x86_64, calls to the 'fabs' function
19   dnl    are inlined by the compiler, therefore linking of these calls does
20   dnl    not require -lm, but taking the function pointer of 'fabs' does.
21   dnl 2) On MSVC 9, many math functions exist only as macros with arguments,
22   dnl    whereas the function pointer is undefined.
23   dnl On the other hand, taking just the function pointer is not enough.
24   dnl 1) On AIX 7.1, when 'long double' is 128 bit large ("xlc -qldbl128" or
25   dnl    "xlc -qlongdouble" or "gcc -mlong-double-128") many math functions
26   dnl    exist as macros with arguments, that may reference libm or even
27   dnl    completely undefined functions such as __rint128.
28   dnl 2) In AIX 7.1 with gcc 4.2, when optimization is turned on, calls to
29   dnl    rint() with simple arguments are turned into rintf() calls by the
30   dnl    compiler. But while rint() is resides in libc, rintf() is in libm.
31   m4_pushdef([func], [$1])
32   m4_pushdef([FUNC], [m4_translit([$1],[abcdefghijklmnopqrstuvwxyz],
33                                        [ABCDEFGHIJKLMNOPQRSTUVWXYZ])])
34   m4_pushdef([ARGS], [m4_bpatsubst(
35                         [m4_bpatsubst(
36                            [m4_bpatsubst(
37                               [m4_bpatsubst(
38                                  [m4_bpatsubst(
39                                     [m4_bpatsubst(
40                                        [m4_bpatsubst(
41                                           [m4_bpatsubst(
42                                              [m4_bpatsubst(
43                                                 [m4_bpatsubst(
44                                                    [m4_bpatsubst(
45                                                       [m4_bpatsubst(
46                                                          [m4_bpatsubst(
47                                                             [$3],
48                                                             [fenv_t\( const\)? \*], [&fenv_ret])],
49                                                          [fexcept_t\( const\)? \*], [&fx_ret])],
50                                                       [int\( const\)? \*],
51                                                       [&i_ret])],
52                                                    [float\( const\)? \*], [&f_ret])],
53                                                 [double\( const\)? \*], [&d_ret])],
54                                              [long double\( const\)? \*], [&l_ret])],
55                                           [fp_rnd], [1])],
56                                        [fp_except_t], [1])],
57                                     [int], [2])],
58                                  [float], [1.618034f])],
59                               [long double], [1.618033988749894848L])],
60                            [double], [1.6180339887])],
61                         [void], [])])
62   FUNC[]_LIBM=
63   AC_CACHE_CHECK([whether func() can be used without linking with libm],
64     [gl_cv_func_]func[_no_libm],
65     [
66       AC_LINK_IFELSE(
67         [AC_LANG_PROGRAM(
68            [[#ifndef __NO_MATH_INLINES
69              # define __NO_MATH_INLINES 1 /* for glibc */
70              #endif
71              #include <math.h>
72              $4
73              $2 (* volatile funcptr) $3 = ]func[;
74              int i_ret;
75              float f_ret;
76              double d_ret;
77              long double l_ret;]],
78            [[$2 y = funcptr ]ARGS[ + ]func[ ]ARGS[;
79              $5
80              return y < 0.3 || y > 1.7;
81            ]])],
82         [gl_cv_func_]func[_no_libm=yes],
83         [gl_cv_func_]func[_no_libm=no])
84     ])
85   if test $gl_cv_func_[]func[]_no_libm = no; then
86     AC_CACHE_CHECK([whether func() can be used with libm],
87       [gl_cv_func_]func[_in_libm],
88       [
89         saved_LIBS="$LIBS"
90         LIBS="$LIBS -lm"
91         AC_LINK_IFELSE(
92           [AC_LANG_PROGRAM(
93              [[#ifndef __NO_MATH_INLINES
94                # define __NO_MATH_INLINES 1 /* for glibc */
95                #endif
96                #include <math.h>
97                $4
98                $2 (* volatile funcptr) $3 = ]func[;
99                int i_ret;
100                float f_ret;
101                double d_ret;
102                long double l_ret;]],
103              [[$2 y = funcptr ]ARGS[ + ]func[ ]ARGS[;
104                $5
105                return y < 0.3 || y > 1.7;
106              ]])],
107           [gl_cv_func_]func[_in_libm=yes],
108           [gl_cv_func_]func[_in_libm=no])
109         LIBS="$saved_LIBS"
110       ])
111     if test $gl_cv_func_[]func[]_in_libm = yes; then
112       FUNC[]_LIBM=-lm
113     fi
114   fi
115   AC_SUBST(FUNC[_LIBM])
116   m4_popdef([ARGS])
117   m4_popdef([FUNC])
118   m4_popdef([func])
121 # gl_COMMON_DOUBLE_MATHFUNC(FUNC)
122 # -------------------------------
123 # tests whether the function FUNC is available in libc or libm.
124 # It sets FUNC_LIBM to empty or "-lm" accordingly.
125 # FUNC must be one of the following functions, that are present on all systems
126 # and provided by libm on all systems except Mac OS X, BeOS, Haiku:
127 #   acos asin atan atan2 cbrt cos cosh erf erfc exp fmod hypot j0 j1 jn lgamma
128 #   log log10 log1p pow remainder sin sinh sqrt tan tanh y0 y1 yn
130 AC_DEFUN([gl_COMMON_DOUBLE_MATHFUNC],
132   AC_REQUIRE([gl_COMMON_DOUBLE_MATHFUNC_TEST])
133   m4_pushdef([FUNC], [m4_translit([$1],[abcdefghijklmnopqrstuvwxyz],
134                                        [ABCDEFGHIJKLMNOPQRSTUVWXYZ])])
135   FUNC[]_LIBM="$POW_LIBM"
136   AC_SUBST(FUNC[_LIBM])
137   m4_popdef([FUNC])
140 AC_DEFUN([gl_COMMON_DOUBLE_MATHFUNC_TEST],
142   dnl We could use any of the following:
143   dnl gl_MATHFUNC([acos], [double], [(double)])
144   dnl gl_MATHFUNC([asin], [double], [(double)])
145   dnl gl_MATHFUNC([atan], [double], [(double)])
146   dnl gl_MATHFUNC([atan2], [double], [(double, double)])
147   dnl gl_MATHFUNC([cbrt], [double], [(double)])
148   dnl gl_MATHFUNC([cos], [double], [(double)])
149   dnl gl_MATHFUNC([cosh], [double], [(double)])
150   dnl gl_MATHFUNC([erf], [double], [(double)])
151   dnl gl_MATHFUNC([erfc], [double], [(double)])
152   dnl gl_MATHFUNC([exp], [double], [(double)])
153   dnl gl_MATHFUNC([fmod], [double], [(double, double)])
154   dnl gl_MATHFUNC([hypot], [double], [(double, double)])
155   dnl gl_MATHFUNC([j0], [double], [(double)])
156   dnl gl_MATHFUNC([j1], [double], [(double)])
157   dnl gl_MATHFUNC([jn], [double], [(int, double)])
158   dnl gl_MATHFUNC([lgamma], [double], [(double)])
159   dnl gl_MATHFUNC([log], [double], [(double)])
160   dnl gl_MATHFUNC([log10], [double], [(double)])
161   dnl gl_MATHFUNC([log1p], [double], [(double)])
162   dnl gl_MATHFUNC([pow], [double], [(double, double)])
163   dnl gl_MATHFUNC([remainder], [double], [(double, double)])
164   dnl gl_MATHFUNC([sin], [double], [(double)])
165   dnl gl_MATHFUNC([sinh], [double], [(double)])
166   dnl gl_MATHFUNC([sqrt], [double], [(double)])
167   dnl gl_MATHFUNC([tan], [double], [(double)])
168   dnl gl_MATHFUNC([tanh], [double], [(double)])
169   dnl gl_MATHFUNC([y0], [double], [(double)])
170   dnl gl_MATHFUNC([y1], [double], [(double)])
171   dnl gl_MATHFUNC([yn], [double], [(int, double)])
172   gl_MATHFUNC([pow], [double], [(double, double)])