summaryrefslogtreecommitdiff
path: root/m4/mathfunc.m4
blob: 4ba6b02035a7c4ea32b3ad39133c28e42d5cf77d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# mathfunc.m4 serial 12
dnl Copyright (C) 2010-2020 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.

# gl_MATHFUNC(FUNC, RETTYPE, PARAMTYPES [, INCLUDES] [, EXTRA-CODE])
# ------------------------------------------------------------------
# tests whether the function FUNC is available in libc or libm.
# RETTYPE is the return type. PARAMTYPES is a parameter list, with parentheses.
# It sets FUNC_LIBM to empty or "-lm" accordingly.

AC_DEFUN([gl_MATHFUNC],
[
  dnl We need the RETTYPE and PARAMTYPES in order to force linking with the
  dnl function.
  dnl 1) With gcc >= 4.3 on glibc/x86_64, calls to the 'fabs' function
  dnl    are inlined by the compiler, therefore linking of these calls does
  dnl    not require -lm, but taking the function pointer of 'fabs' does.
  dnl 2) On MSVC 9, many math functions exist only as macros with arguments,
  dnl    whereas the function pointer is undefined.
  dnl On the other hand, taking just the function pointer is not enough.
  dnl 1) On AIX 7.1, when 'long double' is 128 bit large ("xlc -qldbl128" or
  dnl    "xlc -qlongdouble" or "gcc -mlong-double-128") many math functions
  dnl    exist as macros with arguments, that may reference libm or even
  dnl    completely undefined functions such as __rint128.
  dnl 2) In AIX 7.1 with gcc 4.2, when optimization is turned on, calls to
  dnl    rint() with simple arguments are turned into rintf() calls by the
  dnl    compiler. But while rint() is resides in libc, rintf() is in libm.
  m4_pushdef([func], [$1])
  m4_pushdef([FUNC], [m4_translit([$1],[abcdefghijklmnopqrstuvwxyz],
                                       [ABCDEFGHIJKLMNOPQRSTUVWXYZ])])
  m4_pushdef([ARGS], [m4_bpatsubst(
                        [m4_bpatsubst(
                           [m4_bpatsubst(
                              [m4_bpatsubst(
                                 [m4_bpatsubst(
                                    [m4_bpatsubst(
                                       [m4_bpatsubst(
                                          [m4_bpatsubst(
                                             [m4_bpatsubst(
                                                [$3],
                                                [int \*], [&i_ret])],
                                             [float \*], [&f_ret])],
                                          [double \*], [&d_ret])],
                                       [long double \*], [&l_ret])],
                                    [int], [2])],
                                 [float], [1.618034f])],
                              [long double], [1.618033988749894848L])],
                           [double], [1.6180339887])],
                        [void], [])])
  FUNC[]_LIBM=
  AC_CACHE_CHECK([whether func() can be used without linking with libm],
    [gl_cv_func_]func[_no_libm],
    [
      AC_LINK_IFELSE(
        [AC_LANG_PROGRAM(
           [[#ifndef __NO_MATH_INLINES
             # define __NO_MATH_INLINES 1 /* for glibc */
             #endif
             #include <math.h>
             $4
             $2 (* volatile funcptr) $3 = ]func[;
             int i_ret;
             float f_ret;
             double d_ret;
             long double l_ret;]],
           [[$2 y = funcptr ]ARGS[ + ]func[ ]ARGS[;
             $5
             return y < 0.3 || y > 1.7;
           ]])],
        [gl_cv_func_]func[_no_libm=yes],
        [gl_cv_func_]func[_no_libm=no])
    ])
  if test $gl_cv_func_[]func[]_no_libm = no; then
    AC_CACHE_CHECK([whether func() can be used with libm],
      [gl_cv_func_]func[_in_libm],
      [
        save_LIBS="$LIBS"
        LIBS="$LIBS -lm"
        AC_LINK_IFELSE(
          [AC_LANG_PROGRAM(
             [[#ifndef __NO_MATH_INLINES
               # define __NO_MATH_INLINES 1 /* for glibc */
               #endif
               #include <math.h>
               $4
               $2 (* volatile funcptr) $3 = ]func[;
               int i_ret;
               float f_ret;
               double d_ret;
               long double l_ret;]],
             [[$2 y = funcptr ]ARGS[ + ]func[ ]ARGS[;
               $5
               return y < 0.3 || y > 1.7;
             ]])],
          [gl_cv_func_]func[_in_libm=yes],
          [gl_cv_func_]func[_in_libm=no])
        LIBS="$save_LIBS"
      ])
    if test $gl_cv_func_[]func[]_in_libm = yes; then
      FUNC[]_LIBM=-lm
    fi
  fi
  AC_SUBST(FUNC[_LIBM])
  m4_popdef([ARGS])
  m4_popdef([FUNC])
  m4_popdef([func])
])

