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