Skip to content

Commit 90d53a9

Browse files
Maciej W. Rozyckiralfbaechle
authored andcommitted
MIPS: math-emu: Add IEEE Std 754-2008 NaN encoding emulation
Implement IEEE Std 754-2008 NaN encoding wired to the state of the FCSR.NAN2008 bit. Make the interpretation of the quiet bit in NaN data as follows: * in the legacy mode originally defined by the MIPS architecture the value of 1 denotes an sNaN whereas the value of 0 denotes a qNaN, * in the 2008 mode introduced with revision 5 of the MIPS architecture the value of 0 denotes an sNaN whereas the value of 1 denotes a qNaN, following the definition of the preferred NaN encoding introduced with IEEE Std 754-2008. In the 2008 mode, following the requirement of the said standard, quiet an sNaN where needed by setting the quiet bit to 1 and leaving all the NaN payload bits unchanged. Update format conversion operations according to the rules set by IEEE Std 754-2008 and the MIPS architecture. Specifically: * propagate NaN payload bits through conversions between floating-point formats such that as much information as possible is preserved and specifically a conversion from a narrower format to a wider format and then back to the original format does not change a qNaN payload in any way, * conversions from a floating-point to an integer format where the source is a NaN, infinity or a value that would convert to an integer outside the range of the result format produce, under the default exception handling, the respective values defined by the MIPS architecture. In full FPU emulation set the FIR.HAS2008 bit to 1, however do not make any further FCSR bits writable. Signed-off-by: Maciej W. Rozycki <macro@imgtec.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Matthew Fortune <Matthew.Fortune@imgtec.com> Cc: linux-mips@linux-mips.org Cc: linux-kernel@vger.kernel.org Patchwork: https://patchwork.linux-mips.org/patch/11477/ Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
1 parent 198f705 commit 90d53a9

11 files changed

Lines changed: 93 additions & 42 deletions

File tree

‎arch/mips/kernel/cpu-probe.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ static void cpu_set_nofpu_id(struct cpuinfo_mips *c)
113113
if (c->isa_level & (MIPS_CPU_ISA_M32R2 | MIPS_CPU_ISA_M64R2 |
114114
MIPS_CPU_ISA_M32R6 | MIPS_CPU_ISA_M64R6))
115115
value |= MIPS_FPIR_F64 | MIPS_FPIR_L | MIPS_FPIR_W;
116+
if (c->options & MIPS_CPU_NAN_2008)
117+
value |= MIPS_FPIR_HAS2008;
116118
c->fpu_id = value;
117119
}
118120

