blob: 22796e0120607157abb40b91d5467f6761735c9d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * MIPS floating point support
3 * Copyright (C) 1994-2000 Algorithmics Ltd.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * 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.
Ralf Baechlecd21dfc2005-04-28 13:39:10 +000023 */
Ralf Baechlea663bf92005-10-23 13:44:31 +010024#ifndef __ARCH_MIPS_MATH_EMU_IEEE754_H
25#define __ARCH_MIPS_MATH_EMU_IEEE754_H
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Ralf Baechlecd21dfc2005-04-28 13:39:10 +000027#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/types.h>
Ralf Baechlecd21dfc2005-04-28 13:39:10 +000029#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/*
32 * Not very pretty, but the Linux kernel's normal va_list definition
33 * does not allow it to be used as a structure element, as it is here.
34 */
35#ifndef _STDARG_H
36#include <stdarg.h>
37#endif
38
Ralf Baechlecd21dfc2005-04-28 13:39:10 +000039#ifdef __LITTLE_ENDIAN
Linus Torvalds1da177e2005-04-16 15:20:36 -070040struct ieee754dp_konst {
41 unsigned mantlo:32;
42 unsigned manthi:20;
43 unsigned bexp:11;
44 unsigned sign:1;
45};
46struct ieee754sp_konst {
47 unsigned mant:23;
48 unsigned bexp:8;
49 unsigned sign:1;
50};
51
52typedef union _ieee754dp {
53 struct ieee754dp_konst oparts;
54 struct {
55 u64 mant:52;
56 unsigned int bexp:11;
57 unsigned int sign:1;
58 } parts;
59 u64 bits;
60 double d;
61} ieee754dp;
62
63typedef union _ieee754sp {
64 struct ieee754sp_konst parts;
65 float f;
66 u32 bits;
67} ieee754sp;
68#endif
69
Ralf Baechlecd21dfc2005-04-28 13:39:10 +000070#ifdef __BIG_ENDIAN
Linus Torvalds1da177e2005-04-16 15:20:36 -070071struct ieee754dp_konst {
72 unsigned sign:1;
73 unsigned bexp:11;
74 unsigned manthi:20;
75 unsigned mantlo:32;
76};
Ralf Baechlecd21dfc2005-04-28 13:39:10 +000077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078typedef union _ieee754dp {
79 struct ieee754dp_konst oparts;
80 struct {
81 unsigned int sign:1;
82 unsigned int bexp:11;
83 u64 mant:52;
84 } parts;
85 double d;
86 u64 bits;
87} ieee754dp;
88
89struct ieee754sp_konst {
90 unsigned sign:1;
91 unsigned bexp:8;
92 unsigned mant:23;
93};
94
95typedef union _ieee754sp {
96 struct ieee754sp_konst parts;
97 float f;
98 u32 bits;
99} ieee754sp;
100#endif
101
102/*
103 * single precision (often aka float)
104*/
105int ieee754sp_finite(ieee754sp x);
106int ieee754sp_class(ieee754sp x);
107
108ieee754sp ieee754sp_abs(ieee754sp x);
109ieee754sp ieee754sp_neg(ieee754sp x);
110ieee754sp ieee754sp_scalb(ieee754sp x, int);
111ieee754sp ieee754sp_logb(ieee754sp x);
112
113/* x with sign of y */
114ieee754sp ieee754sp_copysign(ieee754sp x, ieee754sp y);
115
116ieee754sp ieee754sp_add(ieee754sp x, ieee754sp y);
117ieee754sp ieee754sp_sub(ieee754sp x, ieee754sp y);
118ieee754sp ieee754sp_mul(ieee754sp x, ieee754sp y);
119ieee754sp ieee754sp_div(ieee754sp x, ieee754sp y);
120
121ieee754sp ieee754sp_fint(int x);
122ieee754sp ieee754sp_funs(unsigned x);
123ieee754sp ieee754sp_flong(s64 x);
124ieee754sp ieee754sp_fulong(u64 x);
125ieee754sp ieee754sp_fdp(ieee754dp x);
126
127int ieee754sp_tint(ieee754sp x);
128unsigned int ieee754sp_tuns(ieee754sp x);
129s64 ieee754sp_tlong(ieee754sp x);
130u64 ieee754sp_tulong(ieee754sp x);
131
132int ieee754sp_cmp(ieee754sp x, ieee754sp y, int cop, int sig);
133/*
134 * basic sp math
135 */
136ieee754sp ieee754sp_modf(ieee754sp x, ieee754sp * ip);
137ieee754sp ieee754sp_frexp(ieee754sp x, int *exp);
138ieee754sp ieee754sp_ldexp(ieee754sp x, int exp);
139
140ieee754sp ieee754sp_ceil(ieee754sp x);
141ieee754sp ieee754sp_floor(ieee754sp x);
142ieee754sp ieee754sp_trunc(ieee754sp x);
143
144ieee754sp ieee754sp_sqrt(ieee754sp x);
145
146/*
147 * double precision (often aka double)
148*/
149int ieee754dp_finite(ieee754dp x);
150int ieee754dp_class(ieee754dp x);
151
152/* x with sign of y */
153ieee754dp ieee754dp_copysign(ieee754dp x, ieee754dp y);
154
155ieee754dp ieee754dp_add(ieee754dp x, ieee754dp y);
156ieee754dp ieee754dp_sub(ieee754dp x, ieee754dp y);
157ieee754dp ieee754dp_mul(ieee754dp x, ieee754dp y);
158ieee754dp ieee754dp_div(ieee754dp x, ieee754dp y);
159
160ieee754dp ieee754dp_abs(ieee754dp x);
161ieee754dp ieee754dp_neg(ieee754dp x);
162ieee754dp ieee754dp_scalb(ieee754dp x, int);
163
164/* return exponent as integer in floating point format
165 */
166ieee754dp ieee754dp_logb(ieee754dp x);
167
168ieee754dp ieee754dp_fint(int x);
169ieee754dp ieee754dp_funs(unsigned x);
170ieee754dp ieee754dp_flong(s64 x);
171ieee754dp ieee754dp_fulong(u64 x);
172ieee754dp ieee754dp_fsp(ieee754sp x);
173
174ieee754dp ieee754dp_ceil(ieee754dp x);
175ieee754dp ieee754dp_floor(ieee754dp x);
176ieee754dp ieee754dp_trunc(ieee754dp x);
177
178int ieee754dp_tint(ieee754dp x);
179unsigned int ieee754dp_tuns(ieee754dp x);
180s64 ieee754dp_tlong(ieee754dp x);
181u64 ieee754dp_tulong(ieee754dp x);
182
183int ieee754dp_cmp(ieee754dp x, ieee754dp y, int cop, int sig);
184/*
185 * basic sp math
186 */
187ieee754dp ieee754dp_modf(ieee754dp x, ieee754dp * ip);
188ieee754dp ieee754dp_frexp(ieee754dp x, int *exp);
189ieee754dp ieee754dp_ldexp(ieee754dp x, int exp);
190
191ieee754dp ieee754dp_ceil(ieee754dp x);
192ieee754dp ieee754dp_floor(ieee754dp x);
193ieee754dp ieee754dp_trunc(ieee754dp x);
194
195ieee754dp ieee754dp_sqrt(ieee754dp x);
196
197
198
199/* 5 types of floating point number
200*/
201#define IEEE754_CLASS_NORM 0x00
202#define IEEE754_CLASS_ZERO 0x01
203#define IEEE754_CLASS_DNORM 0x02
204#define IEEE754_CLASS_INF 0x03
205#define IEEE754_CLASS_SNAN 0x04
206#define IEEE754_CLASS_QNAN 0x05
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208/* exception numbers */
209#define IEEE754_INEXACT 0x01
210#define IEEE754_UNDERFLOW 0x02
211#define IEEE754_OVERFLOW 0x04
212#define IEEE754_ZERO_DIVIDE 0x08
213#define IEEE754_INVALID_OPERATION 0x10
214
215/* cmp operators
216*/
217#define IEEE754_CLT 0x01
218#define IEEE754_CEQ 0x02
219#define IEEE754_CGT 0x04
220#define IEEE754_CUN 0x08
221
222/* rounding mode
223*/
224#define IEEE754_RN 0 /* round to nearest */
225#define IEEE754_RZ 1 /* round toward zero */
226#define IEEE754_RD 2 /* round toward -Infinity */
227#define IEEE754_RU 3 /* round toward +Infinity */
228
229/* other naming */
230#define IEEE754_RM IEEE754_RD
231#define IEEE754_RP IEEE754_RU
232
233/* "normal" comparisons
234*/
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000235static inline int ieee754sp_eq(ieee754sp x, ieee754sp y)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 return ieee754sp_cmp(x, y, IEEE754_CEQ, 0);
238}
239
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000240static inline int ieee754sp_ne(ieee754sp x, ieee754sp y)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 return ieee754sp_cmp(x, y,
243 IEEE754_CLT | IEEE754_CGT | IEEE754_CUN, 0);
244}
245
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000246static inline int ieee754sp_lt(ieee754sp x, ieee754sp y)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 return ieee754sp_cmp(x, y, IEEE754_CLT, 0);
249}
250
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000251static inline int ieee754sp_le(ieee754sp x, ieee754sp y)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 return ieee754sp_cmp(x, y, IEEE754_CLT | IEEE754_CEQ, 0);
254}
255
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000256static inline int ieee754sp_gt(ieee754sp x, ieee754sp y)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 return ieee754sp_cmp(x, y, IEEE754_CGT, 0);
259}
260
261
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000262static inline int ieee754sp_ge(ieee754sp x, ieee754sp y)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 return ieee754sp_cmp(x, y, IEEE754_CGT | IEEE754_CEQ, 0);
265}
266
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000267static inline int ieee754dp_eq(ieee754dp x, ieee754dp y)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 return ieee754dp_cmp(x, y, IEEE754_CEQ, 0);
270}
271
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000272static inline int ieee754dp_ne(ieee754dp x, ieee754dp y)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 return ieee754dp_cmp(x, y,
275 IEEE754_CLT | IEEE754_CGT | IEEE754_CUN, 0);
276}
277
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000278static inline int ieee754dp_lt(ieee754dp x, ieee754dp y)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 return ieee754dp_cmp(x, y, IEEE754_CLT, 0);
281}
282
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000283static inline int ieee754dp_le(ieee754dp x, ieee754dp y)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 return ieee754dp_cmp(x, y, IEEE754_CLT | IEEE754_CEQ, 0);
286}
287
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000288static inline int ieee754dp_gt(ieee754dp x, ieee754dp y)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 return ieee754dp_cmp(x, y, IEEE754_CGT, 0);
291}
292
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000293static inline int ieee754dp_ge(ieee754dp x, ieee754dp y)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 return ieee754dp_cmp(x, y, IEEE754_CGT | IEEE754_CEQ, 0);
296}
297
298
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000299/*
300 * Like strtod
301 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302ieee754dp ieee754dp_fstr(const char *s, char **endp);
303char *ieee754dp_tstr(ieee754dp x, int prec, int fmt, int af);
304
305
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000306/*
307 * The control status register
308 */
309struct _ieee754_csr {
310#ifdef __BIG_ENDIAN
311 unsigned pad0:7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 unsigned nod:1; /* set 1 for no denormalised numbers */
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000313 unsigned c:1; /* condition */
314 unsigned pad1:5;
315 unsigned cx:6; /* exceptions this operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 unsigned mx:5; /* exception enable mask */
317 unsigned sx:5; /* exceptions total */
318 unsigned rm:2; /* current rounding mode */
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000319#endif
320#ifdef __LITTLE_ENDIAN
321 unsigned rm:2; /* current rounding mode */
322 unsigned sx:5; /* exceptions total */
323 unsigned mx:5; /* exception enable mask */
324 unsigned cx:6; /* exceptions this operation */
325 unsigned pad1:5;
326 unsigned c:1; /* condition */
327 unsigned nod:1; /* set 1 for no denormalised numbers */
328 unsigned pad0:7;
329#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330};
Atsushi Nemotoeae89072006-05-16 01:26:03 +0900331#define ieee754_csr (*(struct _ieee754_csr *)(&current->thread.fpu.fcr31))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000333static inline unsigned ieee754_getrm(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 return (ieee754_csr.rm);
336}
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000337static inline unsigned ieee754_setrm(unsigned rm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 return (ieee754_csr.rm = rm);
340}
341
342/*
343 * get current exceptions
344 */
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000345static inline unsigned ieee754_getcx(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347 return (ieee754_csr.cx);
348}
349
350/* test for current exception condition
351 */
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000352static inline int ieee754_cxtest(unsigned n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 return (ieee754_csr.cx & n);
355}
356
357/*
358 * get sticky exceptions
359 */
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000360static inline unsigned ieee754_getsx(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 return (ieee754_csr.sx);
363}
364
365/* clear sticky conditions
366*/
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000367static inline unsigned ieee754_clrsx(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 return (ieee754_csr.sx = 0);
370}
371
372/* test for sticky exception condition
373 */
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000374static inline int ieee754_sxtest(unsigned n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 return (ieee754_csr.sx & n);
377}
378
379/* debugging */
380ieee754sp ieee754sp_dump(char *s, ieee754sp x);
381ieee754dp ieee754dp_dump(char *s, ieee754dp x);
382
383#define IEEE754_SPCVAL_PZERO 0
384#define IEEE754_SPCVAL_NZERO 1
385#define IEEE754_SPCVAL_PONE 2
386#define IEEE754_SPCVAL_NONE 3
387#define IEEE754_SPCVAL_PTEN 4
388#define IEEE754_SPCVAL_NTEN 5
389#define IEEE754_SPCVAL_PINFINITY 6
390#define IEEE754_SPCVAL_NINFINITY 7
391#define IEEE754_SPCVAL_INDEF 8
392#define IEEE754_SPCVAL_PMAX 9 /* +max norm */
393#define IEEE754_SPCVAL_NMAX 10 /* -max norm */
394#define IEEE754_SPCVAL_PMIN 11 /* +min norm */
395#define IEEE754_SPCVAL_NMIN 12 /* +min norm */
396#define IEEE754_SPCVAL_PMIND 13 /* +min denorm */
397#define IEEE754_SPCVAL_NMIND 14 /* +min denorm */
398#define IEEE754_SPCVAL_P1E31 15 /* + 1.0e31 */
399#define IEEE754_SPCVAL_P1E63 16 /* + 1.0e63 */
400
401extern const struct ieee754dp_konst __ieee754dp_spcvals[];
402extern const struct ieee754sp_konst __ieee754sp_spcvals[];
403#define ieee754dp_spcvals ((const ieee754dp *)__ieee754dp_spcvals)
404#define ieee754sp_spcvals ((const ieee754sp *)__ieee754sp_spcvals)
405
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000406/*
407 * Return infinity with given sign
408 */
409#define ieee754dp_inf(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PINFINITY+(sn)])
410#define ieee754dp_zero(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PZERO+(sn)])
411#define ieee754dp_one(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PONE+(sn)])
412#define ieee754dp_ten(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PTEN+(sn)])
413#define ieee754dp_indef() (ieee754dp_spcvals[IEEE754_SPCVAL_INDEF])
414#define ieee754dp_max(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PMAX+(sn)])
415#define ieee754dp_min(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PMIN+(sn)])
416#define ieee754dp_mind(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PMIND+(sn)])
417#define ieee754dp_1e31() (ieee754dp_spcvals[IEEE754_SPCVAL_P1E31])
418#define ieee754dp_1e63() (ieee754dp_spcvals[IEEE754_SPCVAL_P1E63])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000420#define ieee754sp_inf(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PINFINITY+(sn)])
421#define ieee754sp_zero(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PZERO+(sn)])
422#define ieee754sp_one(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PONE+(sn)])
423#define ieee754sp_ten(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PTEN+(sn)])
424#define ieee754sp_indef() (ieee754sp_spcvals[IEEE754_SPCVAL_INDEF])
425#define ieee754sp_max(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PMAX+(sn)])
426#define ieee754sp_min(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PMIN+(sn)])
427#define ieee754sp_mind(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PMIND+(sn)])
428#define ieee754sp_1e31() (ieee754sp_spcvals[IEEE754_SPCVAL_P1E31])
429#define ieee754sp_1e63() (ieee754sp_spcvals[IEEE754_SPCVAL_P1E63])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000431/*
432 * Indefinite integer value
433 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434#define ieee754si_indef() INT_MAX
435#ifdef LONG_LONG_MAX
436#define ieee754di_indef() LONG_LONG_MAX
437#else
438#define ieee754di_indef() ((s64)(~0ULL>>1))
439#endif
440
441/* IEEE exception context, passed to handler */
442struct ieee754xctx {
443 const char *op; /* operation name */
444 int rt; /* result type */
445 union {
446 ieee754sp sp; /* single precision */
447 ieee754dp dp; /* double precision */
448#ifdef IEEE854_XP
449 ieee754xp xp; /* extended precision */
450#endif
451 int si; /* standard signed integer (32bits) */
452 s64 di; /* extended signed integer (64bits) */
453 } rv; /* default result format implied by op */
454 va_list ap;
455};
456
457/* result types for xctx.rt */
458#define IEEE754_RT_SP 0
459#define IEEE754_RT_DP 1
460#define IEEE754_RT_XP 2
461#define IEEE754_RT_SI 3
462#define IEEE754_RT_DI 4
463
464extern void ieee754_xcpt(struct ieee754xctx *xcp);
465
466/* compat */
467#define ieee754dp_fix(x) ieee754dp_tint(x)
468#define ieee754sp_fix(x) ieee754sp_tint(x)
Ralf Baechlea663bf92005-10-23 13:44:31 +0100469
470#endif /* __ARCH_MIPS_MATH_EMU_IEEE754_H */