/****************************************************************12The author of this software is David M. Gay.34Copyright (C) 2005 by David M. Gay5All Rights Reserved67Permission to use, copy, modify, and distribute this software and its8documentation for any purpose and without fee is hereby granted,9provided that the above copyright notice appear in all copies and that10both that the copyright notice and this permission notice and warranty11disclaimer appear in supporting documentation, and that the name of12the author or any of his current or former employers not be used in13advertising or publicity pertaining to distribution of the software14without specific, written prior permission.1516THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,17INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN18NO EVENT SHALL THE AUTHOR OR ANY OF HIS CURRENT OR FORMER EMPLOYERS BE19LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY20DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,21WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,22ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS23SOFTWARE.2425****************************************************************/2627/* Please send bug reports to David M. Gay (dmg at acm dot org,28* with " at " changed at "@" and " dot " changed to "."). */2930/* Program to compute quiet NaNs of various precisions (float, */31/* double, and perhaps long double) on the current system, */32/* provided the system uses binary IEEE (P754) arithmetic. */33/* Note that one system's quiet NaN may be a signaling NaN on */34/* another system. The IEEE arithmetic standards (P754, P854) */35/* do not specify how to distinguish signaling NaNs from quiet */36/* ones, and this detail varies across systems. The computed */37/* NaN values are encoded in #defines for values for an */38/* unsigned 32-bit integer type, called Ulong below, and */39/* (for long double) perhaps as unsigned short values. Once */40/* upon a time, there were PC compilers for Intel CPUs that */41/* had sizeof(long double) = 10. Are such compilers still */42/* distributed? */4344#include <stdio.h>45#include "arith.h"4647#ifndef Long48#define Long long49#endif5051typedef unsigned Long Ulong;5253#undef HAVE_IEEE54#ifdef IEEE_808755#define _0 156#define _1 057#define HAVE_IEEE58#endif59#ifdef IEEE_MC68k60#define _0 061#define _1 162#define HAVE_IEEE63#endif6465#define UL (unsigned long)6667int68main(void)69{70#ifdef HAVE_IEEE71typedef union {72float f;73double d;74Ulong L[4];75#ifndef NO_LONG_LONG76unsigned short u[5];77long double D;78#endif79} U;80U a, b, c;81int i;8283a.L[0] = b.L[0] = 0x7f800000;84c.f = a.f - b.f;85printf("#define f_QNAN 0x%lx\n", UL c.L[0]);86a.L[_0] = b.L[_0] = 0x7ff00000;87a.L[_1] = b.L[_1] = 0;88c.d = a.d - b.d; /* quiet NaN */89printf("#define d_QNAN0 0x%lx\n", UL c.L[0]);90printf("#define d_QNAN1 0x%lx\n", UL c.L[1]);91#ifdef NO_LONG_LONG92for(i = 0; i < 4; i++)93printf("#define ld_QNAN%d 0xffffffff\n", i);94for(i = 0; i < 5; i++)95printf("#define ldus_QNAN%d 0xffff\n", i);96#else97b.D = c.D = a.d;98if (printf("") < 0)99c.D = 37; /* never executed; just defeat optimization */100a.L[2] = a.L[3] = 0;101a.D = b.D - c.D;102for(i = 0; i < 4; i++)103printf("#define ld_QNAN%d 0x%lx\n", i, UL a.L[i]);104for(i = 0; i < 5; i++)105printf("#define ldus_QNAN%d 0x%x\n", i, a.u[i]);106#endif107#endif /* HAVE_IEEE */108return 0;109}110111112