# gl_COMMON_DOUBLE_MATHFUNC(FUNC)
# -------------------------------
# tests whether the function FUNC is available in libc or libm.
# It sets FUNC_LIBM to empty or "-lm" accordingly.
# FUNC must be one of the following functions, that are present on all systems
# and provided by libm on all systems except Mac OS X, BeOS, Haiku:
#   acos asin atan atan2 cbrt cos cosh erf erfc exp fmod hypot j0 j1 jn lgamma
#   log log10 log1p pow remainder sin sinh sqrt tan tanh y0 y1 yn

AC_DEFUN([gl_COMMON_DOUBLE_MATHFUNC],
[
  AC_REQUIRE([gl_COMMON_DOUBLE_MATHFUNC_TEST])
  m4_pushdef([FUNC], [m4_translit([$1],[abcdefghijklmnopqrstuvwxyz],
                                       [ABCDEFGHIJKLMNOPQRSTUVWXYZ])])
  FUNC[]_LIBM="$POW_LIBM"
  AC_SUBST(FUNC[_LIBM])
  m4_popdef([FUNC])
])

AC_DEFUN([gl_COMMON_DOUBLE_MATHFUNC_TEST],
[
  dnl We could use any of the following:
  dnl gl_MATHFUNC([acos], [double], [(double)])
  dnl gl_MATHFUNC([asin], [double], [(double)])
  dnl gl_MATHFUNC([atan], [double], [(double)])
  dnl gl_MATHFUNC([atan2], [double], [(double, double)])
  dnl gl_MATHFUNC([cbrt], [double], [(double)])
  dnl gl_MATHFUNC([cos], [double], [(double)])
  dnl gl_MATHFUNC([cosh], [double], [(double)])
  dnl gl_MATHFUNC([erf], [double], [(double)])
  dnl gl_MATHFUNC([erfc], [double], [(double)])
  dnl gl_MATHFUNC([exp], [double], [(double)])
  dnl gl_MATHFUNC([fmod], [double], [(double, double)])
  dnl gl_MATHFUNC([hypot], [double], [(double, double)])
  dnl gl_MATHFUNC([j0], [double], [(double)])
  dnl gl_MATHFUNC([j1], [double], [(double)])
  dnl gl_MATHFUNC([jn], [double], [(int, double)])
  dnl gl_MATHFUNC([lgamma], [double], [(double)])
  dnl gl_MATHFUNC([log], [double], [(double)])
  dnl gl_MATHFUNC([log10], [double], [(double)])
  dnl gl_MATHFUNC([log1p], [double], [(double)])
  dnl gl_MATHFUNC([pow], [double], [(double, double)])
  dnl gl_MATHFUNC([remainder], [double], [(double, double)])
  dnl gl_MATHFUNC([sin], [double], [(double)])
  dnl gl_MATHFUNC([sinh], [double], [(double)])
  dnl gl_MATHFUNC([sqrt], [double], [(double)])
  dnl gl_MATHFUNC([tan], [double], [(double)])
  dnl gl_MATHFUNC([tanh], [double], [(double)])
  dnl gl_MATHFUNC([y0], [double], [(double)])
  dnl gl_MATHFUNC([y1], [double], [(double)])
  dnl gl_MATHFUNC([yn], [double], [(int, double)])
  gl_MATHFUNC([pow], [double], [(double, double)])
])