‎arch/mips/math-emu/dp_tint.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,13 @@ int ieee754dp_tint(union ieee754dp x)
3838
switch (xc) {
3939
case IEEE754_CLASS_SNAN:
4040
case IEEE754_CLASS_QNAN:
41-
case IEEE754_CLASS_INF:
4241
ieee754_setcx(IEEE754_INVALID_OPERATION);
4342
return ieee754si_indef();
4443

44+
case IEEE754_CLASS_INF:
45+
ieee754_setcx(IEEE754_INVALID_OPERATION);
46+
return ieee754si_overflow(xs);
47+
4548
case IEEE754_CLASS_ZERO:
4649
return 0;
4750

@@ -53,7 +56,7 @@ int ieee754dp_tint(union ieee754dp x)
5356
/* Set invalid. We will only use overflow for floating
5457
point overflow */
5558
ieee754_setcx(IEEE754_INVALID_OPERATION);
56-
return ieee754si_indef();
59+
return ieee754si_overflow(xs);
5760
}
5861
/* oh gawd */
5962
if (xe > DP_FBITS) {
@@ -93,7 +96,7 @@ int ieee754dp_tint(union ieee754dp x)
9396
if ((xm >> 31) != 0 && (xs == 0 || xm != 0x80000000)) {
9497
/* This can happen after rounding */
9598
ieee754_setcx(IEEE754_INVALID_OPERATION);
96-
return ieee754si_indef();
99+
return ieee754si_overflow(xs);
97100
}
98101
if (round || sticky)
99102
ieee754_setcx(IEEE754_INEXACT);

‎arch/mips/math-emu/dp_tlong.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,13 @@ s64 ieee754dp_tlong(union ieee754dp x)
3838
switch (xc) {
3939
case IEEE754_CLASS_SNAN:
4040
case IEEE754_CLASS_QNAN:
41-
case IEEE754_CLASS_INF:
4241
ieee754_setcx(IEEE754_INVALID_OPERATION);
4342
return ieee754di_indef();
4443

44+
case IEEE754_CLASS_INF:
45+
ieee754_setcx(IEEE754_INVALID_OPERATION);
46+
return ieee754di_overflow(xs);
47+
4548
case IEEE754_CLASS_ZERO:
4649
return 0;
4750

@@ -56,7 +59,7 @@ s64 ieee754dp_tlong(union ieee754dp x)
5659
/* Set invalid. We will only use overflow for floating
5760
point overflow */
5861
ieee754_setcx(IEEE754_INVALID_OPERATION);
59-
return ieee754di_indef();
62+
return ieee754di_overflow(xs);
6063
}
6164
/* oh gawd */
6265
if (xe > DP_FBITS) {
@@ -97,7 +100,7 @@ s64 ieee754dp_tlong(union ieee754dp x)
97100
if ((xm >> 63) != 0) {
98101
/* This can happen after rounding */
99102
ieee754_setcx(IEEE754_INVALID_OPERATION);
100-
return ieee754di_indef();
103+
return ieee754di_overflow(xs);
101104
}
102105
if (round || sticky)
103106
ieee754_setcx(IEEE754_INEXACT);

‎arch/mips/math-emu/ieee754.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ const union ieee754dp __ieee754dp_spcvals[] = {
5959
DPCNST(1, 3, 0x4000000000000ULL), /* - 10.0 */
6060
DPCNST(0, DP_EMAX + 1, 0x0000000000000ULL), /* + infinity */
6161
DPCNST(1, DP_EMAX + 1, 0x0000000000000ULL), /* - infinity */
62-
DPCNST(0, DP_EMAX + 1, 0x7FFFFFFFFFFFFULL), /* + indef quiet Nan */
62+
DPCNST(0, DP_EMAX + 1, 0x7FFFFFFFFFFFFULL), /* + ind legacy qNaN */
63+
DPCNST(0, DP_EMAX + 1, 0x8000000000000ULL), /* + indef 2008 qNaN */
6364
DPCNST(0, DP_EMAX, 0xFFFFFFFFFFFFFULL), /* + max */
6465
DPCNST(1, DP_EMAX, 0xFFFFFFFFFFFFFULL), /* - max */
6566
DPCNST(0, DP_EMIN, 0x0000000000000ULL), /* + min normal */
@@ -82,7 +83,8 @@ const union ieee754sp __ieee754sp_spcvals[] = {
8283
SPCNST(1, 3, 0x200000), /* - 10.0 */
8384
SPCNST(0, SP_EMAX + 1, 0x000000), /* + infinity */
8485
SPCNST(1, SP_EMAX + 1, 0x000000), /* - infinity */
85-
SPCNST(0, SP_EMAX + 1, 0x3FFFFF), /* + indef quiet Nan */
86+
SPCNST(0, SP_EMAX + 1, 0x3FFFFF), /* + indef legacy quiet NaN */
87+
SPCNST(0, SP_EMAX + 1, 0x400000), /* + indef 2008 quiet NaN */
8688
SPCNST(0, SP_EMAX, 0x7FFFFF), /* + max normal */
8789
SPCNST(1, SP_EMAX, 0x7FFFFF), /* - max normal */
8890
SPCNST(0, SP_EMIN, 0x000000), /* + min normal */

‎arch/mips/math-emu/ieee754.h‎

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,16 @@ union ieee754dp ieee754dp_dump(char *s, union ieee754dp x);
221221
#define IEEE754_SPCVAL_NTEN 5 /* -10.0 */
222222
#define IEEE754_SPCVAL_PINFINITY 6 /* +inf */
223223
#define IEEE754_SPCVAL_NINFINITY 7 /* -inf */
224-
#define IEEE754_SPCVAL_INDEF 8 /* quiet NaN */
225-
#define IEEE754_SPCVAL_PMAX 9 /* +max norm */
226-
#define IEEE754_SPCVAL_NMAX 10 /* -max norm */
227-
#define IEEE754_SPCVAL_PMIN 11 /* +min norm */
228-
#define IEEE754_SPCVAL_NMIN 12 /* -min norm */
229-
#define IEEE754_SPCVAL_PMIND 13 /* +min denorm */
230-
#define IEEE754_SPCVAL_NMIND 14 /* -min denorm */
231-
#define IEEE754_SPCVAL_P1E31 15 /* + 1.0e31 */
232-
#define IEEE754_SPCVAL_P1E63 16 /* + 1.0e63 */
224+
#define IEEE754_SPCVAL_INDEF_LEG 8 /* legacy quiet NaN */
225+
#define IEEE754_SPCVAL_INDEF_2008 9 /* IEEE 754-2008 quiet NaN */
226+
#define IEEE754_SPCVAL_PMAX 10 /* +max norm */
227+
#define IEEE754_SPCVAL_NMAX 11 /* -max norm */
228+
#define IEEE754_SPCVAL_PMIN 12 /* +min norm */
229+
#define IEEE754_SPCVAL_NMIN 13 /* -min norm */
230+
#define IEEE754_SPCVAL_PMIND 14 /* +min denorm */
231+
#define IEEE754_SPCVAL_NMIND 15 /* -min denorm */
232+
#define IEEE754_SPCVAL_P1E31 16 /* + 1.0e31 */
233+
#define IEEE754_SPCVAL_P1E63 17 /* + 1.0e63 */
233234

234235
extern const union ieee754dp __ieee754dp_spcvals[];
235236
extern const union ieee754sp __ieee754sp_spcvals[];
@@ -243,7 +244,8 @@ extern const union ieee754sp __ieee754sp_spcvals[];
243244
#define ieee754dp_zero(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PZERO+(sn)])
244245
#define ieee754dp_one(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PONE+(sn)])
245246
#define ieee754dp_ten(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PTEN+(sn)])
246-
#define ieee754dp_indef() (ieee754dp_spcvals[IEEE754_SPCVAL_INDEF])
247+
#define ieee754dp_indef() (ieee754dp_spcvals[IEEE754_SPCVAL_INDEF_LEG + \
248+
ieee754_csr.nan2008])
247249
#define ieee754dp_max(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PMAX+(sn)])
248250
#define ieee754dp_min(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PMIN+(sn)])
249251
#define ieee754dp_mind(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PMIND+(sn)])
@@ -254,7 +256,8 @@ extern const union ieee754sp __ieee754sp_spcvals[];
254256
#define ieee754sp_zero(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PZERO+(sn)])
255257
#define ieee754sp_one(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PONE+(sn)])
256258
#define ieee754sp_ten(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PTEN+(sn)])
257-
#define ieee754sp_indef() (ieee754sp_spcvals[IEEE754_SPCVAL_INDEF])
259+
#define ieee754sp_indef() (ieee754sp_spcvals[IEEE754_SPCVAL_INDEF_LEG + \
260+
ieee754_csr.nan2008])
258261
#define ieee754sp_max(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PMAX+(sn)])
259262
#define ieee754sp_min(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PMIN+(sn)])
260263
#define ieee754sp_mind(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PMIND+(sn)])
@@ -266,12 +269,25 @@ extern const union ieee754sp __ieee754sp_spcvals[];
266269
*/
267270
static inline int ieee754si_indef(void)
268271
{
269-
return INT_MAX;
272+
return ieee754_csr.nan2008 ? 0 : INT_MAX;
270273
}
271274

