1178172Simp/* $NetBSD: ieee754.h,v 1.4 2003/10/27 02:30:26 simonb Exp $ */ 2178172Simp 3178172Simp/*- 4178172Simp * Copyright (c) 1992, 1993 5178172Simp * The Regents of the University of California. All rights reserved. 6178172Simp * 7178172Simp * This software was developed by the Computer Systems Engineering group 8178172Simp * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 9178172Simp * contributed to Berkeley. 10178172Simp * 11178172Simp * All advertising materials mentioning features or use of this software 12178172Simp * must display the following acknowledgement: 13178172Simp * This product includes software developed by the University of 14178172Simp * California, Lawrence Berkeley Laboratory. 15178172Simp * 16178172Simp * Redistribution and use in source and binary forms, with or without 17178172Simp * modification, are permitted provided that the following conditions 18178172Simp * are met: 19178172Simp * 1. Redistributions of source code must retain the above copyright 20178172Simp * notice, this list of conditions and the following disclaimer. 21178172Simp * 2. Redistributions in binary form must reproduce the above copyright 22178172Simp * notice, this list of conditions and the following disclaimer in the 23178172Simp * documentation and/or other materials provided with the distribution. 24178172Simp * 3. Neither the name of the University nor the names of its contributors 25178172Simp * may be used to endorse or promote products derived from this software 26178172Simp * without specific prior written permission. 27178172Simp * 28178172Simp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29178172Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30178172Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31178172Simp * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32178172Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33178172Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34178172Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35178172Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36178172Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37178172Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38178172Simp * SUCH DAMAGE. 39178172Simp * 40178172Simp * @(#)ieee.h 8.1 (Berkeley) 6/11/93 41178172Simp * 42178172Simp * $FreeBSD$ 43178172Simp * 44178172Simp */ 45178172Simp 46178172Simp/* 47178172Simp * NOTICE: This is not a standalone file. To use it, #include it in 48178172Simp * your port's ieee.h header. 49178172Simp */ 50178172Simp 51178172Simp#include <machine/endian.h> 52178172Simp 53178172Simp/* 54178172Simp * <sys/ieee754.h> defines the layout of IEEE 754 floating point types. 55178172Simp * Only single-precision and double-precision types are defined here; 56178172Simp * extended types, if available, are defined in the machine-dependent 57178172Simp * header. 58178172Simp */ 59178172Simp 60178172Simp/* 61178172Simp * Define the number of bits in each fraction and exponent. 62178172Simp * 63178172Simp * k k+1 64178172Simp * Note that 1.0 x 2 == 0.1 x 2 and that denorms are represented 65178172Simp * 66178172Simp * (-exp_bias+1) 67178172Simp * as fractions that look like 0.fffff x 2 . This means that 68178172Simp * 69178172Simp * -126 70178172Simp * the number 0.10000 x 2 , for instance, is the same as the normalized 71178172Simp * 72178172Simp * -127 -128 73178172Simp * float 1.0 x 2 . Thus, to represent 2 , we need one leading zero 74178172Simp * 75178172Simp * -129 76178172Simp * in the fraction; to represent 2 , we need two, and so on. This 77178172Simp * 78178172Simp * (-exp_bias-fracbits+1) 79178172Simp * implies that the smallest denormalized number is 2 80178172Simp * 81178172Simp * for whichever format we are talking about: for single precision, for 82178172Simp * 83178172Simp * -126 -149 84178172Simp * instance, we get .00000000000000000000001 x 2 , or 1.0 x 2 , and 85178172Simp * 86178172Simp * -149 == -127 - 23 + 1. 87178172Simp */ 88178172Simp#define SNG_EXPBITS 8 89178172Simp#define SNG_FRACBITS 23 90178172Simp 91178172Simp#define DBL_EXPBITS 11 92178172Simp#define DBL_FRACBITS 52 93178172Simp 94178172Simpstruct ieee_single { 95178172Simp#if _BYTE_ORDER == _BIG_ENDIAN 96178172Simp u_int sng_sign:1; 97178172Simp u_int sng_exp:8; 98178172Simp u_int sng_frac:23; 99178172Simp#else 100178172Simp u_int sng_frac:23; 101178172Simp u_int sng_exp:8; 102178172Simp u_int sng_sign:1; 103178172Simp#endif 104178172Simp}; 105178172Simp 106178172Simpstruct ieee_double { 107178172Simp#if _BYTE_ORDER == _BIG_ENDIAN 108178172Simp u_int dbl_sign:1; 109178172Simp u_int dbl_exp:11; 110178172Simp u_int dbl_frach:20; 111178172Simp u_int dbl_fracl; 112178172Simp#else 113178172Simp u_int dbl_fracl; 114178172Simp u_int dbl_frach:20; 115178172Simp u_int dbl_exp:11; 116178172Simp u_int dbl_sign:1; 117178172Simp#endif 118178172Simp}; 119178172Simp 120178172Simp/* 121178172Simp * Floats whose exponent is in [1..INFNAN) (of whatever type) are 122178172Simp * `normal'. Floats whose exponent is INFNAN are either Inf or NaN. 123178172Simp * Floats whose exponent is zero are either zero (iff all fraction 124178172Simp * bits are zero) or subnormal values. 125178172Simp * 126178172Simp * A NaN is a `signalling NaN' if its QUIETNAN bit is clear in its 127178172Simp * high fraction; if the bit is set, it is a `quiet NaN'. 128178172Simp */ 129178172Simp#define SNG_EXP_INFNAN 255 130178172Simp#define DBL_EXP_INFNAN 2047 131178172Simp 132178172Simp#if 0 133178172Simp#define SNG_QUIETNAN (1 << 22) 134178172Simp#define DBL_QUIETNAN (1 << 19) 135178172Simp#endif 136178172Simp 137178172Simp/* 138178172Simp * Exponent biases. 139178172Simp */ 140178172Simp#define SNG_EXP_BIAS 127 141178172Simp#define DBL_EXP_BIAS 1023 142178172Simp 143178172Simp/* 144178172Simp * Convenience data structures. 145178172Simp */ 146178172Simpunion ieee_single_u { 147178172Simp float sngu_f; 148178172Simp struct ieee_single sngu_sng; 149178172Simp}; 150178172Simp 151178172Simpunion ieee_double_u { 152178172Simp double dblu_d; 153178172Simp struct ieee_double dblu_dbl; 154178172Simp}; 155