Thomas Gleixner | 9d5a634 | 2019-05-31 01:09:55 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* IEEE754 floating point arithmetic |
| 3 | * single precision |
| 4 | */ |
| 5 | /* |
| 6 | * MIPS floating point support |
| 7 | * Copyright (C) 1994-2000 Algorithmics Ltd. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include "ieee754sp.h" |
| 11 | |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 12 | int ieee754sp_tint(union ieee754sp x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | { |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame] | 14 | u32 residue; |
| 15 | int round; |
| 16 | int sticky; |
| 17 | int odd; |
| 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | COMPXSP; |
| 20 | |
Ralf Baechle | 9e8bad1 | 2014-04-19 00:36:32 +0200 | [diff] [blame] | 21 | ieee754_clearcx(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | EXPLODEXSP; |
| 24 | FLUSHXSP; |
| 25 | |
| 26 | switch (xc) { |
| 27 | case IEEE754_CLASS_SNAN: |
| 28 | case IEEE754_CLASS_QNAN: |
Ralf Baechle | 9e8bad1 | 2014-04-19 00:36:32 +0200 | [diff] [blame] | 29 | ieee754_setcx(IEEE754_INVALID_OPERATION); |
Ralf Baechle | 90efba3 | 2014-04-25 03:19:57 +0200 | [diff] [blame] | 30 | return ieee754si_indef(); |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame] | 31 | |
Maciej W. Rozycki | 90d53a9 | 2015-11-13 00:47:28 +0000 | [diff] [blame] | 32 | case IEEE754_CLASS_INF: |
| 33 | ieee754_setcx(IEEE754_INVALID_OPERATION); |
| 34 | return ieee754si_overflow(xs); |
| 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | case IEEE754_CLASS_ZERO: |
| 37 | return 0; |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame] | 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | case IEEE754_CLASS_DNORM: |
| 40 | case IEEE754_CLASS_NORM: |
| 41 | break; |
| 42 | } |
| 43 | if (xe >= 31) { |
| 44 | /* look for valid corner case */ |
| 45 | if (xe == 31 && xs && xm == SP_HIDDEN_BIT) |
| 46 | return -0x80000000; |
| 47 | /* Set invalid. We will only use overflow for floating |
| 48 | point overflow */ |
Ralf Baechle | 9e8bad1 | 2014-04-19 00:36:32 +0200 | [diff] [blame] | 49 | ieee754_setcx(IEEE754_INVALID_OPERATION); |
Maciej W. Rozycki | 90d53a9 | 2015-11-13 00:47:28 +0000 | [diff] [blame] | 50 | return ieee754si_overflow(xs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | } |
| 52 | /* oh gawd */ |
Ralf Baechle | ad8fb553 | 2014-04-22 15:51:55 +0200 | [diff] [blame] | 53 | if (xe > SP_FBITS) { |
| 54 | xm <<= xe - SP_FBITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | if (xe < -1) { |
| 57 | residue = xm; |
| 58 | round = 0; |
| 59 | sticky = residue != 0; |
| 60 | xm = 0; |
Andrea Gelmini | 3e4088a | 2010-05-23 21:52:09 +0200 | [diff] [blame] | 61 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | /* Shifting a u32 32 times does not work, |
| 63 | * so we do it in two steps. Be aware that xe |
| 64 | * may be -1 */ |
| 65 | residue = xm << (xe + 1); |
Ralf Baechle | ad8fb553 | 2014-04-22 15:51:55 +0200 | [diff] [blame] | 66 | residue <<= 31 - SP_FBITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | round = (residue >> 31) != 0; |
| 68 | sticky = (residue << 1) != 0; |
Ralf Baechle | ad8fb553 | 2014-04-22 15:51:55 +0200 | [diff] [blame] | 69 | xm >>= SP_FBITS - xe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | } |
| 71 | odd = (xm & 0x1) != 0x0; |
| 72 | switch (ieee754_csr.rm) { |
Ralf Baechle | 56a6473 | 2014-04-30 11:21:55 +0200 | [diff] [blame] | 73 | case FPU_CSR_RN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | if (round && (sticky || odd)) |
| 75 | xm++; |
| 76 | break; |
Ralf Baechle | 56a6473 | 2014-04-30 11:21:55 +0200 | [diff] [blame] | 77 | case FPU_CSR_RZ: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | break; |
Ralf Baechle | 56a6473 | 2014-04-30 11:21:55 +0200 | [diff] [blame] | 79 | case FPU_CSR_RU: /* toward +Infinity */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | if ((round || sticky) && !xs) |
| 81 | xm++; |
| 82 | break; |
Ralf Baechle | 56a6473 | 2014-04-30 11:21:55 +0200 | [diff] [blame] | 83 | case FPU_CSR_RD: /* toward -Infinity */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | if ((round || sticky) && xs) |
| 85 | xm++; |
| 86 | break; |
| 87 | } |
| 88 | if ((xm >> 31) != 0) { |
| 89 | /* This can happen after rounding */ |
Ralf Baechle | 9e8bad1 | 2014-04-19 00:36:32 +0200 | [diff] [blame] | 90 | ieee754_setcx(IEEE754_INVALID_OPERATION); |
Maciej W. Rozycki | 90d53a9 | 2015-11-13 00:47:28 +0000 | [diff] [blame] | 91 | return ieee754si_overflow(xs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | } |
| 93 | if (round || sticky) |
Ralf Baechle | 9e8bad1 | 2014-04-19 00:36:32 +0200 | [diff] [blame] | 94 | ieee754_setcx(IEEE754_INEXACT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | } |
| 96 | if (xs) |
| 97 | return -xm; |
| 98 | else |
| 99 | return xm; |
| 100 | } |