softfloat-specialize.c.inc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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. * Any future contributions to this file after December 1st 2014 will be
  14. * taken to be licensed under the Softfloat-2a license unless specifically
  15. * indicated otherwise.
  16. */
  17. /*
  18. ===============================================================================
  19. This C source fragment is part of the SoftFloat IEC/IEEE Floating-point
  20. Arithmetic Package, Release 2a.
  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. THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort
  31. has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
  32. TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO
  33. PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
  34. AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
  35. Derivative works are acceptable, even for commercial purposes, so long as
  36. (1) they include prominent notice that the work is derivative, and (2) they
  37. include prominent notice akin to these four paragraphs for those parts of
  38. this code that are retained.
  39. ===============================================================================
  40. */
  41. /* BSD licensing:
  42. * Copyright (c) 2006, Fabrice Bellard
  43. * All rights reserved.
  44. *
  45. * Redistribution and use in source and binary forms, with or without
  46. * modification, are permitted provided that the following conditions are met:
  47. *
  48. * 1. Redistributions of source code must retain the above copyright notice,
  49. * this list of conditions and the following disclaimer.
  50. *
  51. * 2. Redistributions in binary form must reproduce the above copyright notice,
  52. * this list of conditions and the following disclaimer in the documentation
  53. * and/or other materials provided with the distribution.
  54. *
  55. * 3. Neither the name of the copyright holder nor the names of its contributors
  56. * may be used to endorse or promote products derived from this software without
  57. * specific prior written permission.
  58. *
  59. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  60. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  61. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  62. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  63. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  64. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  65. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  66. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  67. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  68. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  69. * THE POSSIBILITY OF SUCH DAMAGE.
  70. */
  71. /* Portions of this work are licensed under the terms of the GNU GPL,
  72. * version 2 or later. See the COPYING file in the top-level directory.
  73. */
  74. /*
  75. * Define whether architecture deviates from IEEE in not supporting
  76. * signaling NaNs (so all NaNs are treated as quiet).
  77. */
  78. static inline bool no_signaling_nans(float_status *status)
  79. {
  80. #if defined(TARGET_XTENSA)
  81. return status->no_signaling_nans;
  82. #else
  83. return false;
  84. #endif
  85. }
  86. /* Define how the architecture discriminates signaling NaNs.
  87. * This done with the most significant bit of the fraction.
  88. * In IEEE 754-1985 this was implementation defined, but in IEEE 754-2008
  89. * the msb must be zero. MIPS is (so far) unique in supporting both the
  90. * 2008 revision and backward compatibility with their original choice.
  91. * Thus for MIPS we must make the choice at runtime.
  92. */
  93. static inline bool snan_bit_is_one(float_status *status)
  94. {
  95. #if defined(TARGET_MIPS)
  96. return status->snan_bit_is_one;
  97. #elif defined(TARGET_HPPA) || defined(TARGET_SH4)
  98. return 1;
  99. #else
  100. return 0;
  101. #endif
  102. }
  103. /*----------------------------------------------------------------------------
  104. | For the deconstructed floating-point with fraction FRAC, return true
  105. | if the fraction represents a signalling NaN; otherwise false.
  106. *----------------------------------------------------------------------------*/
  107. static bool parts_is_snan_frac(uint64_t frac, float_status *status)
  108. {
  109. if (no_signaling_nans(status)) {
  110. return false;
  111. } else {
  112. bool msb = extract64(frac, DECOMPOSED_BINARY_POINT - 1, 1);
  113. return msb == snan_bit_is_one(status);
  114. }
  115. }
  116. /*----------------------------------------------------------------------------
  117. | The pattern for a default generated deconstructed floating-point NaN.
  118. *----------------------------------------------------------------------------*/
  119. static void parts64_default_nan(FloatParts64 *p, float_status *status)
  120. {
  121. bool sign = 0;
  122. uint64_t frac;
  123. #if defined(TARGET_SPARC) || defined(TARGET_M68K)
  124. /* !snan_bit_is_one, set all bits */
  125. frac = (1ULL << DECOMPOSED_BINARY_POINT) - 1;
  126. #elif defined(TARGET_I386) || defined(TARGET_X86_64) \
  127. || defined(TARGET_MICROBLAZE)
  128. /* !snan_bit_is_one, set sign and msb */
  129. frac = 1ULL << (DECOMPOSED_BINARY_POINT - 1);
  130. sign = 1;
  131. #elif defined(TARGET_HPPA)
  132. /* snan_bit_is_one, set msb-1. */
  133. frac = 1ULL << (DECOMPOSED_BINARY_POINT - 2);
  134. #elif defined(TARGET_HEXAGON)
  135. sign = 1;
  136. frac = ~0ULL;
  137. #else
  138. /*
  139. * This case is true for Alpha, ARM, MIPS, OpenRISC, PPC, RISC-V,
  140. * S390, SH4, TriCore, and Xtensa. Our other supported targets,
  141. * CRIS, Nios2, and Tile, do not have floating-point.
  142. */
  143. if (snan_bit_is_one(status)) {
  144. /* set all bits other than msb */
  145. frac = (1ULL << (DECOMPOSED_BINARY_POINT - 1)) - 1;
  146. } else {
  147. /* set msb */
  148. frac = 1ULL << (DECOMPOSED_BINARY_POINT - 1);
  149. }
  150. #endif
  151. *p = (FloatParts64) {
  152. .cls = float_class_qnan,
  153. .sign = sign,
  154. .exp = INT_MAX,
  155. .frac = frac
  156. };
  157. }
  158. static void parts128_default_nan(FloatParts128 *p, float_status *status)
  159. {
  160. /*
  161. * Extrapolate from the choices made by parts64_default_nan to fill
  162. * in the quad-floating format. If the low bit is set, assume we
  163. * want to set all non-snan bits.
  164. */
  165. FloatParts64 p64;
  166. parts64_default_nan(&p64, status);
  167. *p = (FloatParts128) {
  168. .cls = float_class_qnan,
  169. .sign = p64.sign,
  170. .exp = INT_MAX,
  171. .frac_hi = p64.frac,
  172. .frac_lo = -(p64.frac & 1)
  173. };
  174. }
  175. /*----------------------------------------------------------------------------
  176. | Returns a quiet NaN from a signalling NaN for the deconstructed
  177. | floating-point parts.
  178. *----------------------------------------------------------------------------*/
  179. static uint64_t parts_silence_nan_frac(uint64_t frac, float_status *status)
  180. {
  181. g_assert(!no_signaling_nans(status));
  182. /* The only snan_bit_is_one target without default_nan_mode is HPPA. */
  183. if (snan_bit_is_one(status)) {
  184. frac &= ~(1ULL << (DECOMPOSED_BINARY_POINT - 1));
  185. frac |= 1ULL << (DECOMPOSED_BINARY_POINT - 2);
  186. } else {
  187. frac |= 1ULL << (DECOMPOSED_BINARY_POINT - 1);
  188. }
  189. return frac;
  190. }
  191. static void parts64_silence_nan(FloatParts64 *p, float_status *status)
  192. {
  193. p->frac = parts_silence_nan_frac(p->frac, status);
  194. p->cls = float_class_qnan;
  195. }
  196. static void parts128_silence_nan(FloatParts128 *p, float_status *status)
  197. {
  198. p->frac_hi = parts_silence_nan_frac(p->frac_hi, status);
  199. p->cls = float_class_qnan;
  200. }
  201. /*----------------------------------------------------------------------------
  202. | The pattern for a default generated extended double-precision NaN.
  203. *----------------------------------------------------------------------------*/
  204. floatx80 floatx80_default_nan(float_status *status)
  205. {
  206. floatx80 r;
  207. /* None of the targets that have snan_bit_is_one use floatx80. */
  208. assert(!snan_bit_is_one(status));
  209. #if defined(TARGET_M68K)
  210. r.low = UINT64_C(0xFFFFFFFFFFFFFFFF);
  211. r.high = 0x7FFF;
  212. #else
  213. /* X86 */
  214. r.low = UINT64_C(0xC000000000000000);
  215. r.high = 0xFFFF;
  216. #endif
  217. return r;
  218. }
  219. /*----------------------------------------------------------------------------
  220. | The pattern for a default generated extended double-precision inf.
  221. *----------------------------------------------------------------------------*/
  222. #define floatx80_infinity_high 0x7FFF
  223. #if defined(TARGET_M68K)
  224. #define floatx80_infinity_low UINT64_C(0x0000000000000000)
  225. #else
  226. #define floatx80_infinity_low UINT64_C(0x8000000000000000)
  227. #endif
  228. const floatx80 floatx80_infinity
  229. = make_floatx80_init(floatx80_infinity_high, floatx80_infinity_low);
  230. /*----------------------------------------------------------------------------
  231. | Returns 1 if the half-precision floating-point value `a' is a quiet
  232. | NaN; otherwise returns 0.
  233. *----------------------------------------------------------------------------*/
  234. bool float16_is_quiet_nan(float16 a_, float_status *status)
  235. {
  236. if (no_signaling_nans(status)) {
  237. return float16_is_any_nan(a_);
  238. } else {
  239. uint16_t a = float16_val(a_);
  240. if (snan_bit_is_one(status)) {
  241. return (((a >> 9) & 0x3F) == 0x3E) && (a & 0x1FF);
  242. } else {
  243. return ((a >> 9) & 0x3F) == 0x3F;
  244. }
  245. }
  246. }
  247. /*----------------------------------------------------------------------------
  248. | Returns 1 if the bfloat16 value `a' is a quiet
  249. | NaN; otherwise returns 0.
  250. *----------------------------------------------------------------------------*/
  251. bool bfloat16_is_quiet_nan(bfloat16 a_, float_status *status)
  252. {
  253. if (no_signaling_nans(status)) {
  254. return bfloat16_is_any_nan(a_);
  255. } else {
  256. uint16_t a = a_;
  257. if (snan_bit_is_one(status)) {
  258. return (((a >> 6) & 0x1FF) == 0x1FE) && (a & 0x3F);
  259. } else {
  260. return ((a >> 6) & 0x1FF) == 0x1FF;
  261. }
  262. }
  263. }
  264. /*----------------------------------------------------------------------------
  265. | Returns 1 if the half-precision floating-point value `a' is a signaling
  266. | NaN; otherwise returns 0.
  267. *----------------------------------------------------------------------------*/
  268. bool float16_is_signaling_nan(float16 a_, float_status *status)
  269. {
  270. if (no_signaling_nans(status)) {
  271. return 0;
  272. } else {
  273. uint16_t a = float16_val(a_);
  274. if (snan_bit_is_one(status)) {
  275. return ((a >> 9) & 0x3F) == 0x3F;
  276. } else {
  277. return (((a >> 9) & 0x3F) == 0x3E) && (a & 0x1FF);
  278. }
  279. }
  280. }
  281. /*----------------------------------------------------------------------------
  282. | Returns 1 if the bfloat16 value `a' is a signaling
  283. | NaN; otherwise returns 0.
  284. *----------------------------------------------------------------------------*/
  285. bool bfloat16_is_signaling_nan(bfloat16 a_, float_status *status)
  286. {
  287. if (no_signaling_nans(status)) {
  288. return 0;
  289. } else {
  290. uint16_t a = a_;
  291. if (snan_bit_is_one(status)) {
  292. return ((a >> 6) & 0x1FF) == 0x1FF;
  293. } else {
  294. return (((a >> 6) & 0x1FF) == 0x1FE) && (a & 0x3F);
  295. }
  296. }
  297. }
  298. /*----------------------------------------------------------------------------
  299. | Returns 1 if the single-precision floating-point value `a' is a quiet
  300. | NaN; otherwise returns 0.
  301. *----------------------------------------------------------------------------*/
  302. bool float32_is_quiet_nan(float32 a_, float_status *status)
  303. {
  304. if (no_signaling_nans(status)) {
  305. return float32_is_any_nan(a_);
  306. } else {
  307. uint32_t a = float32_val(a_);
  308. if (snan_bit_is_one(status)) {
  309. return (((a >> 22) & 0x1FF) == 0x1FE) && (a & 0x003FFFFF);
  310. } else {
  311. return ((uint32_t)(a << 1) >= 0xFF800000);
  312. }
  313. }
  314. }
  315. /*----------------------------------------------------------------------------
  316. | Returns 1 if the single-precision floating-point value `a' is a signaling
  317. | NaN; otherwise returns 0.
  318. *----------------------------------------------------------------------------*/
  319. bool float32_is_signaling_nan(float32 a_, float_status *status)
  320. {
  321. if (no_signaling_nans(status)) {
  322. return 0;
  323. } else {
  324. uint32_t a = float32_val(a_);
  325. if (snan_bit_is_one(status)) {
  326. return ((uint32_t)(a << 1) >= 0xFF800000);
  327. } else {
  328. return (((a >> 22) & 0x1FF) == 0x1FE) && (a & 0x003FFFFF);
  329. }
  330. }
  331. }
  332. /*----------------------------------------------------------------------------
  333. | Select which NaN to propagate for a two-input operation.
  334. | IEEE754 doesn't specify all the details of this, so the
  335. | algorithm is target-specific.
  336. | The routine is passed various bits of information about the
  337. | two NaNs and should return 0 to select NaN a and 1 for NaN b.
  338. | Note that signalling NaNs are always squashed to quiet NaNs
  339. | by the caller, by calling floatXX_silence_nan() before
  340. | returning them.
  341. |
  342. | aIsLargerSignificand is only valid if both a and b are NaNs
  343. | of some kind, and is true if a has the larger significand,
  344. | or if both a and b have the same significand but a is
  345. | positive but b is negative. It is only needed for the x87
  346. | tie-break rule.
  347. *----------------------------------------------------------------------------*/
  348. static int pickNaN(FloatClass a_cls, FloatClass b_cls,
  349. bool aIsLargerSignificand, float_status *status)
  350. {
  351. #if defined(TARGET_ARM) || defined(TARGET_MIPS) || defined(TARGET_HPPA) || \
  352. defined(TARGET_LOONGARCH64) || defined(TARGET_S390X)
  353. /* ARM mandated NaN propagation rules (see FPProcessNaNs()), take
  354. * the first of:
  355. * 1. A if it is signaling
  356. * 2. B if it is signaling
  357. * 3. A (quiet)
  358. * 4. B (quiet)
  359. * A signaling NaN is always quietened before returning it.
  360. */
  361. /* According to MIPS specifications, if one of the two operands is
  362. * a sNaN, a new qNaN has to be generated. This is done in
  363. * floatXX_silence_nan(). For qNaN inputs the specifications
  364. * says: "When possible, this QNaN result is one of the operand QNaN
  365. * values." In practice it seems that most implementations choose
  366. * the first operand if both operands are qNaN. In short this gives
  367. * the following rules:
  368. * 1. A if it is signaling
  369. * 2. B if it is signaling
  370. * 3. A (quiet)
  371. * 4. B (quiet)
  372. * A signaling NaN is always silenced before returning it.
  373. */
  374. if (is_snan(a_cls)) {
  375. return 0;
  376. } else if (is_snan(b_cls)) {
  377. return 1;
  378. } else if (is_qnan(a_cls)) {
  379. return 0;
  380. } else {
  381. return 1;
  382. }
  383. #elif defined(TARGET_PPC) || defined(TARGET_M68K)
  384. /* PowerPC propagation rules:
  385. * 1. A if it sNaN or qNaN
  386. * 2. B if it sNaN or qNaN
  387. * A signaling NaN is always silenced before returning it.
  388. */
  389. /* M68000 FAMILY PROGRAMMER'S REFERENCE MANUAL
  390. * 3.4 FLOATING-POINT INSTRUCTION DETAILS
  391. * If either operand, but not both operands, of an operation is a
  392. * nonsignaling NaN, then that NaN is returned as the result. If both
  393. * operands are nonsignaling NaNs, then the destination operand
  394. * nonsignaling NaN is returned as the result.
  395. * If either operand to an operation is a signaling NaN (SNaN), then the
  396. * SNaN bit is set in the FPSR EXC byte. If the SNaN exception enable bit
  397. * is set in the FPCR ENABLE byte, then the exception is taken and the
  398. * destination is not modified. If the SNaN exception enable bit is not
  399. * set, setting the SNaN bit in the operand to a one converts the SNaN to
  400. * a nonsignaling NaN. The operation then continues as described in the
  401. * preceding paragraph for nonsignaling NaNs.
  402. */
  403. if (is_nan(a_cls)) {
  404. return 0;
  405. } else {
  406. return 1;
  407. }
  408. #elif defined(TARGET_XTENSA)
  409. /*
  410. * Xtensa has two NaN propagation modes.
  411. * Which one is active is controlled by float_status::use_first_nan.
  412. */
  413. if (status->use_first_nan) {
  414. if (is_nan(a_cls)) {
  415. return 0;
  416. } else {
  417. return 1;
  418. }
  419. } else {
  420. if (is_nan(b_cls)) {
  421. return 1;
  422. } else {
  423. return 0;
  424. }
  425. }
  426. #else
  427. /* This implements x87 NaN propagation rules:
  428. * SNaN + QNaN => return the QNaN
  429. * two SNaNs => return the one with the larger significand, silenced
  430. * two QNaNs => return the one with the larger significand
  431. * SNaN and a non-NaN => return the SNaN, silenced
  432. * QNaN and a non-NaN => return the QNaN
  433. *
  434. * If we get down to comparing significands and they are the same,
  435. * return the NaN with the positive sign bit (if any).
  436. */
  437. if (is_snan(a_cls)) {
  438. if (is_snan(b_cls)) {
  439. return aIsLargerSignificand ? 0 : 1;
  440. }
  441. return is_qnan(b_cls) ? 1 : 0;
  442. } else if (is_qnan(a_cls)) {
  443. if (is_snan(b_cls) || !is_qnan(b_cls)) {
  444. return 0;
  445. } else {
  446. return aIsLargerSignificand ? 0 : 1;
  447. }
  448. } else {
  449. return 1;
  450. }
  451. #endif
  452. }
  453. /*----------------------------------------------------------------------------
  454. | Select which NaN to propagate for a three-input operation.
  455. | For the moment we assume that no CPU needs the 'larger significand'
  456. | information.
  457. | Return values : 0 : a; 1 : b; 2 : c; 3 : default-NaN
  458. *----------------------------------------------------------------------------*/
  459. static int pickNaNMulAdd(FloatClass a_cls, FloatClass b_cls, FloatClass c_cls,
  460. bool infzero, float_status *status)
  461. {
  462. #if defined(TARGET_ARM)
  463. /* For ARM, the (inf,zero,qnan) case sets InvalidOp and returns
  464. * the default NaN
  465. */
  466. if (infzero && is_qnan(c_cls)) {
  467. float_raise(float_flag_invalid | float_flag_invalid_imz, status);
  468. return 3;
  469. }
  470. /* This looks different from the ARM ARM pseudocode, because the ARM ARM
  471. * puts the operands to a fused mac operation (a*b)+c in the order c,a,b.
  472. */
  473. if (is_snan(c_cls)) {
  474. return 2;
  475. } else if (is_snan(a_cls)) {
  476. return 0;
  477. } else if (is_snan(b_cls)) {
  478. return 1;
  479. } else if (is_qnan(c_cls)) {
  480. return 2;
  481. } else if (is_qnan(a_cls)) {
  482. return 0;
  483. } else {
  484. return 1;
  485. }
  486. #elif defined(TARGET_MIPS)
  487. if (snan_bit_is_one(status)) {
  488. /*
  489. * For MIPS systems that conform to IEEE754-1985, the (inf,zero,nan)
  490. * case sets InvalidOp and returns the default NaN
  491. */
  492. if (infzero) {
  493. float_raise(float_flag_invalid | float_flag_invalid_imz, status);
  494. return 3;
  495. }
  496. /* Prefer sNaN over qNaN, in the a, b, c order. */
  497. if (is_snan(a_cls)) {
  498. return 0;
  499. } else if (is_snan(b_cls)) {
  500. return 1;
  501. } else if (is_snan(c_cls)) {
  502. return 2;
  503. } else if (is_qnan(a_cls)) {
  504. return 0;
  505. } else if (is_qnan(b_cls)) {
  506. return 1;
  507. } else {
  508. return 2;
  509. }
  510. } else {
  511. /*
  512. * For MIPS systems that conform to IEEE754-2008, the (inf,zero,nan)
  513. * case sets InvalidOp and returns the input value 'c'
  514. */
  515. if (infzero) {
  516. float_raise(float_flag_invalid | float_flag_invalid_imz, status);
  517. return 2;
  518. }
  519. /* Prefer sNaN over qNaN, in the c, a, b order. */
  520. if (is_snan(c_cls)) {
  521. return 2;
  522. } else if (is_snan(a_cls)) {
  523. return 0;
  524. } else if (is_snan(b_cls)) {
  525. return 1;
  526. } else if (is_qnan(c_cls)) {
  527. return 2;
  528. } else if (is_qnan(a_cls)) {
  529. return 0;
  530. } else {
  531. return 1;
  532. }
  533. }
  534. #elif defined(TARGET_LOONGARCH64)
  535. /*
  536. * For LoongArch systems that conform to IEEE754-2008, the (inf,zero,nan)
  537. * case sets InvalidOp and returns the input value 'c'
  538. */
  539. if (infzero) {
  540. float_raise(float_flag_invalid | float_flag_invalid_imz, status);
  541. return 2;
  542. }
  543. /* Prefer sNaN over qNaN, in the c, a, b order. */
  544. if (is_snan(c_cls)) {
  545. return 2;
  546. } else if (is_snan(a_cls)) {
  547. return 0;
  548. } else if (is_snan(b_cls)) {
  549. return 1;
  550. } else if (is_qnan(c_cls)) {
  551. return 2;
  552. } else if (is_qnan(a_cls)) {
  553. return 0;
  554. } else {
  555. return 1;
  556. }
  557. #elif defined(TARGET_PPC)
  558. /* For PPC, the (inf,zero,qnan) case sets InvalidOp, but we prefer
  559. * to return an input NaN if we have one (ie c) rather than generating
  560. * a default NaN
  561. */
  562. if (infzero) {
  563. float_raise(float_flag_invalid | float_flag_invalid_imz, status);
  564. return 2;
  565. }
  566. /* If fRA is a NaN return it; otherwise if fRB is a NaN return it;
  567. * otherwise return fRC. Note that muladd on PPC is (fRA * fRC) + frB
  568. */
  569. if (is_nan(a_cls)) {
  570. return 0;
  571. } else if (is_nan(c_cls)) {
  572. return 2;
  573. } else {
  574. return 1;
  575. }
  576. #elif defined(TARGET_RISCV)
  577. /* For RISC-V, InvalidOp is set when multiplicands are Inf and zero */
  578. if (infzero) {
  579. float_raise(float_flag_invalid | float_flag_invalid_imz, status);
  580. }
  581. return 3; /* default NaN */
  582. #elif defined(TARGET_XTENSA)
  583. /*
  584. * For Xtensa, the (inf,zero,nan) case sets InvalidOp and returns
  585. * an input NaN if we have one (ie c).
  586. */
  587. if (infzero) {
  588. float_raise(float_flag_invalid | float_flag_invalid_imz, status);
  589. return 2;
  590. }
  591. if (status->use_first_nan) {
  592. if (is_nan(a_cls)) {
  593. return 0;
  594. } else if (is_nan(b_cls)) {
  595. return 1;
  596. } else {
  597. return 2;
  598. }
  599. } else {
  600. if (is_nan(c_cls)) {
  601. return 2;
  602. } else if (is_nan(b_cls)) {
  603. return 1;
  604. } else {
  605. return 0;
  606. }
  607. }
  608. #else
  609. /* A default implementation: prefer a to b to c.
  610. * This is unlikely to actually match any real implementation.
  611. */
  612. if (is_nan(a_cls)) {
  613. return 0;
  614. } else if (is_nan(b_cls)) {
  615. return 1;
  616. } else {
  617. return 2;
  618. }
  619. #endif
  620. }
  621. /*----------------------------------------------------------------------------
  622. | Returns 1 if the double-precision floating-point value `a' is a quiet
  623. | NaN; otherwise returns 0.
  624. *----------------------------------------------------------------------------*/
  625. bool float64_is_quiet_nan(float64 a_, float_status *status)
  626. {
  627. if (no_signaling_nans(status)) {
  628. return float64_is_any_nan(a_);
  629. } else {
  630. uint64_t a = float64_val(a_);
  631. if (snan_bit_is_one(status)) {
  632. return (((a >> 51) & 0xFFF) == 0xFFE)
  633. && (a & 0x0007FFFFFFFFFFFFULL);
  634. } else {
  635. return ((a << 1) >= 0xFFF0000000000000ULL);
  636. }
  637. }
  638. }
  639. /*----------------------------------------------------------------------------
  640. | Returns 1 if the double-precision floating-point value `a' is a signaling
  641. | NaN; otherwise returns 0.
  642. *----------------------------------------------------------------------------*/
  643. bool float64_is_signaling_nan(float64 a_, float_status *status)
  644. {
  645. if (no_signaling_nans(status)) {
  646. return 0;
  647. } else {
  648. uint64_t a = float64_val(a_);
  649. if (snan_bit_is_one(status)) {
  650. return ((a << 1) >= 0xFFF0000000000000ULL);
  651. } else {
  652. return (((a >> 51) & 0xFFF) == 0xFFE)
  653. && (a & UINT64_C(0x0007FFFFFFFFFFFF));
  654. }
  655. }
  656. }
  657. /*----------------------------------------------------------------------------
  658. | Returns 1 if the extended double-precision floating-point value `a' is a
  659. | quiet NaN; otherwise returns 0. This slightly differs from the same
  660. | function for other types as floatx80 has an explicit bit.
  661. *----------------------------------------------------------------------------*/
  662. int floatx80_is_quiet_nan(floatx80 a, float_status *status)
  663. {
  664. if (no_signaling_nans(status)) {
  665. return floatx80_is_any_nan(a);
  666. } else {
  667. if (snan_bit_is_one(status)) {
  668. uint64_t aLow;
  669. aLow = a.low & ~0x4000000000000000ULL;
  670. return ((a.high & 0x7FFF) == 0x7FFF)
  671. && (aLow << 1)
  672. && (a.low == aLow);
  673. } else {
  674. return ((a.high & 0x7FFF) == 0x7FFF)
  675. && (UINT64_C(0x8000000000000000) <= ((uint64_t)(a.low << 1)));
  676. }
  677. }
  678. }
  679. /*----------------------------------------------------------------------------
  680. | Returns 1 if the extended double-precision floating-point value `a' is a
  681. | signaling NaN; otherwise returns 0. This slightly differs from the same
  682. | function for other types as floatx80 has an explicit bit.
  683. *----------------------------------------------------------------------------*/
  684. int floatx80_is_signaling_nan(floatx80 a, float_status *status)
  685. {
  686. if (no_signaling_nans(status)) {
  687. return 0;
  688. } else {
  689. if (snan_bit_is_one(status)) {
  690. return ((a.high & 0x7FFF) == 0x7FFF)
  691. && ((a.low << 1) >= 0x8000000000000000ULL);
  692. } else {
  693. uint64_t aLow;
  694. aLow = a.low & ~UINT64_C(0x4000000000000000);
  695. return ((a.high & 0x7FFF) == 0x7FFF)
  696. && (uint64_t)(aLow << 1)
  697. && (a.low == aLow);
  698. }
  699. }
  700. }
  701. /*----------------------------------------------------------------------------
  702. | Returns a quiet NaN from a signalling NaN for the extended double-precision
  703. | floating point value `a'.
  704. *----------------------------------------------------------------------------*/
  705. floatx80 floatx80_silence_nan(floatx80 a, float_status *status)
  706. {
  707. /* None of the targets that have snan_bit_is_one use floatx80. */
  708. assert(!snan_bit_is_one(status));
  709. a.low |= UINT64_C(0xC000000000000000);
  710. return a;
  711. }
  712. /*----------------------------------------------------------------------------
  713. | Takes two extended double-precision floating-point values `a' and `b', one
  714. | of which is a NaN, and returns the appropriate NaN result. If either `a' or
  715. | `b' is a signaling NaN, the invalid exception is raised.
  716. *----------------------------------------------------------------------------*/
  717. floatx80 propagateFloatx80NaN(floatx80 a, floatx80 b, float_status *status)
  718. {
  719. bool aIsLargerSignificand;
  720. FloatClass a_cls, b_cls;
  721. /* This is not complete, but is good enough for pickNaN. */
  722. a_cls = (!floatx80_is_any_nan(a)
  723. ? float_class_normal
  724. : floatx80_is_signaling_nan(a, status)
  725. ? float_class_snan
  726. : float_class_qnan);
  727. b_cls = (!floatx80_is_any_nan(b)
  728. ? float_class_normal
  729. : floatx80_is_signaling_nan(b, status)
  730. ? float_class_snan
  731. : float_class_qnan);
  732. if (is_snan(a_cls) || is_snan(b_cls)) {
  733. float_raise(float_flag_invalid, status);
  734. }
  735. if (status->default_nan_mode) {
  736. return floatx80_default_nan(status);
  737. }
  738. if (a.low < b.low) {
  739. aIsLargerSignificand = 0;
  740. } else if (b.low < a.low) {
  741. aIsLargerSignificand = 1;
  742. } else {
  743. aIsLargerSignificand = (a.high < b.high) ? 1 : 0;
  744. }
  745. if (pickNaN(a_cls, b_cls, aIsLargerSignificand, status)) {
  746. if (is_snan(b_cls)) {
  747. return floatx80_silence_nan(b, status);
  748. }
  749. return b;
  750. } else {
  751. if (is_snan(a_cls)) {
  752. return floatx80_silence_nan(a, status);
  753. }
  754. return a;
  755. }
  756. }
  757. /*----------------------------------------------------------------------------
  758. | Returns 1 if the quadruple-precision floating-point value `a' is a quiet
  759. | NaN; otherwise returns 0.
  760. *----------------------------------------------------------------------------*/
  761. bool float128_is_quiet_nan(float128 a, float_status *status)
  762. {
  763. if (no_signaling_nans(status)) {
  764. return float128_is_any_nan(a);
  765. } else {
  766. if (snan_bit_is_one(status)) {
  767. return (((a.high >> 47) & 0xFFFF) == 0xFFFE)
  768. && (a.low || (a.high & 0x00007FFFFFFFFFFFULL));
  769. } else {
  770. return ((a.high << 1) >= 0xFFFF000000000000ULL)
  771. && (a.low || (a.high & 0x0000FFFFFFFFFFFFULL));
  772. }
  773. }
  774. }
  775. /*----------------------------------------------------------------------------
  776. | Returns 1 if the quadruple-precision floating-point value `a' is a
  777. | signaling NaN; otherwise returns 0.
  778. *----------------------------------------------------------------------------*/
  779. bool float128_is_signaling_nan(float128 a, float_status *status)
  780. {
  781. if (no_signaling_nans(status)) {
  782. return 0;
  783. } else {
  784. if (snan_bit_is_one(status)) {
  785. return ((a.high << 1) >= 0xFFFF000000000000ULL)
  786. && (a.low || (a.high & 0x0000FFFFFFFFFFFFULL));
  787. } else {
  788. return (((a.high >> 47) & 0xFFFF) == 0xFFFE)
  789. && (a.low || (a.high & UINT64_C(0x00007FFFFFFFFFFF)));
  790. }
  791. }
  792. }