master
h 455 lines 17.6 KB
Raw
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/core/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 * Open Compute Project (OCP) Microscaling Formats
124 */
125 typedef uint8_t float4_e2m1;
126 typedef uint8_t float8_e4m3;
127 typedef uint8_t float8_e5m2;
128
129 /*
130 * Software IEC/IEEE floating-point underflow tininess-detection mode.
131 */
132
133 #define float_tininess_after_rounding false
134 #define float_tininess_before_rounding true
135
136 /*
137 *Software IEC/IEEE floating-point rounding mode.
138 */
139
140 typedef enum __attribute__((__packed__)) {
141 float_round_nearest_even = 0,
142 float_round_down = 1,
143 float_round_up = 2,
144 float_round_to_zero = 3,
145 float_round_ties_away = 4,
146 /* Not an IEEE rounding mode: round to closest odd, overflow to max */
147 float_round_to_odd = 5,
148 /* Not an IEEE rounding mode: round to closest odd, overflow to inf */
149 float_round_to_odd_inf = 6,
150 /* Not an IEEE rounding mode: round to nearest even, overflow to max */
151 float_round_nearest_even_max = 7,
152 } FloatRoundMode;
153
154 /*
155 * Software IEC/IEEE floating-point exception flags.
156 */
157
158 enum {
159 float_flag_invalid = 0x0001,
160 float_flag_divbyzero = 0x0002,
161 float_flag_overflow = 0x0004,
162 float_flag_underflow = 0x0008,
163 float_flag_inexact = 0x0010,
164 /* We flushed an input denormal to 0 (because of flush_inputs_to_zero) */
165 float_flag_input_denormal_flushed = 0x0020,
166 /* We flushed an output denormal to 0 (because of flush_to_zero) */
167 float_flag_output_denormal_flushed = 0x0040,
168 float_flag_invalid_isi = 0x0080, /* inf - inf */
169 float_flag_invalid_imz = 0x0100, /* inf * 0 */
170 float_flag_invalid_idi = 0x0200, /* inf / inf */
171 float_flag_invalid_zdz = 0x0400, /* 0 / 0 */
172 float_flag_invalid_sqrt = 0x0800, /* sqrt(-x) */
173 float_flag_invalid_cvti = 0x1000, /* non-nan to integer */
174 float_flag_invalid_snan = 0x2000, /* any operand was snan */
175 /*
176 * An input was denormal and we used it (without flushing it to zero).
177 * Not set if we do not actually use the denormal input (e.g.
178 * because some other input was a NaN, or because the operation
179 * wasn't actually carried out (divide-by-zero; invalid))
180 */
181 float_flag_input_denormal_used = 0x4000,
182 };
183
184 typedef uint16_t FloatExceptionFlags;
185
186 /*
187 * Rounding precision for floatx80.
188 */
189 typedef enum __attribute__((__packed__)) {
190 floatx80_precision_x,
191 floatx80_precision_d,
192 floatx80_precision_s,
193 } FloatX80RoundPrec;
194
195 /*
196 * Define how the architecture discriminates signaling NaNs.
197 * This done with the most significant bit of the fraction.
198 *
199 * In IEEE 754-1985 this was implementation defined, but in IEEE 754-2008
200 * the msb must be 0. But setting the msb to 1 got baked into HPPA, SH4,
201 * and pre-2008 MIPS.
202 *
203 * Further, some architectures (or modes of architectures) do not detect
204 * signaling NaNs at all.
205 */
206 typedef enum __attribute__((__packed__)) {
207 float_snan_bit_is_zero,
208 float_snan_bit_is_one,
209 float_snan_never,
210 } FloatSNaNRule;
211
212 /*
213 * 2-input NaN propagation rule. Individual architectures have
214 * different rules for which input NaN is propagated to the output
215 * when there is more than one NaN on the input.
216 *
217 * If default_nan_mode is enabled then it is valid not to set a
218 * NaN propagation rule, because the softfloat code guarantees
219 * not to try to pick a NaN to propagate in default NaN mode.
220 * When not in default-NaN mode, it is an error for the target
221 * not to set the rule in float_status, and we will assert if
222 * we need to handle an input NaN and no rule was selected.
223 */
224 typedef enum __attribute__((__packed__)) {
225 /* No propagation rule specified */
226 float_2nan_prop_none = 0,
227 /* Prefer SNaN over QNaN, then operand A over B */
228 float_2nan_prop_s_ab,
229 /* Prefer SNaN over QNaN, then operand B over A */
230 float_2nan_prop_s_ba,
231 /* Prefer A over B regardless of SNaN vs QNaN */
232 float_2nan_prop_ab,
233 /* Prefer B over A regardless of SNaN vs QNaN */
234 float_2nan_prop_ba,
235 /*
236 * This implements x87 NaN propagation rules:
237 * SNaN + QNaN => return the QNaN
238 * two SNaNs => return the one with the larger significand, silenced
239 * two QNaNs => return the one with the larger significand
240 * SNaN and a non-NaN => return the SNaN, silenced
241 * QNaN and a non-NaN => return the QNaN
242 *
243 * If we get down to comparing significands and they are the same,
244 * return the NaN with the positive sign bit (if any).
245 */
246 float_2nan_prop_x87,
247 } Float2NaNPropRule;
248
249 /*
250 * 3-input NaN propagation rule, for fused multiply-add. Individual
251 * architectures have different rules for which input NaN is
252 * propagated to the output when there is more than one NaN on the
253 * input.
254 *
255 * If default_nan_mode is enabled then it is valid not to set a NaN
256 * propagation rule, because the softfloat code guarantees not to try
257 * to pick a NaN to propagate in default NaN mode. When not in
258 * default-NaN mode, it is an error for the target not to set the rule
259 * in float_status if it uses a muladd, and we will assert if we need
260 * to handle an input NaN and no rule was selected.
261 *
262 * The naming scheme for Float3NaNPropRule values is:
263 * float_3nan_prop_s_abc:
264 * = "Prefer SNaN over QNaN, then operand A over B over C"
265 * float_3nan_prop_abc:
266 * = "Prefer A over B over C regardless of SNaN vs QNAN"
267 *
268 * For QEMU, the multiply-add operation is A * B + C.
269 */
270
271 /*
272 * We set the Float3NaNPropRule enum values up so we can select the
273 * right value in pickNaNMulAdd in a data driven way.
274 */
275 FIELD(3NAN, 1ST, 0, 2) /* which operand is most preferred ? */
276 FIELD(3NAN, 2ND, 2, 2) /* which operand is next most preferred ? */
277 FIELD(3NAN, 3RD, 4, 2) /* which operand is least preferred ? */
278 FIELD(3NAN, SNAN, 6, 1) /* do we prefer SNaN over QNaN ? */
279
280 #define PROPRULE(X, Y, Z) \
281 ((X << R_3NAN_1ST_SHIFT) | (Y << R_3NAN_2ND_SHIFT) | (Z << R_3NAN_3RD_SHIFT))
282
283 typedef enum __attribute__((__packed__)) {
284 float_3nan_prop_none = 0, /* No propagation rule specified */
285 float_3nan_prop_abc = PROPRULE(0, 1, 2),
286 float_3nan_prop_acb = PROPRULE(0, 2, 1),
287 float_3nan_prop_bac = PROPRULE(1, 0, 2),
288 float_3nan_prop_bca = PROPRULE(1, 2, 0),
289 float_3nan_prop_cab = PROPRULE(2, 0, 1),
290 float_3nan_prop_cba = PROPRULE(2, 1, 0),
291 float_3nan_prop_s_abc = float_3nan_prop_abc | R_3NAN_SNAN_MASK,
292 float_3nan_prop_s_acb = float_3nan_prop_acb | R_3NAN_SNAN_MASK,
293 float_3nan_prop_s_bac = float_3nan_prop_bac | R_3NAN_SNAN_MASK,
294 float_3nan_prop_s_bca = float_3nan_prop_bca | R_3NAN_SNAN_MASK,
295 float_3nan_prop_s_cab = float_3nan_prop_cab | R_3NAN_SNAN_MASK,
296 float_3nan_prop_s_cba = float_3nan_prop_cba | R_3NAN_SNAN_MASK,
297 } Float3NaNPropRule;
298
299 #undef PROPRULE
300
301 /*
302 * Rule for result of fused multiply-add 0 * Inf + NaN.
303 * This must be a NaN, but implementations differ on whether this
304 * is the input NaN or the default NaN.
305 *
306 * You don't need to set this if default_nan_mode is enabled.
307 * When not in default-NaN mode, it is an error for the target
308 * not to set the rule in float_status if it uses muladd, and we
309 * will assert if we need to handle an input NaN and no rule was
310 * selected.
311 */
312 typedef enum __attribute__((__packed__)) {
313 /* No propagation rule specified */
314 float_infzeronan_none = 0,
315 /* Result is never the default NaN (so always the input NaN) */
316 float_infzeronan_dnan_never = 1,
317 /* Result is always the default NaN */
318 float_infzeronan_dnan_always = 2,
319 /* Result is the default NaN if the input NaN is quiet */
320 float_infzeronan_dnan_if_qnan = 3,
321 /*
322 * Don't raise Invalid for 0 * Inf + NaN. Default is to raise.
323 * IEEE 754-2008 section 7.2 makes it implementation defined whether
324 * 0 * Inf + QNaN raises Invalid or not. Note that 0 * Inf + SNaN will
325 * raise the Invalid flag for the SNaN anyway.
326 *
327 * This is a flag which can be ORed in with any of the above
328 * DNaN behaviour options.
329 */
330 float_infzeronan_suppress_invalid = (1 << 2),
331 } FloatInfZeroNaNRule;
332
333 /*
334 * When flush_to_zero is set, should we detect denormal results to
335 * be flushed before or after rounding? For most architectures this
336 * should be set to match the tininess_before_rounding setting,
337 * but a few architectures, e.g. MIPS MSA, detect FTZ before
338 * rounding but tininess after rounding.
339 *
340 * This enum is arranged so that the default if the target doesn't
341 * configure it matches the default for tininess_before_rounding
342 * (i.e. "after rounding").
343 */
344 #define float_ftz_after_rounding false
345 #define float_ftz_before_rounding true
346
347 /*
348 * floatx80 is primarily used by x86 and m68k, and there are
349 * differences in the handling, largely related to the explicit
350 * Integer bit which floatx80 has and the other float formats do not.
351 * These flag values allow specification of the target's requirements
352 * and can be ORed together to set floatx80_behaviour.
353 */
354 typedef enum __attribute__((__packed__)) {
355 /* In the default Infinity value, is the Integer bit 0 ? */
356 floatx80_default_inf_int_bit_is_zero = 1,
357 /*
358 * Are Pseudo-infinities (Inf with the Integer bit zero) valid?
359 * If so, floatx80_is_infinity() will return true for them.
360 * If not, floatx80_invalid_encoding will return false for them,
361 * and using them as inputs to a float op will raise Invalid.
362 */
363 floatx80_pseudo_inf_valid = 2,
364 /*
365 * Are Pseudo-NaNs (NaNs where the Integer bit is zero) valid?
366 * If not, floatx80_invalid_encoding() will return false for them,
367 * and using them as inputs to a float op will raise Invalid.
368 */
369 floatx80_pseudo_nan_valid = 4,
370 /*
371 * Are Unnormals (0 < exp < 0x7fff, Integer bit zero) valid?
372 * If not, floatx80_invalid_encoding() will return false for them,
373 * and using them as inputs to a float op will raise Invalid.
374 */
375 floatx80_unnormal_valid = 8,
376
377 /*
378 * If the exponent is 0 and the Integer bit is set, Intel call
379 * this a "pseudo-denormal"; x86 supports that only on input
380 * (treating them as denormals by ignoring the Integer bit).
381 * For m68k, the integer bit is considered validly part of the
382 * input value when the exponent is 0, and may be 0 or 1,
383 * giving extra range. They may also be generated as outputs.
384 * (The m68k manual actually calls these values part of the
385 * normalized number range, not the denormalized number range.)
386 *
387 * By default you get the Intel behaviour where the Integer
388 * bit is ignored; if this is set then the Integer bit value
389 * is honoured, m68k-style.
390 *
391 * Either way, floatx80_invalid_encoding() will always accept
392 * pseudo-denormals.
393 */
394 floatx80_pseudo_denormal_valid = 16,
395 } FloatX80Behaviour;
396
397 /*
398 * Floating Point Status. Individual architectures may maintain
399 * several versions of float_status for different functions. The
400 * correct status for the operation is then passed by reference to
401 * most of the softfloat functions.
402 */
403
404 typedef struct float_status {
405 FloatExceptionFlags float_exception_flags : 16;
406
407 /*
408 * Floating point status controls.
409 * Items that, in general, may be updated by writes to an architectural
410 * floating point control register.
411 */
412 FloatRoundMode float_rounding_mode : 3;
413 FloatX80RoundPrec floatx80_rounding_precision : 2;
414 /* should denormalised results go to zero and set output_denormal_flushed? */
415 bool flush_to_zero : 1;
416 /* should denormalised inputs go to zero and set input_denormal_flushed? */
417 bool flush_inputs_to_zero : 1;
418 /* should default nans be produced instead of propagating an input nan? */
419 bool default_nan_mode : 1;
420 /* should overflowed results subtract re_bias to its exponent? */
421 bool rebias_overflow : 1;
422 /* should underflowed results add re_bias to its exponent? */
423 bool rebias_underflow : 1;
424
425 /*
426 * Floating point behaviour controls.
427 * Items that, in general, will be set at cpu realization because
428 * the behaviour is baked into the specific hardware implementation.
429 */
430 bool tininess_before_rounding : 1;
431 /* do we detect and flush denormal results before or after rounding? */
432 bool ftz_before_rounding : 1;
433 FloatSNaNRule float_snan_rule : 2;
434 /*
435 * Overriding float_snan_rule, is the single NaN representation for
436 * the OCP E4M3 format an SNaN or QNaN?
437 */
438 bool e4m3_nan_is_snan : 1;
439 Float2NaNPropRule float_2nan_prop_rule : 3;
440 Float3NaNPropRule float_3nan_prop_rule : 7;
441 FloatInfZeroNaNRule float_infzeronan_rule: 3;
442 FloatX80Behaviour floatx80_behaviour : 5;
443 /*
444 * The pattern to use for the default NaN. Here the high bit specifies
445 * the default NaN's sign bit, and bits 6..0 specify the high bits of the
446 * fractional part. The low bits of the fractional part are copies of bit 0.
447 * The exponent of the default NaN is (as for any NaN) always all 1s.
448 * Note that a value of 0 here is not a valid NaN. The target must set
449 * this to the correct non-zero value, or we will assert when trying to
450 * create a default NaN.
451 */
452 unsigned default_nan_pattern : 8;
453 } float_status;
454
455 #endif /* SOFTFLOAT_TYPES_H */