1 // SPDX-License-Identifier: GPL-2.0-only
2 /* IEEE754 floating point arithmetic
6 * MIPS floating point support
7 * Copyright (C) 1994-2000 Algorithmics Ltd.
10 #include "ieee754sp.h"
12 union ieee754sp
ieee754sp_mul(union ieee754sp x
, union ieee754sp y
)
37 switch (CLPAIR(xc
, yc
)) {
38 case CLPAIR(IEEE754_CLASS_QNAN
, IEEE754_CLASS_SNAN
):
39 case CLPAIR(IEEE754_CLASS_ZERO
, IEEE754_CLASS_SNAN
):
40 case CLPAIR(IEEE754_CLASS_NORM
, IEEE754_CLASS_SNAN
):
41 case CLPAIR(IEEE754_CLASS_DNORM
, IEEE754_CLASS_SNAN
):
42 case CLPAIR(IEEE754_CLASS_INF
, IEEE754_CLASS_SNAN
):
43 return ieee754sp_nanxcpt(y
);
45 case CLPAIR(IEEE754_CLASS_SNAN
, IEEE754_CLASS_SNAN
):
46 case CLPAIR(IEEE754_CLASS_SNAN
, IEEE754_CLASS_QNAN
):
47 case CLPAIR(IEEE754_CLASS_SNAN
, IEEE754_CLASS_ZERO
):
48 case CLPAIR(IEEE754_CLASS_SNAN
, IEEE754_CLASS_NORM
):
49 case CLPAIR(IEEE754_CLASS_SNAN
, IEEE754_CLASS_DNORM
):
50 case CLPAIR(IEEE754_CLASS_SNAN
, IEEE754_CLASS_INF
):
51 return ieee754sp_nanxcpt(x
);
53 case CLPAIR(IEEE754_CLASS_ZERO
, IEEE754_CLASS_QNAN
):
54 case CLPAIR(IEEE754_CLASS_NORM
, IEEE754_CLASS_QNAN
):
55 case CLPAIR(IEEE754_CLASS_DNORM
, IEEE754_CLASS_QNAN
):
56 case CLPAIR(IEEE754_CLASS_INF
, IEEE754_CLASS_QNAN
):
59 case CLPAIR(IEEE754_CLASS_QNAN
, IEEE754_CLASS_QNAN
):
60 case CLPAIR(IEEE754_CLASS_QNAN
, IEEE754_CLASS_ZERO
):
61 case CLPAIR(IEEE754_CLASS_QNAN
, IEEE754_CLASS_NORM
):
62 case CLPAIR(IEEE754_CLASS_QNAN
, IEEE754_CLASS_DNORM
):
63 case CLPAIR(IEEE754_CLASS_QNAN
, IEEE754_CLASS_INF
):
70 case CLPAIR(IEEE754_CLASS_INF
, IEEE754_CLASS_ZERO
):
71 case CLPAIR(IEEE754_CLASS_ZERO
, IEEE754_CLASS_INF
):
72 ieee754_setcx(IEEE754_INVALID_OPERATION
);
73 return ieee754sp_indef();
75 case CLPAIR(IEEE754_CLASS_NORM
, IEEE754_CLASS_INF
):
76 case CLPAIR(IEEE754_CLASS_DNORM
, IEEE754_CLASS_INF
):
77 case CLPAIR(IEEE754_CLASS_INF
, IEEE754_CLASS_NORM
):
78 case CLPAIR(IEEE754_CLASS_INF
, IEEE754_CLASS_DNORM
):
79 case CLPAIR(IEEE754_CLASS_INF
, IEEE754_CLASS_INF
):
80 return ieee754sp_inf(xs
^ ys
);
82 case CLPAIR(IEEE754_CLASS_ZERO
, IEEE754_CLASS_ZERO
):
83 case CLPAIR(IEEE754_CLASS_ZERO
, IEEE754_CLASS_NORM
):
84 case CLPAIR(IEEE754_CLASS_ZERO
, IEEE754_CLASS_DNORM
):
85 case CLPAIR(IEEE754_CLASS_NORM
, IEEE754_CLASS_ZERO
):
86 case CLPAIR(IEEE754_CLASS_DNORM
, IEEE754_CLASS_ZERO
):
87 return ieee754sp_zero(xs
^ ys
);
90 case CLPAIR(IEEE754_CLASS_DNORM
, IEEE754_CLASS_DNORM
):
94 case CLPAIR(IEEE754_CLASS_NORM
, IEEE754_CLASS_DNORM
):
98 case CLPAIR(IEEE754_CLASS_DNORM
, IEEE754_CLASS_NORM
):
102 case CLPAIR(IEEE754_CLASS_NORM
, IEEE754_CLASS_NORM
):
105 /* rm = xm * ym, re = xe+ye basically */
106 assert(xm
& SP_HIDDEN_BIT
);
107 assert(ym
& SP_HIDDEN_BIT
);
112 /* shunt to top of word */
113 xm
<<= 32 - (SP_FBITS
+ 1);
114 ym
<<= 32 - (SP_FBITS
+ 1);
117 * Multiply 32 bits xm, ym to give high 32 bits rm with stickness.
124 lrm
= lxm
* lym
; /* 16 * 16 => 32 */
125 hrm
= hxm
* hym
; /* 16 * 16 => 32 */
127 t
= lxm
* hym
; /* 16 * 16 => 32 */
128 at
= lrm
+ (t
<< 16);
131 hrm
= hrm
+ (t
>> 16);
133 t
= hxm
* lym
; /* 16 * 16 => 32 */
134 at
= lrm
+ (t
<< 16);
137 hrm
= hrm
+ (t
>> 16);
139 rm
= hrm
| (lrm
!= 0);
142 * Sticky shift down to normal rounding precision.
145 rm
= (rm
>> (32 - (SP_FBITS
+ 1 + 3))) |
146 ((rm
<< (SP_FBITS
+ 1 + 3)) != 0);
149 rm
= (rm
>> (32 - (SP_FBITS
+ 1 + 3 + 1))) |
150 ((rm
<< (SP_FBITS
+ 1 + 3 + 1)) != 0);
152 assert(rm
& (SP_HIDDEN_BIT
<< 3));
154 return ieee754sp_format(rs
, re
, rm
);