xref: /freebsd/contrib/gdtoa/qnan.c (revision 732a02b4e77866604a120a275c082bb6221bd2ff)
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 "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  int
69 main(void)
70 {
71 #ifdef HAVE_IEEE
72 	typedef union {
73 		float f;
74 		double d;
75 		Ulong L[4];
76 #ifndef NO_LONG_LONG
77 		unsigned short u[5];
78 		long double D;
79 #endif
80 		} U;
81 	U a, b, c;
82 	int i;
83 
84 	a.L[0] = b.L[0] = 0x7f800000;
85 	c.f = a.f - b.f;
86 	printf("#define f_QNAN 0x%lx\n", UL c.L[0]);
87 	a.L[_0] = b.L[_0] = 0x7ff00000;
88 	a.L[_1] = b.L[_1] = 0;
89 	c.d = a.d - b.d;	/* quiet NaN */
90 	printf("#define d_QNAN0 0x%lx\n", UL c.L[0]);
91 	printf("#define d_QNAN1 0x%lx\n", UL c.L[1]);
92 #ifdef NO_LONG_LONG
93 	for(i = 0; i < 4; i++)
94 		printf("#define ld_QNAN%d 0xffffffff\n", i);
95 	for(i = 0; i < 5; i++)
96 		printf("#define ldus_QNAN%d 0xffff\n", i);
97 #else
98 	b.D = c.D = a.d;
99 	if (printf("") < 0)
100 		c.D = 37;	/* never executed; just defeat optimization */
101 	a.L[2] = a.L[3] = 0;
102 	a.D = b.D - c.D;
103 	for(i = 0; i < 4; i++)
104 		printf("#define ld_QNAN%d 0x%lx\n", i, UL a.L[i]);
105 	for(i = 0; i < 5; i++)
106 		printf("#define ldus_QNAN%d 0x%x\n", i, a.u[i]);
107 #endif
108 #endif /* HAVE_IEEE */
109 	return 0;
110 	}
111