1129198Scognet/*	$NetBSD: ieee754.h,v 1.4 2003/10/27 02:30:26 simonb Exp $	*/
2129198Scognet
3139735Simp/*-
4129198Scognet * Copyright (c) 1992, 1993
5129198Scognet *	The Regents of the University of California.  All rights reserved.
6129198Scognet *
7129198Scognet * This software was developed by the Computer Systems Engineering group
8129198Scognet * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9129198Scognet * contributed to Berkeley.
10129198Scognet *
11129198Scognet * All advertising materials mentioning features or use of this software
12129198Scognet * must display the following acknowledgement:
13129198Scognet *	This product includes software developed by the University of
14129198Scognet *	California, Lawrence Berkeley Laboratory.
15129198Scognet *
16129198Scognet * Redistribution and use in source and binary forms, with or without
17129198Scognet * modification, are permitted provided that the following conditions
18129198Scognet * are met:
19129198Scognet * 1. Redistributions of source code must retain the above copyright
20129198Scognet *    notice, this list of conditions and the following disclaimer.
21129198Scognet * 2. Redistributions in binary form must reproduce the above copyright
22129198Scognet *    notice, this list of conditions and the following disclaimer in the
23129198Scognet *    documentation and/or other materials provided with the distribution.
24129198Scognet * 3. Neither the name of the University nor the names of its contributors
25129198Scognet *    may be used to endorse or promote products derived from this software
26129198Scognet *    without specific prior written permission.
27129198Scognet *
28129198Scognet * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29129198Scognet * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30129198Scognet * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31129198Scognet * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32129198Scognet * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33129198Scognet * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34129198Scognet * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35129198Scognet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36129198Scognet * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37129198Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38129198Scognet * SUCH DAMAGE.
39129198Scognet *
40129198Scognet *	@(#)ieee.h	8.1 (Berkeley) 6/11/93
41129198Scognet *
42129198Scognet * $FreeBSD$
43129198Scognet *
44129198Scognet */
45129198Scognet
46129198Scognet/*
47129198Scognet * NOTICE: This is not a standalone file.  To use it, #include it in
48129198Scognet * your port's ieee.h header.
49129198Scognet */
50129198Scognet
51129198Scognet#include <machine/endian.h>
52129198Scognet
53129198Scognet/*
54129198Scognet * <sys/ieee754.h> defines the layout of IEEE 754 floating point types.
55129198Scognet * Only single-precision and double-precision types are defined here;
56129198Scognet * extended types, if available, are defined in the machine-dependent
57129198Scognet * header.
58129198Scognet */
59129198Scognet
60129198Scognet/*
61129198Scognet * Define the number of bits in each fraction and exponent.
62129198Scognet *
63129198Scognet *		     k	         k+1
64129198Scognet * Note that  1.0 x 2  == 0.1 x 2      and that denorms are represented
65129198Scognet *
66129198Scognet *					  (-exp_bias+1)
67129198Scognet * as fractions that look like 0.fffff x 2             .  This means that
68129198Scognet *
69129198Scognet *			 -126
70129198Scognet * the number 0.10000 x 2    , for instance, is the same as the normalized
71129198Scognet *
72129198Scognet *		-127			   -128
73129198Scognet * float 1.0 x 2    .  Thus, to represent 2    , we need one leading zero
74129198Scognet *
75129198Scognet *				  -129
76129198Scognet * in the fraction; to represent 2    , we need two, and so on.  This
77129198Scognet *
78129198Scognet *						     (-exp_bias-fracbits+1)
79129198Scognet * implies that the smallest denormalized number is 2
80129198Scognet *
81129198Scognet * for whichever format we are talking about: for single precision, for
82129198Scognet *
83129198Scognet *						-126		-149
84129198Scognet * instance, we get .00000000000000000000001 x 2    , or 1.0 x 2    , and
85129198Scognet *
86129198Scognet * -149 == -127 - 23 + 1.
87129198Scognet */
88129198Scognet#define	SNG_EXPBITS	8
89129198Scognet#define	SNG_FRACBITS	23
90129198Scognet
91129198Scognet#define	DBL_EXPBITS	11
92129198Scognet#define	DBL_FRACBITS	52
93129198Scognet
94255361Sandrew#if defined(__VFP_FP__) || defined(__ARM_EABI__)
95186461Smarcel#define	_IEEE_WORD_ORDER	_BYTE_ORDER
96186461Smarcel#else
97186461Smarcel#define	_IEEE_WORD_ORDER	_BIG_ENDIAN
98186461Smarcel#endif
99186461Smarcel
100129198Scognetstruct ieee_single {
101129198Scognet#if _BYTE_ORDER == _BIG_ENDIAN
102129198Scognet	u_int	sng_sign:1;
103129198Scognet	u_int	sng_exp:8;
104129198Scognet	u_int	sng_frac:23;
105129198Scognet#else
106129198Scognet	u_int	sng_frac:23;
107129198Scognet	u_int	sng_exp:8;
108129198Scognet	u_int	sng_sign:1;
109129198Scognet#endif
110129198Scognet};
111129198Scognet
112129198Scognetstruct ieee_double {
113129198Scognet#if _BYTE_ORDER == _BIG_ENDIAN
114129198Scognet	u_int	dbl_sign:1;
115129198Scognet	u_int	dbl_exp:11;
116129198Scognet	u_int	dbl_frach:20;
117129198Scognet	u_int	dbl_fracl;
118129198Scognet#else
119186461Smarcel#if _IEEE_WORD_ORDER == _LITTLE_ENDIAN
120129198Scognet	u_int	dbl_fracl;
121186461Smarcel#endif
122129198Scognet	u_int	dbl_frach:20;
123129198Scognet	u_int	dbl_exp:11;
124129198Scognet	u_int	dbl_sign:1;
125186461Smarcel#if _IEEE_WORD_ORDER == _BIG_ENDIAN
126186461Smarcel	u_int   dbl_fracl;
127129198Scognet#endif
128236992Simp#endif
129129198Scognet};
130129198Scognet
131129198Scognet/*
132129198Scognet * Floats whose exponent is in [1..INFNAN) (of whatever type) are
133129198Scognet * `normal'.  Floats whose exponent is INFNAN are either Inf or NaN.
134129198Scognet * Floats whose exponent is zero are either zero (iff all fraction
135129198Scognet * bits are zero) or subnormal values.
136129198Scognet *
137129198Scognet * A NaN is a `signalling NaN' if its QUIETNAN bit is clear in its
138129198Scognet * high fraction; if the bit is set, it is a `quiet NaN'.
139129198Scognet */
140129198Scognet#define	SNG_EXP_INFNAN	255
141129198Scognet#define	DBL_EXP_INFNAN	2047
142129198Scognet
143129198Scognet#if 0
144129198Scognet#define	SNG_QUIETNAN	(1 << 22)
145129198Scognet#define	DBL_QUIETNAN	(1 << 19)
146129198Scognet#endif
147129198Scognet
148129198Scognet/*
149129198Scognet * Exponent biases.
150129198Scognet */
151129198Scognet#define	SNG_EXP_BIAS	127
152129198Scognet#define	DBL_EXP_BIAS	1023
153129198Scognet
154129198Scognet/*
155129198Scognet * Convenience data structures.
156129198Scognet */
157129198Scognetunion ieee_single_u {
158129198Scognet	float			sngu_f;
159129198Scognet	struct ieee_single	sngu_sng;
160129198Scognet};
161129198Scognet
162129198Scognetunion ieee_double_u {
163129198Scognet	double			dblu_d;
164129198Scognet	struct ieee_double	dblu_dbl;
165129198Scognet};
166