xref: /openbmc/linux/arch/mips/math-emu/ieee754.c (revision 8142294d)
11da177e4SLinus Torvalds /* ieee754 floating point arithmetic
21da177e4SLinus Torvalds  * single and double precision
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * BUGS
51da177e4SLinus Torvalds  * not much dp done
61da177e4SLinus Torvalds  * doesn't generate IEEE754_INEXACT
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds /*
101da177e4SLinus Torvalds  * MIPS floating point support
111da177e4SLinus Torvalds  * Copyright (C) 1994-2000 Algorithmics Ltd.
121da177e4SLinus Torvalds  * http://www.algor.co.uk
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  * ########################################################################
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *  This program is free software; you can distribute it and/or modify it
171da177e4SLinus Torvalds  *  under the terms of the GNU General Public License (Version 2) as
181da177e4SLinus Torvalds  *  published by the Free Software Foundation.
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  *  This program is distributed in the hope it will be useful, but WITHOUT
211da177e4SLinus Torvalds  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
221da177e4SLinus Torvalds  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
231da177e4SLinus Torvalds  *  for more details.
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  *  You should have received a copy of the GNU General Public License along
261da177e4SLinus Torvalds  *  with this program; if not, write to the Free Software Foundation, Inc.,
271da177e4SLinus Torvalds  *  59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
281da177e4SLinus Torvalds  *
291da177e4SLinus Torvalds  * ########################################################################
301da177e4SLinus Torvalds  */
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds #include "ieee754int.h"
34efec3c4eSRalf Baechle #include "ieee754sp.h"
35efec3c4eSRalf Baechle #include "ieee754dp.h"
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds #define DP_EBIAS	1023
381da177e4SLinus Torvalds #define DP_EMIN		(-1022)
391da177e4SLinus Torvalds #define DP_EMAX		1023
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #define SP_EBIAS	127
421da177e4SLinus Torvalds #define SP_EMIN		(-126)
431da177e4SLinus Torvalds #define SP_EMAX		127
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds /* special constants
461da177e4SLinus Torvalds */
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds #if (defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN) || defined(__MIPSEL__)
501da177e4SLinus Torvalds #define SPSTR(s, b, m) {m, b, s}
511da177e4SLinus Torvalds #define DPSTR(s, b, mh, ml) {ml, mh, b, s}
521da177e4SLinus Torvalds #endif
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds #ifdef __MIPSEB__
551da177e4SLinus Torvalds #define SPSTR(s, b, m) {s, b, m}
561da177e4SLinus Torvalds #define DPSTR(s, b, mh, ml) {s, b, mh, ml}
571da177e4SLinus Torvalds #endif
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds const struct ieee754dp_konst __ieee754dp_spcvals[] = {
601da177e4SLinus Torvalds 	DPSTR(0, DP_EMIN - 1 + DP_EBIAS, 0, 0),	/* + zero   */
611da177e4SLinus Torvalds 	DPSTR(1, DP_EMIN - 1 + DP_EBIAS, 0, 0),	/* - zero   */
621da177e4SLinus Torvalds 	DPSTR(0, DP_EBIAS, 0, 0),	/* + 1.0   */
631da177e4SLinus Torvalds 	DPSTR(1, DP_EBIAS, 0, 0),	/* - 1.0   */
641da177e4SLinus Torvalds 	DPSTR(0, 3 + DP_EBIAS, 0x40000, 0),	/* + 10.0   */
651da177e4SLinus Torvalds 	DPSTR(1, 3 + DP_EBIAS, 0x40000, 0),	/* - 10.0   */
661da177e4SLinus Torvalds 	DPSTR(0, DP_EMAX + 1 + DP_EBIAS, 0, 0),	/* + infinity */
671da177e4SLinus Torvalds 	DPSTR(1, DP_EMAX + 1 + DP_EBIAS, 0, 0),	/* - infinity */
681da177e4SLinus Torvalds 	DPSTR(0, DP_EMAX+1+DP_EBIAS, 0x7FFFF, 0xFFFFFFFF), /* + indef quiet Nan */
691da177e4SLinus Torvalds 	DPSTR(0, DP_EMAX + DP_EBIAS, 0xFFFFF, 0xFFFFFFFF),	/* + max */
701da177e4SLinus Torvalds 	DPSTR(1, DP_EMAX + DP_EBIAS, 0xFFFFF, 0xFFFFFFFF),	/* - max */
711da177e4SLinus Torvalds 	DPSTR(0, DP_EMIN + DP_EBIAS, 0, 0),	/* + min normal */
721da177e4SLinus Torvalds 	DPSTR(1, DP_EMIN + DP_EBIAS, 0, 0),	/* - min normal */
731da177e4SLinus Torvalds 	DPSTR(0, DP_EMIN - 1 + DP_EBIAS, 0, 1),	/* + min denormal */
741da177e4SLinus Torvalds 	DPSTR(1, DP_EMIN - 1 + DP_EBIAS, 0, 1),	/* - min denormal */
751da177e4SLinus Torvalds 	DPSTR(0, 31 + DP_EBIAS, 0, 0),	/* + 1.0e31 */
761da177e4SLinus Torvalds 	DPSTR(0, 63 + DP_EBIAS, 0, 0),	/* + 1.0e63 */
771da177e4SLinus Torvalds };
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds const struct ieee754sp_konst __ieee754sp_spcvals[] = {
801da177e4SLinus Torvalds 	SPSTR(0, SP_EMIN - 1 + SP_EBIAS, 0),	/* + zero   */
811da177e4SLinus Torvalds 	SPSTR(1, SP_EMIN - 1 + SP_EBIAS, 0),	/* - zero   */
821da177e4SLinus Torvalds 	SPSTR(0, SP_EBIAS, 0),	/* + 1.0   */
831da177e4SLinus Torvalds 	SPSTR(1, SP_EBIAS, 0),	/* - 1.0   */
841da177e4SLinus Torvalds 	SPSTR(0, 3 + SP_EBIAS, 0x200000),	/* + 10.0   */
851da177e4SLinus Torvalds 	SPSTR(1, 3 + SP_EBIAS, 0x200000),	/* - 10.0   */
861da177e4SLinus Torvalds 	SPSTR(0, SP_EMAX + 1 + SP_EBIAS, 0),	/* + infinity */
871da177e4SLinus Torvalds 	SPSTR(1, SP_EMAX + 1 + SP_EBIAS, 0),	/* - infinity */
881da177e4SLinus Torvalds 	SPSTR(0, SP_EMAX+1+SP_EBIAS, 0x3FFFFF),     /* + indef quiet Nan  */
891da177e4SLinus Torvalds 	SPSTR(0, SP_EMAX + SP_EBIAS, 0x7FFFFF),	/* + max normal */
901da177e4SLinus Torvalds 	SPSTR(1, SP_EMAX + SP_EBIAS, 0x7FFFFF),	/* - max normal */
911da177e4SLinus Torvalds 	SPSTR(0, SP_EMIN + SP_EBIAS, 0),	/* + min normal */
921da177e4SLinus Torvalds 	SPSTR(1, SP_EMIN + SP_EBIAS, 0),	/* - min normal */
931da177e4SLinus Torvalds 	SPSTR(0, SP_EMIN - 1 + SP_EBIAS, 1),	/* + min denormal */
941da177e4SLinus Torvalds 	SPSTR(1, SP_EMIN - 1 + SP_EBIAS, 1),	/* - min denormal */
951da177e4SLinus Torvalds 	SPSTR(0, 31 + SP_EBIAS, 0),	/* + 1.0e31 */
961da177e4SLinus Torvalds 	SPSTR(0, 63 + SP_EBIAS, 0),	/* + 1.0e63 */
971da177e4SLinus Torvalds };
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds int ieee754si_xcpt(int r, const char *op, ...)
1011da177e4SLinus Torvalds {
1021da177e4SLinus Torvalds 	struct ieee754xctx ax;
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 	if (!TSTX())
1051da177e4SLinus Torvalds 		return r;
1061da177e4SLinus Torvalds 	ax.op = op;
1071da177e4SLinus Torvalds 	ax.rt = IEEE754_RT_SI;
1081da177e4SLinus Torvalds 	ax.rv.si = r;
1091da177e4SLinus Torvalds 	va_start(ax.ap, op);
1101da177e4SLinus Torvalds 	ieee754_xcpt(&ax);
1118142294dSRichard Knutsson 	va_end(ax.ap);
1121da177e4SLinus Torvalds 	return ax.rv.si;
1131da177e4SLinus Torvalds }
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds s64 ieee754di_xcpt(s64 r, const char *op, ...)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	struct ieee754xctx ax;
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds 	if (!TSTX())
1201da177e4SLinus Torvalds 		return r;
1211da177e4SLinus Torvalds 	ax.op = op;
1221da177e4SLinus Torvalds 	ax.rt = IEEE754_RT_DI;
1231da177e4SLinus Torvalds 	ax.rv.di = r;
1241da177e4SLinus Torvalds 	va_start(ax.ap, op);
1251da177e4SLinus Torvalds 	ieee754_xcpt(&ax);
1268142294dSRichard Knutsson 	va_end(ax.ap);
1271da177e4SLinus Torvalds 	return ax.rv.di;
1281da177e4SLinus Torvalds }
129