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., | |
16 | * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA. | |
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 | |
23708818 | 34 | struct ieee754dp_const { |
f80cc08d RB |
35 | __BITFIELD_FIELD(unsigned sign:1, |
36 | __BITFIELD_FIELD(unsigned bexp:11, | |
37 | __BITFIELD_FIELD(unsigned manthi:20, | |
38 | __BITFIELD_FIELD(unsigned mantlo:32, | |
39 | ;)))) | |
1da177e4 LT |
40 | }; |
41 | ||
2209bcb1 | 42 | union ieee754dp { |
23708818 | 43 | struct ieee754dp_const oparts; |
1da177e4 | 44 | struct { |
f80cc08d RB |
45 | __BITFIELD_FIELD(unsigned int sign:1, |
46 | __BITFIELD_FIELD(unsigned int bexp:11, | |
47 | __BITFIELD_FIELD(u64 mant:52, | |
48 | ;))) | |
1da177e4 LT |
49 | } parts; |
50 | double d; | |
51 | u64 bits; | |
2209bcb1 | 52 | }; |
1da177e4 | 53 | |
23708818 | 54 | struct ieee754sp_const { |
f80cc08d RB |
55 | __BITFIELD_FIELD(unsigned sign:1, |
56 | __BITFIELD_FIELD(unsigned bexp:8, | |
57 | __BITFIELD_FIELD(unsigned mant:23, | |
58 | ;))) | |
1da177e4 LT |
59 | }; |
60 | ||
2209bcb1 | 61 | union ieee754sp { |
23708818 | 62 | struct ieee754sp_const parts; |
1da177e4 LT |
63 | float f; |
64 | u32 bits; | |
2209bcb1 | 65 | }; |
1da177e4 LT |
66 | |
67 | /* | |
68 | * single precision (often aka float) | |
69 | */ | |
2209bcb1 | 70 | int ieee754sp_class(union ieee754sp x); |
1da177e4 | 71 | |
2209bcb1 RB |
72 | union ieee754sp ieee754sp_abs(union ieee754sp x); |
73 | union ieee754sp ieee754sp_neg(union ieee754sp x); | |
1da177e4 | 74 | |
2209bcb1 RB |
75 | union ieee754sp ieee754sp_add(union ieee754sp x, union ieee754sp y); |
76 | union ieee754sp ieee754sp_sub(union ieee754sp x, union ieee754sp y); | |
77 | union ieee754sp ieee754sp_mul(union ieee754sp x, union ieee754sp y); | |
78 | union ieee754sp ieee754sp_div(union ieee754sp x, union ieee754sp y); | |
1da177e4 | 79 | |
2209bcb1 | 80 | union ieee754sp ieee754sp_fint(int x); |
2209bcb1 | 81 | union ieee754sp ieee754sp_flong(s64 x); |
2209bcb1 | 82 | union ieee754sp ieee754sp_fdp(union ieee754dp x); |
1da177e4 | 83 | |
2209bcb1 | 84 | int ieee754sp_tint(union ieee754sp x); |
2209bcb1 | 85 | s64 ieee754sp_tlong(union ieee754sp x); |
1da177e4 | 86 | |
2209bcb1 | 87 | int ieee754sp_cmp(union ieee754sp x, union ieee754sp y, int cop, int sig); |
1da177e4 | 88 | |
2209bcb1 | 89 | union ieee754sp ieee754sp_sqrt(union ieee754sp x); |
1da177e4 LT |
90 | |
91 | /* | |
92 | * double precision (often aka double) | |
93 | */ | |
2209bcb1 | 94 | int ieee754dp_class(union ieee754dp x); |
1da177e4 | 95 | |
2209bcb1 RB |
96 | union ieee754dp ieee754dp_add(union ieee754dp x, union ieee754dp y); |
97 | union ieee754dp ieee754dp_sub(union ieee754dp x, union ieee754dp y); | |
98 | union ieee754dp ieee754dp_mul(union ieee754dp x, union ieee754dp y); | |
99 | union ieee754dp ieee754dp_div(union ieee754dp x, union ieee754dp y); | |
1da177e4 | 100 | |
2209bcb1 RB |
101 | union ieee754dp ieee754dp_abs(union ieee754dp x); |
102 | union ieee754dp ieee754dp_neg(union ieee754dp x); | |
1da177e4 | 103 | |
2209bcb1 | 104 | union ieee754dp ieee754dp_fint(int x); |
2209bcb1 | 105 | union ieee754dp ieee754dp_flong(s64 x); |
2209bcb1 | 106 | union ieee754dp ieee754dp_fsp(union ieee754sp x); |
1da177e4 | 107 | |
2209bcb1 | 108 | int ieee754dp_tint(union ieee754dp x); |
2209bcb1 | 109 | s64 ieee754dp_tlong(union ieee754dp x); |
1da177e4 | 110 | |
2209bcb1 | 111 | int ieee754dp_cmp(union ieee754dp x, union ieee754dp y, int cop, int sig); |
1da177e4 | 112 | |
2209bcb1 | 113 | union ieee754dp ieee754dp_sqrt(union ieee754dp x); |
1da177e4 LT |
114 | |
115 | ||
116 | ||
117 | /* 5 types of floating point number | |
118 | */ | |
9e8bad1f RB |
119 | enum { |
120 | IEEE754_CLASS_NORM = 0x00, | |
121 | IEEE754_CLASS_ZERO = 0x01, | |
122 | IEEE754_CLASS_DNORM = 0x02, | |
123 | IEEE754_CLASS_INF = 0x03, | |
124 | IEEE754_CLASS_SNAN = 0x04, | |
125 | IEEE754_CLASS_QNAN = 0x05, | |
126 | }; | |
1da177e4 LT |
127 | |
128 | /* exception numbers */ | |
129 | #define IEEE754_INEXACT 0x01 | |
130 | #define IEEE754_UNDERFLOW 0x02 | |
131 | #define IEEE754_OVERFLOW 0x04 | |
132 | #define IEEE754_ZERO_DIVIDE 0x08 | |
133 | #define IEEE754_INVALID_OPERATION 0x10 | |
134 | ||
135 | /* cmp operators | |
136 | */ | |
137 | #define IEEE754_CLT 0x01 | |
138 | #define IEEE754_CEQ 0x02 | |
139 | #define IEEE754_CGT 0x04 | |
140 | #define IEEE754_CUN 0x08 | |
141 | ||
142 | /* rounding mode | |
143 | */ | |
144 | #define IEEE754_RN 0 /* round to nearest */ | |
145 | #define IEEE754_RZ 1 /* round toward zero */ | |
146 | #define IEEE754_RD 2 /* round toward -Infinity */ | |
147 | #define IEEE754_RU 3 /* round toward +Infinity */ | |
148 | ||
149 | /* other naming */ | |
150 | #define IEEE754_RM IEEE754_RD | |
151 | #define IEEE754_RP IEEE754_RU | |
152 | ||
153 | /* "normal" comparisons | |
154 | */ | |
2209bcb1 | 155 | static inline int ieee754sp_eq(union ieee754sp x, union ieee754sp y) |
1da177e4 LT |
156 | { |
157 | return ieee754sp_cmp(x, y, IEEE754_CEQ, 0); | |
158 | } | |
159 | ||
2209bcb1 | 160 | static inline int ieee754sp_ne(union ieee754sp x, union ieee754sp y) |
1da177e4 LT |
161 | { |
162 | return ieee754sp_cmp(x, y, | |
163 | IEEE754_CLT | IEEE754_CGT | IEEE754_CUN, 0); | |
164 | } | |
165 | ||
2209bcb1 | 166 | static inline int ieee754sp_lt(union ieee754sp x, union ieee754sp y) |
1da177e4 LT |
167 | { |
168 | return ieee754sp_cmp(x, y, IEEE754_CLT, 0); | |
169 | } | |
170 | ||
2209bcb1 | 171 | static inline int ieee754sp_le(union ieee754sp x, union ieee754sp y) |
1da177e4 LT |
172 | { |
173 | return ieee754sp_cmp(x, y, IEEE754_CLT | IEEE754_CEQ, 0); | |
174 | } | |
175 | ||
2209bcb1 | 176 | static inline int ieee754sp_gt(union ieee754sp x, union ieee754sp y) |
1da177e4 LT |
177 | { |
178 | return ieee754sp_cmp(x, y, IEEE754_CGT, 0); | |
179 | } | |
180 | ||
181 | ||
2209bcb1 | 182 | static inline int ieee754sp_ge(union ieee754sp x, union ieee754sp y) |
1da177e4 LT |
183 | { |
184 | return ieee754sp_cmp(x, y, IEEE754_CGT | IEEE754_CEQ, 0); | |
185 | } | |
186 | ||
2209bcb1 | 187 | static inline int ieee754dp_eq(union ieee754dp x, union ieee754dp y) |
1da177e4 LT |
188 | { |
189 | return ieee754dp_cmp(x, y, IEEE754_CEQ, 0); | |
190 | } | |
191 | ||
2209bcb1 | 192 | static inline int ieee754dp_ne(union ieee754dp x, union ieee754dp y) |
1da177e4 LT |
193 | { |
194 | return ieee754dp_cmp(x, y, | |
195 | IEEE754_CLT | IEEE754_CGT | IEEE754_CUN, 0); | |
196 | } | |
197 | ||
2209bcb1 | 198 | static inline int ieee754dp_lt(union ieee754dp x, union ieee754dp y) |
1da177e4 LT |
199 | { |
200 | return ieee754dp_cmp(x, y, IEEE754_CLT, 0); | |
201 | } | |
202 | ||
2209bcb1 | 203 | static inline int ieee754dp_le(union ieee754dp x, union ieee754dp y) |
1da177e4 LT |
204 | { |
205 | return ieee754dp_cmp(x, y, IEEE754_CLT | IEEE754_CEQ, 0); | |
206 | } | |
207 | ||
2209bcb1 | 208 | static inline int ieee754dp_gt(union ieee754dp x, union ieee754dp y) |
1da177e4 LT |
209 | { |
210 | return ieee754dp_cmp(x, y, IEEE754_CGT, 0); | |
211 | } | |
212 | ||
2209bcb1 | 213 | static inline int ieee754dp_ge(union ieee754dp x, union ieee754dp y) |
1da177e4 LT |
214 | { |
215 | return ieee754dp_cmp(x, y, IEEE754_CGT | IEEE754_CEQ, 0); | |
216 | } | |
217 | ||
cd21dfcf RB |
218 | /* |
219 | * The control status register | |
220 | */ | |
221 | struct _ieee754_csr { | |
f80cc08d RB |
222 | __BITFIELD_FIELD(unsigned pad0:7, |
223 | __BITFIELD_FIELD(unsigned nod:1, /* set 1 for no denormalised numbers */ | |
224 | __BITFIELD_FIELD(unsigned c:1, /* condition */ | |
225 | __BITFIELD_FIELD(unsigned pad1:5, | |
226 | __BITFIELD_FIELD(unsigned cx:6, /* exceptions this operation */ | |
227 | __BITFIELD_FIELD(unsigned mx:5, /* exception enable mask */ | |
228 | __BITFIELD_FIELD(unsigned sx:5, /* exceptions total */ | |
229 | __BITFIELD_FIELD(unsigned rm:2, /* current rounding mode */ | |
230 | ;)))))))) | |
1da177e4 | 231 | }; |
eae89076 | 232 | #define ieee754_csr (*(struct _ieee754_csr *)(¤t->thread.fpu.fcr31)) |
1da177e4 | 233 | |
cd21dfcf | 234 | static inline unsigned ieee754_getrm(void) |
1da177e4 LT |
235 | { |
236 | return (ieee754_csr.rm); | |
237 | } | |
cd21dfcf | 238 | static inline unsigned ieee754_setrm(unsigned rm) |
1da177e4 LT |
239 | { |
240 | return (ieee754_csr.rm = rm); | |
241 | } | |
242 | ||
243 | /* | |
244 | * get current exceptions | |
245 | */ | |
cd21dfcf | 246 | static inline unsigned ieee754_getcx(void) |
1da177e4 LT |
247 | { |
248 | return (ieee754_csr.cx); | |
249 | } | |
250 | ||
251 | /* test for current exception condition | |
252 | */ | |
cd21dfcf | 253 | static inline int ieee754_cxtest(unsigned n) |
1da177e4 LT |
254 | { |
255 | return (ieee754_csr.cx & n); | |
256 | } | |
257 | ||
258 | /* | |
259 | * get sticky exceptions | |
260 | */ | |
cd21dfcf | 261 | static inline unsigned ieee754_getsx(void) |
1da177e4 LT |
262 | { |
263 | return (ieee754_csr.sx); | |
264 | } | |
265 | ||
266 | /* clear sticky conditions | |
267 | */ | |
cd21dfcf | 268 | static inline unsigned ieee754_clrsx(void) |
1da177e4 LT |
269 | { |
270 | return (ieee754_csr.sx = 0); | |
271 | } | |
272 | ||
273 | /* test for sticky exception condition | |
274 | */ | |
cd21dfcf | 275 | static inline int ieee754_sxtest(unsigned n) |
1da177e4 LT |
276 | { |
277 | return (ieee754_csr.sx & n); | |
278 | } | |
279 | ||
280 | /* debugging */ | |
2209bcb1 RB |
281 | union ieee754sp ieee754sp_dump(char *s, union ieee754sp x); |
282 | union ieee754dp ieee754dp_dump(char *s, union ieee754dp x); | |
1da177e4 LT |
283 | |
284 | #define IEEE754_SPCVAL_PZERO 0 | |
285 | #define IEEE754_SPCVAL_NZERO 1 | |
286 | #define IEEE754_SPCVAL_PONE 2 | |
287 | #define IEEE754_SPCVAL_NONE 3 | |
288 | #define IEEE754_SPCVAL_PTEN 4 | |
289 | #define IEEE754_SPCVAL_NTEN 5 | |
290 | #define IEEE754_SPCVAL_PINFINITY 6 | |
291 | #define IEEE754_SPCVAL_NINFINITY 7 | |
292 | #define IEEE754_SPCVAL_INDEF 8 | |
293 | #define IEEE754_SPCVAL_PMAX 9 /* +max norm */ | |
294 | #define IEEE754_SPCVAL_NMAX 10 /* -max norm */ | |
295 | #define IEEE754_SPCVAL_PMIN 11 /* +min norm */ | |
296 | #define IEEE754_SPCVAL_NMIN 12 /* +min norm */ | |
297 | #define IEEE754_SPCVAL_PMIND 13 /* +min denorm */ | |
298 | #define IEEE754_SPCVAL_NMIND 14 /* +min denorm */ | |
299 | #define IEEE754_SPCVAL_P1E31 15 /* + 1.0e31 */ | |
300 | #define IEEE754_SPCVAL_P1E63 16 /* + 1.0e63 */ | |
301 | ||
23708818 RB |
302 | extern const struct ieee754dp_const __ieee754dp_spcvals[]; |
303 | extern const struct ieee754sp_const __ieee754sp_spcvals[]; | |
2209bcb1 RB |
304 | #define ieee754dp_spcvals ((const union ieee754dp *)__ieee754dp_spcvals) |
305 | #define ieee754sp_spcvals ((const union ieee754sp *)__ieee754sp_spcvals) | |
1da177e4 | 306 | |
cd21dfcf RB |
307 | /* |
308 | * Return infinity with given sign | |
309 | */ | |
310 | #define ieee754dp_inf(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PINFINITY+(sn)]) | |
311 | #define ieee754dp_zero(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PZERO+(sn)]) | |
312 | #define ieee754dp_one(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PONE+(sn)]) | |
313 | #define ieee754dp_ten(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PTEN+(sn)]) | |
314 | #define ieee754dp_indef() (ieee754dp_spcvals[IEEE754_SPCVAL_INDEF]) | |
315 | #define ieee754dp_max(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PMAX+(sn)]) | |
316 | #define ieee754dp_min(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PMIN+(sn)]) | |
317 | #define ieee754dp_mind(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PMIND+(sn)]) | |
318 | #define ieee754dp_1e31() (ieee754dp_spcvals[IEEE754_SPCVAL_P1E31]) | |
319 | #define ieee754dp_1e63() (ieee754dp_spcvals[IEEE754_SPCVAL_P1E63]) | |
320 | ||
321 | #define ieee754sp_inf(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PINFINITY+(sn)]) | |
322 | #define ieee754sp_zero(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PZERO+(sn)]) | |
323 | #define ieee754sp_one(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PONE+(sn)]) | |
324 | #define ieee754sp_ten(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PTEN+(sn)]) | |
325 | #define ieee754sp_indef() (ieee754sp_spcvals[IEEE754_SPCVAL_INDEF]) | |
326 | #define ieee754sp_max(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PMAX+(sn)]) | |
327 | #define ieee754sp_min(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PMIN+(sn)]) | |
328 | #define ieee754sp_mind(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PMIND+(sn)]) | |
329 | #define ieee754sp_1e31() (ieee754sp_spcvals[IEEE754_SPCVAL_P1E31]) | |
330 | #define ieee754sp_1e63() (ieee754sp_spcvals[IEEE754_SPCVAL_P1E63]) | |
331 | ||
332 | /* | |
333 | * Indefinite integer value | |
334 | */ | |
e812a739 RB |
335 | static inline int ieee754si_indef(void) |
336 | { | |
337 | return INT_MAX; | |
338 | } | |
339 | ||
340 | static inline s64 ieee754di_indef(void) | |
341 | { | |
342 | return S64_MAX; | |
343 | } | |
1da177e4 | 344 | |
1da177e4 LT |
345 | /* result types for xctx.rt */ |
346 | #define IEEE754_RT_SP 0 | |
347 | #define IEEE754_RT_DP 1 | |
348 | #define IEEE754_RT_XP 2 | |
349 | #define IEEE754_RT_SI 3 | |
350 | #define IEEE754_RT_DI 4 | |
351 | ||
1da177e4 LT |
352 | /* compat */ |
353 | #define ieee754dp_fix(x) ieee754dp_tint(x) | |
354 | #define ieee754sp_fix(x) ieee754sp_tint(x) | |
a663bf90 RB |
355 | |
356 | #endif /* __ARCH_MIPS_MATH_EMU_IEEE754_H */ |