blob: a5ca108ce4679aba5cd003a25542fc6705c08cb3 [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.,
Ralf Baechle3f7cac42014-04-26 01:49:14 +020016 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 *
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 Baechlecae55062014-04-16 00:47:59 +020027#include <linux/compiler.h>
Ralf Baechlecd21dfc2005-04-28 13:39:10 +000028#include <asm/byteorder.h>
Ralf Baechlee812a732014-04-19 14:20:54 +020029#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/types.h>
Ralf Baechlecd21dfc2005-04-28 13:39:10 +000031#include <linux/sched.h>
Ralf Baechlef80cc082014-04-16 00:40:02 +020032#include <asm/bitfield.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Ralf Baechle2209bcb2014-04-16 01:31:11 +020034union ieee754dp {
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 struct {
Ralf Baechlef80cc082014-04-16 00:40:02 +020036 __BITFIELD_FIELD(unsigned int sign:1,
37 __BITFIELD_FIELD(unsigned int bexp:11,
38 __BITFIELD_FIELD(u64 mant:52,
39 ;)))
Ralf Baechle49548b02014-04-25 15:48:40 +020040 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 u64 bits;
Ralf Baechle2209bcb2014-04-16 01:31:11 +020042};
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Ralf Baechle2209bcb2014-04-16 01:31:11 +020044union ieee754sp {
Ralf Baechle49548b02014-04-25 15:48:40 +020045 struct {
46 __BITFIELD_FIELD(unsigned sign:1,
47 __BITFIELD_FIELD(unsigned bexp:8,
48 __BITFIELD_FIELD(unsigned mant:23,
49 ;)))
50 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 u32 bits;
Ralf Baechle2209bcb2014-04-16 01:31:11 +020052};
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/*
55 * single precision (often aka float)
56*/
Ralf Baechle2209bcb2014-04-16 01:31:11 +020057int ieee754sp_class(union ieee754sp x);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Ralf Baechle2209bcb2014-04-16 01:31:11 +020059union ieee754sp ieee754sp_abs(union ieee754sp x);
60union ieee754sp ieee754sp_neg(union ieee754sp x);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Ralf Baechle2209bcb2014-04-16 01:31:11 +020062union ieee754sp ieee754sp_add(union ieee754sp x, union ieee754sp y);
63union ieee754sp ieee754sp_sub(union ieee754sp x, union ieee754sp y);
64union ieee754sp ieee754sp_mul(union ieee754sp x, union ieee754sp y);
65union ieee754sp ieee754sp_div(union ieee754sp x, union ieee754sp y);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Ralf Baechle2209bcb2014-04-16 01:31:11 +020067union ieee754sp ieee754sp_fint(int x);
Ralf Baechle2209bcb2014-04-16 01:31:11 +020068union ieee754sp ieee754sp_flong(s64 x);
Ralf Baechle2209bcb2014-04-16 01:31:11 +020069union ieee754sp ieee754sp_fdp(union ieee754dp x);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Ralf Baechle2209bcb2014-04-16 01:31:11 +020071int ieee754sp_tint(union ieee754sp x);
Ralf Baechle2209bcb2014-04-16 01:31:11 +020072s64 ieee754sp_tlong(union ieee754sp x);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Ralf Baechle2209bcb2014-04-16 01:31:11 +020074int ieee754sp_cmp(union ieee754sp x, union ieee754sp y, int cop, int sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Ralf Baechle2209bcb2014-04-16 01:31:11 +020076union ieee754sp ieee754sp_sqrt(union ieee754sp x);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/*
79 * double precision (often aka double)
80*/
Ralf Baechle2209bcb2014-04-16 01:31:11 +020081int ieee754dp_class(union ieee754dp x);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Ralf Baechle2209bcb2014-04-16 01:31:11 +020083union ieee754dp ieee754dp_add(union ieee754dp x, union ieee754dp y);
84union ieee754dp ieee754dp_sub(union ieee754dp x, union ieee754dp y);
85union ieee754dp ieee754dp_mul(union ieee754dp x, union ieee754dp y);
86union ieee754dp ieee754dp_div(union ieee754dp x, union ieee754dp y);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Ralf Baechle2209bcb2014-04-16 01:31:11 +020088union ieee754dp ieee754dp_abs(union ieee754dp x);
89union ieee754dp ieee754dp_neg(union ieee754dp x);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Ralf Baechle2209bcb2014-04-16 01:31:11 +020091union ieee754dp ieee754dp_fint(int x);
Ralf Baechle2209bcb2014-04-16 01:31:11 +020092union ieee754dp ieee754dp_flong(s64 x);
Ralf Baechle2209bcb2014-04-16 01:31:11 +020093union ieee754dp ieee754dp_fsp(union ieee754sp x);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Ralf Baechle2209bcb2014-04-16 01:31:11 +020095int ieee754dp_tint(union ieee754dp x);
Ralf Baechle2209bcb2014-04-16 01:31:11 +020096s64 ieee754dp_tlong(union ieee754dp x);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Ralf Baechle2209bcb2014-04-16 01:31:11 +020098int ieee754dp_cmp(union ieee754dp x, union ieee754dp y, int cop, int sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Ralf Baechle2209bcb2014-04-16 01:31:11 +0200100union ieee754dp ieee754dp_sqrt(union ieee754dp x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102
103
104/* 5 types of floating point number
105*/
Ralf Baechle9e8bad12014-04-19 00:36:32 +0200106enum {
107 IEEE754_CLASS_NORM = 0x00,
108 IEEE754_CLASS_ZERO = 0x01,
109 IEEE754_CLASS_DNORM = 0x02,
110 IEEE754_CLASS_INF = 0x03,
111 IEEE754_CLASS_SNAN = 0x04,
112 IEEE754_CLASS_QNAN = 0x05,
113};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115/* exception numbers */
116#define IEEE754_INEXACT 0x01
117#define IEEE754_UNDERFLOW 0x02
118#define IEEE754_OVERFLOW 0x04
119#define IEEE754_ZERO_DIVIDE 0x08
120#define IEEE754_INVALID_OPERATION 0x10
121
122/* cmp operators
123*/
124#define IEEE754_CLT 0x01
125#define IEEE754_CEQ 0x02
126#define IEEE754_CGT 0x04
127#define IEEE754_CUN 0x08
128
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000129/*
130 * The control status register
131 */
132struct _ieee754_csr {
Maciej W. Rozyckif1f3b7e2015-04-03 23:27:38 +0100133 __BITFIELD_FIELD(unsigned fcc:7, /* condition[7:1] */
134 __BITFIELD_FIELD(unsigned nod:1, /* set 1 for no denormals */
135 __BITFIELD_FIELD(unsigned c:1, /* condition[0] */
136 __BITFIELD_FIELD(unsigned pad0:3,
137 __BITFIELD_FIELD(unsigned abs2008:1, /* IEEE 754-2008 ABS/NEG.fmt */
138 __BITFIELD_FIELD(unsigned nan2008:1, /* IEEE 754-2008 NaN mode */
Ralf Baechlef80cc082014-04-16 00:40:02 +0200139 __BITFIELD_FIELD(unsigned cx:6, /* exceptions this operation */
140 __BITFIELD_FIELD(unsigned mx:5, /* exception enable mask */
141 __BITFIELD_FIELD(unsigned sx:5, /* exceptions total */
142 __BITFIELD_FIELD(unsigned rm:2, /* current rounding mode */
Maciej W. Rozyckif1f3b7e2015-04-03 23:27:38 +0100143 ;))))))))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
Atsushi Nemotoeae89072006-05-16 01:26:03 +0900145#define ieee754_csr (*(struct _ieee754_csr *)(&current->thread.fpu.fcr31))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000147static inline unsigned ieee754_getrm(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 return (ieee754_csr.rm);
150}
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000151static inline unsigned ieee754_setrm(unsigned rm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 return (ieee754_csr.rm = rm);
154}
155
156/*
157 * get current exceptions
158 */
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000159static inline unsigned ieee754_getcx(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 return (ieee754_csr.cx);
162}
163
164/* test for current exception condition
165 */
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000166static inline int ieee754_cxtest(unsigned n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 return (ieee754_csr.cx & n);
169}
170
171/*
172 * get sticky exceptions
173 */
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000174static inline unsigned ieee754_getsx(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 return (ieee754_csr.sx);
177}
178
179/* clear sticky conditions
180*/
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000181static inline unsigned ieee754_clrsx(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 return (ieee754_csr.sx = 0);
184}
185
186/* test for sticky exception condition
187 */
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000188static inline int ieee754_sxtest(unsigned n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 return (ieee754_csr.sx & n);
191}
192
193/* debugging */
Ralf Baechle2209bcb2014-04-16 01:31:11 +0200194union ieee754sp ieee754sp_dump(char *s, union ieee754sp x);
195union ieee754dp ieee754dp_dump(char *s, union ieee754dp x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Maciej W. Rozycki1796ec72015-04-03 23:24:09 +0100197#define IEEE754_SPCVAL_PZERO 0 /* +0.0 */
198#define IEEE754_SPCVAL_NZERO 1 /* -0.0 */
199#define IEEE754_SPCVAL_PONE 2 /* +1.0 */
200#define IEEE754_SPCVAL_NONE 3 /* -1.0 */
201#define IEEE754_SPCVAL_PTEN 4 /* +10.0 */
202#define IEEE754_SPCVAL_NTEN 5 /* -10.0 */
203#define IEEE754_SPCVAL_PINFINITY 6 /* +inf */
204#define IEEE754_SPCVAL_NINFINITY 7 /* -inf */
205#define IEEE754_SPCVAL_INDEF 8 /* quiet NaN */
206#define IEEE754_SPCVAL_PMAX 9 /* +max norm */
207#define IEEE754_SPCVAL_NMAX 10 /* -max norm */
208#define IEEE754_SPCVAL_PMIN 11 /* +min norm */
209#define IEEE754_SPCVAL_NMIN 12 /* -min norm */
210#define IEEE754_SPCVAL_PMIND 13 /* +min denorm */
211#define IEEE754_SPCVAL_NMIND 14 /* -min denorm */
212#define IEEE754_SPCVAL_P1E31 15 /* + 1.0e31 */
213#define IEEE754_SPCVAL_P1E63 16 /* + 1.0e63 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Ralf Baechle49548b02014-04-25 15:48:40 +0200215extern const union ieee754dp __ieee754dp_spcvals[];
216extern const union ieee754sp __ieee754sp_spcvals[];
Ralf Baechle2209bcb2014-04-16 01:31:11 +0200217#define ieee754dp_spcvals ((const union ieee754dp *)__ieee754dp_spcvals)
218#define ieee754sp_spcvals ((const union ieee754sp *)__ieee754sp_spcvals)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000220/*
221 * Return infinity with given sign
222 */
223#define ieee754dp_inf(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PINFINITY+(sn)])
224#define ieee754dp_zero(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PZERO+(sn)])
225#define ieee754dp_one(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PONE+(sn)])
226#define ieee754dp_ten(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PTEN+(sn)])
227#define ieee754dp_indef() (ieee754dp_spcvals[IEEE754_SPCVAL_INDEF])
228#define ieee754dp_max(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PMAX+(sn)])
229#define ieee754dp_min(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PMIN+(sn)])
230#define ieee754dp_mind(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PMIND+(sn)])
231#define ieee754dp_1e31() (ieee754dp_spcvals[IEEE754_SPCVAL_P1E31])
232#define ieee754dp_1e63() (ieee754dp_spcvals[IEEE754_SPCVAL_P1E63])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000234#define ieee754sp_inf(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PINFINITY+(sn)])
235#define ieee754sp_zero(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PZERO+(sn)])
236#define ieee754sp_one(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PONE+(sn)])
237#define ieee754sp_ten(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PTEN+(sn)])
238#define ieee754sp_indef() (ieee754sp_spcvals[IEEE754_SPCVAL_INDEF])
239#define ieee754sp_max(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PMAX+(sn)])
240#define ieee754sp_min(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PMIN+(sn)])
241#define ieee754sp_mind(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PMIND+(sn)])
242#define ieee754sp_1e31() (ieee754sp_spcvals[IEEE754_SPCVAL_P1E31])
243#define ieee754sp_1e63() (ieee754sp_spcvals[IEEE754_SPCVAL_P1E63])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000245/*
246 * Indefinite integer value
247 */
Ralf Baechlee812a732014-04-19 14:20:54 +0200248static inline int ieee754si_indef(void)
249{
250 return INT_MAX;
251}
252
253static inline s64 ieee754di_indef(void)
254{
255 return S64_MAX;
256}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258/* result types for xctx.rt */
259#define IEEE754_RT_SP 0
260#define IEEE754_RT_DP 1
261#define IEEE754_RT_XP 2
262#define IEEE754_RT_SI 3
263#define IEEE754_RT_DI 4
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265/* compat */
266#define ieee754dp_fix(x) ieee754dp_tint(x)
267#define ieee754sp_fix(x) ieee754sp_tint(x)
Ralf Baechlea663bf92005-10-23 13:44:31 +0100268
269#endif /* __ARCH_MIPS_MATH_EMU_IEEE754_H */