New module 'isnanf-nolibm'.
[gnulib.git] / m4 / isnanf.m4
1 # isnanf.m4 serial 1
2 dnl Copyright (C) 2007 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 dnl Check how to get or define isnanf() without linking with libm.
8
9 AC_DEFUN([gl_FUNC_ISNANF_NO_LIBM],
10 [
11   gl_HAVE_ISNANF_NO_LIBM
12   if test $gl_cv_func_isnanf_no_libm = yes; then
13     AC_DEFINE([HAVE_ISNANF_IN_LIBC], 1,
14       [Define if the isnan(float) function is available in libc.])
15   else
16     AC_LIBOBJ([isnanf])
17     gl_FLOAT_EXPONENT_LOCATION
18   fi
19 ])
20
21 dnl Test whether isnanf() can be used without libm.
22 AC_DEFUN([gl_HAVE_ISNANF_NO_LIBM],
23 [
24   AC_CACHE_CHECK([whether isnan(float) can be used without linking with libm],
25     [gl_cv_func_isnanf_no_libm],
26     [
27       AC_TRY_LINK([#include <math.h>
28                    #ifdef isnan
29                    # undef isnanf
30                    # define isnanf(x) isnan ((float)(x))
31                    #endif
32                    float x;],
33                   [return isnanf (x);],
34         [gl_cv_func_isnanf_no_libm=yes],
35         [gl_cv_func_isnanf_no_libm=no])
36     ])
37 ])
38
39 AC_DEFUN([gl_FLOAT_EXPONENT_LOCATION],
40 [
41   AC_CACHE_CHECK([where to find the exponent in a 'float'],
42     [gl_cv_cc_float_expbit0],
43     [
44       AC_TRY_RUN([
45 #include <float.h>
46 #include <stddef.h>
47 #include <stdio.h>
48 #include <string.h>
49 #define NWORDS \
50   ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
51 typedef union { float value; unsigned int word[NWORDS]; } memory_float;
52 static unsigned int ored_words[NWORDS];
53 static unsigned int anded_words[NWORDS];
54 static void add_to_ored_words (float x)
55 {
56   memory_float m;
57   size_t i;
58   /* Clear it first, in case
59      sizeof (float) < sizeof (memory_float).  */
60   memset (&m, 0, sizeof (memory_float));
61   m.value = x;
62   for (i = 0; i < NWORDS; i++)
63     {
64       ored_words[i] |= m.word[i];
65       anded_words[i] &= m.word[i];
66     }
67 }
68 int main ()
69 {
70   size_t j;
71   FILE *fp = fopen ("conftest.out", "w");
72   if (fp == NULL)
73     return 1;
74   for (j = 0; j < NWORDS; j++)
75     anded_words[j] = ~ (unsigned int) 0;
76   add_to_ored_words (0.25f);
77   add_to_ored_words (0.5f);
78   add_to_ored_words (1.0f);
79   add_to_ored_words (2.0f);
80   add_to_ored_words (4.0f);
81   /* Remove bits that are common (e.g. if representation of the first mantissa
82      bit is explicit).  */
83   for (j = 0; j < NWORDS; j++)
84     ored_words[j] &= ~anded_words[j];
85   /* Now find the nonzero word.  */
86   for (j = 0; j < NWORDS; j++)
87     if (ored_words[j] != 0)
88       break;
89   if (j < NWORDS)
90     {
91       size_t i;
92       for (i = j + 1; i < NWORDS; i++)
93         if (ored_words[i] != 0)
94           {
95             fprintf (fp, "unknown");
96             return (fclose (fp) != 0);
97           }
98       for (i = 0; ; i++)
99         if ((ored_words[j] >> i) & 1)
100           {
101             fprintf (fp, "word %d bit %d", (int) j, (int) i);
102             return (fclose (fp) != 0);
103           }
104     }
105   fprintf (fp, "unknown");
106   return (fclose (fp) != 0);
107 }
108         ],
109         [gl_cv_cc_float_expbit0=`cat conftest.out`],
110         [gl_cv_cc_float_expbit0="unknown"],
111         [gl_cv_cc_double_expbit0="word 0 bit 23"])
112       rm -f conftest.out
113     ])
114   case "$gl_cv_cc_float_expbit0" in
115     word*bit*)
116       word=`echo "$gl_cv_cc_float_expbit0" | sed -e 's/word //' -e 's/ bit.*//'`
117       bit=`echo "$gl_cv_cc_float_expbit0" | sed -e 's/word.*bit //'`
118       AC_DEFINE_UNQUOTED([FLT_EXPBIT0_WORD], [$word],
119         [Define as the word index where to find the exponent of 'float'.])
120       AC_DEFINE_UNQUOTED([FLT_EXPBIT0_BIT], [$bit],
121         [Define as the bit index in the word where to find bit 0 of the exponent of 'float'.])
122       ;;
123   esac
124 ])