Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * MIPS floating point support | |
3 | * Copyright (C) 1994-2000 Algorithmics Ltd. | |
1da177e4 | 4 | * |
1da177e4 LT |
5 | * This program is free software; you can distribute it and/or modify it |
6 | * under the terms of the GNU General Public License (Version 2) as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope it will be useful, but WITHOUT | |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 | * for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License along | |
15 | * with this program; if not, write to the Free Software Foundation, Inc., | |
3f7cac41 | 16 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
1da177e4 | 17 | * |
1da177e4 LT |
18 | * Nov 7, 2000 |
19 | * Modification to allow integration with Linux kernel | |
20 | * | |
21 | * Kevin D. Kissell, kevink@mips.com and Carsten Langgard, carstenl@mips.com | |
22 | * Copyright (C) 2000 MIPS Technologies, Inc. All rights reserved. | |
cd21dfcf | 23 | */ |
a663bf90 RB |
24 | #ifndef __ARCH_MIPS_MATH_EMU_IEEE754_H |
25 | #define __ARCH_MIPS_MATH_EMU_IEEE754_H | |
1da177e4 | 26 | |
cae55066 | 27 | #include <linux/compiler.h> |
cd21dfcf | 28 | #include <asm/byteorder.h> |
e812a739 | 29 | #include <linux/kernel.h> |
1da177e4 | 30 | #include <linux/types.h> |
cd21dfcf | 31 | #include <linux/sched.h> |
f80cc08d | 32 | #include <asm/bitfield.h> |
1da177e4 | 33 | |
2209bcb1 | 34 | union ieee754dp { |
1da177e4 | 35 | struct { |
f80cc08d RB |
36 | __BITFIELD_FIELD(unsigned int sign:1, |
37 | __BITFIELD_FIELD(unsigned int bexp:11, | |
38 | __BITFIELD_FIELD(u64 mant:52, | |
39 | ;))) | |
49548b09 | 40 | }; |
1da177e4 | 41 | u64 bits; |
2209bcb1 | 42 | }; |
1da177e4 | 43 | |
2209bcb1 | 44 | union ieee754sp { |
49548b09 RB |
45 | struct { |
46 | __BITFIELD_FIELD(unsigned sign:1, | |
47 | __BITFIELD_FIELD(unsigned bexp:8, | |
48 | __BITFIELD_FIELD(unsigned mant:23, | |
49 | ;))) | |
50 | }; | |
1da177e4 | 51 | u32 bits; |
2209bcb1 | 52 | }; |
1da177e4 LT |
53 | |
54 | /* | |
55 | * single precision (often aka float) | |
56 | */ | |
2209bcb1 | 57 | int ieee754sp_class(union ieee754sp x); |
1da177e4 | 58 | |
2209bcb1 RB |
59 | union ieee754sp ieee754sp_abs(union ieee754sp x); |
60 | union ieee754sp ieee754sp_neg(union ieee754sp x); | |
1da177e4 | 61 | |
2209bcb1 RB |
62 | union ieee754sp ieee754sp_add(union ieee754sp x, union ieee754sp y); |
63 | union ieee754sp ieee754sp_sub(union ieee754sp x, union ieee754sp y); | |
64 | union ieee754sp ieee754sp_mul(union ieee754sp x, union ieee754sp y); | |
65 | union ieee754sp ieee754sp_div(union ieee754sp x, union ieee754sp y); | |
1da177e4 | 66 | |
2209bcb1 | 67 | union ieee754sp ieee754sp_fint(int x); |
2209bcb1 | 68 | union ieee754sp ieee754sp_flong(s64 x); |
2209bcb1 | 69 | union ieee754sp ieee754sp_fdp(union ieee754dp x); |
1da177e4 | 70 | |
2209bcb1 | 71 | int ieee754sp_tint(union ieee754sp x); |
2209bcb1 | 72 | s64 ieee754sp_tlong(union ieee754sp x); |
1da177e4 | 73 | |
2209bcb1 | 74 | int ieee754sp_cmp(union ieee754sp x, union ieee754sp y, int cop, int sig); |
1da177e4 | 75 | |
2209bcb1 | 76 | union ieee754sp ieee754sp_sqrt(union ieee754sp x); |
1da177e4 LT |
77 | |
78 | /* | |
79 | * double precision (often aka double) | |
80 | */ | |
2209bcb1 | 81 | int ieee754dp_class(union ieee754dp x); |
1da177e4 | 82 | |
2209bcb1 RB |
83 | union ieee754dp ieee754dp_add(union ieee754dp x, union ieee754dp y); |
84 | union ieee754dp ieee754dp_sub(union ieee754dp x, union ieee754dp y); | |
85 | union ieee754dp ieee754dp_mul(union ieee754dp x, union ieee754dp y); | |
86 | union ieee754dp ieee754dp_div(union ieee754dp x, union ieee754dp y); | |
1da177e4 | 87 | |
2209bcb1 RB |
88 | union ieee754dp ieee754dp_abs(union ieee754dp x); |
89 | union ieee754dp ieee754dp_neg(union ieee754dp x); | |
1da177e4 | 90 | |
2209bcb1 | 91 | union ieee754dp ieee754dp_fint(int x); |
2209bcb1 | 92 | union ieee754dp ieee754dp_flong(s64 x); |
2209bcb1 | 93 | union ieee754dp ieee754dp_fsp(union ieee754sp x); |
1da177e4 | 94 | |
2209bcb1 | 95 | int ieee754dp_tint(union ieee754dp x); |
2209bcb1 | 96 | s64 ieee754dp_tlong(union ieee754dp x); |
1da177e4 | 97 | |
2209bcb1 | 98 | int ieee754dp_cmp(union ieee754dp x, union ieee754dp y, int cop, int sig); |
1da177e4 | 99 | |
2209bcb1 | 100 | union ieee754dp ieee754dp_sqrt(union ieee754dp x); |
1da177e4 LT |
101 | |
102 | ||
103 | ||
104 | /* 5 types of floating point number | |
105 | */ | |
9e8bad1f RB |
106 | enum { |
107 | IEEE754_CLASS_NORM = 0x00, | |
108 | IEEE754_CLASS_ZERO = 0x01, | |
109 | IEEE754_CLASS_DNORM = 0x02, | |
110 | IEEE754_CLASS_INF = 0x03, | |
111 | IEEE754_CLASS_SNAN = 0x04, | |
112 | IEEE754_CLASS_QNAN = 0x05, | |
113 | }; | |
1da177e4 LT |
114 | |
115 | /* exception numbers */ | |
116 | #define IEEE754_INEXACT 0x01 | |
117 | #define IEEE754_UNDERFLOW 0x02 | |
118 | #define IEEE754_OVERFLOW 0x04 | |
119 | #define IEEE754_ZERO_DIVIDE 0x08 | |
120 | #define IEEE754_INVALID_OPERATION 0x10 | |
121 | ||
122 | /* cmp operators | |
123 | */ | |
124 | #define IEEE754_CLT 0x01 | |
125 | #define IEEE754_CEQ 0x02 | |
126 | #define IEEE754_CGT 0x04 | |
127 | #define IEEE754_CUN 0x08 | |
128 | ||
129 | /* rounding mode | |
130 | */ | |
131 | #define IEEE754_RN 0 /* round to nearest */ | |
132 | #define IEEE754_RZ 1 /* round toward zero */ | |
133 | #define IEEE754_RD 2 /* round toward -Infinity */ | |
134 | #define IEEE754_RU 3 /* round toward +Infinity */ | |
135 | ||
136 | /* other naming */ | |
137 | #define IEEE754_RM IEEE754_RD | |
138 | #define IEEE754_RP IEEE754_RU | |
139 | ||
140 | /* "normal" comparisons | |
141 | */ | |
2209bcb1 | 142 | static inline int ieee754sp_eq(union ieee754sp x, union ieee754sp y) |
1da177e4 LT |
143 | { |
144 | return ieee754sp_cmp(x, y, IEEE754_CEQ, 0); | |
145 | } | |
146 | ||
2209bcb1 | 147 | static inline int ieee754sp_ne(union ieee754sp x, union ieee754sp y) |
1da177e4 LT |
148 | { |
149 | return ieee754sp_cmp(x, y, | |
150 | IEEE754_CLT | IEEE754_CGT | IEEE754_CUN, 0); | |
151 | } | |
152 | ||
2209bcb1 | 153 | static inline int ieee754sp_lt(union ieee754sp x, union ieee754sp y) |
1da177e4 LT |
154 | { |
155 | return ieee754sp_cmp(x, y, IEEE754_CLT, 0); | |
156 | } | |
157 | ||
2209bcb1 | 158 | static inline int ieee754sp_le(union ieee754sp x, union ieee754sp y) |
1da177e4 LT |
159 | { |
160 | return ieee754sp_cmp(x, y, IEEE754_CLT | IEEE754_CEQ, 0); | |
161 | } | |
162 | ||
2209bcb1 | 163 | static inline int ieee754sp_gt(union ieee754sp x, union ieee754sp y) |
1da177e4 LT |
164 | { |
165 | return ieee754sp_cmp(x, y, IEEE754_CGT, 0); | |
166 | } | |
167 | ||
168 | ||
2209bcb1 | 169 | static inline int ieee754sp_ge(union ieee754sp x, union ieee754sp y) |
1da177e4 LT |
170 | { |
171 | return ieee754sp_cmp(x, y, IEEE754_CGT | IEEE754_CEQ, 0); | |
172 | } | |
173 | ||
2209bcb1 | 174 | static inline int ieee754dp_eq(union ieee754dp x, union ieee754dp y) |
1da177e4 LT |
175 | { |
176 | return ieee754dp_cmp(x, y, IEEE754_CEQ, 0); | |
177 | } | |
178 | ||
2209bcb1 | 179 | static inline int ieee754dp_ne(union ieee754dp x, union ieee754dp y) |
1da177e4 LT |
180 | { |
181 | return ieee754dp_cmp(x, y, | |
182 | IEEE754_CLT | IEEE754_CGT | IEEE754_CUN, 0); | |
183 | } | |
184 | ||
2209bcb1 | 185 | static inline int ieee754dp_lt(union ieee754dp x, union ieee754dp y) |
1da177e4 LT |
186 | { |
187 | return ieee754dp_cmp(x, y, IEEE754_CLT, 0); | |
188 | } | |
189 | ||
2209bcb1 | 190 | static inline int ieee754dp_le(union ieee754dp x, union ieee754dp y) |
1da177e4 LT |
191 | { |
192 | return ieee754dp_cmp(x, y, IEEE754_CLT | IEEE754_CEQ, 0); | |
193 | } | |
194 | ||
2209bcb1 | 195 | static inline int ieee754dp_gt(union ieee754dp x, union ieee754dp y) |
1da177e4 LT |
196 | { |
197 | return ieee754dp_cmp(x, y, IEEE754_CGT, 0); | |
198 | } | |
199 | ||
2209bcb1 | 200 | static inline int ieee754dp_ge(union ieee754dp x, union ieee754dp y) |
1da177e4 LT |
201 | { |
202 | return ieee754dp_cmp(x, y, IEEE754_CGT | IEEE754_CEQ, 0); | |
203 | } | |
204 | ||
cd21dfcf RB |
205 | /* |
206 | * The control status register | |
207 | */ | |
208 | struct _ieee754_csr { | |
f80cc08d RB |
209 | __BITFIELD_FIELD(unsigned pad0:7, |
210 | __BITFIELD_FIELD(unsigned nod:1, /* set 1 for no denormalised numbers */ | |
211 | __BITFIELD_FIELD(unsigned c:1, /* condition */ | |
212 | __BITFIELD_FIELD(unsigned pad1:5, | |
213 | __BITFIELD_FIELD(unsigned cx:6, /* exceptions this operation */ | |
214 | __BITFIELD_FIELD(unsigned mx:5, /* exception enable mask */ | |
215 | __BITFIELD_FIELD(unsigned sx:5, /* exceptions total */ | |
216 | __BITFIELD_FIELD(unsigned rm:2, /* current rounding mode */ | |
217 | ;)))))))) | |
1da177e4 | 218 | }; |
eae89076 | 219 | #define ieee754_csr (*(struct _ieee754_csr *)(¤t->thread.fpu.fcr31)) |
1da177e4 | 220 | |
cd21dfcf | 221 | static inline unsigned ieee754_getrm(void) |
1da177e4 LT |
222 | { |
223 | return (ieee754_csr.rm); | |
224 | } | |
cd21dfcf | 225 | static inline unsigned ieee754_setrm(unsigned rm) |
1da177e4 LT |
226 | { |
227 | return (ieee754_csr.rm = rm); | |
228 | } | |
229 | ||
230 | /* | |
231 | * get current exceptions | |
232 | */ | |
cd21dfcf | 233 | static inline unsigned ieee754_getcx(void) |
1da177e4 LT |
234 | { |
235 | return (ieee754_csr.cx); | |
236 | } | |
237 | ||
238 | /* test for current exception condition | |
239 | */ | |
cd21dfcf | 240 | static inline int ieee754_cxtest(unsigned n) |
1da177e4 LT |
241 | { |
242 | return (ieee754_csr.cx & n); | |
243 | } | |
244 | ||
245 | /* | |
246 | * get sticky exceptions | |
247 | */ | |
cd21dfcf | 248 | static inline unsigned ieee754_getsx(void) |
1da177e4 LT |
249 | { |
250 | return (ieee754_csr.sx); | |
251 | } | |
252 | ||
253 | /* clear sticky conditions | |
254 | */ | |
cd21dfcf | 255 | static inline unsigned ieee754_clrsx(void) |
1da177e4 LT |
256 | { |
257 | return (ieee754_csr.sx = 0); | |
258 | } | |
259 | ||
260 | /* test for sticky exception condition | |
261 | */ | |
cd21dfcf | 262 | static inline int ieee754_sxtest(unsigned n) |
1da177e4 LT |
263 | { |
264 | return (ieee754_csr.sx & n); | |
265 | } | |
266 | ||
267 | /* debugging */ | |
2209bcb1 RB |
268 | union ieee754sp ieee754sp_dump(char *s, union ieee754sp x); |
269 | union ieee754dp ieee754dp_dump(char *s, union ieee754dp x); | |
1da177e4 LT |
270 | |
271 | #define IEEE754_SPCVAL_PZERO 0 | |
272 | #define IEEE754_SPCVAL_NZERO 1 | |
273 | #define IEEE754_SPCVAL_PONE 2 | |
274 | #define IEEE754_SPCVAL_NONE 3 | |
275 | #define IEEE754_SPCVAL_PTEN 4 | |
276 | #define IEEE754_SPCVAL_NTEN 5 | |
277 | #define IEEE754_SPCVAL_PINFINITY 6 | |
278 | #define IEEE754_SPCVAL_NINFINITY 7 | |
279 | #define IEEE754_SPCVAL_INDEF 8 | |
280 | #define IEEE754_SPCVAL_PMAX 9 /* +max norm */ | |
281 | #define IEEE754_SPCVAL_NMAX 10 /* -max norm */ | |
282 | #define IEEE754_SPCVAL_PMIN 11 /* +min norm */ | |
283 | #define IEEE754_SPCVAL_NMIN 12 /* +min norm */ | |
284 | #define IEEE754_SPCVAL_PMIND 13 /* +min denorm */ | |
285 | #define IEEE754_SPCVAL_NMIND 14 /* +min denorm */ | |
286 | #define IEEE754_SPCVAL_P1E31 15 /* + 1.0e31 */ | |
287 | #define IEEE754_SPCVAL_P1E63 16 /* + 1.0e63 */ | |
288 | ||
49548b09 RB |
289 | extern const union ieee754dp __ieee754dp_spcvals[]; |
290 | extern const union ieee754sp __ieee754sp_spcvals[]; | |
2209bcb1 RB |
291 | #define ieee754dp_spcvals ((const union ieee754dp *)__ieee754dp_spcvals) |
292 | #define ieee754sp_spcvals ((const union ieee754sp *)__ieee754sp_spcvals) | |
1da177e4 | 293 | |
cd21dfcf RB |
294 | /* |
295 | * Return infinity with given sign | |
296 | */ | |
297 | #define ieee754dp_inf(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PINFINITY+(sn)]) | |
298 | #define ieee754dp_zero(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PZERO+(sn)]) | |
299 | #define ieee754dp_one(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PONE+(sn)]) | |
300 | #define ieee754dp_ten(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PTEN+(sn)]) | |
301 | #define ieee754dp_indef() (ieee754dp_spcvals[IEEE754_SPCVAL_INDEF]) | |
302 | #define ieee754dp_max(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PMAX+(sn)]) | |
303 | #define ieee754dp_min(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PMIN+(sn)]) | |
304 | #define ieee754dp_mind(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PMIND+(sn)]) | |
305 | #define ieee754dp_1e31() (ieee754dp_spcvals[IEEE754_SPCVAL_P1E31]) | |
306 | #define ieee754dp_1e63() (ieee754dp_spcvals[IEEE754_SPCVAL_P1E63]) | |
307 | ||
308 | #define ieee754sp_inf(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PINFINITY+(sn)]) | |
309 | #define ieee754sp_zero(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PZERO+(sn)]) | |
310 | #define ieee754sp_one(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PONE+(sn)]) | |
311 | #define ieee754sp_ten(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PTEN+(sn)]) | |
312 | #define ieee754sp_indef() (ieee754sp_spcvals[IEEE754_SPCVAL_INDEF]) | |
313 | #define ieee754sp_max(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PMAX+(sn)]) | |
314 | #define ieee754sp_min(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PMIN+(sn)]) | |
315 | #define ieee754sp_mind(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PMIND+(sn)]) | |
316 | #define ieee754sp_1e31() (ieee754sp_spcvals[IEEE754_SPCVAL_P1E31]) | |
317 | #define ieee754sp_1e63() (ieee754sp_spcvals[IEEE754_SPCVAL_P1E63]) | |
318 | ||
319 | /* | |
320 | * Indefinite integer value | |
321 | */ | |
e812a739 RB |
322 | static inline int ieee754si_indef(void) |
323 | { | |
324 | return INT_MAX; | |
325 | } | |
326 | ||
327 | static inline s64 ieee754di_indef(void) | |
328 | { | |
329 | return S64_MAX; | |
330 | } | |
1da177e4 | 331 | |
1da177e4 LT |
332 | /* result types for xctx.rt */ |
333 | #define IEEE754_RT_SP 0 | |
334 | #define IEEE754_RT_DP 1 | |
335 | #define IEEE754_RT_XP 2 | |
336 | #define IEEE754_RT_SI 3 | |
337 | #define IEEE754_RT_DI 4 | |
338 | ||
1da177e4 LT |
339 | /* compat */ |
340 | #define ieee754dp_fix(x) ieee754dp_tint(x) | |
341 | #define ieee754sp_fix(x) ieee754sp_tint(x) | |
a663bf90 RB |
342 | |
343 | #endif /* __ARCH_MIPS_MATH_EMU_IEEE754_H */ |