Oops, fix last commit so that it also works with modf().
[gnulib.git] / m4 / mathfunc.m4
1 # mathfunc.m4 serial 6
2 dnl Copyright (C) 2010 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.
6
7 # gl_MATHFUNC(FUNC, RETTYPE, PARAMTYPES)
8 # --------------------------------------------------
9 # tests whether the function FUNC is available in libc or libm.
10 # RETTYPE is the return type. PARAMTYPES is a parameter list, with parentheses.
11 # It sets FUNC_LIBM to empty or "-lm" accordingly.
12
13 AC_DEFUN([gl_MATHFUNC],
14 [
15   dnl We need the RETTYPE and PARAMTYPES in order to force linking with the
16   dnl function. With gcc >= 4.3 on glibc/x86_64, calls to the 'fabs' function
17   dnl are inlined by the compiler, therefore linking of these calls does not
18   dnl require -lm, but taking the function pointer of 'fabs' does.
19   m4_pushdef([func], [$1])
20   m4_pushdef([FUNC], [m4_translit([$1],[abcdefghijklmnopqrstuvwxyz],
21                                        [ABCDEFGHIJKLMNOPQRSTUVWXYZ])])
22   FUNC[]_LIBM=
23   AC_CACHE_CHECK([whether func() can be used without linking with libm],
24     [gl_cv_func_]func[_no_libm],
25     [
26       AC_LINK_IFELSE(
27         [AC_LANG_PROGRAM(
28            [[#ifndef __NO_MATH_INLINES
29              # define __NO_MATH_INLINES 1 /* for glibc */
30              #endif
31              #include <math.h>
32              $2 (*funcptr) $3 = ]func[;
33              double d_ret;]],
34            [[$2 y = funcptr ]m4_bpatsubst([m4_bpatsubst([m4_bpatsubst([$3], [int], [2])], [double \*], [&d_ret])], [double], [1.6180339887])[;
35              return y < 0.3 || y > 1.7;
36            ]])],
37         [gl_cv_func_]func[_no_libm=yes],
38         [gl_cv_func_]func[_no_libm=no])
39     ])
40   if test $gl_cv_func_[]func[]_no_libm = no; then
41     AC_CACHE_CHECK([whether func() can be used with libm],
42       [gl_cv_func_]func[_in_libm],
43       [
44         save_LIBS="$LIBS"
45         LIBS="$LIBS -lm"
46         AC_LINK_IFELSE(
47           [AC_LANG_PROGRAM(
48              [[#ifndef __NO_MATH_INLINES
49                # define __NO_MATH_INLINES 1 /* for glibc */
50                #endif
51                #include <math.h>
52                $2 (*funcptr) $3 = ]func[;
53                double d_ret;]],
54              [[$2 y = funcptr ]m4_bpatsubst([m4_bpatsubst([m4_bpatsubst([$3], [int], [2])], [double \*], [&d_ret])], [double], [1.6180339887])[;
55                return y < 0.3 || y > 1.7;
56              ]])],
57           [gl_cv_func_]func[_in_libm=yes],
58           [gl_cv_func_]func[_in_libm=no])
59         LIBS="$save_LIBS"
60       ])
61     if test $gl_cv_func_[]func[]_in_libm = yes; then
62       FUNC[]_LIBM=-lm
63     fi
64   fi
65   AC_SUBST(FUNC[_LIBM])
66   m4_popdef([FUNC])
67   m4_popdef([func])
68 ])
69
70 # gl_COMMON_DOUBLE_MATHFUNC(FUNC)
71 # -------------------------------
72 # tests whether the function FUNC is available in libc or libm.
73 # It sets FUNC_LIBM to empty or "-lm" accordingly.
74 # FUNC must be one of the following functions, that are present on all systems
75 # and provided by libm on all systems except MacOS X, BeOS, Haiku:
76 #   acos asin atan atan2 cbrt cos cosh erf erfc exp fmod hypot j0 j1 jn lgamma
77 #   log log10 log1p pow remainder sin sinh sqrt tan tanh y0 y1 yn
78
79 AC_DEFUN([gl_COMMON_DOUBLE_MATHFUNC],
80 [
81   AC_REQUIRE([gl_COMMON_DOUBLE_MATHFUNC_TEST])
82   m4_pushdef([FUNC], [m4_translit([$1],[abcdefghijklmnopqrstuvwxyz],
83                                        [ABCDEFGHIJKLMNOPQRSTUVWXYZ])])
84   FUNC[]_LIBM="$POW_LIBM"
85   AC_SUBST(FUNC[_LIBM])
86   m4_popdef([FUNC])
87 ])
88
89 AC_DEFUN([gl_COMMON_DOUBLE_MATHFUNC_TEST],
90 [
91   dnl We could use any of the following:
92   dnl gl_MATHFUNC([acos], [double], [(double)])
93   dnl gl_MATHFUNC([asin], [double], [(double)])
94   dnl gl_MATHFUNC([atan], [double], [(double)])
95   dnl gl_MATHFUNC([atan2], [double], [(double, double)])
96   dnl gl_MATHFUNC([cbrt], [double], [(double)])
97   dnl gl_MATHFUNC([cos], [double], [(double)])
98   dnl gl_MATHFUNC([cosh], [double], [(double)])
99   dnl gl_MATHFUNC([erf], [double], [(double)])
100   dnl gl_MATHFUNC([erfc], [double], [(double)])
101   dnl gl_MATHFUNC([exp], [double], [(double)])
102   dnl gl_MATHFUNC([fmod], [double], [(double, double)])
103   dnl gl_MATHFUNC([hypot], [double], [(double, double)])
104   dnl gl_MATHFUNC([j0], [double], [(double)])
105   dnl gl_MATHFUNC([j1], [double], [(double)])
106   dnl gl_MATHFUNC([jn], [double], [(int, double)])
107   dnl gl_MATHFUNC([lgamma], [double], [(double)])
108   dnl gl_MATHFUNC([log], [double], [(double)])
109   dnl gl_MATHFUNC([log10], [double], [(double)])
110   dnl gl_MATHFUNC([log1p], [double], [(double)])
111   dnl gl_MATHFUNC([pow], [double], [(double, double)])
112   dnl gl_MATHFUNC([remainder], [double], [(double, double)])
113   dnl gl_MATHFUNC([sin], [double], [(double)])
114   dnl gl_MATHFUNC([sinh], [double], [(double)])
115   dnl gl_MATHFUNC([sqrt], [double], [(double)])
116   dnl gl_MATHFUNC([tan], [double], [(double)])
117   dnl gl_MATHFUNC([tanh], [double], [(double)])
118   dnl gl_MATHFUNC([y0], [double], [(double)])
119   dnl gl_MATHFUNC([y1], [double], [(double)])
120   dnl gl_MATHFUNC([yn], [double], [(int, double)])
121   gl_MATHFUNC([pow], [double], [(double, double)])
122 ])