1/* 2 * QEMU float support 3 * 4 * The code in this source file is derived from release 2a of the SoftFloat 5 * IEC/IEEE Floating-point Arithmetic Package. Those parts of the code (and 6 * some later contributions) are provided under that license, as detailed below. 7 * It has subsequently been modified by contributors to the QEMU Project, 8 * so some portions are provided under: 9 * the SoftFloat-2a license 10 * the BSD license 11 * GPL-v2-or-later 12 * 13 * Any future contributions to this file after December 1st 2014 will be 14 * taken to be licensed under the Softfloat-2a license unless specifically 15 * indicated otherwise. 16 */ 17 18/* 19=============================================================================== 20This C source fragment is part of the SoftFloat IEC/IEEE Floating-point 21Arithmetic Package, Release 2a. 22 23Written by John R. Hauser. This work was made possible in part by the 24International Computer Science Institute, located at Suite 600, 1947 Center 25Street, Berkeley, California 94704. Funding was partially provided by the 26National Science Foundation under grant MIP-9311980. The original version 27of this code was written as part of a project to build a fixed-point vector 28processor in collaboration with the University of California at Berkeley, 29overseen by Profs. Nelson Morgan and John Wawrzynek. More information 30is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/ 31arithmetic/SoftFloat.html'. 32 33THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort 34has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT 35TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO 36PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY 37AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE. 38 39Derivative works are acceptable, even for commercial purposes, so long as 40(1) they include prominent notice that the work is derivative, and (2) they 41include prominent notice akin to these four paragraphs for those parts of 42this code that are retained. 43 44=============================================================================== 45*/ 46 47/* BSD licensing: 48 * Copyright (c) 2006, Fabrice Bellard 49 * All rights reserved. 50 * 51 * Redistribution and use in source and binary forms, with or without 52 * modification, are permitted provided that the following conditions are met: 53 * 54 * 1. Redistributions of source code must retain the above copyright notice, 55 * this list of conditions and the following disclaimer. 56 * 57 * 2. Redistributions in binary form must reproduce the above copyright notice, 58 * this list of conditions and the following disclaimer in the documentation 59 * and/or other materials provided with the distribution. 60 * 61 * 3. Neither the name of the copyright holder nor the names of its contributors 62 * may be used to endorse or promote products derived from this software without 63 * specific prior written permission. 64 * 65 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 66 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 67 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 68 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 69 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 70 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 71 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 72 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 73 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 74 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 75 * THE POSSIBILITY OF SUCH DAMAGE. 76 */ 77 78/* Portions of this work are licensed under the terms of the GNU GPL, 79 * version 2 or later. See the COPYING file in the top-level directory. 80 */ 81 82/* 83 * Define whether architecture deviates from IEEE in not supporting 84 * signaling NaNs (so all NaNs are treated as quiet). 85 */ 86static inline bool no_signaling_nans(float_status *status) 87{ 88#if defined(TARGET_XTENSA) 89 return status->no_signaling_nans; 90#else 91 return false; 92#endif 93} 94 95/* Define how the architecture discriminates signaling NaNs. 96 * This done with the most significant bit of the fraction. 97 * In IEEE 754-1985 this was implementation defined, but in IEEE 754-2008 98 * the msb must be zero. MIPS is (so far) unique in supporting both the 99 * 2008 revision and backward compatibility with their original choice. 100 * Thus for MIPS we must make the choice at runtime. 101 */ 102static inline bool snan_bit_is_one(float_status *status) 103{ 104#if defined(TARGET_MIPS) 105 return status->snan_bit_is_one; 106#elif defined(TARGET_HPPA) || defined(TARGET_SH4) 107 return 1; 108#else 109 return 0; 110#endif 111} 112 113/*---------------------------------------------------------------------------- 114| For the deconstructed floating-point with fraction FRAC, return true 115| if the fraction represents a signalling NaN; otherwise false. 116*----------------------------------------------------------------------------*/ 117 118static bool parts_is_snan_frac(uint64_t frac, float_status *status) 119{ 120 if (no_signaling_nans(status)) { 121 return false; 122 } else { 123 bool msb = extract64(frac, DECOMPOSED_BINARY_POINT - 1, 1); 124 return msb == snan_bit_is_one(status); 125 } 126} 127 128/*---------------------------------------------------------------------------- 129| The pattern for a default generated deconstructed floating-point NaN. 130*----------------------------------------------------------------------------*/ 131 132static void parts64_default_nan(FloatParts64 *p, float_status *status) 133{ 134 bool sign = 0; 135 uint64_t frac; 136 uint8_t dnan_pattern = status->default_nan_pattern; 137 138 assert(dnan_pattern != 0); 139 140 sign = dnan_pattern >> 7; 141 /* 142 * Place default_nan_pattern [6:0] into bits [62:56], 143 * and replecate bit [0] down into [55:0] 144 */ 145 frac = deposit64(0, DECOMPOSED_BINARY_POINT - 7, 7, dnan_pattern); 146 frac = deposit64(frac, 0, DECOMPOSED_BINARY_POINT - 7, -(dnan_pattern & 1)); 147 148 *p = (FloatParts64) { 149 .cls = float_class_qnan, 150 .sign = sign, 151 .exp = INT_MAX, 152 .frac = frac 153 }; 154} 155 156static void parts128_default_nan(FloatParts128 *p, float_status *status) 157{ 158 /* 159 * Extrapolate from the choices made by parts64_default_nan to fill 160 * in the quad-floating format. If the low bit is set, assume we 161 * want to set all non-snan bits. 162 */ 163 FloatParts64 p64; 164 parts64_default_nan(&p64, status); 165 166 *p = (FloatParts128) { 167 .cls = float_class_qnan, 168 .sign = p64.sign, 169 .exp = INT_MAX, 170 .frac_hi = p64.frac, 171 .frac_lo = -(p64.frac & 1) 172 }; 173} 174 175/*---------------------------------------------------------------------------- 176| Returns a quiet NaN from a signalling NaN for the deconstructed 177| floating-point parts. 178*----------------------------------------------------------------------------*/ 179 180static uint64_t parts_silence_nan_frac(uint64_t frac, float_status *status) 181{ 182 g_assert(!no_signaling_nans(status)); 183 184 /* The only snan_bit_is_one target without default_nan_mode is HPPA. */ 185 if (snan_bit_is_one(status)) { 186 frac &= ~(1ULL << (DECOMPOSED_BINARY_POINT - 1)); 187 frac |= 1ULL << (DECOMPOSED_BINARY_POINT - 2); 188 } else { 189 frac |= 1ULL << (DECOMPOSED_BINARY_POINT - 1); 190 } 191 return frac; 192} 193 194static void parts64_silence_nan(FloatParts64 *p, float_status *status) 195{ 196 p->frac = parts_silence_nan_frac(p->frac, status); 197 p->cls = float_class_qnan; 198} 199 200static void parts128_silence_nan(FloatParts128 *p, float_status *status) 201{ 202 p->frac_hi = parts_silence_nan_frac(p->frac_hi, status); 203 p->cls = float_class_qnan; 204} 205 206/*---------------------------------------------------------------------------- 207| The pattern for a default generated extended double-precision NaN. 208*----------------------------------------------------------------------------*/ 209floatx80 floatx80_default_nan(float_status *status) 210{ 211 floatx80 r; 212 /* 213 * Extrapolate from the choices made by parts64_default_nan to fill 214 * in the floatx80 format. We assume that floatx80's explicit 215 * integer bit is always set (this is true for i386 and m68k, 216 * which are the only real users of this format). 217 */ 218 FloatParts64 p64; 219 parts64_default_nan(&p64, status); 220 221 r.high = 0x7FFF | (p64.sign << 15); 222 r.low = (1ULL << DECOMPOSED_BINARY_POINT) | p64.frac; 223 return r; 224} 225 226/*---------------------------------------------------------------------------- 227| The pattern for a default generated extended double-precision inf. 228*----------------------------------------------------------------------------*/ 229 230#define floatx80_infinity_high 0x7FFF 231#if defined(TARGET_M68K) 232#define floatx80_infinity_low UINT64_C(0x0000000000000000) 233#else 234#define floatx80_infinity_low UINT64_C(0x8000000000000000) 235#endif 236 237const floatx80 floatx80_infinity 238 = make_floatx80_init(floatx80_infinity_high, floatx80_infinity_low); 239 240/*---------------------------------------------------------------------------- 241| Returns 1 if the half-precision floating-point value `a' is a quiet 242| NaN; otherwise returns 0. 243*----------------------------------------------------------------------------*/ 244 245bool float16_is_quiet_nan(float16 a_, float_status *status) 246{ 247 if (no_signaling_nans(status)) { 248 return float16_is_any_nan(a_); 249 } else { 250 uint16_t a = float16_val(a_); 251 if (snan_bit_is_one(status)) { 252 return (((a >> 9) & 0x3F) == 0x3E) && (a & 0x1FF); 253 } else { 254 255 return ((a >> 9) & 0x3F) == 0x3F; 256 } 257 } 258} 259 260/*---------------------------------------------------------------------------- 261| Returns 1 if the bfloat16 value `a' is a quiet 262| NaN; otherwise returns 0. 263*----------------------------------------------------------------------------*/ 264 265bool bfloat16_is_quiet_nan(bfloat16 a_, float_status *status) 266{ 267 if (no_signaling_nans(status)) { 268 return bfloat16_is_any_nan(a_); 269 } else { 270 uint16_t a = a_; 271 if (snan_bit_is_one(status)) { 272 return (((a >> 6) & 0x1FF) == 0x1FE) && (a & 0x3F); 273 } else { 274 return ((a >> 6) & 0x1FF) == 0x1FF; 275 } 276 } 277} 278 279/*---------------------------------------------------------------------------- 280| Returns 1 if the half-precision floating-point value `a' is a signaling 281| NaN; otherwise returns 0. 282*----------------------------------------------------------------------------*/ 283 284bool float16_is_signaling_nan(float16 a_, float_status *status) 285{ 286 if (no_signaling_nans(status)) { 287 return 0; 288 } else { 289 uint16_t a = float16_val(a_); 290 if (snan_bit_is_one(status)) { 291 return ((a >> 9) & 0x3F) == 0x3F; 292 } else { 293 return (((a >> 9) & 0x3F) == 0x3E) && (a & 0x1FF); 294 } 295 } 296} 297 298/*---------------------------------------------------------------------------- 299| Returns 1 if the bfloat16 value `a' is a signaling 300| NaN; otherwise returns 0. 301*----------------------------------------------------------------------------*/ 302 303bool bfloat16_is_signaling_nan(bfloat16 a_, float_status *status) 304{ 305 if (no_signaling_nans(status)) { 306 return 0; 307 } else { 308 uint16_t a = a_; 309 if (snan_bit_is_one(status)) { 310 return ((a >> 6) & 0x1FF) == 0x1FF; 311 } else { 312 return (((a >> 6) & 0x1FF) == 0x1FE) && (a & 0x3F); 313 } 314 } 315} 316 317/*---------------------------------------------------------------------------- 318| Returns 1 if the single-precision floating-point value `a' is a quiet 319| NaN; otherwise returns 0. 320*----------------------------------------------------------------------------*/ 321 322bool float32_is_quiet_nan(float32 a_, float_status *status) 323{ 324 if (no_signaling_nans(status)) { 325 return float32_is_any_nan(a_); 326 } else { 327 uint32_t a = float32_val(a_); 328 if (snan_bit_is_one(status)) { 329 return (((a >> 22) & 0x1FF) == 0x1FE) && (a & 0x003FFFFF); 330 } else { 331 return ((uint32_t)(a << 1) >= 0xFF800000); 332 } 333 } 334} 335 336/*---------------------------------------------------------------------------- 337| Returns 1 if the single-precision floating-point value `a' is a signaling 338| NaN; otherwise returns 0. 339*----------------------------------------------------------------------------*/ 340 341bool float32_is_signaling_nan(float32 a_, float_status *status) 342{ 343 if (no_signaling_nans(status)) { 344 return 0; 345 } else { 346 uint32_t a = float32_val(a_); 347 if (snan_bit_is_one(status)) { 348 return ((uint32_t)(a << 1) >= 0xFF800000); 349 } else { 350 return (((a >> 22) & 0x1FF) == 0x1FE) && (a & 0x003FFFFF); 351 } 352 } 353} 354 355/*---------------------------------------------------------------------------- 356| Returns 1 if the double-precision floating-point value `a' is a quiet 357| NaN; otherwise returns 0. 358*----------------------------------------------------------------------------*/ 359 360bool float64_is_quiet_nan(float64 a_, float_status *status) 361{ 362 if (no_signaling_nans(status)) { 363 return float64_is_any_nan(a_); 364 } else { 365 uint64_t a = float64_val(a_); 366 if (snan_bit_is_one(status)) { 367 return (((a >> 51) & 0xFFF) == 0xFFE) 368 && (a & 0x0007FFFFFFFFFFFFULL); 369 } else { 370 return ((a << 1) >= 0xFFF0000000000000ULL); 371 } 372 } 373} 374 375/*---------------------------------------------------------------------------- 376| Returns 1 if the double-precision floating-point value `a' is a signaling 377| NaN; otherwise returns 0. 378*----------------------------------------------------------------------------*/ 379 380bool float64_is_signaling_nan(float64 a_, float_status *status) 381{ 382 if (no_signaling_nans(status)) { 383 return 0; 384 } else { 385 uint64_t a = float64_val(a_); 386 if (snan_bit_is_one(status)) { 387 return ((a << 1) >= 0xFFF0000000000000ULL); 388 } else { 389 return (((a >> 51) & 0xFFF) == 0xFFE) 390 && (a & UINT64_C(0x0007FFFFFFFFFFFF)); 391 } 392 } 393} 394 395/*---------------------------------------------------------------------------- 396| Returns 1 if the extended double-precision floating-point value `a' is a 397| quiet NaN; otherwise returns 0. This slightly differs from the same 398| function for other types as floatx80 has an explicit bit. 399*----------------------------------------------------------------------------*/ 400 401int floatx80_is_quiet_nan(floatx80 a, float_status *status) 402{ 403 if (no_signaling_nans(status)) { 404 return floatx80_is_any_nan(a); 405 } else { 406 if (snan_bit_is_one(status)) { 407 uint64_t aLow; 408 409 aLow = a.low & ~0x4000000000000000ULL; 410 return ((a.high & 0x7FFF) == 0x7FFF) 411 && (aLow << 1) 412 && (a.low == aLow); 413 } else { 414 return ((a.high & 0x7FFF) == 0x7FFF) 415 && (UINT64_C(0x8000000000000000) <= ((uint64_t)(a.low << 1))); 416 } 417 } 418} 419 420/*---------------------------------------------------------------------------- 421| Returns 1 if the extended double-precision floating-point value `a' is a 422| signaling NaN; otherwise returns 0. This slightly differs from the same 423| function for other types as floatx80 has an explicit bit. 424*----------------------------------------------------------------------------*/ 425 426int floatx80_is_signaling_nan(floatx80 a, float_status *status) 427{ 428 if (no_signaling_nans(status)) { 429 return 0; 430 } else { 431 if (snan_bit_is_one(status)) { 432 return ((a.high & 0x7FFF) == 0x7FFF) 433 && ((a.low << 1) >= 0x8000000000000000ULL); 434 } else { 435 uint64_t aLow; 436 437 aLow = a.low & ~UINT64_C(0x4000000000000000); 438 return ((a.high & 0x7FFF) == 0x7FFF) 439 && (uint64_t)(aLow << 1) 440 && (a.low == aLow); 441 } 442 } 443} 444 445/*---------------------------------------------------------------------------- 446| Returns a quiet NaN from a signalling NaN for the extended double-precision 447| floating point value `a'. 448*----------------------------------------------------------------------------*/ 449 450floatx80 floatx80_silence_nan(floatx80 a, float_status *status) 451{ 452 /* None of the targets that have snan_bit_is_one use floatx80. */ 453 assert(!snan_bit_is_one(status)); 454 a.low |= UINT64_C(0xC000000000000000); 455 return a; 456} 457 458/*---------------------------------------------------------------------------- 459| Returns 1 if the quadruple-precision floating-point value `a' is a quiet 460| NaN; otherwise returns 0. 461*----------------------------------------------------------------------------*/ 462 463bool float128_is_quiet_nan(float128 a, float_status *status) 464{ 465 if (no_signaling_nans(status)) { 466 return float128_is_any_nan(a); 467 } else { 468 if (snan_bit_is_one(status)) { 469 return (((a.high >> 47) & 0xFFFF) == 0xFFFE) 470 && (a.low || (a.high & 0x00007FFFFFFFFFFFULL)); 471 } else { 472 return ((a.high << 1) >= 0xFFFF000000000000ULL) 473 && (a.low || (a.high & 0x0000FFFFFFFFFFFFULL)); 474 } 475 } 476} 477 478/*---------------------------------------------------------------------------- 479| Returns 1 if the quadruple-precision floating-point value `a' is a 480| signaling NaN; otherwise returns 0. 481*----------------------------------------------------------------------------*/ 482 483bool float128_is_signaling_nan(float128 a, float_status *status) 484{ 485 if (no_signaling_nans(status)) { 486 return 0; 487 } else { 488 if (snan_bit_is_one(status)) { 489 return ((a.high << 1) >= 0xFFFF000000000000ULL) 490 && (a.low || (a.high & 0x0000FFFFFFFFFFFFULL)); 491 } else { 492 return (((a.high >> 47) & 0xFFFF) == 0xFFFE) 493 && (a.low || (a.high & UINT64_C(0x00007FFFFFFFFFFF))); 494 } 495 } 496} 497