1 /* $OpenBSD: softfloat.h,v 1.4 2008/10/07 22:06:29 martynas Exp $ */ 2 /* $NetBSD: softfloat.h,v 1.1 2001/04/26 03:10:48 ross Exp $ */ 3 4 /* This is a derivative work. */ 5 6 /*- 7 * Copyright (c) 2001 The NetBSD Foundation, Inc. 8 * All rights reserved. 9 * 10 * This code is derived from software contributed to The NetBSD Foundation 11 * by Ross Harvey. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 /* 36 =============================================================================== 37 38 This C header file is part of the SoftFloat IEC/IEEE Floating-point 39 Arithmetic Package, Release 2a. 40 41 Written by John R. Hauser. This work was made possible in part by the 42 International Computer Science Institute, located at Suite 600, 1947 Center 43 Street, Berkeley, California 94704. Funding was partially provided by the 44 National Science Foundation under grant MIP-9311980. The original version 45 of this code was written as part of a project to build a fixed-point vector 46 processor in collaboration with the University of California at Berkeley, 47 overseen by Profs. Nelson Morgan and John Wawrzynek. More information 48 is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/ 49 arithmetic/SoftFloat.html'. 50 51 THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable 52 effort has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT 53 WILL AT TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS 54 RESTRICTED TO PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL 55 RESPONSIBILITY FOR ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM 56 THEIR OWN USE OF THE SOFTWARE, AND WHO ALSO EFFECTIVELY INDEMNIFY 57 (possibly via similar legal warning) JOHN HAUSER AND THE INTERNATIONAL 58 COMPUTER SCIENCE INSTITUTE AGAINST ALL LOSSES, COSTS, OR OTHER PROBLEMS 59 ARISING FROM THE USE OF THE SOFTWARE BY THEIR CUSTOMERS AND CLIENTS. 60 61 Derivative works are acceptable, even for commercial purposes, so long as 62 (1) they include prominent notice that the work is derivative, and (2) they 63 include prominent notice akin to these four paragraphs for those parts of 64 this code that are retained. 65 66 =============================================================================== 67 */ 68 69 #include <sys/types.h> 70 71 #if !defined(_KERNEL) && !defined(_STANDALONE) 72 #include <ieeefp.h> 73 #else 74 #include "machine/ieeefp.h" 75 #endif 76 #include <sys/endian.h> 77 78 /* 79 ------------------------------------------------------------------------------- 80 The macro `FLOATX80' must be defined to enable the extended double-precision 81 floating-point format `floatx80'. If this macro is not defined, the 82 `floatx80' type will not be defined, and none of the functions that either 83 input or output the `floatx80' type will be defined. The same applies to 84 the `FLOAT128' macro and the quadruple-precision format `float128'. 85 ------------------------------------------------------------------------------- 86 */ 87 /* #define FLOATX80 */ 88 /* #define FLOAT128 */ 89 90 /* 91 ------------------------------------------------------------------------------- 92 Software IEC/IEEE floating-point types. 93 ------------------------------------------------------------------------------- 94 */ 95 typedef u_int32_t float32; 96 typedef u_int64_t float64; 97 #ifdef FLOATX80 98 typedef struct { 99 #if BYTE_ORDER == BIG_ENDIAN 100 u_int16_t high; 101 u_int64_t low; 102 #else 103 u_int64_t low; 104 u_int16_t high; 105 #endif 106 } floatx80; 107 #endif 108 #ifdef FLOAT128 109 typedef struct { 110 u_int64_t high, low; 111 } float128; 112 #endif 113 114 /* 115 * Some of the global variables that used to be here have been removed for 116 * fairly obvious (defopt-MULTIPROCESSOR) reasons. The rest (which don't 117 * change dynamically) will be removed later. [ross] 118 */ 119 120 #define float_rounding_mode() fpgetround() 121 122 /* 123 ------------------------------------------------------------------------------- 124 Software IEC/IEEE floating-point underflow tininess-detection mode. 125 ------------------------------------------------------------------------------- 126 */ 127 128 extern int float_detect_tininess; 129 enum { 130 float_tininess_after_rounding = 1, 131 float_tininess_before_rounding = 0 132 }; 133 134 /* 135 ------------------------------------------------------------------------------- 136 Software IEC/IEEE floating-point rounding mode. 137 ------------------------------------------------------------------------------- 138 */ 139 140 enum { 141 float_round_nearest_even = FP_RN, 142 float_round_to_zero = FP_RZ, 143 float_round_down = FP_RM, 144 float_round_up = FP_RP 145 }; 146 147 /* 148 ------------------------------------------------------------------------------- 149 Software IEC/IEEE floating-point exception flags. 150 ------------------------------------------------------------------------------- 151 */ 152 153 enum { 154 float_flag_inexact = FP_X_IMP, 155 float_flag_underflow = FP_X_UFL, 156 float_flag_overflow = FP_X_OFL, 157 float_flag_divbyzero = FP_X_DZ, 158 float_flag_invalid = FP_X_INV 159 }; 160 161 /* 162 ------------------------------------------------------------------------------- 163 Software IEC/IEEE integer-to-floating-point conversion routines. 164 ------------------------------------------------------------------------------- 165 */ 166 float32 int32_to_float32( int ); 167 float64 int32_to_float64( int ); 168 #ifdef FLOATX80 169 floatx80 int32_to_floatx80( int ); 170 #endif 171 #ifdef FLOAT128 172 float128 int32_to_float128( int ); 173 #endif 174 #ifndef SOFTFLOAT_FOR_GCC /* __floatdi?f is in libgcc2.c */ 175 float32 int64_to_float32( int64_t ); 176 float64 int64_to_float64( int64_t ); 177 #ifdef FLOATX80 178 floatx80 int64_to_floatx80( int64_t ); 179 #endif 180 #ifdef FLOAT128 181 float128 int64_to_float128( int64_t ); 182 #endif 183 #endif 184 185 /* 186 ------------------------------------------------------------------------------- 187 Software IEC/IEEE single-precision conversion routines. 188 ------------------------------------------------------------------------------- 189 */ 190 int float32_to_int32( float32 ); 191 int float32_to_int32_round_to_zero( float32 ); 192 #ifndef SOFTFLOAT_FOR_GCC /* __fix?fdi provided by libgcc2.c */ 193 int64_t float32_to_int64( float32 ); 194 int64_t float32_to_int64_round_to_zero( float32 ); 195 #endif 196 float64 float32_to_float64( float32 ); 197 #ifdef FLOATX80 198 floatx80 float32_to_floatx80( float32 ); 199 #endif 200 #ifdef FLOAT128 201 float128 float32_to_float128( float32 ); 202 #endif 203 204 /* 205 ------------------------------------------------------------------------------- 206 Software IEC/IEEE single-precision operations. 207 ------------------------------------------------------------------------------- 208 */ 209 float32 float32_round_to_int( float32 ); 210 float32 float32_add( float32, float32 ); 211 float32 float32_sub( float32, float32 ); 212 float32 float32_mul( float32, float32 ); 213 float32 float32_div( float32, float32 ); 214 float32 float32_rem( float32, float32 ); 215 float32 float32_sqrt( float32 ); 216 int float32_eq( float32, float32 ); 217 int float32_le( float32, float32 ); 218 int float32_lt( float32, float32 ); 219 int float32_eq_signaling( float32, float32 ); 220 int float32_le_quiet( float32, float32 ); 221 int float32_lt_quiet( float32, float32 ); 222 #ifndef SOFTFLOAT_FOR_GCC 223 int float32_is_signaling_nan( float32 ); 224 #endif 225 226 /* 227 ------------------------------------------------------------------------------- 228 Software IEC/IEEE double-precision conversion routines. 229 ------------------------------------------------------------------------------- 230 */ 231 int float64_to_int32( float64 ); 232 int float64_to_int32_round_to_zero( float64 ); 233 #ifndef SOFTFLOAT_FOR_GCC /* __fix?fdi provided by libgcc2.c */ 234 int64_t float64_to_int64( float64 ); 235 #ifdef __alpha__ 236 int64_t float64_to_int64_no_overflow( float64 ); 237 #endif /* __alpha__ */ 238 int64_t float64_to_int64_round_to_zero( float64 ); 239 #endif 240 float32 float64_to_float32( float64 ); 241 #ifdef FLOATX80 242 floatx80 float64_to_floatx80( float64 ); 243 #endif 244 #ifdef FLOAT128 245 float128 float64_to_float128( float64 ); 246 #endif 247 248 /* 249 ------------------------------------------------------------------------------- 250 Software IEC/IEEE double-precision operations. 251 ------------------------------------------------------------------------------- 252 */ 253 #define float64_default_nan 0xFFF8000000000000LL 254 255 static __inline int 256 float64_is_nan(float64 a) 257 { 258 return 0xFFE0000000000000LL < a << 1; 259 } 260 261 static __inline int 262 float64_is_signaling_nan(float64 a) 263 { 264 return (a >> 51 & 0xFFF) == 0xFFE && (a & 0x0007FFFFFFFFFFFFLL); 265 } 266 267 float64 float64_round_to_int( float64 ); 268 float64 float64_add( float64, float64 ); 269 float64 float64_sub( float64, float64 ); 270 float64 float64_mul( float64, float64 ); 271 float64 float64_div( float64, float64 ); 272 float64 float64_rem( float64, float64 ); 273 float64 float64_sqrt( float64 ); 274 int float64_eq( float64, float64 ); 275 int float64_le( float64, float64 ); 276 int float64_lt( float64, float64 ); 277 int float64_eq_signaling( float64, float64 ); 278 int float64_le_quiet( float64, float64 ); 279 int float64_lt_quiet( float64, float64 ); 280 #ifndef SOFTFLOAT_FOR_GCC 281 int float64_is_signaling_nan( float64 ); 282 #endif 283 284 #ifdef FLOATX80 285 286 /* 287 ------------------------------------------------------------------------------- 288 Software IEC/IEEE extended double-precision conversion routines. 289 ------------------------------------------------------------------------------- 290 */ 291 int floatx80_to_int32( floatx80 ); 292 int floatx80_to_int32_round_to_zero( floatx80 ); 293 int64_t floatx80_to_int64( floatx80 ); 294 int64_t floatx80_to_int64_round_to_zero( floatx80 ); 295 float32 floatx80_to_float32( floatx80 ); 296 float64 floatx80_to_float64( floatx80 ); 297 #ifdef FLOAT128 298 float128 floatx80_to_float128( floatx80 ); 299 #endif 300 301 /* 302 ------------------------------------------------------------------------------- 303 Software IEC/IEEE extended double-precision rounding precision. Valid 304 values are 32, 64, and 80. 305 ------------------------------------------------------------------------------- 306 */ 307 extern int floatx80_rounding_precision; 308 309 /* 310 ------------------------------------------------------------------------------- 311 Software IEC/IEEE extended double-precision operations. 312 ------------------------------------------------------------------------------- 313 */ 314 floatx80 floatx80_round_to_int( floatx80 ); 315 floatx80 floatx80_add( floatx80, floatx80 ); 316 floatx80 floatx80_sub( floatx80, floatx80 ); 317 floatx80 floatx80_mul( floatx80, floatx80 ); 318 floatx80 floatx80_div( floatx80, floatx80 ); 319 floatx80 floatx80_rem( floatx80, floatx80 ); 320 floatx80 floatx80_sqrt( floatx80 ); 321 int floatx80_eq( floatx80, floatx80 ); 322 int floatx80_le( floatx80, floatx80 ); 323 int floatx80_lt( floatx80, floatx80 ); 324 int floatx80_eq_signaling( floatx80, floatx80 ); 325 int floatx80_le_quiet( floatx80, floatx80 ); 326 int floatx80_lt_quiet( floatx80, floatx80 ); 327 int floatx80_is_signaling_nan( floatx80 ); 328 329 #endif 330 331 #ifdef FLOAT128 332 333 /* 334 ------------------------------------------------------------------------------- 335 Software IEC/IEEE quadruple-precision conversion routines. 336 ------------------------------------------------------------------------------- 337 */ 338 int float128_to_int32( float128 ); 339 int float128_to_int32_round_to_zero( float128 ); 340 int64_t float128_to_int64( float128 ); 341 int64_t float128_to_int64_round_to_zero( float128 ); 342 float32 float128_to_float32( float128 ); 343 float64 float128_to_float64( float128 ); 344 #ifdef FLOATX80 345 floatx80 float128_to_floatx80( float128 ); 346 #endif 347 348 /* 349 ------------------------------------------------------------------------------- 350 Software IEC/IEEE quadruple-precision operations. 351 ------------------------------------------------------------------------------- 352 */ 353 float128 float128_round_to_int( float128 ); 354 float128 float128_add( float128, float128 ); 355 float128 float128_sub( float128, float128 ); 356 float128 float128_mul( float128, float128 ); 357 float128 float128_div( float128, float128 ); 358 float128 float128_rem( float128, float128 ); 359 float128 float128_sqrt( float128 ); 360 int float128_eq( float128, float128 ); 361 int float128_le( float128, float128 ); 362 int float128_lt( float128, float128 ); 363 int float128_eq_signaling( float128, float128 ); 364 int float128_le_quiet( float128, float128 ); 365 int float128_lt_quiet( float128, float128 ); 366 int float128_is_signaling_nan( float128 ); 367 368 #endif 369