New modules for common <math.h> functions.
[gnulib.git] / m4 / mathfunc.m4
1 # mathfunc.m4 serial 1
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([sqrt])
8 # tests whether the sqrt function is available in libc or libm. It sets
9 # SQRT_LIBM to empty or "-lm" accordingly.
10
11 AC_DEFUN([gl_MATHFUNC],
12 [
13   m4_pushdef([func], [$1])
14   m4_pushdef([FUNC], [translit([$1],[abcdefghijklmnopqrstuvwxyz],
15                                     [ABCDEFGHIJKLMNOPQRSTUVWXYZ])])
16   FUNC[]_LIBM=
17   AC_CACHE_CHECK([whether func() can be used without linking with libm],
18     [gl_cv_func_]func[_no_libm],
19     [
20       AC_LINK_IFELSE(
21         [AC_LANG_PROGRAM([[#ifndef __NO_MATH_INLINES
22                            # define __NO_MATH_INLINES 1 /* for glibc */
23                            #endif
24                            #include <math.h>
25                            double x;]],
26                          [[return ]func[ (x) > 2 || ]func[ (x) < 0.4;]])],
27         [gl_cv_func_]func[_no_libm=yes],
28         [gl_cv_func_]func[_no_libm=no])
29     ])
30   if test $gl_cv_func_[]func[]_no_libm = no; then
31     AC_CACHE_CHECK([whether func() can be used with libm],
32       [gl_cv_func_]func[_in_libm],
33       [
34         save_LIBS="$LIBS"
35         LIBS="$LIBS -lm"
36         AC_LINK_IFELSE(
37           [AC_LANG_PROGRAM([[#ifndef __NO_MATH_INLINES
38                              # define __NO_MATH_INLINES 1 /* for glibc */
39                              #endif
40                              #include <math.h>
41                              double x;]],
42                            [[return ]func[ (x) > 2 || ]func[ (x) < 0.4;]])],
43           [gl_cv_func_]func[_in_libm=yes],
44           [gl_cv_func_]func[_in_libm=no])
45         LIBS="$save_LIBS"
46       ])
47     if test $gl_cv_func_[]func[]_in_libm = yes; then
48       FUNC[]_LIBM=-lm
49     fi
50   fi
51   m4_popdef([FUNC])
52   m4_popdef([func])
53 ])