2 * MIPS floating point support
3 * Copyright (C) 1994-2000 Algorithmics Ltd.
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.
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
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.
19 * Modification to allow integration with Linux kernel
21 * Kevin D. Kissell, kevink@mips.com and Carsten Langgard, carstenl@mips.com
22 * Copyright (C) 2000 MIPS Technologies, Inc. All rights reserved.
24 #ifndef __ARCH_MIPS_MATH_EMU_IEEE754_H
25 #define __ARCH_MIPS_MATH_EMU_IEEE754_H
28 #include <linux/compiler.h>
29 #include <asm/byteorder.h>
30 #include <linux/kernel.h>
31 #include <linux/types.h>
32 #include <linux/sched.h>
33 #include <asm/bitfield.h>
35 struct ieee754dp_const {
36 __BITFIELD_FIELD(unsigned sign:1,
37 __BITFIELD_FIELD(unsigned bexp:11,
38 __BITFIELD_FIELD(unsigned manthi:20,
39 __BITFIELD_FIELD(unsigned mantlo:32,
44 struct ieee754dp_const oparts;
46 __BITFIELD_FIELD(unsigned int sign:1,
47 __BITFIELD_FIELD(unsigned int bexp:11,
48 __BITFIELD_FIELD(u64 mant:52,
55 struct ieee754sp_const {
56 __BITFIELD_FIELD(unsigned sign:1,
57 __BITFIELD_FIELD(unsigned bexp:8,
58 __BITFIELD_FIELD(unsigned mant:23,
63 struct ieee754sp_const parts;
69 * single precision (often aka float)
71 int ieee754sp_finite(union ieee754sp x);
72 int ieee754sp_class(union ieee754sp x);
74 union ieee754sp ieee754sp_abs(union ieee754sp x);
75 union ieee754sp ieee754sp_neg(union ieee754sp x);
76 union ieee754sp ieee754sp_scalb(union ieee754sp x, int);
77 union ieee754sp ieee754sp_logb(union ieee754sp x);
79 /* x with sign of y */
80 union ieee754sp ieee754sp_copysign(union ieee754sp x, union ieee754sp y);
82 union ieee754sp ieee754sp_add(union ieee754sp x, union ieee754sp y);
83 union ieee754sp ieee754sp_sub(union ieee754sp x, union ieee754sp y);
84 union ieee754sp ieee754sp_mul(union ieee754sp x, union ieee754sp y);
85 union ieee754sp ieee754sp_div(union ieee754sp x, union ieee754sp y);
87 union ieee754sp ieee754sp_fint(int x);
88 union ieee754sp ieee754sp_funs(unsigned x);
89 union ieee754sp ieee754sp_flong(s64 x);
90 union ieee754sp ieee754sp_fulong(u64 x);
91 union ieee754sp ieee754sp_fdp(union ieee754dp x);
93 int ieee754sp_tint(union ieee754sp x);
94 unsigned int ieee754sp_tuns(union ieee754sp x);
95 s64 ieee754sp_tlong(union ieee754sp x);
96 u64 ieee754sp_tulong(union ieee754sp x);
98 int ieee754sp_cmp(union ieee754sp x, union ieee754sp y, int cop, int sig);
102 union ieee754sp ieee754sp_modf(union ieee754sp x, union ieee754sp * ip);
103 union ieee754sp ieee754sp_frexp(union ieee754sp x, int *exp);
104 union ieee754sp ieee754sp_ldexp(union ieee754sp x, int exp);
106 union ieee754sp ieee754sp_ceil(union ieee754sp x);
107 union ieee754sp ieee754sp_floor(union ieee754sp x);
108 union ieee754sp ieee754sp_trunc(union ieee754sp x);
110 union ieee754sp ieee754sp_sqrt(union ieee754sp x);
113 * double precision (often aka double)
115 int ieee754dp_finite(union ieee754dp x);
116 int ieee754dp_class(union ieee754dp x);
118 /* x with sign of y */
119 union ieee754dp ieee754dp_copysign(union ieee754dp x, union ieee754dp y);
121 union ieee754dp ieee754dp_add(union ieee754dp x, union ieee754dp y);
122 union ieee754dp ieee754dp_sub(union ieee754dp x, union ieee754dp y);
123 union ieee754dp ieee754dp_mul(union ieee754dp x, union ieee754dp y);
124 union ieee754dp ieee754dp_div(union ieee754dp x, union ieee754dp y);
126 union ieee754dp ieee754dp_abs(union ieee754dp x);
127 union ieee754dp ieee754dp_neg(union ieee754dp x);
128 union ieee754dp ieee754dp_scalb(union ieee754dp x, int);
130 /* return exponent as integer in floating point format
132 union ieee754dp ieee754dp_logb(union ieee754dp x);
134 union ieee754dp ieee754dp_fint(int x);
135 union ieee754dp ieee754dp_funs(unsigned x);
136 union ieee754dp ieee754dp_flong(s64 x);
137 union ieee754dp ieee754dp_fulong(u64 x);
138 union ieee754dp ieee754dp_fsp(union ieee754sp x);
140 union ieee754dp ieee754dp_ceil(union ieee754dp x);
141 union ieee754dp ieee754dp_floor(union ieee754dp x);
142 union ieee754dp ieee754dp_trunc(union ieee754dp x);
144 int ieee754dp_tint(union ieee754dp x);
145 unsigned int ieee754dp_tuns(union ieee754dp x);
146 s64 ieee754dp_tlong(union ieee754dp x);
147 u64 ieee754dp_tulong(union ieee754dp x);
149 int ieee754dp_cmp(union ieee754dp x, union ieee754dp y, int cop, int sig);
153 union ieee754dp ieee754dp_modf(union ieee754dp x, union ieee754dp * ip);
154 union ieee754dp ieee754dp_frexp(union ieee754dp x, int *exp);
155 union ieee754dp ieee754dp_ldexp(union ieee754dp x, int exp);
157 union ieee754dp ieee754dp_ceil(union ieee754dp x);
158 union ieee754dp ieee754dp_floor(union ieee754dp x);
159 union ieee754dp ieee754dp_trunc(union ieee754dp x);
161 union ieee754dp ieee754dp_sqrt(union ieee754dp x);
165 /* 5 types of floating point number
168 IEEE754_CLASS_NORM = 0x00,
169 IEEE754_CLASS_ZERO = 0x01,
170 IEEE754_CLASS_DNORM = 0x02,
171 IEEE754_CLASS_INF = 0x03,
172 IEEE754_CLASS_SNAN = 0x04,
173 IEEE754_CLASS_QNAN = 0x05,
176 /* exception numbers */
177 #define IEEE754_INEXACT 0x01
178 #define IEEE754_UNDERFLOW 0x02
179 #define IEEE754_OVERFLOW 0x04
180 #define IEEE754_ZERO_DIVIDE 0x08
181 #define IEEE754_INVALID_OPERATION 0x10
185 #define IEEE754_CLT 0x01
186 #define IEEE754_CEQ 0x02
187 #define IEEE754_CGT 0x04
188 #define IEEE754_CUN 0x08
192 #define IEEE754_RN 0 /* round to nearest */
193 #define IEEE754_RZ 1 /* round toward zero */
194 #define IEEE754_RD 2 /* round toward -Infinity */
195 #define IEEE754_RU 3 /* round toward +Infinity */
198 #define IEEE754_RM IEEE754_RD
199 #define IEEE754_RP IEEE754_RU
201 /* "normal" comparisons
203 static inline int ieee754sp_eq(union ieee754sp x, union ieee754sp y)
205 return ieee754sp_cmp(x, y, IEEE754_CEQ, 0);
208 static inline int ieee754sp_ne(union ieee754sp x, union ieee754sp y)
210 return ieee754sp_cmp(x, y,
211 IEEE754_CLT | IEEE754_CGT | IEEE754_CUN, 0);
214 static inline int ieee754sp_lt(union ieee754sp x, union ieee754sp y)
216 return ieee754sp_cmp(x, y, IEEE754_CLT, 0);
219 static inline int ieee754sp_le(union ieee754sp x, union ieee754sp y)
221 return ieee754sp_cmp(x, y, IEEE754_CLT | IEEE754_CEQ, 0);
224 static inline int ieee754sp_gt(union ieee754sp x, union ieee754sp y)
226 return ieee754sp_cmp(x, y, IEEE754_CGT, 0);
230 static inline int ieee754sp_ge(union ieee754sp x, union ieee754sp y)
232 return ieee754sp_cmp(x, y, IEEE754_CGT | IEEE754_CEQ, 0);
235 static inline int ieee754dp_eq(union ieee754dp x, union ieee754dp y)
237 return ieee754dp_cmp(x, y, IEEE754_CEQ, 0);
240 static inline int ieee754dp_ne(union ieee754dp x, union ieee754dp y)
242 return ieee754dp_cmp(x, y,
243 IEEE754_CLT | IEEE754_CGT | IEEE754_CUN, 0);
246 static inline int ieee754dp_lt(union ieee754dp x, union ieee754dp y)
248 return ieee754dp_cmp(x, y, IEEE754_CLT, 0);
251 static inline int ieee754dp_le(union ieee754dp x, union ieee754dp y)
253 return ieee754dp_cmp(x, y, IEEE754_CLT | IEEE754_CEQ, 0);
256 static inline int ieee754dp_gt(union ieee754dp x, union ieee754dp y)
258 return ieee754dp_cmp(x, y, IEEE754_CGT, 0);
261 static inline int ieee754dp_ge(union ieee754dp x, union ieee754dp y)
263 return ieee754dp_cmp(x, y, IEEE754_CGT | IEEE754_CEQ, 0);
270 union ieee754dp ieee754dp_fstr(const char *s, char **endp);
271 char *ieee754dp_tstr(union ieee754dp x, int prec, int fmt, int af);
275 * The control status register
277 struct _ieee754_csr {
278 __BITFIELD_FIELD(unsigned pad0:7,
279 __BITFIELD_FIELD(unsigned nod:1, /* set 1 for no denormalised numbers */
280 __BITFIELD_FIELD(unsigned c:1, /* condition */
281 __BITFIELD_FIELD(unsigned pad1:5,
282 __BITFIELD_FIELD(unsigned cx:6, /* exceptions this operation */
283 __BITFIELD_FIELD(unsigned mx:5, /* exception enable mask */
284 __BITFIELD_FIELD(unsigned sx:5, /* exceptions total */
285 __BITFIELD_FIELD(unsigned rm:2, /* current rounding mode */
288 #define ieee754_csr (*(struct _ieee754_csr *)(¤t->thread.fpu.fcr31))
290 static inline unsigned ieee754_getrm(void)
292 return (ieee754_csr.rm);
294 static inline unsigned ieee754_setrm(unsigned rm)
296 return (ieee754_csr.rm = rm);
300 * get current exceptions
302 static inline unsigned ieee754_getcx(void)
304 return (ieee754_csr.cx);
307 /* test for current exception condition
309 static inline int ieee754_cxtest(unsigned n)
311 return (ieee754_csr.cx & n);
315 * get sticky exceptions
317 static inline unsigned ieee754_getsx(void)
319 return (ieee754_csr.sx);
322 /* clear sticky conditions
324 static inline unsigned ieee754_clrsx(void)
326 return (ieee754_csr.sx = 0);
329 /* test for sticky exception condition
331 static inline int ieee754_sxtest(unsigned n)
333 return (ieee754_csr.sx & n);
337 union ieee754sp ieee754sp_dump(char *s, union ieee754sp x);
338 union ieee754dp ieee754dp_dump(char *s, union ieee754dp x);
340 #define IEEE754_SPCVAL_PZERO 0
341 #define IEEE754_SPCVAL_NZERO 1
342 #define IEEE754_SPCVAL_PONE 2
343 #define IEEE754_SPCVAL_NONE 3
344 #define IEEE754_SPCVAL_PTEN 4
345 #define IEEE754_SPCVAL_NTEN 5
346 #define IEEE754_SPCVAL_PINFINITY 6
347 #define IEEE754_SPCVAL_NINFINITY 7
348 #define IEEE754_SPCVAL_INDEF 8
349 #define IEEE754_SPCVAL_PMAX 9 /* +max norm */
350 #define IEEE754_SPCVAL_NMAX 10 /* -max norm */
351 #define IEEE754_SPCVAL_PMIN 11 /* +min norm */
352 #define IEEE754_SPCVAL_NMIN 12 /* +min norm */
353 #define IEEE754_SPCVAL_PMIND 13 /* +min denorm */
354 #define IEEE754_SPCVAL_NMIND 14 /* +min denorm */
355 #define IEEE754_SPCVAL_P1E31 15 /* + 1.0e31 */
356 #define IEEE754_SPCVAL_P1E63 16 /* + 1.0e63 */
358 extern const struct ieee754dp_const __ieee754dp_spcvals[];
359 extern const struct ieee754sp_const __ieee754sp_spcvals[];
360 #define ieee754dp_spcvals ((const union ieee754dp *)__ieee754dp_spcvals)
361 #define ieee754sp_spcvals ((const union ieee754sp *)__ieee754sp_spcvals)
364 * Return infinity with given sign
366 #define ieee754dp_inf(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PINFINITY+(sn)])
367 #define ieee754dp_zero(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PZERO+(sn)])
368 #define ieee754dp_one(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PONE+(sn)])
369 #define ieee754dp_ten(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PTEN+(sn)])
370 #define ieee754dp_indef() (ieee754dp_spcvals[IEEE754_SPCVAL_INDEF])
371 #define ieee754dp_max(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PMAX+(sn)])
372 #define ieee754dp_min(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PMIN+(sn)])
373 #define ieee754dp_mind(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PMIND+(sn)])
374 #define ieee754dp_1e31() (ieee754dp_spcvals[IEEE754_SPCVAL_P1E31])
375 #define ieee754dp_1e63() (ieee754dp_spcvals[IEEE754_SPCVAL_P1E63])
377 #define ieee754sp_inf(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PINFINITY+(sn)])
378 #define ieee754sp_zero(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PZERO+(sn)])
379 #define ieee754sp_one(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PONE+(sn)])
380 #define ieee754sp_ten(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PTEN+(sn)])
381 #define ieee754sp_indef() (ieee754sp_spcvals[IEEE754_SPCVAL_INDEF])
382 #define ieee754sp_max(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PMAX+(sn)])
383 #define ieee754sp_min(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PMIN+(sn)])
384 #define ieee754sp_mind(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PMIND+(sn)])
385 #define ieee754sp_1e31() (ieee754sp_spcvals[IEEE754_SPCVAL_P1E31])
386 #define ieee754sp_1e63() (ieee754sp_spcvals[IEEE754_SPCVAL_P1E63])
389 * Indefinite integer value
391 static inline int ieee754si_indef(void)
396 static inline s64 ieee754di_indef(void)
401 /* IEEE exception context, passed to handler */
403 const char *op; /* operation name */
404 int rt; /* result type */
406 union ieee754sp sp; /* single precision */
407 union ieee754dp dp; /* double precision */
408 int si; /* standard signed integer (32bits) */
409 s64 di; /* extended signed integer (64bits) */
410 } rv; /* default result format implied by op */
414 /* result types for xctx.rt */
415 #define IEEE754_RT_SP 0
416 #define IEEE754_RT_DP 1
417 #define IEEE754_RT_XP 2
418 #define IEEE754_RT_SI 3
419 #define IEEE754_RT_DI 4
421 extern void __cold ieee754_xcpt(struct ieee754xctx *xcp);
424 #define ieee754dp_fix(x) ieee754dp_tint(x)
425 #define ieee754sp_fix(x) ieee754sp_tint(x)
427 #endif /* __ARCH_MIPS_MATH_EMU_IEEE754_H */