1 /* single and double precision fp ops
2 * missing extended precision.
5 * MIPS floating point support
6 * Copyright (C) 1994-2000 Algorithmics Ltd.
7 * http://www.algor.co.uk
9 * ########################################################################
11 * This program is free software; you can distribute it and/or modify it
12 * under the terms of the GNU General Public License (Version 2) as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
24 * ########################################################################
27 /**************************************************************************
29 * Modification to allow integration with Linux kernel
31 * Kevin D. Kissell, kevink@mips.com and Carsten Langgard, carstenl@mips.com
32 * Copyright (C) 2000 MIPS Technologies, Inc. All rights reserved.
33 *************************************************************************/
36 /* Going from Algorithmics to Linux native environment, add this */
37 #include <linux/types.h>
40 * Not very pretty, but the Linux kernel's normal va_list definition
41 * does not allow it to be used as a structure element, as it is here.
49 /* Note that __KERNEL__ is taken to mean Linux kernel */
52 #include <machine/types.h>
54 #include <machine/endian.h>
56 #endif /* __KERNEL__ */
58 #if (defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN) || defined(__MIPSEL__)
59 struct ieee754dp_konst
{
65 struct ieee754sp_konst
{
71 typedef union _ieee754dp
{
72 struct ieee754dp_konst oparts
;
82 typedef union _ieee754sp
{
83 struct ieee754sp_konst parts
;
89 #if (defined(BYTE_ORDER) && BYTE_ORDER == BIG_ENDIAN) || defined(__MIPSEB__)
90 struct ieee754dp_konst
{
96 typedef union _ieee754dp
{
97 struct ieee754dp_konst oparts
;
100 unsigned int bexp
:11;
107 struct ieee754sp_konst
{
113 typedef union _ieee754sp
{
114 struct ieee754sp_konst parts
;
121 * single precision (often aka float)
123 int ieee754sp_finite(ieee754sp x
);
124 int ieee754sp_class(ieee754sp x
);
126 ieee754sp
ieee754sp_abs(ieee754sp x
);
127 ieee754sp
ieee754sp_neg(ieee754sp x
);
128 ieee754sp
ieee754sp_scalb(ieee754sp x
, int);
129 ieee754sp
ieee754sp_logb(ieee754sp x
);
131 /* x with sign of y */
132 ieee754sp
ieee754sp_copysign(ieee754sp x
, ieee754sp y
);
134 ieee754sp
ieee754sp_add(ieee754sp x
, ieee754sp y
);
135 ieee754sp
ieee754sp_sub(ieee754sp x
, ieee754sp y
);
136 ieee754sp
ieee754sp_mul(ieee754sp x
, ieee754sp y
);
137 ieee754sp
ieee754sp_div(ieee754sp x
, ieee754sp y
);
139 ieee754sp
ieee754sp_fint(int x
);
140 ieee754sp
ieee754sp_funs(unsigned x
);
141 ieee754sp
ieee754sp_flong(s64 x
);
142 ieee754sp
ieee754sp_fulong(u64 x
);
143 ieee754sp
ieee754sp_fdp(ieee754dp x
);
145 int ieee754sp_tint(ieee754sp x
);
146 unsigned int ieee754sp_tuns(ieee754sp x
);
147 s64
ieee754sp_tlong(ieee754sp x
);
148 u64
ieee754sp_tulong(ieee754sp x
);
150 int ieee754sp_cmp(ieee754sp x
, ieee754sp y
, int cop
, int sig
);
154 ieee754sp
ieee754sp_modf(ieee754sp x
, ieee754sp
* ip
);
155 ieee754sp
ieee754sp_frexp(ieee754sp x
, int *exp
);
156 ieee754sp
ieee754sp_ldexp(ieee754sp x
, int exp
);
158 ieee754sp
ieee754sp_ceil(ieee754sp x
);
159 ieee754sp
ieee754sp_floor(ieee754sp x
);
160 ieee754sp
ieee754sp_trunc(ieee754sp x
);
162 ieee754sp
ieee754sp_sqrt(ieee754sp x
);
165 * double precision (often aka double)
167 int ieee754dp_finite(ieee754dp x
);
168 int ieee754dp_class(ieee754dp x
);
170 /* x with sign of y */
171 ieee754dp
ieee754dp_copysign(ieee754dp x
, ieee754dp y
);
173 ieee754dp
ieee754dp_add(ieee754dp x
, ieee754dp y
);
174 ieee754dp
ieee754dp_sub(ieee754dp x
, ieee754dp y
);
175 ieee754dp
ieee754dp_mul(ieee754dp x
, ieee754dp y
);
176 ieee754dp
ieee754dp_div(ieee754dp x
, ieee754dp y
);
178 ieee754dp
ieee754dp_abs(ieee754dp x
);
179 ieee754dp
ieee754dp_neg(ieee754dp x
);
180 ieee754dp
ieee754dp_scalb(ieee754dp x
, int);
182 /* return exponent as integer in floating point format
184 ieee754dp
ieee754dp_logb(ieee754dp x
);
186 ieee754dp
ieee754dp_fint(int x
);
187 ieee754dp
ieee754dp_funs(unsigned x
);
188 ieee754dp
ieee754dp_flong(s64 x
);
189 ieee754dp
ieee754dp_fulong(u64 x
);
190 ieee754dp
ieee754dp_fsp(ieee754sp x
);
192 ieee754dp
ieee754dp_ceil(ieee754dp x
);
193 ieee754dp
ieee754dp_floor(ieee754dp x
);
194 ieee754dp
ieee754dp_trunc(ieee754dp x
);
196 int ieee754dp_tint(ieee754dp x
);
197 unsigned int ieee754dp_tuns(ieee754dp x
);
198 s64
ieee754dp_tlong(ieee754dp x
);
199 u64
ieee754dp_tulong(ieee754dp x
);
201 int ieee754dp_cmp(ieee754dp x
, ieee754dp y
, int cop
, int sig
);
205 ieee754dp
ieee754dp_modf(ieee754dp x
, ieee754dp
* ip
);
206 ieee754dp
ieee754dp_frexp(ieee754dp x
, int *exp
);
207 ieee754dp
ieee754dp_ldexp(ieee754dp x
, int exp
);
209 ieee754dp
ieee754dp_ceil(ieee754dp x
);
210 ieee754dp
ieee754dp_floor(ieee754dp x
);
211 ieee754dp
ieee754dp_trunc(ieee754dp x
);
213 ieee754dp
ieee754dp_sqrt(ieee754dp x
);
217 /* 5 types of floating point number
219 #define IEEE754_CLASS_NORM 0x00
220 #define IEEE754_CLASS_ZERO 0x01
221 #define IEEE754_CLASS_DNORM 0x02
222 #define IEEE754_CLASS_INF 0x03
223 #define IEEE754_CLASS_SNAN 0x04
224 #define IEEE754_CLASS_QNAN 0x05
225 extern const char *const ieee754_cname
[];
227 /* exception numbers */
228 #define IEEE754_INEXACT 0x01
229 #define IEEE754_UNDERFLOW 0x02
230 #define IEEE754_OVERFLOW 0x04
231 #define IEEE754_ZERO_DIVIDE 0x08
232 #define IEEE754_INVALID_OPERATION 0x10
236 #define IEEE754_CLT 0x01
237 #define IEEE754_CEQ 0x02
238 #define IEEE754_CGT 0x04
239 #define IEEE754_CUN 0x08
243 #define IEEE754_RN 0 /* round to nearest */
244 #define IEEE754_RZ 1 /* round toward zero */
245 #define IEEE754_RD 2 /* round toward -Infinity */
246 #define IEEE754_RU 3 /* round toward +Infinity */
249 #define IEEE754_RM IEEE754_RD
250 #define IEEE754_RP IEEE754_RU
252 /* "normal" comparisons
254 static __inline
int ieee754sp_eq(ieee754sp x
, ieee754sp y
)
256 return ieee754sp_cmp(x
, y
, IEEE754_CEQ
, 0);
259 static __inline
int ieee754sp_ne(ieee754sp x
, ieee754sp y
)
261 return ieee754sp_cmp(x
, y
,
262 IEEE754_CLT
| IEEE754_CGT
| IEEE754_CUN
, 0);
265 static __inline
int ieee754sp_lt(ieee754sp x
, ieee754sp y
)
267 return ieee754sp_cmp(x
, y
, IEEE754_CLT
, 0);
270 static __inline
int ieee754sp_le(ieee754sp x
, ieee754sp y
)
272 return ieee754sp_cmp(x
, y
, IEEE754_CLT
| IEEE754_CEQ
, 0);
275 static __inline
int ieee754sp_gt(ieee754sp x
, ieee754sp y
)
277 return ieee754sp_cmp(x
, y
, IEEE754_CGT
, 0);
281 static __inline
int ieee754sp_ge(ieee754sp x
, ieee754sp y
)
283 return ieee754sp_cmp(x
, y
, IEEE754_CGT
| IEEE754_CEQ
, 0);
286 static __inline
int ieee754dp_eq(ieee754dp x
, ieee754dp y
)
288 return ieee754dp_cmp(x
, y
, IEEE754_CEQ
, 0);
291 static __inline
int ieee754dp_ne(ieee754dp x
, ieee754dp y
)
293 return ieee754dp_cmp(x
, y
,
294 IEEE754_CLT
| IEEE754_CGT
| IEEE754_CUN
, 0);
297 static __inline
int ieee754dp_lt(ieee754dp x
, ieee754dp y
)
299 return ieee754dp_cmp(x
, y
, IEEE754_CLT
, 0);
302 static __inline
int ieee754dp_le(ieee754dp x
, ieee754dp y
)
304 return ieee754dp_cmp(x
, y
, IEEE754_CLT
| IEEE754_CEQ
, 0);
307 static __inline
int ieee754dp_gt(ieee754dp x
, ieee754dp y
)
309 return ieee754dp_cmp(x
, y
, IEEE754_CGT
, 0);
312 static __inline
int ieee754dp_ge(ieee754dp x
, ieee754dp y
)
314 return ieee754dp_cmp(x
, y
, IEEE754_CGT
| IEEE754_CEQ
, 0);
320 ieee754dp
ieee754dp_fstr(const char *s
, char **endp
);
321 char *ieee754dp_tstr(ieee754dp x
, int prec
, int fmt
, int af
);
324 /* the control status register
328 unsigned nod
:1; /* set 1 for no denormalised numbers */
329 unsigned cx
:5; /* exceptions this operation */
330 unsigned mx
:5; /* exception enable mask */
331 unsigned sx
:5; /* exceptions total */
332 unsigned rm
:2; /* current rounding mode */
334 extern struct ieee754_csr ieee754_csr
;
336 static __inline
unsigned ieee754_getrm(void)
338 return (ieee754_csr
.rm
);
340 static __inline
unsigned ieee754_setrm(unsigned rm
)
342 return (ieee754_csr
.rm
= rm
);
346 * get current exceptions
348 static __inline
unsigned ieee754_getcx(void)
350 return (ieee754_csr
.cx
);
353 /* test for current exception condition
355 static __inline
int ieee754_cxtest(unsigned n
)
357 return (ieee754_csr
.cx
& n
);
361 * get sticky exceptions
363 static __inline
unsigned ieee754_getsx(void)
365 return (ieee754_csr
.sx
);
368 /* clear sticky conditions
370 static __inline
unsigned ieee754_clrsx(void)
372 return (ieee754_csr
.sx
= 0);
375 /* test for sticky exception condition
377 static __inline
int ieee754_sxtest(unsigned n
)
379 return (ieee754_csr
.sx
& n
);
383 ieee754sp
ieee754sp_dump(char *s
, ieee754sp x
);
384 ieee754dp
ieee754dp_dump(char *s
, ieee754dp x
);
386 #define IEEE754_SPCVAL_PZERO 0
387 #define IEEE754_SPCVAL_NZERO 1
388 #define IEEE754_SPCVAL_PONE 2
389 #define IEEE754_SPCVAL_NONE 3
390 #define IEEE754_SPCVAL_PTEN 4
391 #define IEEE754_SPCVAL_NTEN 5
392 #define IEEE754_SPCVAL_PINFINITY 6
393 #define IEEE754_SPCVAL_NINFINITY 7
394 #define IEEE754_SPCVAL_INDEF 8
395 #define IEEE754_SPCVAL_PMAX 9 /* +max norm */
396 #define IEEE754_SPCVAL_NMAX 10 /* -max norm */
397 #define IEEE754_SPCVAL_PMIN 11 /* +min norm */
398 #define IEEE754_SPCVAL_NMIN 12 /* +min norm */
399 #define IEEE754_SPCVAL_PMIND 13 /* +min denorm */
400 #define IEEE754_SPCVAL_NMIND 14 /* +min denorm */
401 #define IEEE754_SPCVAL_P1E31 15 /* + 1.0e31 */
402 #define IEEE754_SPCVAL_P1E63 16 /* + 1.0e63 */
404 extern const struct ieee754dp_konst __ieee754dp_spcvals
[];
405 extern const struct ieee754sp_konst __ieee754sp_spcvals
[];
406 #define ieee754dp_spcvals ((const ieee754dp *)__ieee754dp_spcvals)
407 #define ieee754sp_spcvals ((const ieee754sp *)__ieee754sp_spcvals)
409 /* return infinity with given sign
411 #define ieee754dp_inf(sn) \
412 (ieee754dp_spcvals[IEEE754_SPCVAL_PINFINITY+(sn)])
413 #define ieee754dp_zero(sn) \
414 (ieee754dp_spcvals[IEEE754_SPCVAL_PZERO+(sn)])
415 #define ieee754dp_one(sn) \
416 (ieee754dp_spcvals[IEEE754_SPCVAL_PONE+(sn)])
417 #define ieee754dp_ten(sn) \
418 (ieee754dp_spcvals[IEEE754_SPCVAL_PTEN+(sn)])
419 #define ieee754dp_indef() \
420 (ieee754dp_spcvals[IEEE754_SPCVAL_INDEF])
421 #define ieee754dp_max(sn) \
422 (ieee754dp_spcvals[IEEE754_SPCVAL_PMAX+(sn)])
423 #define ieee754dp_min(sn) \
424 (ieee754dp_spcvals[IEEE754_SPCVAL_PMIN+(sn)])
425 #define ieee754dp_mind(sn) \
426 (ieee754dp_spcvals[IEEE754_SPCVAL_PMIND+(sn)])
427 #define ieee754dp_1e31() \
428 (ieee754dp_spcvals[IEEE754_SPCVAL_P1E31])
429 #define ieee754dp_1e63() \
430 (ieee754dp_spcvals[IEEE754_SPCVAL_P1E63])
432 #define ieee754sp_inf(sn) \
433 (ieee754sp_spcvals[IEEE754_SPCVAL_PINFINITY+(sn)])
434 #define ieee754sp_zero(sn) \
435 (ieee754sp_spcvals[IEEE754_SPCVAL_PZERO+(sn)])
436 #define ieee754sp_one(sn) \
437 (ieee754sp_spcvals[IEEE754_SPCVAL_PONE+(sn)])
438 #define ieee754sp_ten(sn) \
439 (ieee754sp_spcvals[IEEE754_SPCVAL_PTEN+(sn)])
440 #define ieee754sp_indef() \
441 (ieee754sp_spcvals[IEEE754_SPCVAL_INDEF])
442 #define ieee754sp_max(sn) \
443 (ieee754sp_spcvals[IEEE754_SPCVAL_PMAX+(sn)])
444 #define ieee754sp_min(sn) \
445 (ieee754sp_spcvals[IEEE754_SPCVAL_PMIN+(sn)])
446 #define ieee754sp_mind(sn) \
447 (ieee754sp_spcvals[IEEE754_SPCVAL_PMIND+(sn)])
448 #define ieee754sp_1e31() \
449 (ieee754sp_spcvals[IEEE754_SPCVAL_P1E31])
450 #define ieee754sp_1e63() \
451 (ieee754sp_spcvals[IEEE754_SPCVAL_P1E63])
453 /* indefinite integer value
455 #define ieee754si_indef() INT_MAX
457 #define ieee754di_indef() LONG_LONG_MAX
459 #define ieee754di_indef() ((s64)(~0ULL>>1))
462 /* IEEE exception context, passed to handler */
464 const char *op
; /* operation name */
465 int rt
; /* result type */
467 ieee754sp sp
; /* single precision */
468 ieee754dp dp
; /* double precision */
470 ieee754xp xp
; /* extended precision */
472 int si
; /* standard signed integer (32bits) */
473 s64 di
; /* extended signed integer (64bits) */
474 } rv
; /* default result format implied by op */
478 /* result types for xctx.rt */
479 #define IEEE754_RT_SP 0
480 #define IEEE754_RT_DP 1
481 #define IEEE754_RT_XP 2
482 #define IEEE754_RT_SI 3
483 #define IEEE754_RT_DI 4
485 extern void ieee754_xcpt(struct ieee754xctx
*xcp
);
488 #define ieee754dp_fix(x) ieee754dp_tint(x)
489 #define ieee754sp_fix(x) ieee754sp_tint(x)