16fc729afSOlivier Houchard /* $NetBSD: ieee754.h,v 1.4 2003/10/27 02:30:26 simonb Exp $ */ 26fc729afSOlivier Houchard 3d8315c79SWarner Losh /*- 4*51369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 5*51369649SPedro F. Giffuni * 66fc729afSOlivier Houchard * Copyright (c) 1992, 1993 76fc729afSOlivier Houchard * The Regents of the University of California. All rights reserved. 86fc729afSOlivier Houchard * 96fc729afSOlivier Houchard * This software was developed by the Computer Systems Engineering group 106fc729afSOlivier Houchard * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 116fc729afSOlivier Houchard * contributed to Berkeley. 126fc729afSOlivier Houchard * 136fc729afSOlivier Houchard * All advertising materials mentioning features or use of this software 146fc729afSOlivier Houchard * must display the following acknowledgement: 156fc729afSOlivier Houchard * This product includes software developed by the University of 166fc729afSOlivier Houchard * California, Lawrence Berkeley Laboratory. 176fc729afSOlivier Houchard * 186fc729afSOlivier Houchard * Redistribution and use in source and binary forms, with or without 196fc729afSOlivier Houchard * modification, are permitted provided that the following conditions 206fc729afSOlivier Houchard * are met: 216fc729afSOlivier Houchard * 1. Redistributions of source code must retain the above copyright 226fc729afSOlivier Houchard * notice, this list of conditions and the following disclaimer. 236fc729afSOlivier Houchard * 2. Redistributions in binary form must reproduce the above copyright 246fc729afSOlivier Houchard * notice, this list of conditions and the following disclaimer in the 256fc729afSOlivier Houchard * documentation and/or other materials provided with the distribution. 266fc729afSOlivier Houchard * 3. Neither the name of the University nor the names of its contributors 276fc729afSOlivier Houchard * may be used to endorse or promote products derived from this software 286fc729afSOlivier Houchard * without specific prior written permission. 296fc729afSOlivier Houchard * 306fc729afSOlivier Houchard * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 316fc729afSOlivier Houchard * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 326fc729afSOlivier Houchard * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 336fc729afSOlivier Houchard * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 346fc729afSOlivier Houchard * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 356fc729afSOlivier Houchard * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 366fc729afSOlivier Houchard * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 376fc729afSOlivier Houchard * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 386fc729afSOlivier Houchard * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 396fc729afSOlivier Houchard * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 406fc729afSOlivier Houchard * SUCH DAMAGE. 416fc729afSOlivier Houchard * 426fc729afSOlivier Houchard */ 436fc729afSOlivier Houchard 446fc729afSOlivier Houchard /* 456fc729afSOlivier Houchard * NOTICE: This is not a standalone file. To use it, #include it in 466fc729afSOlivier Houchard * your port's ieee.h header. 476fc729afSOlivier Houchard */ 486fc729afSOlivier Houchard 496fc729afSOlivier Houchard #include <machine/endian.h> 506fc729afSOlivier Houchard 516fc729afSOlivier Houchard /* 526fc729afSOlivier Houchard * <sys/ieee754.h> defines the layout of IEEE 754 floating point types. 536fc729afSOlivier Houchard * Only single-precision and double-precision types are defined here; 546fc729afSOlivier Houchard * extended types, if available, are defined in the machine-dependent 556fc729afSOlivier Houchard * header. 566fc729afSOlivier Houchard */ 576fc729afSOlivier Houchard 586fc729afSOlivier Houchard /* 596fc729afSOlivier Houchard * Define the number of bits in each fraction and exponent. 606fc729afSOlivier Houchard * 616fc729afSOlivier Houchard * k k+1 626fc729afSOlivier Houchard * Note that 1.0 x 2 == 0.1 x 2 and that denorms are represented 636fc729afSOlivier Houchard * 646fc729afSOlivier Houchard * (-exp_bias+1) 656fc729afSOlivier Houchard * as fractions that look like 0.fffff x 2 . This means that 666fc729afSOlivier Houchard * 676fc729afSOlivier Houchard * -126 686fc729afSOlivier Houchard * the number 0.10000 x 2 , for instance, is the same as the normalized 696fc729afSOlivier Houchard * 706fc729afSOlivier Houchard * -127 -128 716fc729afSOlivier Houchard * float 1.0 x 2 . Thus, to represent 2 , we need one leading zero 726fc729afSOlivier Houchard * 736fc729afSOlivier Houchard * -129 746fc729afSOlivier Houchard * in the fraction; to represent 2 , we need two, and so on. This 756fc729afSOlivier Houchard * 766fc729afSOlivier Houchard * (-exp_bias-fracbits+1) 776fc729afSOlivier Houchard * implies that the smallest denormalized number is 2 786fc729afSOlivier Houchard * 796fc729afSOlivier Houchard * for whichever format we are talking about: for single precision, for 806fc729afSOlivier Houchard * 816fc729afSOlivier Houchard * -126 -149 826fc729afSOlivier Houchard * instance, we get .00000000000000000000001 x 2 , or 1.0 x 2 , and 836fc729afSOlivier Houchard * 846fc729afSOlivier Houchard * -149 == -127 - 23 + 1. 856fc729afSOlivier Houchard */ 866fc729afSOlivier Houchard #define SNG_EXPBITS 8 876fc729afSOlivier Houchard #define SNG_FRACBITS 23 886fc729afSOlivier Houchard 896fc729afSOlivier Houchard #define DBL_EXPBITS 11 906fc729afSOlivier Houchard #define DBL_FRACBITS 52 916fc729afSOlivier Houchard 920a10f22aSAndrew Turner #if defined(__VFP_FP__) || defined(__ARM_EABI__) 9374aed985SMarcel Moolenaar #define _IEEE_WORD_ORDER _BYTE_ORDER 9474aed985SMarcel Moolenaar #else 9574aed985SMarcel Moolenaar #define _IEEE_WORD_ORDER _BIG_ENDIAN 9674aed985SMarcel Moolenaar #endif 9774aed985SMarcel Moolenaar 986fc729afSOlivier Houchard struct ieee_single { 996fc729afSOlivier Houchard #if _BYTE_ORDER == _BIG_ENDIAN 1006fc729afSOlivier Houchard u_int sng_sign:1; 1016fc729afSOlivier Houchard u_int sng_exp:8; 1026fc729afSOlivier Houchard u_int sng_frac:23; 1036fc729afSOlivier Houchard #else 1046fc729afSOlivier Houchard u_int sng_frac:23; 1056fc729afSOlivier Houchard u_int sng_exp:8; 1066fc729afSOlivier Houchard u_int sng_sign:1; 1076fc729afSOlivier Houchard #endif 1086fc729afSOlivier Houchard }; 1096fc729afSOlivier Houchard 1106fc729afSOlivier Houchard struct ieee_double { 1116fc729afSOlivier Houchard #if _BYTE_ORDER == _BIG_ENDIAN 1126fc729afSOlivier Houchard u_int dbl_sign:1; 1136fc729afSOlivier Houchard u_int dbl_exp:11; 1146fc729afSOlivier Houchard u_int dbl_frach:20; 1156fc729afSOlivier Houchard u_int dbl_fracl; 1166fc729afSOlivier Houchard #else 11774aed985SMarcel Moolenaar #if _IEEE_WORD_ORDER == _LITTLE_ENDIAN 1186fc729afSOlivier Houchard u_int dbl_fracl; 11974aed985SMarcel Moolenaar #endif 1206fc729afSOlivier Houchard u_int dbl_frach:20; 1216fc729afSOlivier Houchard u_int dbl_exp:11; 1226fc729afSOlivier Houchard u_int dbl_sign:1; 12374aed985SMarcel Moolenaar #if _IEEE_WORD_ORDER == _BIG_ENDIAN 12474aed985SMarcel Moolenaar u_int dbl_fracl; 12574aed985SMarcel Moolenaar #endif 1266fc729afSOlivier Houchard #endif 1276fc729afSOlivier Houchard }; 1286fc729afSOlivier Houchard 1296fc729afSOlivier Houchard /* 1306fc729afSOlivier Houchard * Floats whose exponent is in [1..INFNAN) (of whatever type) are 1316fc729afSOlivier Houchard * `normal'. Floats whose exponent is INFNAN are either Inf or NaN. 1326fc729afSOlivier Houchard * Floats whose exponent is zero are either zero (iff all fraction 1336fc729afSOlivier Houchard * bits are zero) or subnormal values. 1346fc729afSOlivier Houchard * 1356fc729afSOlivier Houchard * A NaN is a `signalling NaN' if its QUIETNAN bit is clear in its 1366fc729afSOlivier Houchard * high fraction; if the bit is set, it is a `quiet NaN'. 1376fc729afSOlivier Houchard */ 1386fc729afSOlivier Houchard #define SNG_EXP_INFNAN 255 1396fc729afSOlivier Houchard #define DBL_EXP_INFNAN 2047 1406fc729afSOlivier Houchard 1416fc729afSOlivier Houchard #if 0 1426fc729afSOlivier Houchard #define SNG_QUIETNAN (1 << 22) 1436fc729afSOlivier Houchard #define DBL_QUIETNAN (1 << 19) 1446fc729afSOlivier Houchard #endif 1456fc729afSOlivier Houchard 1466fc729afSOlivier Houchard /* 1476fc729afSOlivier Houchard * Exponent biases. 1486fc729afSOlivier Houchard */ 1496fc729afSOlivier Houchard #define SNG_EXP_BIAS 127 1506fc729afSOlivier Houchard #define DBL_EXP_BIAS 1023 1516fc729afSOlivier Houchard 1526fc729afSOlivier Houchard /* 1536fc729afSOlivier Houchard * Convenience data structures. 1546fc729afSOlivier Houchard */ 1556fc729afSOlivier Houchard union ieee_single_u { 1566fc729afSOlivier Houchard float sngu_f; 1576fc729afSOlivier Houchard struct ieee_single sngu_sng; 1586fc729afSOlivier Houchard }; 1596fc729afSOlivier Houchard 1606fc729afSOlivier Houchard union ieee_double_u { 1616fc729afSOlivier Houchard double dblu_d; 1626fc729afSOlivier Houchard struct ieee_double dblu_dbl; 1636fc729afSOlivier Houchard }; 164