272275
static inline s64 ieee754di_indef(void)
273276
{
274-
return S64_MAX;
277+
return ieee754_csr.nan2008 ? 0 : S64_MAX;
278+
}
279+
280+
/*
281+
* Overflow integer value
282+
*/
283+
static inline int ieee754si_overflow(int xs)
284+
{
285+
return ieee754_csr.nan2008 && xs ? INT_MIN : INT_MAX;
286+
}
287+
288+
static inline s64 ieee754di_overflow(int xs)
289+
{
290+
return ieee754_csr.nan2008 && xs ? S64_MIN : S64_MAX;
275291
}
276292

277293
/* result types for xctx.rt */

‎arch/mips/math-emu/ieee754dp.c‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ static inline int ieee754dp_isnan(union ieee754dp x)
3737

3838
static inline int ieee754dp_issnan(union ieee754dp x)
3939
{
40+
int qbit;
41+
4042
assert(ieee754dp_isnan(x));
41-
return (DPMANT(x) & DP_MBIT(DP_FBITS - 1)) == DP_MBIT(DP_FBITS - 1);
43+
qbit = (DPMANT(x) & DP_MBIT(DP_FBITS - 1)) == DP_MBIT(DP_FBITS - 1);
44+
return ieee754_csr.nan2008 ^ qbit;
4245
}
4346

4447

@@ -51,7 +54,12 @@ union ieee754dp __cold ieee754dp_nanxcpt(union ieee754dp r)
5154
assert(ieee754dp_issnan(r));
5255

5356
ieee754_setcx(IEEE754_INVALID_OPERATION);
54-
return ieee754dp_indef();
57+
if (ieee754_csr.nan2008)
58+
DPMANT(r) |= DP_MBIT(DP_FBITS - 1);
59+
else
60+
r = ieee754dp_indef();
61+
62+
return r;
5563
}
5664

5765
static u64 ieee754dp_get_rounding(int sn, u64 xm)

