modff-ieee: Work around test failures on *BSD, IRIX, OSF/1, etc.
[gnulib.git] / m4 / modff.m4
1 # modff.m4 serial 3
2 dnl Copyright (C) 2011-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_MODFF],
8 [
9   m4_divert_text([DEFAULTS], [gl_modff_required=plain])
10   AC_REQUIRE([gl_MATH_H_DEFAULTS])
11   AC_REQUIRE([gl_FUNC_MODF])
12
13   dnl Test whether modff() exists. We cannot assume that modff(), if it
14   dnl exists, is defined in the same library as modf(). This is not the case
15   dnl on FreeBSD, NetBSD, OpenBSD.
16   gl_MATHFUNC([modff], [float], [(float, float *)])
17   if test $gl_cv_func_modff_no_libm = yes \
18      || test $gl_cv_func_modff_in_libm = yes; then
19     m4_ifdef([gl_FUNC_MODFF_IEEE], [
20       if test $gl_modff_required = ieee && test $REPLACE_MODFF = 0; then
21         AC_CACHE_CHECK([whether modff works according to ISO C 99 with IEC 60559],
22           [gl_cv_func_modff_ieee],
23           [
24             save_LIBS="$LIBS"
25             LIBS="$LIBS $MODFF_LIBM"
26             AC_RUN_IFELSE(
27               [AC_LANG_SOURCE([[
28 #ifndef __NO_MATH_INLINES
29 # define __NO_MATH_INLINES 1 /* for glibc */
30 #endif
31 #include <math.h>
32 ]gl_FLOAT_MINUS_ZERO_CODE[
33 ]gl_FLOAT_SIGNBIT_CODE[
34 /* Compare two numbers with ==.
35    This is a separate function because IRIX 6.5 "cc -O" miscompiles an
36    'x == x' test.  */
37 static int
38 numeric_equal (float x, float y)
39 {
40   return x == y;
41 }
42 static float dummy (float x, float *iptr) { return 0; }
43 float zero;
44 float minus_one = - 1.0f;
45 int main (int argc, char *argv[])
46 {
47   float (*my_modff) (float, float *) = argc ? modff : dummy;
48   int result = 0;
49   float i;
50   float f;
51   /* Test modff(NaN,...).
52      This test fails on NetBSD 5.1, OpenBSD 4.9, Solaris 9, Cygwin.  */
53   f = my_modff (zero / zero, &i);
54   if (numeric_equal (f, f))
55     result |= 1;
56   /* Test modff(-Inf,...).
57      This test fails on IRIX 6.5, OSF/1 5.1, mingw.  */
58   f = my_modff (minus_one / zero, &i);
59   if (!(f == 0.0f) || (signbitf (minus_zerof) && !signbitf (f)))
60     result |= 2;
61   return result;
62 }
63               ]])],
64               [gl_cv_func_modff_ieee=yes],
65               [gl_cv_func_modff_ieee=no],
66               [gl_cv_func_modff_ieee="guessing no"])
67             LIBS="$save_LIBS"
68           ])
69         case "$gl_cv_func_modff_ieee" in
70           *yes) ;;
71           *) REPLACE_MODFF=1 ;;
72         esac
73       fi
74     ])
75   else
76     HAVE_MODFF=0
77   fi
78   if test $HAVE_MODFF = 0 || test $REPLACE_MODFF = 1; then
79     dnl Find libraries needed to link lib/modff.c.
80     MODFF_LIBM="$MODF_LIBM"
81   fi
82   AC_SUBST([MODFF_LIBM])
83 ])