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