xref: /qemu/include/fpu/softfloat-types.h (revision e4a8e093dc74be049f4829831dce76e5edab0003)
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 #include "hw/registerfields.h"
84 
85 /*
86  * Software IEC/IEEE floating-point types.
87  */
88 
89 typedef uint16_t float16;
90 typedef uint32_t float32;
91 typedef uint64_t float64;
92 #define float16_val(x) (x)
93 #define float32_val(x) (x)
94 #define float64_val(x) (x)
95 #define make_float16(x) (x)
96 #define make_float32(x) (x)
97 #define make_float64(x) (x)
98 #define const_float16(x) (x)
99 #define const_float32(x) (x)
100 #define const_float64(x) (x)
101 typedef struct {
102     uint64_t low;
103     uint16_t high;
104 } floatx80;
105 #define make_floatx80(exp, mant) ((floatx80) { mant, exp })
106 #define make_floatx80_init(exp, mant) { .low = mant, .high = exp }
107 typedef struct {
108 #if HOST_BIG_ENDIAN
109     uint64_t high, low;
110 #else
111     uint64_t low, high;
112 #endif
113 } float128;
114 #define make_float128(high_, low_) ((float128) { .high = high_, .low = low_ })
115 #define make_float128_init(high_, low_) { .high = high_, .low = low_ }
116 
117 /*
118  * Software neural-network floating-point types.
119  */
120 typedef uint16_t bfloat16;
121 
122 /*
123  * Software IEC/IEEE floating-point underflow tininess-detection mode.
124  */
125 
126 #define float_tininess_after_rounding  false
127 #define float_tininess_before_rounding true
128 
129 /*
130  *Software IEC/IEEE floating-point rounding mode.
131  */
132 
133 typedef enum __attribute__((__packed__)) {
134     float_round_nearest_even = 0,
135     float_round_down         = 1,
136     float_round_up           = 2,
137     float_round_to_zero      = 3,
138     float_round_ties_away    = 4,
139     /* Not an IEEE rounding mode: round to closest odd, overflow to max */
140     float_round_to_odd       = 5,
141     /* Not an IEEE rounding mode: round to closest odd, overflow to inf */
142     float_round_to_odd_inf   = 6,
143     /* Not an IEEE rounding mode: round to nearest even, overflow to max */
144     float_round_nearest_even_max = 7,
145 } FloatRoundMode;
146 
147 /*
148  * Software IEC/IEEE floating-point exception flags.
149  */
150 
151 enum {
152     float_flag_invalid         = 0x0001,
153     float_flag_divbyzero       = 0x0002,
154     float_flag_overflow        = 0x0004,
155     float_flag_underflow       = 0x0008,
156     float_flag_inexact         = 0x0010,
157     float_flag_input_denormal  = 0x0020,
158     float_flag_output_denormal = 0x0040,
159     float_flag_invalid_isi     = 0x0080,  /* inf - inf */
160     float_flag_invalid_imz     = 0x0100,  /* inf * 0 */
161     float_flag_invalid_idi     = 0x0200,  /* inf / inf */
162     float_flag_invalid_zdz     = 0x0400,  /* 0 / 0 */
163     float_flag_invalid_sqrt    = 0x0800,  /* sqrt(-x) */
164     float_flag_invalid_cvti    = 0x1000,  /* non-nan to integer */
165     float_flag_invalid_snan    = 0x2000,  /* any operand was snan */
166 };
167 
168 /*
169  * Rounding precision for floatx80.
170  */
171 typedef enum __attribute__((__packed__)) {
172     floatx80_precision_x,
173     floatx80_precision_d,
174     floatx80_precision_s,
175 } FloatX80RoundPrec;
176 
177 /*
178  * 2-input NaN propagation rule. Individual architectures have
179  * different rules for which input NaN is propagated to the output
180  * when there is more than one NaN on the input.
181  *
182  * If default_nan_mode is enabled then it is valid not to set a
183  * NaN propagation rule, because the softfloat code guarantees
184  * not to try to pick a NaN to propagate in default NaN mode.
185  * When not in default-NaN mode, it is an error for the target
186  * not to set the rule in float_status, and we will assert if
187  * we need to handle an input NaN and no rule was selected.
188  */
189 typedef enum __attribute__((__packed__)) {
190     /* No propagation rule specified */
191     float_2nan_prop_none = 0,
192     /* Prefer SNaN over QNaN, then operand A over B */
193     float_2nan_prop_s_ab,
194     /* Prefer SNaN over QNaN, then operand B over A */
195     float_2nan_prop_s_ba,
196     /* Prefer A over B regardless of SNaN vs QNaN */
197     float_2nan_prop_ab,
198     /* Prefer B over A regardless of SNaN vs QNaN */
199     float_2nan_prop_ba,
200     /*
201      * This implements x87 NaN propagation rules:
202      * SNaN + QNaN => return the QNaN
203      * two SNaNs => return the one with the larger significand, silenced
204      * two QNaNs => return the one with the larger significand
205      * SNaN and a non-NaN => return the SNaN, silenced
206      * QNaN and a non-NaN => return the QNaN
207      *
208      * If we get down to comparing significands and they are the same,
209      * return the NaN with the positive sign bit (if any).
210      */
211     float_2nan_prop_x87,
212 } Float2NaNPropRule;
213 
214 /*
215  * 3-input NaN propagation rule, for fused multiply-add. Individual
216  * architectures have different rules for which input NaN is
217  * propagated to the output when there is more than one NaN on the
218  * input.
219  *
220  * If default_nan_mode is enabled then it is valid not to set a NaN
221  * propagation rule, because the softfloat code guarantees not to try
222  * to pick a NaN to propagate in default NaN mode.  When not in
223  * default-NaN mode, it is an error for the target not to set the rule
224  * in float_status if it uses a muladd, and we will assert if we need
225  * to handle an input NaN and no rule was selected.
226  *
227  * The naming scheme for Float3NaNPropRule values is:
228  *  float_3nan_prop_s_abc:
229  *    = "Prefer SNaN over QNaN, then operand A over B over C"
230  *  float_3nan_prop_abc:
231  *    = "Prefer A over B over C regardless of SNaN vs QNAN"
232  *
233  * For QEMU, the multiply-add operation is A * B + C.
234  */
235 
236 /*
237  * We set the Float3NaNPropRule enum values up so we can select the
238  * right value in pickNaNMulAdd in a data driven way.
239  */
240 FIELD(3NAN, 1ST, 0, 2)   /* which operand is most preferred ? */
241 FIELD(3NAN, 2ND, 2, 2)   /* which operand is next most preferred ? */
242 FIELD(3NAN, 3RD, 4, 2)   /* which operand is least preferred ? */
243 FIELD(3NAN, SNAN, 6, 1)  /* do we prefer SNaN over QNaN ? */
244 
245 #define PROPRULE(X, Y, Z) \
246     ((X << R_3NAN_1ST_SHIFT) | (Y << R_3NAN_2ND_SHIFT) | (Z << R_3NAN_3RD_SHIFT))
247 
248 typedef enum __attribute__((__packed__)) {
249     float_3nan_prop_none = 0,     /* No propagation rule specified */
250     float_3nan_prop_abc = PROPRULE(0, 1, 2),
251     float_3nan_prop_acb = PROPRULE(0, 2, 1),
252     float_3nan_prop_bac = PROPRULE(1, 0, 2),
253     float_3nan_prop_bca = PROPRULE(1, 2, 0),
254     float_3nan_prop_cab = PROPRULE(2, 0, 1),
255     float_3nan_prop_cba = PROPRULE(2, 1, 0),
256     float_3nan_prop_s_abc = float_3nan_prop_abc | R_3NAN_SNAN_MASK,
257     float_3nan_prop_s_acb = float_3nan_prop_acb | R_3NAN_SNAN_MASK,
258     float_3nan_prop_s_bac = float_3nan_prop_bac | R_3NAN_SNAN_MASK,
259     float_3nan_prop_s_bca = float_3nan_prop_bca | R_3NAN_SNAN_MASK,
260     float_3nan_prop_s_cab = float_3nan_prop_cab | R_3NAN_SNAN_MASK,
261     float_3nan_prop_s_cba = float_3nan_prop_cba | R_3NAN_SNAN_MASK,
262 } Float3NaNPropRule;
263 
264 #undef PROPRULE
265 
266 /*
267  * Rule for result of fused multiply-add 0 * Inf + NaN.
268  * This must be a NaN, but implementations differ on whether this
269  * is the input NaN or the default NaN.
270  *
271  * You don't need to set this if default_nan_mode is enabled.
272  * When not in default-NaN mode, it is an error for the target
273  * not to set the rule in float_status if it uses muladd, and we
274  * will assert if we need to handle an input NaN and no rule was
275  * selected.
276  */
277 typedef enum __attribute__((__packed__)) {
278     /* No propagation rule specified */
279     float_infzeronan_none = 0,
280     /* Result is never the default NaN (so always the input NaN) */
281     float_infzeronan_dnan_never,
282     /* Result is always the default NaN */
283     float_infzeronan_dnan_always,
284     /* Result is the default NaN if the input NaN is quiet */
285     float_infzeronan_dnan_if_qnan,
286 } FloatInfZeroNaNRule;
287 
288 /*
289  * Floating Point Status. Individual architectures may maintain
290  * several versions of float_status for different functions. The
291  * correct status for the operation is then passed by reference to
292  * most of the softfloat functions.
293  */
294 
295 typedef struct float_status {
296     uint16_t float_exception_flags;
297     FloatRoundMode float_rounding_mode;
298     FloatX80RoundPrec floatx80_rounding_precision;
299     Float2NaNPropRule float_2nan_prop_rule;
300     Float3NaNPropRule float_3nan_prop_rule;
301     FloatInfZeroNaNRule float_infzeronan_rule;
302     bool tininess_before_rounding;
303     /* should denormalised results go to zero and set the inexact flag? */
304     bool flush_to_zero;
305     /* should denormalised inputs go to zero and set the input_denormal flag? */
306     bool flush_inputs_to_zero;
307     bool default_nan_mode;
308     /*
309      * The pattern to use for the default NaN. Here the high bit specifies
310      * the default NaN's sign bit, and bits 6..0 specify the high bits of the
311      * fractional part. The low bits of the fractional part are copies of bit 0.
312      * The exponent of the default NaN is (as for any NaN) always all 1s.
313      * Note that a value of 0 here is not a valid NaN. The target must set
314      * this to the correct non-zero value, or we will assert when trying to
315      * create a default NaN.
316      */
317     uint8_t default_nan_pattern;
318     /*
319      * The flags below are not used on all specializations and may
320      * constant fold away (see snan_bit_is_one()/no_signalling_nans() in
321      * softfloat-specialize.inc.c)
322      */
323     bool snan_bit_is_one;
324     bool no_signaling_nans;
325     /* should overflowed results subtract re_bias to its exponent? */
326     bool rebias_overflow;
327     /* should underflowed results add re_bias to its exponent? */
328     bool rebias_underflow;
329 } float_status;
330 
331 #endif /* SOFTFLOAT_TYPES_H */
332