mathfunc.m4: Refactor.
[gnulib.git] / m4 / mathfunc.m4
1 # mathfunc.m4 serial 8
2 dnl Copyright (C) 2010-2011 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   m4_pushdef([ARGS], [m4_bpatsubst(
23                         [m4_bpatsubst(
24                            [m4_bpatsubst(
25                               [m4_bpatsubst(
26                                  [m4_bpatsubst(
27                                     [m4_bpatsubst(
28                                        [m4_bpatsubst(
29                                           [m4_bpatsubst(
30                                              [$3],
31                                              [int \*], [&i_ret])],
32                                           [float \*], [&f_ret])],
33                                        [double \*], [&d_ret])],
34                                     [long double \*], [&l_ret])],
35                                  [int], [2])],
36                               [float], [1.618034f])],
37                            [long double], [1.618033988749894848L])],
38                         [double], [1.6180339887])])
39   FUNC[]_LIBM=
40   AC_CACHE_CHECK([whether func() can be used without linking with libm],
41     [gl_cv_func_]func[_no_libm],
42     [
43       AC_LINK_IFELSE(
44         [AC_LANG_PROGRAM(
45            [[#ifndef __NO_MATH_INLINES
46              # define __NO_MATH_INLINES 1 /* for glibc */
47              #endif
48              #include <math.h>
49              $2 (*funcptr) $3 = ]func[;
50              int i_ret;
51              float f_ret;
52              double d_ret;
53              long double l_ret;]],
54            [[$2 y = funcptr ]ARGS[;
55              return y < 0.3 || y > 1.7;
56            ]])],
57         [gl_cv_func_]func[_no_libm=yes],
58         [gl_cv_func_]func[_no_libm=no])
59     ])
60   if test $gl_cv_func_[]func[]_no_libm = no; then
61     AC_CACHE_CHECK([whether func() can be used with libm],
62       [gl_cv_func_]func[_in_libm],
63       [
64         save_LIBS="$LIBS"
65         LIBS="$LIBS -lm"
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                $2 (*funcptr) $3 = ]func[;
73                int i_ret;
74                float f_ret;
75                double d_ret;
76                long double l_ret;]],
77              [[$2 y = funcptr ]ARGS[;
78                return y < 0.3 || y > 1.7;
79              ]])],
80           [gl_cv_func_]func[_in_libm=yes],
81           [gl_cv_func_]func[_in_libm=no])
82         LIBS="$save_LIBS"
83       ])
84     if test $gl_cv_func_[]func[]_in_libm = yes; then
85       FUNC[]_LIBM=-lm
86     fi
87   fi
88   AC_SUBST(FUNC[_LIBM])
89   m4_popdef([ARGS])
90   m4_popdef([FUNC])
91   m4_popdef([func])
92 ])
93
94 # gl_COMMON_DOUBLE_MATHFUNC(FUNC)
95 # -------------------------------
96 # tests whether the function FUNC is available in libc or libm.
97 # It sets FUNC_LIBM to empty or "-lm" accordingly.
98 # FUNC must be one of the following functions, that are present on all systems
99 # and provided by libm on all systems except MacOS X, BeOS, Haiku:
100 #   acos asin atan atan2 cbrt cos cosh erf erfc exp fmod hypot j0 j1 jn lgamma
101 #   log log10 log1p pow remainder sin sinh sqrt tan tanh y0 y1 yn
102
103 AC_DEFUN([gl_COMMON_DOUBLE_MATHFUNC],
104 [
105   AC_REQUIRE([gl_COMMON_DOUBLE_MATHFUNC_TEST])
106   m4_pushdef([FUNC], [m4_translit([$1],[abcdefghijklmnopqrstuvwxyz],
107                                        [ABCDEFGHIJKLMNOPQRSTUVWXYZ])])
108   FUNC[]_LIBM="$POW_LIBM"
109   AC_SUBST(FUNC[_LIBM])
110   m4_popdef([FUNC])
111 ])
112
113 AC_DEFUN([gl_COMMON_DOUBLE_MATHFUNC_TEST],
114 [
115   dnl We could use any of the following:
116   dnl gl_MATHFUNC([acos], [double], [(double)])
117   dnl gl_MATHFUNC([asin], [double], [(double)])
118   dnl gl_MATHFUNC([atan], [double], [(double)])
119   dnl gl_MATHFUNC([atan2], [double], [(double, double)])
120   dnl gl_MATHFUNC([cbrt], [double], [(double)])
121   dnl gl_MATHFUNC([cos], [double], [(double)])
122   dnl gl_MATHFUNC([cosh], [double], [(double)])
123   dnl gl_MATHFUNC([erf], [double], [(double)])
124   dnl gl_MATHFUNC([erfc], [double], [(double)])
125   dnl gl_MATHFUNC([exp], [double], [(double)])
126   dnl gl_MATHFUNC([fmod], [double], [(double, double)])
127   dnl gl_MATHFUNC([hypot], [double], [(double, double)])
128   dnl gl_MATHFUNC([j0], [double], [(double)])
129   dnl gl_MATHFUNC([j1], [double], [(double)])
130   dnl gl_MATHFUNC([jn], [double], [(int, double)])
131   dnl gl_MATHFUNC([lgamma], [double], [(double)])
132   dnl gl_MATHFUNC([log], [double], [(double)])
133   dnl gl_MATHFUNC([log10], [double], [(double)])
134   dnl gl_MATHFUNC([log1p], [double], [(double)])
135   dnl gl_MATHFUNC([pow], [double], [(double, double)])
136   dnl gl_MATHFUNC([remainder], [double], [(double, double)])
137   dnl gl_MATHFUNC([sin], [double], [(double)])
138   dnl gl_MATHFUNC([sinh], [double], [(double)])
139   dnl gl_MATHFUNC([sqrt], [double], [(double)])
140   dnl gl_MATHFUNC([tan], [double], [(double)])
141   dnl gl_MATHFUNC([tanh], [double], [(double)])
142   dnl gl_MATHFUNC([y0], [double], [(double)])
143   dnl gl_MATHFUNC([y1], [double], [(double)])
144   dnl gl_MATHFUNC([yn], [double], [(int, double)])
145   gl_MATHFUNC([pow], [double], [(double, double)])
146 ])