xref: /openbmc/qemu/include/fpu/softfloat-types.h (revision c40da5c6)
1cfd88fc6SAlex Bennée /*
2cfd88fc6SAlex Bennée  * QEMU float support
3cfd88fc6SAlex Bennée  *
4cfd88fc6SAlex Bennée  * The code in this source file is derived from release 2a of the SoftFloat
5cfd88fc6SAlex Bennée  * IEC/IEEE Floating-point Arithmetic Package. Those parts of the code (and
6cfd88fc6SAlex Bennée  * some later contributions) are provided under that license, as detailed below.
7cfd88fc6SAlex Bennée  * It has subsequently been modified by contributors to the QEMU Project,
8cfd88fc6SAlex Bennée  * so some portions are provided under:
9cfd88fc6SAlex Bennée  *  the SoftFloat-2a license
10cfd88fc6SAlex Bennée  *  the BSD license
11cfd88fc6SAlex Bennée  *  GPL-v2-or-later
12cfd88fc6SAlex Bennée  *
13cfd88fc6SAlex Bennée  * This header holds definitions for code that might be dealing with
14cfd88fc6SAlex Bennée  * softfloat types but not need access to the actual library functions.
15cfd88fc6SAlex Bennée  */
16cfd88fc6SAlex Bennée /*
17cfd88fc6SAlex Bennée ===============================================================================
18cfd88fc6SAlex Bennée This C header file is part of the SoftFloat IEC/IEEE Floating-point
19cfd88fc6SAlex Bennée Arithmetic Package, Release 2a.
20cfd88fc6SAlex Bennée 
21cfd88fc6SAlex Bennée Written by John R. Hauser.  This work was made possible in part by the
22cfd88fc6SAlex Bennée International Computer Science Institute, located at Suite 600, 1947 Center
23cfd88fc6SAlex Bennée Street, Berkeley, California 94704.  Funding was partially provided by the
24cfd88fc6SAlex Bennée National Science Foundation under grant MIP-9311980.  The original version
25cfd88fc6SAlex Bennée of this code was written as part of a project to build a fixed-point vector
26cfd88fc6SAlex Bennée processor in collaboration with the University of California at Berkeley,
27cfd88fc6SAlex Bennée overseen by Profs. Nelson Morgan and John Wawrzynek.  More information
28cfd88fc6SAlex Bennée is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/
29cfd88fc6SAlex Bennée arithmetic/SoftFloat.html'.
30cfd88fc6SAlex Bennée 
31cfd88fc6SAlex Bennée THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE.  Although reasonable effort
32cfd88fc6SAlex Bennée has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
33cfd88fc6SAlex Bennée TIMES RESULT IN INCORRECT BEHAVIOR.  USE OF THIS SOFTWARE IS RESTRICTED TO
34cfd88fc6SAlex Bennée PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
35cfd88fc6SAlex Bennée AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
36cfd88fc6SAlex Bennée 
37cfd88fc6SAlex Bennée Derivative works are acceptable, even for commercial purposes, so long as
38cfd88fc6SAlex Bennée (1) they include prominent notice that the work is derivative, and (2) they
39cfd88fc6SAlex Bennée include prominent notice akin to these four paragraphs for those parts of
40cfd88fc6SAlex Bennée this code that are retained.
41cfd88fc6SAlex Bennée 
42cfd88fc6SAlex Bennée ===============================================================================
43cfd88fc6SAlex Bennée */
44cfd88fc6SAlex Bennée 
45cfd88fc6SAlex Bennée /* BSD licensing:
46cfd88fc6SAlex Bennée  * Copyright (c) 2006, Fabrice Bellard
47cfd88fc6SAlex Bennée  * All rights reserved.
48cfd88fc6SAlex Bennée  *
49cfd88fc6SAlex Bennée  * Redistribution and use in source and binary forms, with or without
50cfd88fc6SAlex Bennée  * modification, are permitted provided that the following conditions are met:
51cfd88fc6SAlex Bennée  *
52cfd88fc6SAlex Bennée  * 1. Redistributions of source code must retain the above copyright notice,
53cfd88fc6SAlex Bennée  * this list of conditions and the following disclaimer.
54cfd88fc6SAlex Bennée  *
55cfd88fc6SAlex Bennée  * 2. Redistributions in binary form must reproduce the above copyright notice,
56cfd88fc6SAlex Bennée  * this list of conditions and the following disclaimer in the documentation
57cfd88fc6SAlex Bennée  * and/or other materials provided with the distribution.
58cfd88fc6SAlex Bennée  *
59cfd88fc6SAlex Bennée  * 3. Neither the name of the copyright holder nor the names of its contributors
60cfd88fc6SAlex Bennée  * may be used to endorse or promote products derived from this software without
61cfd88fc6SAlex Bennée  * specific prior written permission.
62cfd88fc6SAlex Bennée  *
63cfd88fc6SAlex Bennée  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
64cfd88fc6SAlex Bennée  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65cfd88fc6SAlex Bennée  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66cfd88fc6SAlex Bennée  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
67cfd88fc6SAlex Bennée  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
68cfd88fc6SAlex Bennée  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
69cfd88fc6SAlex Bennée  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
70cfd88fc6SAlex Bennée  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
71cfd88fc6SAlex Bennée  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
72cfd88fc6SAlex Bennée  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
73cfd88fc6SAlex Bennée  * THE POSSIBILITY OF SUCH DAMAGE.
74cfd88fc6SAlex Bennée  */
75cfd88fc6SAlex Bennée 
76cfd88fc6SAlex Bennée /* Portions of this work are licensed under the terms of the GNU GPL,
77cfd88fc6SAlex Bennée  * version 2 or later. See the COPYING file in the top-level directory.
78cfd88fc6SAlex Bennée  */
79cfd88fc6SAlex Bennée 
80cfd88fc6SAlex Bennée #ifndef SOFTFLOAT_TYPES_H
81cfd88fc6SAlex Bennée #define SOFTFLOAT_TYPES_H
82cfd88fc6SAlex Bennée 
83cfd88fc6SAlex Bennée /*
84cfd88fc6SAlex Bennée  * Software IEC/IEEE floating-point types.
85cfd88fc6SAlex Bennée  */
86cfd88fc6SAlex Bennée 
87cfd88fc6SAlex Bennée typedef uint16_t float16;
88cfd88fc6SAlex Bennée typedef uint32_t float32;
89cfd88fc6SAlex Bennée typedef uint64_t float64;
90cfd88fc6SAlex Bennée #define float16_val(x) (x)
91cfd88fc6SAlex Bennée #define float32_val(x) (x)
92cfd88fc6SAlex Bennée #define float64_val(x) (x)
93cfd88fc6SAlex Bennée #define make_float16(x) (x)
94cfd88fc6SAlex Bennée #define make_float32(x) (x)
95cfd88fc6SAlex Bennée #define make_float64(x) (x)
96cfd88fc6SAlex Bennée #define const_float16(x) (x)
97cfd88fc6SAlex Bennée #define const_float32(x) (x)
98cfd88fc6SAlex Bennée #define const_float64(x) (x)
99cfd88fc6SAlex Bennée typedef struct {
100cfd88fc6SAlex Bennée     uint64_t low;
101cfd88fc6SAlex Bennée     uint16_t high;
102cfd88fc6SAlex Bennée } floatx80;
103cfd88fc6SAlex Bennée #define make_floatx80(exp, mant) ((floatx80) { mant, exp })
104cfd88fc6SAlex Bennée #define make_floatx80_init(exp, mant) { .low = mant, .high = exp }
105cfd88fc6SAlex Bennée typedef struct {
106e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
107cfd88fc6SAlex Bennée     uint64_t high, low;
108cfd88fc6SAlex Bennée #else
109cfd88fc6SAlex Bennée     uint64_t low, high;
110cfd88fc6SAlex Bennée #endif
111cfd88fc6SAlex Bennée } float128;
112cfd88fc6SAlex Bennée #define make_float128(high_, low_) ((float128) { .high = high_, .low = low_ })
113cfd88fc6SAlex Bennée #define make_float128_init(high_, low_) { .high = high_, .low = low_ }
114cfd88fc6SAlex Bennée 
11524f91e81SAlex Bennée /*
1168282310dSLIU Zhiwei  * Software neural-network floating-point types.
1178282310dSLIU Zhiwei  */
1188282310dSLIU Zhiwei typedef uint16_t bfloat16;
1198282310dSLIU Zhiwei 
1208282310dSLIU Zhiwei /*
12124f91e81SAlex Bennée  * Software IEC/IEEE floating-point underflow tininess-detection mode.
12224f91e81SAlex Bennée  */
12324f91e81SAlex Bennée 
124a828b373SRichard Henderson #define float_tininess_after_rounding  false
125a828b373SRichard Henderson #define float_tininess_before_rounding true
12624f91e81SAlex Bennée 
12724f91e81SAlex Bennée /*
12824f91e81SAlex Bennée  *Software IEC/IEEE floating-point rounding mode.
12924f91e81SAlex Bennée  */
13024f91e81SAlex Bennée 
1313dede407SRichard Henderson typedef enum __attribute__((__packed__)) {
13224f91e81SAlex Bennée     float_round_nearest_even = 0,
13324f91e81SAlex Bennée     float_round_down         = 1,
13424f91e81SAlex Bennée     float_round_up           = 2,
13524f91e81SAlex Bennée     float_round_to_zero      = 3,
13624f91e81SAlex Bennée     float_round_ties_away    = 4,
13760c8f726SRichard Henderson     /* Not an IEEE rounding mode: round to closest odd, overflow to max */
13824f91e81SAlex Bennée     float_round_to_odd       = 5,
13960c8f726SRichard Henderson     /* Not an IEEE rounding mode: round to closest odd, overflow to inf */
14060c8f726SRichard Henderson     float_round_to_odd_inf   = 6,
1413dede407SRichard Henderson } FloatRoundMode;
14224f91e81SAlex Bennée 
14324f91e81SAlex Bennée /*
14424f91e81SAlex Bennée  * Software IEC/IEEE floating-point exception flags.
14524f91e81SAlex Bennée  */
14624f91e81SAlex Bennée 
14724f91e81SAlex Bennée enum {
148149a48f6SRichard Henderson     float_flag_invalid         = 0x0001,
149149a48f6SRichard Henderson     float_flag_divbyzero       = 0x0002,
150149a48f6SRichard Henderson     float_flag_overflow        = 0x0004,
151149a48f6SRichard Henderson     float_flag_underflow       = 0x0008,
152149a48f6SRichard Henderson     float_flag_inexact         = 0x0010,
153149a48f6SRichard Henderson     float_flag_input_denormal  = 0x0020,
154149a48f6SRichard Henderson     float_flag_output_denormal = 0x0040,
155ba11446cSRichard Henderson     float_flag_invalid_isi     = 0x0080,  /* inf - inf */
156bead3c9bSRichard Henderson     float_flag_invalid_imz     = 0x0100,  /* inf * 0 */
15710cc9640SRichard Henderson     float_flag_invalid_idi     = 0x0200,  /* inf / inf */
15810cc9640SRichard Henderson     float_flag_invalid_zdz     = 0x0400,  /* 0 / 0 */
159f8718aabSRichard Henderson     float_flag_invalid_sqrt    = 0x0800,  /* sqrt(-x) */
16081254b02SRichard Henderson     float_flag_invalid_cvti    = 0x1000,  /* non-nan to integer */
161e706d445SRichard Henderson     float_flag_invalid_snan    = 0x2000,  /* any operand was snan */
16224f91e81SAlex Bennée };
16324f91e81SAlex Bennée 
1648da5f1dbSRichard Henderson /*
1658da5f1dbSRichard Henderson  * Rounding precision for floatx80.
1668da5f1dbSRichard Henderson  */
1678da5f1dbSRichard Henderson typedef enum __attribute__((__packed__)) {
1688da5f1dbSRichard Henderson     floatx80_precision_x,
1698da5f1dbSRichard Henderson     floatx80_precision_d,
1708da5f1dbSRichard Henderson     floatx80_precision_s,
1718da5f1dbSRichard Henderson } FloatX80RoundPrec;
17224f91e81SAlex Bennée 
17324f91e81SAlex Bennée /*
17424f91e81SAlex Bennée  * Floating Point Status. Individual architectures may maintain
17524f91e81SAlex Bennée  * several versions of float_status for different functions. The
17624f91e81SAlex Bennée  * correct status for the operation is then passed by reference to
17724f91e81SAlex Bennée  * most of the softfloat functions.
17824f91e81SAlex Bennée  */
17924f91e81SAlex Bennée 
18024f91e81SAlex Bennée typedef struct float_status {
181149a48f6SRichard Henderson     uint16_t float_exception_flags;
1823dede407SRichard Henderson     FloatRoundMode float_rounding_mode;
1838da5f1dbSRichard Henderson     FloatX80RoundPrec floatx80_rounding_precision;
184a828b373SRichard Henderson     bool tininess_before_rounding;
18524f91e81SAlex Bennée     /* should denormalised results go to zero and set the inexact flag? */
186c120391cSRichard Henderson     bool flush_to_zero;
18724f91e81SAlex Bennée     /* should denormalised inputs go to zero and set the input_denormal flag? */
188c120391cSRichard Henderson     bool flush_inputs_to_zero;
189c120391cSRichard Henderson     bool default_nan_mode;
190cc43c692SMax Filippov     /*
191cc43c692SMax Filippov      * The flags below are not used on all specializations and may
192cc43c692SMax Filippov      * constant fold away (see snan_bit_is_one()/no_signalling_nans() in
193cc43c692SMax Filippov      * softfloat-specialize.inc.c)
194cc43c692SMax Filippov      */
195c120391cSRichard Henderson     bool snan_bit_is_one;
196913602e3SMax Filippov     bool use_first_nan;
197cc43c692SMax Filippov     bool no_signaling_nans;
198*c40da5c6SLucas Mateus Castro (alqotel)     /* should overflowed results subtract re_bias to its exponent? */
199*c40da5c6SLucas Mateus Castro (alqotel)     bool rebias_overflow;
200*c40da5c6SLucas Mateus Castro (alqotel)     /* should underflowed results add re_bias to its exponent? */
201*c40da5c6SLucas Mateus Castro (alqotel)     bool rebias_underflow;
20224f91e81SAlex Bennée } float_status;
20324f91e81SAlex Bennée 
204cfd88fc6SAlex Bennée #endif /* SOFTFLOAT_TYPES_H */
205