xref: /haiku/src/system/libroot/os/arch/sparc/softfloat.c (revision 803a4704e82749bce36c05ee56792dad62576144)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2002 Jake Burkholder.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 
31 #include <sys/types.h>
32 
33 #include "fsr.h"
34 #include "fpu_emu.h"
35 #include "fpu_extern.h"
36 
37 #define	_QP_OP(op) \
38 void _Qp_ ## op(u_int *c, u_int *a, u_int *b); \
39 void \
40 _Qp_ ## op(u_int *c, u_int *a, u_int *b) \
41 { \
42 	struct fpemu fe; \
43 	struct fpn *r; \
44 	__asm __volatile("stx %%fsr, %0" : "=m" (fe.fe_fsr) :); \
45 	fe.fe_cx = 0; \
46 	fe.fe_f1.fp_sign = a[0] >> 31; \
47 	fe.fe_f1.fp_sticky = 0; \
48 	fe.fe_f1.fp_class = __fpu_qtof(&fe.fe_f1, a[0], a[1], a[2], a[3]); \
49 	fe.fe_f2.fp_sign = b[0] >> 31; \
50 	fe.fe_f2.fp_sticky = 0; \
51 	fe.fe_f2.fp_class = __fpu_qtof(&fe.fe_f2, b[0], b[1], b[2], b[3]); \
52 	r = __fpu_ ## op(&fe); \
53 	c[0] = __fpu_ftoq(&fe, r, c); \
54 	fe.fe_fsr |= fe.fe_cx << FSR_AEXC_SHIFT; \
55 	__asm __volatile("ldx %0, %%fsr" : : "m" (fe.fe_fsr)); \
56 }
57 
58 #define	_QP_TTOQ(qname, fname, ntype, signpos, atype, ...) \
59 void _Qp_ ## qname ## toq(u_int *c, ntype n); \
60 void \
61 _Qp_ ## qname ## toq(u_int *c, ntype n) \
62 { \
63 	struct fpemu fe; \
64 	union { atype a[2]; ntype n; } u = { .n = n }; \
65 	__asm __volatile("stx %%fsr, %0" : "=m" (fe.fe_fsr) :); \
66 	fe.fe_cx = 0; \
67 	fe.fe_f1.fp_sign = (signpos >= 0) ? u.a[0] >> signpos : 0; \
68 	fe.fe_f1.fp_sticky = 0; \
69 	fe.fe_f1.fp_class = __fpu_ ## fname ## tof(&fe.fe_f1, __VA_ARGS__); \
70 	c[0] = __fpu_ftoq(&fe, &fe.fe_f1, c); \
71 	fe.fe_fsr |= fe.fe_cx << FSR_AEXC_SHIFT; \
72 	__asm __volatile("ldx %0, %%fsr" : : "m" (fe.fe_fsr)); \
73 }
74 
75 #define	_QP_QTOT(qname, fname, type, ...) \
76 type _Qp_qto ## qname(u_int *c); \
77 type \
78 _Qp_qto ## qname(u_int *c) \
79 { \
80 	struct fpemu fe; \
81 	union { u_int a; type n; } u; \
82 	__asm __volatile("stx %%fsr, %0" : "=m" (fe.fe_fsr) :); \
83 	fe.fe_cx = 0; \
84 	fe.fe_f1.fp_sign = c[0] >> 31; \
85 	fe.fe_f1.fp_sticky = 0; \
86 	fe.fe_f1.fp_class = __fpu_qtof(&fe.fe_f1, c[0], c[1], c[2], c[3]); \
87 	u.a = __fpu_fto ## fname(&fe, &fe.fe_f1, ## __VA_ARGS__); \
88 	fe.fe_fsr |= fe.fe_cx << FSR_AEXC_SHIFT; \
89 	__asm __volatile("ldx %0, %%fsr" : : "m" (fe.fe_fsr)); \
90 	return (u.n); \
91 }
92 
93 #define	FCC_EQ(fcc)	((fcc) == FSR_CC_EQ)
94 #define	FCC_GE(fcc)	((fcc) == FSR_CC_EQ || (fcc) == FSR_CC_GT)
95 #define	FCC_GT(fcc)	((fcc) == FSR_CC_GT)
96 #define	FCC_LE(fcc)	((fcc) == FSR_CC_EQ || (fcc) == FSR_CC_LT)
97 #define	FCC_LT(fcc)	((fcc) == FSR_CC_LT)
98 #define	FCC_NE(fcc)	((fcc) != FSR_CC_EQ)
99 #define	FCC_ID(fcc)	(fcc)
100 
101 #define	_QP_CMP(name, cmpe, test) \
102 int _Qp_ ## name(u_int *a, u_int *b) ; \
103 int \
104 _Qp_ ## name(u_int *a, u_int *b) \
105 { \
106 	struct fpemu fe; \
107 	__asm __volatile("stx %%fsr, %0" : "=m" (fe.fe_fsr) :); \
108 	fe.fe_cx = 0; \
109 	fe.fe_f1.fp_sign = a[0] >> 31; \
110 	fe.fe_f1.fp_sticky = 0; \
111 	fe.fe_f1.fp_class = __fpu_qtof(&fe.fe_f1, a[0], a[1], a[2], a[3]); \
112 	fe.fe_f2.fp_sign = b[0] >> 31; \
113 	fe.fe_f2.fp_sticky = 0; \
114 	fe.fe_f2.fp_class = __fpu_qtof(&fe.fe_f2, b[0], b[1], b[2], b[3]); \
115 	__fpu_compare(&fe, cmpe, 0); \
116 	fe.fe_fsr |= fe.fe_cx << FSR_AEXC_SHIFT; \
117 	__asm __volatile("ldx %0, %%fsr" : : "m" (fe.fe_fsr)); \
118 	return (test(FSR_GET_FCC0(fe.fe_fsr))); \
119 }
120 
121 void _Qp_sqrt(u_int *c, u_int *a);
122 void
_Qp_sqrt(u_int * c,u_int * a)123 _Qp_sqrt(u_int *c, u_int *a)
124 {
125 	struct fpemu fe;
126 	struct fpn *r;
127 	__asm __volatile("stx %%fsr, %0" : "=m" (fe.fe_fsr) :);
128 	fe.fe_cx = 0;
129 	fe.fe_f1.fp_sign = a[0] >> 31;
130 	fe.fe_f1.fp_sticky = 0;
131 	fe.fe_f1.fp_class = __fpu_qtof(&fe.fe_f1, a[0], a[1], a[2], a[3]);
132 	r = __fpu_sqrt(&fe);
133 	c[0] = __fpu_ftoq(&fe, r, c);
134 	fe.fe_fsr |= fe.fe_cx << FSR_AEXC_SHIFT;
135 	__asm __volatile("ldx %0, %%fsr" : : "m" (fe.fe_fsr));
136 }
137 
138 _QP_OP(add)
139 _QP_OP(div)
140 _QP_OP(mul)
141 _QP_OP(sub)
142 
143 _QP_TTOQ(d,	d,	double,	31, u_int,  u.a[0], u.a[1])
144 _QP_TTOQ(i,	i,	int,	31, u_int,  u.a[0])
145 _QP_TTOQ(s,	s,	float,	31, u_int,  u.a[0])
146 _QP_TTOQ(x,	x,	long,	63, u_long, u.a[0])
147 _QP_TTOQ(ui,	i,	u_int,	-1, u_int,  u.a[0])
148 _QP_TTOQ(ux,	x,	u_long,	-1, u_long, u.a[0])
149 
150 _QP_QTOT(d,	d,	double,	&u.a)
151 _QP_QTOT(i,	i,	int)
152 _QP_QTOT(s,	s,	float)
153 _QP_QTOT(x,	x,	long,	&u.a)
154 _QP_QTOT(ui,	i,	u_int)
155 _QP_QTOT(ux,	x,	u_long,	&u.a)
156 
157 _QP_CMP(feq,	0,	FCC_EQ)
158 _QP_CMP(fge,	1,	FCC_GE)
159 _QP_CMP(fgt,	1,	FCC_GT)
160 _QP_CMP(fle,	1,	FCC_LE)
161 _QP_CMP(flt,	1,	FCC_LT)
162 _QP_CMP(fne,	0, 	FCC_NE)
163 _QP_CMP(cmp,	0, 	FCC_ID)
164 _QP_CMP(cmpe,	1, 	FCC_ID)
165