1c88250a5SDavid Schultz /****************************************************************
2c88250a5SDavid Schultz
3c88250a5SDavid Schultz The author of this software is David M. Gay.
4c88250a5SDavid Schultz
5c88250a5SDavid Schultz Copyright (C) 2005 by David M. Gay
6c88250a5SDavid Schultz All Rights Reserved
7c88250a5SDavid Schultz
8c88250a5SDavid Schultz Permission to use, copy, modify, and distribute this software and its
9c88250a5SDavid Schultz documentation for any purpose and without fee is hereby granted,
10c88250a5SDavid Schultz provided that the above copyright notice appear in all copies and that
11c88250a5SDavid Schultz both that the copyright notice and this permission notice and warranty
12c88250a5SDavid Schultz disclaimer appear in supporting documentation, and that the name of
13c88250a5SDavid Schultz the author or any of his current or former employers not be used in
14c88250a5SDavid Schultz advertising or publicity pertaining to distribution of the software
15c88250a5SDavid Schultz without specific, written prior permission.
16c88250a5SDavid Schultz
17c88250a5SDavid Schultz THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18c88250a5SDavid Schultz INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
19c88250a5SDavid Schultz NO EVENT SHALL THE AUTHOR OR ANY OF HIS CURRENT OR FORMER EMPLOYERS BE
20c88250a5SDavid Schultz LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
21c88250a5SDavid Schultz DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22c88250a5SDavid Schultz WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
23c88250a5SDavid Schultz ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
24c88250a5SDavid Schultz SOFTWARE.
25c88250a5SDavid Schultz
26c88250a5SDavid Schultz ****************************************************************/
27c88250a5SDavid Schultz
28c88250a5SDavid Schultz /* Please send bug reports to David M. Gay (dmg at acm dot org,
29c88250a5SDavid Schultz * with " at " changed at "@" and " dot " changed to "."). */
30c88250a5SDavid Schultz
31c88250a5SDavid Schultz /* Program to compute quiet NaNs of various precisions (float, */
32c88250a5SDavid Schultz /* double, and perhaps long double) on the current system, */
33c88250a5SDavid Schultz /* provided the system uses binary IEEE (P754) arithmetic. */
34c88250a5SDavid Schultz /* Note that one system's quiet NaN may be a signaling NaN on */
35c88250a5SDavid Schultz /* another system. The IEEE arithmetic standards (P754, P854) */
36c88250a5SDavid Schultz /* do not specify how to distinguish signaling NaNs from quiet */
37c88250a5SDavid Schultz /* ones, and this detail varies across systems. The computed */
38c88250a5SDavid Schultz /* NaN values are encoded in #defines for values for an */
39c88250a5SDavid Schultz /* unsigned 32-bit integer type, called Ulong below, and */
40c88250a5SDavid Schultz /* (for long double) perhaps as unsigned short values. Once */
41c88250a5SDavid Schultz /* upon a time, there were PC compilers for Intel CPUs that */
42c88250a5SDavid Schultz /* had sizeof(long double) = 10. Are such compilers still */
43c88250a5SDavid Schultz /* distributed? */
44c88250a5SDavid Schultz
45c88250a5SDavid Schultz #include <stdio.h>
46c88250a5SDavid Schultz #include "arith.h"
47c88250a5SDavid Schultz
48c88250a5SDavid Schultz #ifndef Long
49c88250a5SDavid Schultz #define Long long
50c88250a5SDavid Schultz #endif
51c88250a5SDavid Schultz
52c88250a5SDavid Schultz typedef unsigned Long Ulong;
53c88250a5SDavid Schultz
54c88250a5SDavid Schultz #undef HAVE_IEEE
55c88250a5SDavid Schultz #ifdef IEEE_8087
56c88250a5SDavid Schultz #define _0 1
57c88250a5SDavid Schultz #define _1 0
58c88250a5SDavid Schultz #define HAVE_IEEE
59c88250a5SDavid Schultz #endif
60c88250a5SDavid Schultz #ifdef IEEE_MC68k
61c88250a5SDavid Schultz #define _0 0
62c88250a5SDavid Schultz #define _1 1
63c88250a5SDavid Schultz #define HAVE_IEEE
64c88250a5SDavid Schultz #endif
65c88250a5SDavid Schultz
66c88250a5SDavid Schultz #define UL (unsigned long)
67c88250a5SDavid Schultz
68c88250a5SDavid Schultz int
main(void)69c88250a5SDavid Schultz main(void)
70c88250a5SDavid Schultz {
71c88250a5SDavid Schultz #ifdef HAVE_IEEE
72c88250a5SDavid Schultz typedef union {
73c88250a5SDavid Schultz float f;
74c88250a5SDavid Schultz double d;
75c88250a5SDavid Schultz Ulong L[4];
76c88250a5SDavid Schultz #ifndef NO_LONG_LONG
77c88250a5SDavid Schultz unsigned short u[5];
78c88250a5SDavid Schultz long double D;
79c88250a5SDavid Schultz #endif
80c88250a5SDavid Schultz } U;
81c88250a5SDavid Schultz U a, b, c;
82c88250a5SDavid Schultz int i;
83c88250a5SDavid Schultz
84c88250a5SDavid Schultz a.L[0] = b.L[0] = 0x7f800000;
85c88250a5SDavid Schultz c.f = a.f - b.f;
86c88250a5SDavid Schultz printf("#define f_QNAN 0x%lx\n", UL c.L[0]);
87c88250a5SDavid Schultz a.L[_0] = b.L[_0] = 0x7ff00000;
88c88250a5SDavid Schultz a.L[_1] = b.L[_1] = 0;
89c88250a5SDavid Schultz c.d = a.d - b.d; /* quiet NaN */
90c88250a5SDavid Schultz printf("#define d_QNAN0 0x%lx\n", UL c.L[0]);
91c88250a5SDavid Schultz printf("#define d_QNAN1 0x%lx\n", UL c.L[1]);
92c88250a5SDavid Schultz #ifdef NO_LONG_LONG
93c88250a5SDavid Schultz for(i = 0; i < 4; i++)
94c88250a5SDavid Schultz printf("#define ld_QNAN%d 0xffffffff\n", i);
95c88250a5SDavid Schultz for(i = 0; i < 5; i++)
96c88250a5SDavid Schultz printf("#define ldus_QNAN%d 0xffff\n", i);
97c88250a5SDavid Schultz #else
98c88250a5SDavid Schultz b.D = c.D = a.d;
99c88250a5SDavid Schultz if (printf("") < 0)
100c88250a5SDavid Schultz c.D = 37; /* never executed; just defeat optimization */
101c88250a5SDavid Schultz a.L[2] = a.L[3] = 0;
102c88250a5SDavid Schultz a.D = b.D - c.D;
103c88250a5SDavid Schultz for(i = 0; i < 4; i++)
104c88250a5SDavid Schultz printf("#define ld_QNAN%d 0x%lx\n", i, UL a.L[i]);
105c88250a5SDavid Schultz for(i = 0; i < 5; i++)
106c88250a5SDavid Schultz printf("#define ldus_QNAN%d 0x%x\n", i, a.u[i]);
107c88250a5SDavid Schultz #endif
108c88250a5SDavid Schultz #endif /* HAVE_IEEE */
109c88250a5SDavid Schultz return 0;
110c88250a5SDavid Schultz }
111