Alex Bennée | cfd88fc | 2018-01-19 16:36:40 +0000 | [diff] [blame] | 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 | * This header holds definitions for code that might be dealing with |
| 14 | * softfloat types but not need access to the actual library functions. |
| 15 | */ |
| 16 | /* |
| 17 | =============================================================================== |
| 18 | This C header file is part of the SoftFloat IEC/IEEE Floating-point |
| 19 | Arithmetic Package, Release 2a. |
| 20 | |
| 21 | Written by John R. Hauser. This work was made possible in part by the |
| 22 | International Computer Science Institute, located at Suite 600, 1947 Center |
| 23 | Street, Berkeley, California 94704. Funding was partially provided by the |
| 24 | National Science Foundation under grant MIP-9311980. The original version |
| 25 | of this code was written as part of a project to build a fixed-point vector |
| 26 | processor in collaboration with the University of California at Berkeley, |
| 27 | overseen by Profs. Nelson Morgan and John Wawrzynek. More information |
| 28 | is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/ |
| 29 | arithmetic/SoftFloat.html'. |
| 30 | |
| 31 | THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort |
| 32 | has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT |
| 33 | TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO |
| 34 | PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY |
| 35 | AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE. |
| 36 | |
| 37 | Derivative works are acceptable, even for commercial purposes, so long as |
| 38 | (1) they include prominent notice that the work is derivative, and (2) they |
| 39 | include prominent notice akin to these four paragraphs for those parts of |
| 40 | this code that are retained. |
| 41 | |
| 42 | =============================================================================== |
| 43 | */ |
| 44 | |
| 45 | /* BSD licensing: |
| 46 | * Copyright (c) 2006, Fabrice Bellard |
| 47 | * All rights reserved. |
| 48 | * |
| 49 | * Redistribution and use in source and binary forms, with or without |
| 50 | * modification, are permitted provided that the following conditions are met: |
| 51 | * |
| 52 | * 1. Redistributions of source code must retain the above copyright notice, |
| 53 | * this list of conditions and the following disclaimer. |
| 54 | * |
| 55 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 56 | * this list of conditions and the following disclaimer in the documentation |
| 57 | * and/or other materials provided with the distribution. |
| 58 | * |
| 59 | * 3. Neither the name of the copyright holder nor the names of its contributors |
| 60 | * may be used to endorse or promote products derived from this software without |
| 61 | * specific prior written permission. |
| 62 | * |
| 63 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 64 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 65 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 66 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 67 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 68 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 69 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 70 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 71 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 72 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 73 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 74 | */ |
| 75 | |
| 76 | /* Portions of this work are licensed under the terms of the GNU GPL, |
| 77 | * version 2 or later. See the COPYING file in the top-level directory. |
| 78 | */ |
| 79 | |
| 80 | #ifndef SOFTFLOAT_TYPES_H |
| 81 | #define SOFTFLOAT_TYPES_H |
| 82 | |
| 83 | /* |
| 84 | * Software IEC/IEEE floating-point types. |
| 85 | */ |
| 86 | |
| 87 | typedef uint16_t float16; |
| 88 | typedef uint32_t float32; |
| 89 | typedef uint64_t float64; |
| 90 | #define float16_val(x) (x) |
| 91 | #define float32_val(x) (x) |
| 92 | #define float64_val(x) (x) |
| 93 | #define make_float16(x) (x) |
| 94 | #define make_float32(x) (x) |
| 95 | #define make_float64(x) (x) |
| 96 | #define const_float16(x) (x) |
| 97 | #define const_float32(x) (x) |
| 98 | #define const_float64(x) (x) |
| 99 | typedef struct { |
| 100 | uint64_t low; |
| 101 | uint16_t high; |
| 102 | } floatx80; |
| 103 | #define make_floatx80(exp, mant) ((floatx80) { mant, exp }) |
| 104 | #define make_floatx80_init(exp, mant) { .low = mant, .high = exp } |
| 105 | typedef struct { |
Marc-André Lureau | e03b568 | 2022-03-23 19:57:17 +0400 | [diff] [blame] | 106 | #if HOST_BIG_ENDIAN |
Alex Bennée | cfd88fc | 2018-01-19 16:36:40 +0000 | [diff] [blame] | 107 | uint64_t high, low; |
| 108 | #else |
| 109 | uint64_t low, high; |
| 110 | #endif |
| 111 | } float128; |
| 112 | #define make_float128(high_, low_) ((float128) { .high = high_, .low = low_ }) |
| 113 | #define make_float128_init(high_, low_) { .high = high_, .low = low_ } |
| 114 | |
Alex Bennée | 24f91e8 | 2018-01-19 18:24:22 +0000 | [diff] [blame] | 115 | /* |
LIU Zhiwei | 8282310 | 2020-08-13 15:14:19 +0800 | [diff] [blame] | 116 | * Software neural-network floating-point types. |
| 117 | */ |
| 118 | typedef uint16_t bfloat16; |
| 119 | |
| 120 | /* |
Alex Bennée | 24f91e8 | 2018-01-19 18:24:22 +0000 | [diff] [blame] | 121 | * Software IEC/IEEE floating-point underflow tininess-detection mode. |
| 122 | */ |
| 123 | |
Richard Henderson | a828b37 | 2020-05-04 21:19:39 -0700 | [diff] [blame] | 124 | #define float_tininess_after_rounding false |
| 125 | #define float_tininess_before_rounding true |
Alex Bennée | 24f91e8 | 2018-01-19 18:24:22 +0000 | [diff] [blame] | 126 | |
| 127 | /* |
| 128 | *Software IEC/IEEE floating-point rounding mode. |
| 129 | */ |
| 130 | |
Richard Henderson | 3dede40 | 2020-05-05 09:01:49 -0700 | [diff] [blame] | 131 | typedef enum __attribute__((__packed__)) { |
Alex Bennée | 24f91e8 | 2018-01-19 18:24:22 +0000 | [diff] [blame] | 132 | float_round_nearest_even = 0, |
| 133 | float_round_down = 1, |
| 134 | float_round_up = 2, |
| 135 | float_round_to_zero = 3, |
| 136 | float_round_ties_away = 4, |
Richard Henderson | 60c8f72 | 2021-05-25 15:58:10 -0700 | [diff] [blame] | 137 | /* Not an IEEE rounding mode: round to closest odd, overflow to max */ |
Alex Bennée | 24f91e8 | 2018-01-19 18:24:22 +0000 | [diff] [blame] | 138 | float_round_to_odd = 5, |
Richard Henderson | 60c8f72 | 2021-05-25 15:58:10 -0700 | [diff] [blame] | 139 | /* Not an IEEE rounding mode: round to closest odd, overflow to inf */ |
| 140 | float_round_to_odd_inf = 6, |
Richard Henderson | 3dede40 | 2020-05-05 09:01:49 -0700 | [diff] [blame] | 141 | } FloatRoundMode; |
Alex Bennée | 24f91e8 | 2018-01-19 18:24:22 +0000 | [diff] [blame] | 142 | |
| 143 | /* |
| 144 | * Software IEC/IEEE floating-point exception flags. |
| 145 | */ |
| 146 | |
| 147 | enum { |
Richard Henderson | 149a48f | 2021-12-17 17:57:14 +0100 | [diff] [blame] | 148 | float_flag_invalid = 0x0001, |
| 149 | float_flag_divbyzero = 0x0002, |
| 150 | float_flag_overflow = 0x0004, |
| 151 | float_flag_underflow = 0x0008, |
| 152 | float_flag_inexact = 0x0010, |
| 153 | float_flag_input_denormal = 0x0020, |
| 154 | float_flag_output_denormal = 0x0040, |
Richard Henderson | ba11446 | 2021-12-17 17:57:14 +0100 | [diff] [blame] | 155 | float_flag_invalid_isi = 0x0080, /* inf - inf */ |
Richard Henderson | bead3c9 | 2021-12-17 17:57:14 +0100 | [diff] [blame] | 156 | float_flag_invalid_imz = 0x0100, /* inf * 0 */ |
Richard Henderson | 10cc964 | 2021-12-17 17:57:14 +0100 | [diff] [blame] | 157 | float_flag_invalid_idi = 0x0200, /* inf / inf */ |
| 158 | float_flag_invalid_zdz = 0x0400, /* 0 / 0 */ |
Richard Henderson | f8718aa | 2021-12-17 17:57:14 +0100 | [diff] [blame] | 159 | float_flag_invalid_sqrt = 0x0800, /* sqrt(-x) */ |
Richard Henderson | 81254b0 | 2021-12-17 17:57:14 +0100 | [diff] [blame] | 160 | float_flag_invalid_cvti = 0x1000, /* non-nan to integer */ |
Richard Henderson | e706d44 | 2021-12-17 17:57:14 +0100 | [diff] [blame] | 161 | float_flag_invalid_snan = 0x2000, /* any operand was snan */ |
Alex Bennée | 24f91e8 | 2018-01-19 18:24:22 +0000 | [diff] [blame] | 162 | }; |
| 163 | |
Richard Henderson | 8da5f1d | 2020-11-21 09:33:36 -0800 | [diff] [blame] | 164 | /* |
| 165 | * Rounding precision for floatx80. |
| 166 | */ |
| 167 | typedef enum __attribute__((__packed__)) { |
| 168 | floatx80_precision_x, |
| 169 | floatx80_precision_d, |
| 170 | floatx80_precision_s, |
| 171 | } FloatX80RoundPrec; |
Alex Bennée | 24f91e8 | 2018-01-19 18:24:22 +0000 | [diff] [blame] | 172 | |
| 173 | /* |
| 174 | * Floating Point Status. Individual architectures may maintain |
| 175 | * several versions of float_status for different functions. The |
| 176 | * correct status for the operation is then passed by reference to |
| 177 | * most of the softfloat functions. |
| 178 | */ |
| 179 | |
| 180 | typedef struct float_status { |
Richard Henderson | 149a48f | 2021-12-17 17:57:14 +0100 | [diff] [blame] | 181 | uint16_t float_exception_flags; |
Richard Henderson | 3dede40 | 2020-05-05 09:01:49 -0700 | [diff] [blame] | 182 | FloatRoundMode float_rounding_mode; |
Richard Henderson | 8da5f1d | 2020-11-21 09:33:36 -0800 | [diff] [blame] | 183 | FloatX80RoundPrec floatx80_rounding_precision; |
Richard Henderson | a828b37 | 2020-05-04 21:19:39 -0700 | [diff] [blame] | 184 | bool tininess_before_rounding; |
Alex Bennée | 24f91e8 | 2018-01-19 18:24:22 +0000 | [diff] [blame] | 185 | /* should denormalised results go to zero and set the inexact flag? */ |
Richard Henderson | c120391 | 2020-05-04 19:54:57 -0700 | [diff] [blame] | 186 | bool flush_to_zero; |
Alex Bennée | 24f91e8 | 2018-01-19 18:24:22 +0000 | [diff] [blame] | 187 | /* should denormalised inputs go to zero and set the input_denormal flag? */ |
Richard Henderson | c120391 | 2020-05-04 19:54:57 -0700 | [diff] [blame] | 188 | bool flush_inputs_to_zero; |
| 189 | bool default_nan_mode; |
Max Filippov | cc43c69 | 2020-06-30 19:35:49 -0700 | [diff] [blame] | 190 | /* |
| 191 | * The flags below are not used on all specializations and may |
| 192 | * constant fold away (see snan_bit_is_one()/no_signalling_nans() in |
| 193 | * softfloat-specialize.inc.c) |
| 194 | */ |
Richard Henderson | c120391 | 2020-05-04 19:54:57 -0700 | [diff] [blame] | 195 | bool snan_bit_is_one; |
Max Filippov | 913602e | 2020-06-30 19:35:57 -0700 | [diff] [blame] | 196 | bool use_first_nan; |
Max Filippov | cc43c69 | 2020-06-30 19:35:49 -0700 | [diff] [blame] | 197 | bool no_signaling_nans; |
Lucas Mateus Castro (alqotel) | c40da5c | 2022-08-05 11:15:21 -0300 | [diff] [blame] | 198 | /* should overflowed results subtract re_bias to its exponent? */ |
| 199 | bool rebias_overflow; |
| 200 | /* should underflowed results add re_bias to its exponent? */ |
| 201 | bool rebias_underflow; |
Alex Bennée | 24f91e8 | 2018-01-19 18:24:22 +0000 | [diff] [blame] | 202 | } float_status; |
| 203 | |
Alex Bennée | cfd88fc | 2018-01-19 16:36:40 +0000 | [diff] [blame] | 204 | #endif /* SOFTFLOAT_TYPES_H */ |