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