sqrtl: Bypass broken implementation in OpenBSD 5.1/SPARC.
[gnulib.git] / m4 / sqrtl.m4
1 # sqrtl.m4 serial 8
2 dnl Copyright (C) 2010-2012 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 AC_DEFUN([gl_FUNC_SQRTL],
8 [
9   AC_REQUIRE([gl_MATH_H_DEFAULTS])
10   AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
11
12   dnl Persuade glibc <math.h> to declare sqrtl().
13   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
14
15   SQRTL_LIBM=
16   AC_CACHE_CHECK([whether sqrtl() can be used without linking with libm],
17     [gl_cv_func_sqrtl_no_libm],
18     [
19       AC_LINK_IFELSE(
20         [AC_LANG_PROGRAM(
21            [[#ifndef __NO_MATH_INLINES
22              # define __NO_MATH_INLINES 1 /* for glibc */
23              #endif
24              #include <math.h>
25              long double (*funcptr) (long double) = sqrtl;
26              long double x;]],
27            [[return funcptr (x) > 0.4
28                     || sqrtl (x) > 0.4;]])],
29         [gl_cv_func_sqrtl_no_libm=yes],
30         [gl_cv_func_sqrtl_no_libm=no])
31     ])
32   if test $gl_cv_func_sqrtl_no_libm = no; then
33     AC_CACHE_CHECK([whether sqrtl() can be used with libm],
34       [gl_cv_func_sqrtl_in_libm],
35       [
36         save_LIBS="$LIBS"
37         LIBS="$LIBS -lm"
38         AC_LINK_IFELSE(
39           [AC_LANG_PROGRAM(
40              [[#ifndef __NO_MATH_INLINES
41                # define __NO_MATH_INLINES 1 /* for glibc */
42                #endif
43                #include <math.h>
44                long double (*funcptr) (long double) = sqrtl;
45                long double x;]],
46              [[return funcptr (x) > 0.4
47                       || sqrtl (x) > 0.4;]])],
48           [gl_cv_func_sqrtl_in_libm=yes],
49           [gl_cv_func_sqrtl_in_libm=no])
50         LIBS="$save_LIBS"
51       ])
52     if test $gl_cv_func_sqrtl_in_libm = yes; then
53       SQRTL_LIBM=-lm
54     fi
55   fi
56   if test $gl_cv_func_sqrtl_no_libm = yes \
57      || test $gl_cv_func_sqrtl_in_libm = yes; then
58     dnl Also check whether it's declared.
59     dnl MacOS X 10.3 has sqrtl() in libc but doesn't declare it in <math.h>.
60     AC_CHECK_DECL([sqrtl], , [HAVE_DECL_SQRTL=0], [[#include <math.h>]])
61
62     save_LIBS="$LIBS"
63     LIBS="$LIBS $SQRTL_LIBM"
64     gl_FUNC_SQRTL_WORKS
65     LIBS="$save_LIBS"
66     case "$gl_cv_func_sqrtl_works" in
67       *yes) ;;
68       *) REPLACE_SQRTL=1 ;;
69     esac
70   else
71     HAVE_DECL_SQRTL=0
72     HAVE_SQRTL=0
73   fi
74   if test $HAVE_SQRTL = 0 || test $REPLACE_SQRTL = 1; then
75     dnl Find libraries needed to link lib/sqrtl.c.
76     if test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 1; then
77       AC_REQUIRE([gl_FUNC_SQRT])
78       SQRTL_LIBM="$SQRT_LIBM"
79     else
80       AC_REQUIRE([gl_FUNC_ISNANL])
81       AC_REQUIRE([gl_FUNC_FREXPL])
82       AC_REQUIRE([gl_FUNC_LDEXPL])
83       AC_REQUIRE([gl_FUNC_SQRT])
84       dnl Append $ISNANL_LIBM to SQRTL_LIBM, avoiding gratuitous duplicates.
85       case " $SQRTL_LIBM " in
86         *" $ISNANL_LIBM "*) ;;
87         *) SQRTL_LIBM="$SQRTL_LIBM $ISNANL_LIBM" ;;
88       esac
89       dnl Append $FREXPL_LIBM to SQRTL_LIBM, avoiding gratuitous duplicates.
90       case " $SQRTL_LIBM " in
91         *" $FREXPL_LIBM "*) ;;
92         *) SQRTL_LIBM="$SQRTL_LIBM $FREXPL_LIBM" ;;
93       esac
94       dnl Append $LDEXPL_LIBM to SQRTL_LIBM, avoiding gratuitous duplicates.
95       case " $SQRTL_LIBM " in
96         *" $LDEXPL_LIBM "*) ;;
97         *) SQRTL_LIBM="$SQRTL_LIBM $LDEXPL_LIBM" ;;
98       esac
99       dnl Append $SQRT_LIBM to SQRTL_LIBM, avoiding gratuitous duplicates.
100       case " $SQRTL_LIBM " in
101         *" $SQRT_LIBM "*) ;;
102         *) SQRTL_LIBM="$SQRTL_LIBM $SQRT_LIBM" ;;
103       esac
104     fi
105   fi
106   AC_SUBST([SQRTL_LIBM])
107 ])
108
109 dnl Test whether sqrtl() works.
110 dnl On OpenBSD 5.1/SPARC, sqrtl(8.1974099812331540680810141969554806865L) has
111 dnl rounding errors that eat up the last 8 to 9 decimal digits.
112 AC_DEFUN([gl_FUNC_SQRTL_WORKS],
113 [
114   AC_REQUIRE([AC_PROG_CC])
115   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
116   AC_CACHE_CHECK([whether sqrtl works], [gl_cv_func_sqrtl_works],
117     [
118       AC_RUN_IFELSE(
119         [AC_LANG_SOURCE([[
120 #include <float.h>
121 #include <math.h>
122 extern
123 #ifdef __cplusplus
124 "C"
125 #endif
126 long double sqrtl (long double);
127 static long double
128 my_ldexpl (long double x, int d)
129 {
130   for (; d > 0; d--)
131     x *= 2.0L;
132   for (; d < 0; d++)
133     x *= 0.5L;
134   return x;
135 }
136 volatile long double x;
137 volatile long double y;
138 long double z;
139 int main ()
140 {
141   x = 8.1974099812331540680810141969554806865L;
142   y = sqrtl (x);
143   z = y * y - x;
144   z = my_ldexpl (z, LDBL_MANT_DIG);
145   if (z < 0)
146     z = - z;
147   if (z > 100.0L)
148     return 1;
149   return 0;
150 }
151 ]])],
152         [gl_cv_func_sqrtl_works=yes],
153         [gl_cv_func_sqrtl_works=no],
154         [case "$host_os" in
155            osf*) gl_cv_func_sqrtl_works="guessing no";;
156            *)    gl_cv_func_sqrtl_works="guessing yes";;
157          esac
158         ])
159     ])
160 ])