Avoid compilation error due to MacOS X 10.5 gcc cross-compilation bug.
[gnulib.git] / tests / test-isnanl.h
1 /* Test of isnanl() substitute.
2    Copyright (C) 2007-2008 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
18
19 #include <float.h>
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #define ASSERT(expr) \
25   do                                                                         \
26     {                                                                        \
27       if (!(expr))                                                           \
28         {                                                                    \
29           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
30           fflush (stderr);                                                   \
31           abort ();                                                          \
32         }                                                                    \
33     }                                                                        \
34   while (0)
35
36 /* On HP-UX 10.20, negating 0.0L does not yield -0.0L.
37    So we use minus_zero instead.
38    Note that the expression -LDBL_MIN * LDBL_MIN does not work on other
39    platforms, such as when cross-compiling to PowerPC on MacOS X 10.5.  */
40 #if defined __hpux || defined __sgi
41 long double minus_zero = -LDBL_MIN * LDBL_MIN;
42 #else
43 long double minus_zero = -0.0L;
44 #endif
45
46 int
47 main ()
48 {
49   #define NWORDS \
50     ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
51   typedef union { unsigned int word[NWORDS]; long double value; }
52           memory_long_double;
53
54   /* Finite values.  */
55   ASSERT (!isnanl (3.141L));
56   ASSERT (!isnanl (3.141e30L));
57   ASSERT (!isnanl (3.141e-30L));
58   ASSERT (!isnanl (-2.718L));
59   ASSERT (!isnanl (-2.718e30L));
60   ASSERT (!isnanl (-2.718e-30L));
61   ASSERT (!isnanl (0.0L));
62   ASSERT (!isnanl (minus_zero));
63   /* Infinite values.  */
64   ASSERT (!isnanl (1.0L / 0.0L));
65   ASSERT (!isnanl (-1.0L / 0.0L));
66   /* Quiet NaN.  */
67   ASSERT (isnanl (0.0L / 0.0L));
68
69 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
70   /* A bit pattern that is different from a Quiet NaN.  With a bit of luck,
71      it's a Signalling NaN.  */
72   {
73     memory_long_double m;
74     m.value = 0.0L / 0.0L;
75 # if LDBL_EXPBIT0_BIT > 0
76     m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 1);
77 # else
78     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
79       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
80 # endif
81     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
82       |= (unsigned int) 1 << LDBL_EXPBIT0_BIT;
83     ASSERT (isnanl (m.value));
84   }
85 #endif
86
87 #if ((defined __ia64 && LDBL_MANT_DIG == 64) || (defined __x86_64__ || defined __amd64__) || (defined __i386 || defined __i386__ || defined _I386 || defined _M_IX86 || defined _X86_))
88 /* Representation of an 80-bit 'long double' as an initializer for a sequence
89    of 'unsigned int' words.  */
90 # ifdef WORDS_BIGENDIAN
91 #  define LDBL80_WORDS(exponent,manthi,mantlo) \
92      { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
93        ((unsigned int) (manthi) << 16) | (unsigned int) (mantlo) >> 16),    \
94        (unsigned int) (mantlo) << 16                                        \
95      }
96 # else
97 #  define LDBL80_WORDS(exponent,manthi,mantlo) \
98      { mantlo, manthi, exponent }
99 # endif
100   { /* Quiet NaN.  */
101     static memory_long_double x =
102       { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
103     ASSERT (isnanl (x.value));
104   }
105   {
106     /* Signalling NaN.  */
107     static memory_long_double x =
108       { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
109     ASSERT (isnanl (x.value));
110   }
111   /* The isnanl function should recognize Pseudo-NaNs, Pseudo-Infinities,
112      Pseudo-Zeroes, Unnormalized Numbers, and Pseudo-Denormals, as defined in
113        Intel IA-64 Architecture Software Developer's Manual, Volume 1:
114        Application Architecture.
115        Table 5-2 "Floating-Point Register Encodings"
116        Figure 5-6 "Memory to Floating-Point Register Data Translation"
117    */
118   { /* Pseudo-NaN.  */
119     static memory_long_double x =
120       { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
121     ASSERT (isnanl (x.value));
122   }
123   { /* Pseudo-Infinity.  */
124     static memory_long_double x =
125       { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
126     ASSERT (isnanl (x.value));
127   }
128   { /* Pseudo-Zero.  */
129     static memory_long_double x =
130       { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
131     ASSERT (isnanl (x.value));
132   }
133   { /* Unnormalized number.  */
134     static memory_long_double x =
135       { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
136     ASSERT (isnanl (x.value));
137   }
138   { /* Pseudo-Denormal.  */
139     static memory_long_double x =
140       { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
141     ASSERT (isnanl (x.value));
142   }
143 #endif
144
145   return 0;
146 }