| 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 | =============================================================================== |
| 20 | This C source fragment is part of the SoftFloat IEC/IEEE Floating-point |
| 21 | Arithmetic Package, Release 2a. |
| 22 | |
| 23 | Written by John R. Hauser. This work was made possible in part by the |
| 24 | International Computer Science Institute, located at Suite 600, 1947 Center |
| 25 | Street, Berkeley, California 94704. Funding was partially provided by the |
| 26 | National Science Foundation under grant MIP-9311980. The original version |
| 27 | of this code was written as part of a project to build a fixed-point vector |
| 28 | processor in collaboration with the University of California at Berkeley, |
| 29 | overseen by Profs. Nelson Morgan and John Wawrzynek. More information |
| 30 | is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/ |
| 31 | arithmetic/SoftFloat.html'. |
| 32 | |
| 33 | THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort |
| 34 | has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT |
| 35 | TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO |
| 36 | PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY |
| 37 | AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE. |
| 38 | |
| 39 | Derivative works are acceptable, even for commercial purposes, so long as |
| 40 | (1) they include prominent notice that the work is derivative, and (2) they |
| 41 | include prominent notice akin to these four paragraphs for those parts of |
| 42 | this 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 | static inline bool get_tininess_before_rounding(const float_status *status) |
| 83 | { |
| 84 | return status->tininess_before_rounding; |
| 85 | } |
| 86 | |
| 87 | static inline bool get_ftz_before_rounding(const float_status *status) |
| 88 | { |
| 89 | return status->ftz_before_rounding; |
| 90 | } |
| 91 | |
| 92 | static inline uint8_t get_float_default_nan_pattern(const float_status *status) |
| 93 | { |
| 94 | return status->default_nan_pattern; |
| 95 | } |
| 96 | |
| 97 | static inline bool get_float_rebias_overflow(const float_status *status) |
| 98 | { |
| 99 | return status->rebias_overflow; |
| 100 | } |
| 101 | |
| 102 | static inline bool get_float_rebias_underflow(const float_status *status) |
| 103 | { |
| 104 | return status->rebias_underflow; |
| 105 | } |
| 106 | |
| 107 | static inline bool get_float_e4m3_nan_is_snan(const float_status *status) |
| 108 | { |
| 109 | return status->e4m3_nan_is_snan; |
| 110 | } |
| 111 | |
| 112 | /*---------------------------------------------------------------------------- |
| 113 | | For the deconstructed floating-point with fraction FRAC, return true |
| 114 | | if the fraction represents a signalling NaN; otherwise false. |
| 115 | *----------------------------------------------------------------------------*/ |
| 116 | |
| 117 | static bool frac_msb_is_snan(bool msb, float_status *status) |
| 118 | { |
| 119 | switch (get_snan_rule(status)) { |
| 120 | case float_snan_never: |
| 121 | return false; |
| 122 | case float_snan_bit_is_one: |
| 123 | return msb; |
| 124 | case float_snan_bit_is_zero: |
| 125 | return !msb; |
| 126 | } |
| 127 | g_assert_not_reached(); |
| 128 | } |
| 129 | |
| 130 | static bool parts_is_snan_frac(uint64_t frac, float_status *status) |
| 131 | { |
| 132 | bool msb = extract64(frac, DECOMPOSED_BINARY_POINT - 1, 1); |
| 133 | return frac_msb_is_snan(msb, status); |
| 134 | } |
| 135 | |
| 136 | /*---------------------------------------------------------------------------- |
| 137 | | The pattern for a default generated deconstructed floating-point NaN. |
| 138 | *----------------------------------------------------------------------------*/ |
| 139 | |
| 140 | FloatParts64 parts64_default_nan(float_status *status) |
| 141 | { |
| 142 | bool sign = 0; |
| 143 | uint64_t frac; |
| 144 | uint8_t dnan_pattern = get_float_default_nan_pattern(status); |
| 145 | |
| 146 | assert(dnan_pattern != 0); |
| 147 | |
| 148 | sign = dnan_pattern >> 7; |
| 149 | /* |
| 150 | * Place default_nan_pattern [6:0] into bits [62:56], |
| 151 | * and replecate bit [0] down into [55:0] |
| 152 | */ |
| 153 | frac = deposit64(0, DECOMPOSED_BINARY_POINT - 7, 7, dnan_pattern); |
| 154 | frac = deposit64(frac, 0, DECOMPOSED_BINARY_POINT - 7, -(dnan_pattern & 1)); |
| 155 | |
| 156 | return (FloatParts64) { |
| 157 | .cls = float_class_qnan, |
| 158 | .sign = sign, |
| 159 | .exp = INT_MAX, |
| 160 | .frac = frac |
| 161 | }; |
| 162 | } |
| 163 | |
| 164 | FloatParts128 parts128_default_nan(float_status *status) |
| 165 | { |
| 166 | /* |
| 167 | * Extrapolate from the choices made by parts64_default_nan to fill |
| 168 | * in the quad-floating format. If the low bit is set, assume we |
| 169 | * want to set all non-snan bits. |
| 170 | */ |
| 171 | FloatParts64 p64 = parts64_default_nan(status); |
| 172 | |
| 173 | return (FloatParts128) { |
| 174 | .cls = float_class_qnan, |
| 175 | .sign = p64.sign, |
| 176 | .exp = INT_MAX, |
| 177 | .frac_hi = p64.frac, |
| 178 | .frac_lo = -(p64.frac & 1) |
| 179 | }; |
| 180 | } |
| 181 | |
| 182 | /*---------------------------------------------------------------------------- |
| 183 | | Returns a quiet NaN from a signalling NaN for the deconstructed |
| 184 | | floating-point parts. |
| 185 | *----------------------------------------------------------------------------*/ |
| 186 | |
| 187 | static uint64_t parts_silence_nan_frac(uint64_t frac, float_status *status) |
| 188 | { |
| 189 | switch (get_snan_rule(status)) { |
| 190 | case float_snan_bit_is_zero: |
| 191 | frac |= 1ULL << (DECOMPOSED_BINARY_POINT - 1); |
| 192 | break; |
| 193 | case float_snan_bit_is_one: |
| 194 | /* The only snan_bit_is_one target without default_nan_mode is HPPA. */ |
| 195 | frac &= ~(1ULL << (DECOMPOSED_BINARY_POINT - 1)); |
| 196 | frac |= 1ULL << (DECOMPOSED_BINARY_POINT - 2); |
| 197 | break; |
| 198 | case float_snan_never: |
| 199 | default: |
| 200 | g_assert_not_reached(); |
| 201 | } |
| 202 | return frac; |
| 203 | } |
| 204 | |
| 205 | static FloatParts64 parts64_silence_nan(const FloatParts64 *p, |
| 206 | float_status *status) |
| 207 | { |
| 208 | FloatParts64 r = *p; |
| 209 | |
| 210 | r.frac = parts_silence_nan_frac(r.frac, status); |
| 211 | r.cls = float_class_qnan; |
| 212 | return r; |
| 213 | } |
| 214 | |
| 215 | static FloatParts128 parts128_silence_nan(const FloatParts128 *p, |
| 216 | float_status *status) |
| 217 | { |
| 218 | FloatParts128 r = *p; |
| 219 | |
| 220 | r.frac_hi = parts_silence_nan_frac(r.frac_hi, status); |
| 221 | r.cls = float_class_qnan; |
| 222 | return r; |
| 223 | } |
| 224 | |
| 225 | /*---------------------------------------------------------------------------- |
| 226 | | The pattern for a default generated extended double-precision NaN. |
| 227 | *----------------------------------------------------------------------------*/ |
| 228 | floatx80 floatx80_default_nan(float_status *status) |
| 229 | { |
| 230 | /* |
| 231 | * Extrapolate from the choices made by parts64_default_nan to fill |
| 232 | * in the floatx80 format. We assume that floatx80's explicit |
| 233 | * integer bit is always set (this is true for i386 and m68k, |
| 234 | * which are the only real users of this format). |
| 235 | */ |
| 236 | FloatParts64 p64 = parts64_default_nan(status); |
| 237 | |
| 238 | return (floatx80) { |
| 239 | .high = 0x7FFF | (p64.sign << 15), |
| 240 | .low = (1ULL << DECOMPOSED_BINARY_POINT) | p64.frac, |
| 241 | }; |
| 242 | } |
| 243 | |
| 244 | /*---------------------------------------------------------------------------- |
| 245 | | The pattern for a default generated extended double-precision inf. |
| 246 | *----------------------------------------------------------------------------*/ |
| 247 | |
| 248 | floatx80 floatx80_default_inf(bool zSign, float_status *status) |
| 249 | { |
| 250 | /* |
| 251 | * Whether the Integer bit is set in the default Infinity is |
| 252 | * target dependent. |
| 253 | */ |
| 254 | bool z = get_floatx80_behaviour(status) & floatx80_default_inf_int_bit_is_zero; |
| 255 | return packFloatx80(zSign, 0x7fff, z ? 0 : (1ULL << 63)); |
| 256 | } |
| 257 | |
| 258 | /*---------------------------------------------------------------------------- |
| 259 | | Determine if a float16 NaN is signaling NaN. |
| 260 | *----------------------------------------------------------------------------*/ |
| 261 | |
| 262 | static bool float16_nan_is_snan(float16 a, float_status *status) |
| 263 | { |
| 264 | return frac_msb_is_snan((a >> 9) & 1, status); |
| 265 | } |
| 266 | |
| 267 | /*---------------------------------------------------------------------------- |
| 268 | | Returns 1 if the half-precision floating-point value `a' is a quiet |
| 269 | | NaN; otherwise returns 0. |
| 270 | *----------------------------------------------------------------------------*/ |
| 271 | |
| 272 | bool float16_is_quiet_nan(float16 a_, float_status *status) |
| 273 | { |
| 274 | return float16_is_any_nan(a_) && !float16_nan_is_snan(a_, status); |
| 275 | } |
| 276 | |
| 277 | /*---------------------------------------------------------------------------- |
| 278 | | Returns 1 if the half-precision floating-point value `a' is a signaling |
| 279 | | NaN; otherwise returns 0. |
| 280 | *----------------------------------------------------------------------------*/ |
| 281 | |
| 282 | bool float16_is_signaling_nan(float16 a_, float_status *status) |
| 283 | { |
| 284 | return float16_is_any_nan(a_) && float16_nan_is_snan(a_, status); |
| 285 | } |
| 286 | |
| 287 | /*---------------------------------------------------------------------------- |
| 288 | | Determine if a bfloat16 NaN is signaling NaN. |
| 289 | *----------------------------------------------------------------------------*/ |
| 290 | |
| 291 | static bool bfloat16_nan_is_snan(bfloat16 a, float_status *status) |
| 292 | { |
| 293 | return frac_msb_is_snan((a >> 6) & 1, status); |
| 294 | } |
| 295 | |
| 296 | /*---------------------------------------------------------------------------- |
| 297 | | Returns 1 if the bfloat16 value `a' is a quiet NaN; otherwise returns 0. |
| 298 | *----------------------------------------------------------------------------*/ |
| 299 | |
| 300 | bool bfloat16_is_quiet_nan(bfloat16 a_, float_status *status) |
| 301 | { |
| 302 | return bfloat16_is_any_nan(a_) && !bfloat16_nan_is_snan(a_, status); |
| 303 | } |
| 304 | |
| 305 | /*---------------------------------------------------------------------------- |
| 306 | | Returns 1 if the bfloat16 value `a' is a signaling NaN; otherwise returns 0. |
| 307 | *----------------------------------------------------------------------------*/ |
| 308 | |
| 309 | bool bfloat16_is_signaling_nan(bfloat16 a_, float_status *status) |
| 310 | { |
| 311 | return bfloat16_is_any_nan(a_) && bfloat16_nan_is_snan(a_, status); |
| 312 | } |
| 313 | |
| 314 | /*---------------------------------------------------------------------------- |
| 315 | | Determine if a float32 NaN is signaling NaN. |
| 316 | *----------------------------------------------------------------------------*/ |
| 317 | |
| 318 | static bool float32_nan_is_snan(float32 a, float_status *status) |
| 319 | { |
| 320 | return frac_msb_is_snan((a >> 22) & 1, status); |
| 321 | } |
| 322 | |
| 323 | /*---------------------------------------------------------------------------- |
| 324 | | Returns 1 if the single-precision floating-point value `a' is a quiet |
| 325 | | NaN; otherwise returns 0. |
| 326 | *----------------------------------------------------------------------------*/ |
| 327 | |
| 328 | bool float32_is_quiet_nan(float32 a_, float_status *status) |
| 329 | { |
| 330 | return float32_is_any_nan(a_) && !float32_nan_is_snan(a_, status); |
| 331 | } |
| 332 | |
| 333 | /*---------------------------------------------------------------------------- |
| 334 | | Returns 1 if the single-precision floating-point value `a' is a signaling |
| 335 | | NaN; otherwise returns 0. |
| 336 | *----------------------------------------------------------------------------*/ |
| 337 | |
| 338 | bool float32_is_signaling_nan(float32 a_, float_status *status) |
| 339 | { |
| 340 | return float32_is_any_nan(a_) && float32_nan_is_snan(a_, status); |
| 341 | } |
| 342 | |
| 343 | /*---------------------------------------------------------------------------- |
| 344 | | Determine if a float64 NaN is signaling NaN. |
| 345 | *----------------------------------------------------------------------------*/ |
| 346 | |
| 347 | static bool float64_nan_is_snan(float64 a, float_status *status) |
| 348 | { |
| 349 | return frac_msb_is_snan((a >> 51) & 1, status); |
| 350 | } |
| 351 | |
| 352 | /*---------------------------------------------------------------------------- |
| 353 | | Returns 1 if the double-precision floating-point value `a' is a quiet |
| 354 | | NaN; otherwise returns 0. |
| 355 | *----------------------------------------------------------------------------*/ |
| 356 | |
| 357 | bool float64_is_quiet_nan(float64 a_, float_status *status) |
| 358 | { |
| 359 | return float64_is_any_nan(a_) && !float64_nan_is_snan(a_, status); |
| 360 | } |
| 361 | |
| 362 | /*---------------------------------------------------------------------------- |
| 363 | | Returns 1 if the double-precision floating-point value `a' is a signaling |
| 364 | | NaN; otherwise returns 0. |
| 365 | *----------------------------------------------------------------------------*/ |
| 366 | |
| 367 | bool float64_is_signaling_nan(float64 a_, float_status *status) |
| 368 | { |
| 369 | return float64_is_any_nan(a_) && float64_nan_is_snan(a_, status); |
| 370 | } |
| 371 | |
| 372 | /*---------------------------------------------------------------------------- |
| 373 | | Determine if a floatx80 NaN is signaling NaN. |
| 374 | | The MSB of frac differs from the same function for other types as floatx80 |
| 375 | | has an explicit bit. |
| 376 | *----------------------------------------------------------------------------*/ |
| 377 | |
| 378 | static bool floatx80_nan_is_snan(floatx80 a, float_status *status) |
| 379 | { |
| 380 | return frac_msb_is_snan((a.low >> 62) & 1, status); |
| 381 | } |
| 382 | |
| 383 | /*---------------------------------------------------------------------------- |
| 384 | | Returns 1 if the extended double-precision floating-point value `a' is a |
| 385 | | quiet NaN; otherwise returns 0. |
| 386 | *----------------------------------------------------------------------------*/ |
| 387 | |
| 388 | bool floatx80_is_quiet_nan(floatx80 a, float_status *status) |
| 389 | { |
| 390 | return floatx80_is_any_nan(a) && !floatx80_nan_is_snan(a, status); |
| 391 | } |
| 392 | |
| 393 | /*---------------------------------------------------------------------------- |
| 394 | | Returns 1 if the extended double-precision floating-point value `a' is a |
| 395 | | signaling NaN; otherwise returns 0. |
| 396 | *----------------------------------------------------------------------------*/ |
| 397 | |
| 398 | bool floatx80_is_signaling_nan(floatx80 a, float_status *status) |
| 399 | { |
| 400 | return floatx80_is_any_nan(a) && floatx80_nan_is_snan(a, status); |
| 401 | } |
| 402 | |
| 403 | /*---------------------------------------------------------------------------- |
| 404 | | Returns a quiet NaN from a signalling NaN for the extended double-precision |
| 405 | | floating point value `a'. |
| 406 | *----------------------------------------------------------------------------*/ |
| 407 | |
| 408 | floatx80 floatx80_silence_nan(floatx80 a, float_status *status) |
| 409 | { |
| 410 | /* None of the targets that have snan_bit_is_one use floatx80. */ |
| 411 | assert(get_snan_rule(status) == float_snan_bit_is_zero); |
| 412 | a.low |= UINT64_C(0xC000000000000000); |
| 413 | return a; |
| 414 | } |
| 415 | |
| 416 | /*---------------------------------------------------------------------------- |
| 417 | | Determine if a float128 NaN is signaling NaN. |
| 418 | *----------------------------------------------------------------------------*/ |
| 419 | |
| 420 | static bool float128_nan_is_snan(float128 a, float_status *status) |
| 421 | { |
| 422 | return frac_msb_is_snan((a.high >> 47) & 1, status); |
| 423 | } |
| 424 | |
| 425 | /*---------------------------------------------------------------------------- |
| 426 | | Returns 1 if the quadruple-precision floating-point value `a' is a quiet |
| 427 | | NaN; otherwise returns 0. |
| 428 | *----------------------------------------------------------------------------*/ |
| 429 | |
| 430 | bool float128_is_quiet_nan(float128 a, float_status *status) |
| 431 | { |
| 432 | return float128_is_any_nan(a) && !float128_nan_is_snan(a, status); |
| 433 | } |
| 434 | |
| 435 | /*---------------------------------------------------------------------------- |
| 436 | | Returns 1 if the quadruple-precision floating-point value `a' is a |
| 437 | | signaling NaN; otherwise returns 0. |
| 438 | *----------------------------------------------------------------------------*/ |
| 439 | |
| 440 | bool float128_is_signaling_nan(float128 a, float_status *status) |
| 441 | { |
| 442 | return float128_is_any_nan(a) && float128_nan_is_snan(a, status); |
| 443 | } |