blob: ed7fc87aeac5a5e0aa6da084b5d1552214a81adc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* ieee754 floating point arithmetic
2 * single and double precision
3 *
4 * BUGS
5 * not much dp done
6 * doesn't generate IEEE754_INEXACT
7 *
8 */
9/*
10 * MIPS floating point support
11 * Copyright (C) 1994-2000 Algorithmics Ltd.
12 * http://www.algor.co.uk
13 *
14 * ########################################################################
15 *
16 * This program is free software; you can distribute it and/or modify it
17 * under the terms of the GNU General Public License (Version 2) as
18 * published by the Free Software Foundation.
19 *
20 * This program is distributed in the hope it will be useful, but WITHOUT
21 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 * for more details.
24 *
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
28 *
29 * ########################################################################
30 */
31
32
33#include "ieee754int.h"
Ralf Baechleefec3c42005-10-23 13:46:25 +010034#include "ieee754sp.h"
35#include "ieee754dp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#define DP_EBIAS 1023
38#define DP_EMIN (-1022)
39#define DP_EMAX 1023
40
41#define SP_EBIAS 127
42#define SP_EMIN (-126)
43#define SP_EMAX 127
44
45/* indexed by class */
46const char *const ieee754_cname[] = {
47 "Normal",
48 "Zero",
49 "Denormal",
50 "Infinity",
51 "QNaN",
52 "SNaN",
53};
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/* special constants
56*/
57
58
59#if (defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN) || defined(__MIPSEL__)
60#define SPSTR(s,b,m) {m,b,s}
61#define DPSTR(s,b,mh,ml) {ml,mh,b,s}
62#endif
63
64#ifdef __MIPSEB__
65#define SPSTR(s,b,m) {s,b,m}
66#define DPSTR(s,b,mh,ml) {s,b,mh,ml}
67#endif
68
69const struct ieee754dp_konst __ieee754dp_spcvals[] = {
70 DPSTR(0, DP_EMIN - 1 + DP_EBIAS, 0, 0), /* + zero */
71 DPSTR(1, DP_EMIN - 1 + DP_EBIAS, 0, 0), /* - zero */
72 DPSTR(0, DP_EBIAS, 0, 0), /* + 1.0 */
73 DPSTR(1, DP_EBIAS, 0, 0), /* - 1.0 */
74 DPSTR(0, 3 + DP_EBIAS, 0x40000, 0), /* + 10.0 */
75 DPSTR(1, 3 + DP_EBIAS, 0x40000, 0), /* - 10.0 */
76 DPSTR(0, DP_EMAX + 1 + DP_EBIAS, 0, 0), /* + infinity */
77 DPSTR(1, DP_EMAX + 1 + DP_EBIAS, 0, 0), /* - infinity */
78 DPSTR(0,DP_EMAX+1+DP_EBIAS,0x7FFFF,0xFFFFFFFF), /* + indef quiet Nan */
79 DPSTR(0, DP_EMAX + DP_EBIAS, 0xFFFFF, 0xFFFFFFFF), /* + max */
80 DPSTR(1, DP_EMAX + DP_EBIAS, 0xFFFFF, 0xFFFFFFFF), /* - max */
81 DPSTR(0, DP_EMIN + DP_EBIAS, 0, 0), /* + min normal */
82 DPSTR(1, DP_EMIN + DP_EBIAS, 0, 0), /* - min normal */
83 DPSTR(0, DP_EMIN - 1 + DP_EBIAS, 0, 1), /* + min denormal */
84 DPSTR(1, DP_EMIN - 1 + DP_EBIAS, 0, 1), /* - min denormal */
85 DPSTR(0, 31 + DP_EBIAS, 0, 0), /* + 1.0e31 */
86 DPSTR(0, 63 + DP_EBIAS, 0, 0), /* + 1.0e63 */
87};
88
89const struct ieee754sp_konst __ieee754sp_spcvals[] = {
90 SPSTR(0, SP_EMIN - 1 + SP_EBIAS, 0), /* + zero */
91 SPSTR(1, SP_EMIN - 1 + SP_EBIAS, 0), /* - zero */
92 SPSTR(0, SP_EBIAS, 0), /* + 1.0 */
93 SPSTR(1, SP_EBIAS, 0), /* - 1.0 */
94 SPSTR(0, 3 + SP_EBIAS, 0x200000), /* + 10.0 */
95 SPSTR(1, 3 + SP_EBIAS, 0x200000), /* - 10.0 */
96 SPSTR(0, SP_EMAX + 1 + SP_EBIAS, 0), /* + infinity */
97 SPSTR(1, SP_EMAX + 1 + SP_EBIAS, 0), /* - infinity */
98 SPSTR(0,SP_EMAX+1+SP_EBIAS,0x3FFFFF), /* + indef quiet Nan */
99 SPSTR(0, SP_EMAX + SP_EBIAS, 0x7FFFFF), /* + max normal */
100 SPSTR(1, SP_EMAX + SP_EBIAS, 0x7FFFFF), /* - max normal */
101 SPSTR(0, SP_EMIN + SP_EBIAS, 0), /* + min normal */
102 SPSTR(1, SP_EMIN + SP_EBIAS, 0), /* - min normal */
103 SPSTR(0, SP_EMIN - 1 + SP_EBIAS, 1), /* + min denormal */
104 SPSTR(1, SP_EMIN - 1 + SP_EBIAS, 1), /* - min denormal */
105 SPSTR(0, 31 + SP_EBIAS, 0), /* + 1.0e31 */
106 SPSTR(0, 63 + SP_EBIAS, 0), /* + 1.0e63 */
107};
108
109
110int ieee754si_xcpt(int r, const char *op, ...)
111{
112 struct ieee754xctx ax;
113
114 if (!TSTX())
115 return r;
116 ax.op = op;
117 ax.rt = IEEE754_RT_SI;
118 ax.rv.si = r;
119 va_start(ax.ap, op);
120 ieee754_xcpt(&ax);
121 return ax.rv.si;
122}
123
124s64 ieee754di_xcpt(s64 r, const char *op, ...)
125{
126 struct ieee754xctx ax;
127
128 if (!TSTX())
129 return r;
130 ax.op = op;
131 ax.rt = IEEE754_RT_DI;
132 ax.rv.di = r;
133 va_start(ax.ap, op);
134 ieee754_xcpt(&ax);
135 return ax.rv.di;
136}