blob: cb1b6822711ab90c04591d84e8dc2570f16ba8ec [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/* special constants
46*/
47
48
49#if (defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN) || defined(__MIPSEL__)
Ralf Baechle21a151d2007-10-11 23:46:15 +010050#define SPSTR(s, b, m) {m, b, s}
51#define DPSTR(s, b, mh, ml) {ml, mh, b, s}
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#endif
53
54#ifdef __MIPSEB__
Ralf Baechle21a151d2007-10-11 23:46:15 +010055#define SPSTR(s, b, m) {s, b, m}
56#define DPSTR(s, b, mh, ml) {s, b, mh, ml}
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#endif
58
59const struct ieee754dp_konst __ieee754dp_spcvals[] = {
60 DPSTR(0, DP_EMIN - 1 + DP_EBIAS, 0, 0), /* + zero */
61 DPSTR(1, DP_EMIN - 1 + DP_EBIAS, 0, 0), /* - zero */
62 DPSTR(0, DP_EBIAS, 0, 0), /* + 1.0 */
63 DPSTR(1, DP_EBIAS, 0, 0), /* - 1.0 */
64 DPSTR(0, 3 + DP_EBIAS, 0x40000, 0), /* + 10.0 */
65 DPSTR(1, 3 + DP_EBIAS, 0x40000, 0), /* - 10.0 */
66 DPSTR(0, DP_EMAX + 1 + DP_EBIAS, 0, 0), /* + infinity */
67 DPSTR(1, DP_EMAX + 1 + DP_EBIAS, 0, 0), /* - infinity */
Ralf Baechle21a151d2007-10-11 23:46:15 +010068 DPSTR(0, DP_EMAX+1+DP_EBIAS, 0x7FFFF, 0xFFFFFFFF), /* + indef quiet Nan */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 DPSTR(0, DP_EMAX + DP_EBIAS, 0xFFFFF, 0xFFFFFFFF), /* + max */
70 DPSTR(1, DP_EMAX + DP_EBIAS, 0xFFFFF, 0xFFFFFFFF), /* - max */
71 DPSTR(0, DP_EMIN + DP_EBIAS, 0, 0), /* + min normal */
72 DPSTR(1, DP_EMIN + DP_EBIAS, 0, 0), /* - min normal */
73 DPSTR(0, DP_EMIN - 1 + DP_EBIAS, 0, 1), /* + min denormal */
74 DPSTR(1, DP_EMIN - 1 + DP_EBIAS, 0, 1), /* - min denormal */
75 DPSTR(0, 31 + DP_EBIAS, 0, 0), /* + 1.0e31 */
76 DPSTR(0, 63 + DP_EBIAS, 0, 0), /* + 1.0e63 */
77};
78
79const struct ieee754sp_konst __ieee754sp_spcvals[] = {
80 SPSTR(0, SP_EMIN - 1 + SP_EBIAS, 0), /* + zero */
81 SPSTR(1, SP_EMIN - 1 + SP_EBIAS, 0), /* - zero */
82 SPSTR(0, SP_EBIAS, 0), /* + 1.0 */
83 SPSTR(1, SP_EBIAS, 0), /* - 1.0 */
84 SPSTR(0, 3 + SP_EBIAS, 0x200000), /* + 10.0 */
85 SPSTR(1, 3 + SP_EBIAS, 0x200000), /* - 10.0 */
86 SPSTR(0, SP_EMAX + 1 + SP_EBIAS, 0), /* + infinity */
87 SPSTR(1, SP_EMAX + 1 + SP_EBIAS, 0), /* - infinity */
Ralf Baechle21a151d2007-10-11 23:46:15 +010088 SPSTR(0, SP_EMAX+1+SP_EBIAS, 0x3FFFFF), /* + indef quiet Nan */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 SPSTR(0, SP_EMAX + SP_EBIAS, 0x7FFFFF), /* + max normal */
90 SPSTR(1, SP_EMAX + SP_EBIAS, 0x7FFFFF), /* - max normal */
91 SPSTR(0, SP_EMIN + SP_EBIAS, 0), /* + min normal */
92 SPSTR(1, SP_EMIN + SP_EBIAS, 0), /* - min normal */
93 SPSTR(0, SP_EMIN - 1 + SP_EBIAS, 1), /* + min denormal */
94 SPSTR(1, SP_EMIN - 1 + SP_EBIAS, 1), /* - min denormal */
95 SPSTR(0, 31 + SP_EBIAS, 0), /* + 1.0e31 */
96 SPSTR(0, 63 + SP_EBIAS, 0), /* + 1.0e63 */
97};
98
99
100int ieee754si_xcpt(int r, const char *op, ...)
101{
102 struct ieee754xctx ax;
103
104 if (!TSTX())
105 return r;
106 ax.op = op;
107 ax.rt = IEEE754_RT_SI;
108 ax.rv.si = r;
109 va_start(ax.ap, op);
110 ieee754_xcpt(&ax);
Richard Knutsson81422942007-11-24 22:22:19 +0100111 va_end(ax.ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return ax.rv.si;
113}
114
115s64 ieee754di_xcpt(s64 r, const char *op, ...)
116{
117 struct ieee754xctx ax;
118
119 if (!TSTX())
120 return r;
121 ax.op = op;
122 ax.rt = IEEE754_RT_DI;
123 ax.rv.di = r;
124 va_start(ax.ap, op);
125 ieee754_xcpt(&ax);
Richard Knutsson81422942007-11-24 22:22:19 +0100126 va_end(ax.ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return ax.rv.di;
128}