softfloat-types.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. Written by John R. Hauser. This work was made possible in part by the
  21. International Computer Science Institute, located at Suite 600, 1947 Center
  22. Street, Berkeley, California 94704. Funding was partially provided by the
  23. National Science Foundation under grant MIP-9311980. The original version
  24. of this code was written as part of a project to build a fixed-point vector
  25. processor in collaboration with the University of California at Berkeley,
  26. overseen by Profs. Nelson Morgan and John Wawrzynek. More information
  27. is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/
  28. arithmetic/SoftFloat.html'.
  29. THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort
  30. has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
  31. TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO
  32. PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
  33. AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
  34. Derivative works are acceptable, even for commercial purposes, so long as
  35. (1) they include prominent notice that the work is derivative, and (2) they
  36. include prominent notice akin to these four paragraphs for those parts of
  37. this code that are retained.
  38. ===============================================================================
  39. */
  40. /* BSD licensing:
  41. * Copyright (c) 2006, Fabrice Bellard
  42. * All rights reserved.
  43. *
  44. * Redistribution and use in source and binary forms, with or without
  45. * modification, are permitted provided that the following conditions are met:
  46. *
  47. * 1. Redistributions of source code must retain the above copyright notice,
  48. * this list of conditions and the following disclaimer.
  49. *
  50. * 2. Redistributions in binary form must reproduce the above copyright notice,
  51. * this list of conditions and the following disclaimer in the documentation
  52. * and/or other materials provided with the distribution.
  53. *
  54. * 3. Neither the name of the copyright holder nor the names of its contributors
  55. * may be used to endorse or promote products derived from this software without
  56. * specific prior written permission.
  57. *
  58. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  59. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  60. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  61. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  62. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  63. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  64. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  65. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  66. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  67. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  68. * THE POSSIBILITY OF SUCH DAMAGE.
  69. */
  70. /* Portions of this work are licensed under the terms of the GNU GPL,
  71. * version 2 or later. See the COPYING file in the top-level directory.
  72. */
  73. #ifndef SOFTFLOAT_TYPES_H
  74. #define SOFTFLOAT_TYPES_H
  75. #include "hw/registerfields.h"
  76. /*
  77. * Software IEC/IEEE floating-point types.
  78. */
  79. typedef uint16_t float16;
  80. typedef uint32_t float32;
  81. typedef uint64_t float64;
  82. #define float16_val(x) (x)
  83. #define float32_val(x) (x)
  84. #define float64_val(x) (x)
  85. #define make_float16(x) (x)
  86. #define make_float32(x) (x)
  87. #define make_float64(x) (x)
  88. #define const_float16(x) (x)
  89. #define const_float32(x) (x)
  90. #define const_float64(x) (x)
  91. typedef struct {
  92. uint64_t low;
  93. uint16_t high;
  94. } floatx80;
  95. #define make_floatx80(exp, mant) ((floatx80) { mant, exp })
  96. #define make_floatx80_init(exp, mant) { .low = mant, .high = exp }
  97. typedef struct {
  98. #if HOST_BIG_ENDIAN
  99. uint64_t high, low;
  100. #else
  101. uint64_t low, high;
  102. #endif
  103. } float128;
  104. #define make_float128(high_, low_) ((float128) { .high = high_, .low = low_ })
  105. #define make_float128_init(high_, low_) { .high = high_, .low = low_ }
  106. /*
  107. * Software neural-network floating-point types.
  108. */
  109. typedef uint16_t bfloat16;
  110. /*
  111. * Software IEC/IEEE floating-point underflow tininess-detection mode.
  112. */
  113. #define float_tininess_after_rounding false
  114. #define float_tininess_before_rounding true
  115. /*
  116. *Software IEC/IEEE floating-point rounding mode.
  117. */
  118. typedef enum __attribute__((__packed__)) {
  119. float_round_nearest_even = 0,
  120. float_round_down = 1,
  121. float_round_up = 2,
  122. float_round_to_zero = 3,
  123. float_round_ties_away = 4,
  124. /* Not an IEEE rounding mode: round to closest odd, overflow to max */
  125. float_round_to_odd = 5,
  126. /* Not an IEEE rounding mode: round to closest odd, overflow to inf */
  127. float_round_to_odd_inf = 6,
  128. /* Not an IEEE rounding mode: round to nearest even, overflow to max */
  129. float_round_nearest_even_max = 7,
  130. } FloatRoundMode;
  131. /*
  132. * Software IEC/IEEE floating-point exception flags.
  133. */
  134. enum {
  135. float_flag_invalid = 0x0001,
  136. float_flag_divbyzero = 0x0002,
  137. float_flag_overflow = 0x0004,
  138. float_flag_underflow = 0x0008,
  139. float_flag_inexact = 0x0010,
  140. /* We flushed an input denormal to 0 (because of flush_inputs_to_zero) */
  141. float_flag_input_denormal_flushed = 0x0020,
  142. /* We flushed an output denormal to 0 (because of flush_to_zero) */
  143. float_flag_output_denormal_flushed = 0x0040,
  144. float_flag_invalid_isi = 0x0080, /* inf - inf */
  145. float_flag_invalid_imz = 0x0100, /* inf * 0 */
  146. float_flag_invalid_idi = 0x0200, /* inf / inf */
  147. float_flag_invalid_zdz = 0x0400, /* 0 / 0 */
  148. float_flag_invalid_sqrt = 0x0800, /* sqrt(-x) */
  149. float_flag_invalid_cvti = 0x1000, /* non-nan to integer */
  150. float_flag_invalid_snan = 0x2000, /* any operand was snan */
  151. /*
  152. * An input was denormal and we used it (without flushing it to zero).
  153. * Not set if we do not actually use the denormal input (e.g.
  154. * because some other input was a NaN, or because the operation
  155. * wasn't actually carried out (divide-by-zero; invalid))
  156. */
  157. float_flag_input_denormal_used = 0x4000,
  158. };
  159. /*
  160. * Rounding precision for floatx80.
  161. */
  162. typedef enum __attribute__((__packed__)) {
  163. floatx80_precision_x,
  164. floatx80_precision_d,
  165. floatx80_precision_s,
  166. } FloatX80RoundPrec;
  167. /*
  168. * 2-input NaN propagation rule. Individual architectures have
  169. * different rules for which input NaN is propagated to the output
  170. * when there is more than one NaN on the input.
  171. *
  172. * If default_nan_mode is enabled then it is valid not to set a
  173. * NaN propagation rule, because the softfloat code guarantees
  174. * not to try to pick a NaN to propagate in default NaN mode.
  175. * When not in default-NaN mode, it is an error for the target
  176. * not to set the rule in float_status, and we will assert if
  177. * we need to handle an input NaN and no rule was selected.
  178. */
  179. typedef enum __attribute__((__packed__)) {
  180. /* No propagation rule specified */
  181. float_2nan_prop_none = 0,
  182. /* Prefer SNaN over QNaN, then operand A over B */
  183. float_2nan_prop_s_ab,
  184. /* Prefer SNaN over QNaN, then operand B over A */
  185. float_2nan_prop_s_ba,
  186. /* Prefer A over B regardless of SNaN vs QNaN */
  187. float_2nan_prop_ab,
  188. /* Prefer B over A regardless of SNaN vs QNaN */
  189. float_2nan_prop_ba,
  190. /*
  191. * This implements x87 NaN propagation rules:
  192. * SNaN + QNaN => return the QNaN
  193. * two SNaNs => return the one with the larger significand, silenced
  194. * two QNaNs => return the one with the larger significand
  195. * SNaN and a non-NaN => return the SNaN, silenced
  196. * QNaN and a non-NaN => return the QNaN
  197. *
  198. * If we get down to comparing significands and they are the same,
  199. * return the NaN with the positive sign bit (if any).
  200. */
  201. float_2nan_prop_x87,
  202. } Float2NaNPropRule;
  203. /*
  204. * 3-input NaN propagation rule, for fused multiply-add. Individual
  205. * architectures have different rules for which input NaN is
  206. * propagated to the output when there is more than one NaN on the
  207. * input.
  208. *
  209. * If default_nan_mode is enabled then it is valid not to set a NaN
  210. * propagation rule, because the softfloat code guarantees not to try
  211. * to pick a NaN to propagate in default NaN mode. When not in
  212. * default-NaN mode, it is an error for the target not to set the rule
  213. * in float_status if it uses a muladd, and we will assert if we need
  214. * to handle an input NaN and no rule was selected.
  215. *
  216. * The naming scheme for Float3NaNPropRule values is:
  217. * float_3nan_prop_s_abc:
  218. * = "Prefer SNaN over QNaN, then operand A over B over C"
  219. * float_3nan_prop_abc:
  220. * = "Prefer A over B over C regardless of SNaN vs QNAN"
  221. *
  222. * For QEMU, the multiply-add operation is A * B + C.
  223. */
  224. /*
  225. * We set the Float3NaNPropRule enum values up so we can select the
  226. * right value in pickNaNMulAdd in a data driven way.
  227. */
  228. FIELD(3NAN, 1ST, 0, 2) /* which operand is most preferred ? */
  229. FIELD(3NAN, 2ND, 2, 2) /* which operand is next most preferred ? */
  230. FIELD(3NAN, 3RD, 4, 2) /* which operand is least preferred ? */
  231. FIELD(3NAN, SNAN, 6, 1) /* do we prefer SNaN over QNaN ? */
  232. #define PROPRULE(X, Y, Z) \
  233. ((X << R_3NAN_1ST_SHIFT) | (Y << R_3NAN_2ND_SHIFT) | (Z << R_3NAN_3RD_SHIFT))
  234. typedef enum __attribute__((__packed__)) {
  235. float_3nan_prop_none = 0, /* No propagation rule specified */
  236. float_3nan_prop_abc = PROPRULE(0, 1, 2),
  237. float_3nan_prop_acb = PROPRULE(0, 2, 1),
  238. float_3nan_prop_bac = PROPRULE(1, 0, 2),
  239. float_3nan_prop_bca = PROPRULE(1, 2, 0),
  240. float_3nan_prop_cab = PROPRULE(2, 0, 1),
  241. float_3nan_prop_cba = PROPRULE(2, 1, 0),
  242. float_3nan_prop_s_abc = float_3nan_prop_abc | R_3NAN_SNAN_MASK,
  243. float_3nan_prop_s_acb = float_3nan_prop_acb | R_3NAN_SNAN_MASK,
  244. float_3nan_prop_s_bac = float_3nan_prop_bac | R_3NAN_SNAN_MASK,
  245. float_3nan_prop_s_bca = float_3nan_prop_bca | R_3NAN_SNAN_MASK,
  246. float_3nan_prop_s_cab = float_3nan_prop_cab | R_3NAN_SNAN_MASK,
  247. float_3nan_prop_s_cba = float_3nan_prop_cba | R_3NAN_SNAN_MASK,
  248. } Float3NaNPropRule;
  249. #undef PROPRULE
  250. /*
  251. * Rule for result of fused multiply-add 0 * Inf + NaN.
  252. * This must be a NaN, but implementations differ on whether this
  253. * is the input NaN or the default NaN.
  254. *
  255. * You don't need to set this if default_nan_mode is enabled.
  256. * When not in default-NaN mode, it is an error for the target
  257. * not to set the rule in float_status if it uses muladd, and we
  258. * will assert if we need to handle an input NaN and no rule was
  259. * selected.
  260. */
  261. typedef enum __attribute__((__packed__)) {
  262. /* No propagation rule specified */
  263. float_infzeronan_none = 0,
  264. /* Result is never the default NaN (so always the input NaN) */
  265. float_infzeronan_dnan_never = 1,
  266. /* Result is always the default NaN */
  267. float_infzeronan_dnan_always = 2,
  268. /* Result is the default NaN if the input NaN is quiet */
  269. float_infzeronan_dnan_if_qnan = 3,
  270. /*
  271. * Don't raise Invalid for 0 * Inf + NaN. Default is to raise.
  272. * IEEE 754-2008 section 7.2 makes it implementation defined whether
  273. * 0 * Inf + QNaN raises Invalid or not. Note that 0 * Inf + SNaN will
  274. * raise the Invalid flag for the SNaN anyway.
  275. *
  276. * This is a flag which can be ORed in with any of the above
  277. * DNaN behaviour options.
  278. */
  279. float_infzeronan_suppress_invalid = (1 << 7),
  280. } FloatInfZeroNaNRule;
  281. /*
  282. * When flush_to_zero is set, should we detect denormal results to
  283. * be flushed before or after rounding? For most architectures this
  284. * should be set to match the tininess_before_rounding setting,
  285. * but a few architectures, e.g. MIPS MSA, detect FTZ before
  286. * rounding but tininess after rounding.
  287. *
  288. * This enum is arranged so that the default if the target doesn't
  289. * configure it matches the default for tininess_before_rounding
  290. * (i.e. "after rounding").
  291. */
  292. typedef enum __attribute__((__packed__)) {
  293. float_ftz_after_rounding = 0,
  294. float_ftz_before_rounding = 1,
  295. } FloatFTZDetection;
  296. /*
  297. * floatx80 is primarily used by x86 and m68k, and there are
  298. * differences in the handling, largely related to the explicit
  299. * Integer bit which floatx80 has and the other float formats do not.
  300. * These flag values allow specification of the target's requirements
  301. * and can be ORed together to set floatx80_behaviour.
  302. */
  303. typedef enum __attribute__((__packed__)) {
  304. /* In the default Infinity value, is the Integer bit 0 ? */
  305. floatx80_default_inf_int_bit_is_zero = 1,
  306. /*
  307. * Are Pseudo-infinities (Inf with the Integer bit zero) valid?
  308. * If so, floatx80_is_infinity() will return true for them.
  309. * If not, floatx80_invalid_encoding will return false for them,
  310. * and using them as inputs to a float op will raise Invalid.
  311. */
  312. floatx80_pseudo_inf_valid = 2,
  313. /*
  314. * Are Pseudo-NaNs (NaNs where the Integer bit is zero) valid?
  315. * If not, floatx80_invalid_encoding() will return false for them,
  316. * and using them as inputs to a float op will raise Invalid.
  317. */
  318. floatx80_pseudo_nan_valid = 4,
  319. /*
  320. * Are Unnormals (0 < exp < 0x7fff, Integer bit zero) valid?
  321. * If not, floatx80_invalid_encoding() will return false for them,
  322. * and using them as inputs to a float op will raise Invalid.
  323. */
  324. floatx80_unnormal_valid = 8,
  325. /*
  326. * If the exponent is 0 and the Integer bit is set, Intel call
  327. * this a "pseudo-denormal"; x86 supports that only on input
  328. * (treating them as denormals by ignoring the Integer bit).
  329. * For m68k, the integer bit is considered validly part of the
  330. * input value when the exponent is 0, and may be 0 or 1,
  331. * giving extra range. They may also be generated as outputs.
  332. * (The m68k manual actually calls these values part of the
  333. * normalized number range, not the denormalized number range.)
  334. *
  335. * By default you get the Intel behaviour where the Integer
  336. * bit is ignored; if this is set then the Integer bit value
  337. * is honoured, m68k-style.
  338. *
  339. * Either way, floatx80_invalid_encoding() will always accept
  340. * pseudo-denormals.
  341. */
  342. floatx80_pseudo_denormal_valid = 16,
  343. } FloatX80Behaviour;
  344. /*
  345. * Floating Point Status. Individual architectures may maintain
  346. * several versions of float_status for different functions. The
  347. * correct status for the operation is then passed by reference to
  348. * most of the softfloat functions.
  349. */
  350. typedef struct float_status {
  351. uint16_t float_exception_flags;
  352. FloatRoundMode float_rounding_mode;
  353. FloatX80RoundPrec floatx80_rounding_precision;
  354. FloatX80Behaviour floatx80_behaviour;
  355. Float2NaNPropRule float_2nan_prop_rule;
  356. Float3NaNPropRule float_3nan_prop_rule;
  357. FloatInfZeroNaNRule float_infzeronan_rule;
  358. bool tininess_before_rounding;
  359. /* should denormalised results go to zero and set output_denormal_flushed? */
  360. bool flush_to_zero;
  361. /* do we detect and flush denormal results before or after rounding? */
  362. FloatFTZDetection ftz_detection;
  363. /* should denormalised inputs go to zero and set input_denormal_flushed? */
  364. bool flush_inputs_to_zero;
  365. bool default_nan_mode;
  366. /*
  367. * The pattern to use for the default NaN. Here the high bit specifies
  368. * the default NaN's sign bit, and bits 6..0 specify the high bits of the
  369. * fractional part. The low bits of the fractional part are copies of bit 0.
  370. * The exponent of the default NaN is (as for any NaN) always all 1s.
  371. * Note that a value of 0 here is not a valid NaN. The target must set
  372. * this to the correct non-zero value, or we will assert when trying to
  373. * create a default NaN.
  374. */
  375. uint8_t default_nan_pattern;
  376. /*
  377. * The flags below are not used on all specializations and may
  378. * constant fold away (see snan_bit_is_one()/no_signalling_nans() in
  379. * softfloat-specialize.inc.c)
  380. */
  381. bool snan_bit_is_one;
  382. bool no_signaling_nans;
  383. /* should overflowed results subtract re_bias to its exponent? */
  384. bool rebias_overflow;
  385. /* should underflowed results add re_bias to its exponent? */
  386. bool rebias_underflow;
  387. } float_status;
  388. #endif /* SOFTFLOAT_TYPES_H */