Debian Codegraph @

m4/mathfunc.m4

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