‎arch/mips/math-emu/ieee754int.h‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ static inline int ieee754_class_nan(int xc)
6363
if (ve == SP_EMAX+1+SP_EBIAS) { \
6464
if (vm == 0) \
6565
vc = IEEE754_CLASS_INF; \
66-
else if (vm & SP_MBIT(SP_FBITS-1)) \
67-
vc = IEEE754_CLASS_SNAN; \
68-
else \
66+
else if (ieee754_csr.nan2008 ^ !(vm & SP_MBIT(SP_FBITS - 1))) \
6967
vc = IEEE754_CLASS_QNAN; \
68+
else \
69+
vc = IEEE754_CLASS_SNAN; \
7070
} else if (ve == SP_EMIN-1+SP_EBIAS) { \
7171
if (vm) { \
7272
ve = SP_EMIN; \
@@ -97,10 +97,10 @@ static inline int ieee754_class_nan(int xc)
9797
if (ve == DP_EMAX+1+DP_EBIAS) { \
9898
if (vm == 0) \
9999
vc = IEEE754_CLASS_INF; \
100-
else if (vm & DP_MBIT(DP_FBITS-1)) \
101-
vc = IEEE754_CLASS_SNAN; \
102-
else \
100+
else if (ieee754_csr.nan2008 ^ !(vm & DP_MBIT(DP_FBITS - 1))) \
103101
vc = IEEE754_CLASS_QNAN; \
102+
else \
103+
vc = IEEE754_CLASS_SNAN; \
104104
} else if (ve == DP_EMIN-1+DP_EBIAS) { \
105105
if (vm) { \
106106
ve = DP_EMIN; \

‎arch/mips/math-emu/ieee754sp.c‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ static inline int ieee754sp_isnan(union ieee754sp x)
3737

3838
static inline int ieee754sp_issnan(union ieee754sp x)
3939
{
40+
int qbit;
41+
4042
assert(ieee754sp_isnan(x));
41-
return SPMANT(x) & SP_MBIT(SP_FBITS - 1);
43+
qbit = (SPMANT(x) & SP_MBIT(SP_FBITS - 1)) == SP_MBIT(SP_FBITS - 1);
44+
return ieee754_csr.nan2008 ^ qbit;
4245
}
4346

4447

@@ -51,7 +54,12 @@ union ieee754sp __cold ieee754sp_nanxcpt(union ieee754sp r)
5154
assert(ieee754sp_issnan(r));
5255

5356
ieee754_setcx(IEEE754_INVALID_OPERATION);
54-
return ieee754sp_indef();
57+
if (ieee754_csr.nan2008)
58+
SPMANT(r) |= SP_MBIT(SP_FBITS - 1);
59+
else
60+
r = ieee754sp_indef();
61+
62+
return r;
5563
}
5664

5765
static unsigned ieee754sp_get_rounding(int sn, unsigned xm)

‎arch/mips/math-emu/sp_fdp.c‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@ union ieee754sp ieee754sp_fdp(union ieee754dp x)
4444

4545
switch (xc) {
4646
case IEEE754_CLASS_SNAN:
47-
return ieee754sp_nanxcpt(ieee754sp_nan_fdp(xs, xm));
48-
47+
x = ieee754dp_nanxcpt(x);
48+
EXPLODEXDP;
49+
/* Fall through. */
4950
case IEEE754_CLASS_QNAN:
5051
y = ieee754sp_nan_fdp(xs, xm);
51-
EXPLODEYSP;
52-
if (!ieee754_class_nan(yc))
53-
y = ieee754sp_indef();
52+
if (!ieee754_csr.nan2008) {
53+
EXPLODEYSP;
54+
if (!ieee754_class_nan(yc))
55+
y = ieee754sp_indef();
56+
}
5457
return y;
5558

5659
case IEEE754_CLASS_INF:

‎arch/mips/math-emu/sp_tint.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,13 @@ int ieee754sp_tint(union ieee754sp x)
3838
switch (xc) {
3939
case IEEE754_CLASS_SNAN:
4040
case IEEE754_CLASS_QNAN:
41-
case IEEE754_CLASS_INF:
4241
ieee754_setcx(IEEE754_INVALID_OPERATION);
4342
return ieee754si_indef();
4443

44+
case IEEE754_CLASS_INF:
45+
ieee754_setcx(IEEE754_INVALID_OPERATION);
46+
return ieee754si_overflow(xs);
47+
4548
case IEEE754_CLASS_ZERO:
4649
return 0;
4750

@@ -56,7 +59,7 @@ int ieee754sp_tint(union ieee754sp x)
5659
/* Set invalid. We will only use overflow for floating
5760
point overflow */
5861
ieee754_setcx(IEEE754_INVALID_OPERATION);
59-
return ieee754si_indef();
62+
return ieee754si_overflow(xs);
6063
}
6164
/* oh gawd */
6265
if (xe > SP_FBITS) {
@@ -97,7 +100,7 @@ int ieee754sp_tint(union ieee754sp x)
97100
if ((xm >> 31) != 0) {
98101
/* This can happen after rounding */
99102
ieee754_setcx(IEEE754_INVALID_OPERATION);
100-
return ieee754si_indef();
103+
return ieee754si_overflow(xs);
101104
}
102105
if (round || sticky)
103106
ieee754_setcx(IEEE754_INEXACT);

0 commit comments

Comments
 (0)