Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* IEEE754 floating point arithmetic |
| 2 | * single precision |
| 3 | */ |
| 4 | /* |
| 5 | * MIPS floating point support |
| 6 | * Copyright (C) 1994-2000 Algorithmics Ltd. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * This program is free software; you can distribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License (Version 2) as |
| 10 | * published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 | * for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame] | 19 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "ieee754sp.h" |
Ralf Baechle | b3a7ad2 | 2014-04-22 16:33:07 +0200 | [diff] [blame] | 23 | #include "ieee754dp.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Maciej W. Rozycki | d19cf86 | 2015-04-03 23:25:23 +0100 | [diff] [blame] | 25 | static inline union ieee754sp ieee754sp_nan_fdp(int xs, u64 xm) |
| 26 | { |
| 27 | return buildsp(xs, SP_EMAX + 1 + SP_EBIAS, |
| 28 | xm >> (DP_FBITS - SP_FBITS)); |
| 29 | } |
| 30 | |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 31 | union ieee754sp ieee754sp_fdp(union ieee754dp x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | { |
Maciej W. Rozycki | b0c2f8f | 2015-04-03 23:25:52 +0100 | [diff] [blame] | 33 | union ieee754sp y; |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame] | 34 | u32 rm; |
| 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | COMPXDP; |
Maciej W. Rozycki | b0c2f8f | 2015-04-03 23:25:52 +0100 | [diff] [blame] | 37 | COMPYSP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | EXPLODEXDP; |
| 40 | |
Ralf Baechle | 9e8bad1 | 2014-04-19 00:36:32 +0200 | [diff] [blame] | 41 | ieee754_clearcx(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | FLUSHXDP; |
| 44 | |
| 45 | switch (xc) { |
| 46 | case IEEE754_CLASS_SNAN: |
Maciej W. Rozycki | 90d53a9 | 2015-11-13 00:47:28 +0000 | [diff] [blame] | 47 | x = ieee754dp_nanxcpt(x); |
| 48 | EXPLODEXDP; |
| 49 | /* Fall through. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | case IEEE754_CLASS_QNAN: |
Maciej W. Rozycki | b0c2f8f | 2015-04-03 23:25:52 +0100 | [diff] [blame] | 51 | y = ieee754sp_nan_fdp(xs, xm); |
Maciej W. Rozycki | 90d53a9 | 2015-11-13 00:47:28 +0000 | [diff] [blame] | 52 | if (!ieee754_csr.nan2008) { |
| 53 | EXPLODEYSP; |
| 54 | if (!ieee754_class_nan(yc)) |
| 55 | y = ieee754sp_indef(); |
| 56 | } |
Maciej W. Rozycki | b0c2f8f | 2015-04-03 23:25:52 +0100 | [diff] [blame] | 57 | return y; |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame] | 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | case IEEE754_CLASS_INF: |
| 60 | return ieee754sp_inf(xs); |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame] | 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | case IEEE754_CLASS_ZERO: |
| 63 | return ieee754sp_zero(xs); |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame] | 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | case IEEE754_CLASS_DNORM: |
| 66 | /* can't possibly be sp representable */ |
Ralf Baechle | 9e8bad1 | 2014-04-19 00:36:32 +0200 | [diff] [blame] | 67 | ieee754_setcx(IEEE754_UNDERFLOW); |
| 68 | ieee754_setcx(IEEE754_INEXACT); |
Ralf Baechle | 56a6473 | 2014-04-30 11:21:55 +0200 | [diff] [blame] | 69 | if ((ieee754_csr.rm == FPU_CSR_RU && !xs) || |
| 70 | (ieee754_csr.rm == FPU_CSR_RD && xs)) |
Ralf Baechle | 90efba3 | 2014-04-25 03:19:57 +0200 | [diff] [blame] | 71 | return ieee754sp_mind(xs); |
| 72 | return ieee754sp_zero(xs); |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame] | 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | case IEEE754_CLASS_NORM: |
| 75 | break; |
| 76 | } |
| 77 | |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame] | 78 | /* |
| 79 | * Convert from DP_FBITS to SP_FBITS+3 with sticky right shift. |
| 80 | */ |
| 81 | rm = (xm >> (DP_FBITS - (SP_FBITS + 3))) | |
| 82 | ((xm << (64 - (DP_FBITS - (SP_FBITS + 3)))) != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame] | 84 | return ieee754sp_format(xs, xe, rm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |