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 __FBSDID("$FreeBSD: stable/12/lib/libc/sparc64/fpu/fpu_qp.c 326193 2017-11-25 17:12:48Z pfg $");
31 
32 #include <sys/types.h>
33 #include <machine/fsr.h>
34 
35 #include "fpu_emu.h"
36 #include "fpu_extern.h"
37 
38 #define	_QP_OP(op) \
39 void _Qp_ ## op(u_int *c, u_int *a, u_int *b); \
40 void \
41 _Qp_ ## op(u_int *c, u_int *a, u_int *b) \
42 { \
43 	struct fpemu fe; \
44 	struct fpn *r; \
45 	__asm __volatile("stx %%fsr, %0" : "=m" (fe.fe_fsr) :); \
46 	fe.fe_cx = 0; \
47 	fe.fe_f1.fp_sign = a[0] >> 31; \
48 	fe.fe_f1.fp_sticky = 0; \
49 	fe.fe_f1.fp_class = __fpu_qtof(&fe.fe_f1, a[0], a[1], a[2], a[3]); \
50 	fe.fe_f2.fp_sign = b[0] >> 31; \
51 	fe.fe_f2.fp_sticky = 0; \
52 	fe.fe_f2.fp_class = __fpu_qtof(&fe.fe_f2, b[0], b[1], b[2], b[3]); \
53 	r = __fpu_ ## op(&fe); \
54 	c[0] = __fpu_ftoq(&fe, r, c); \
55 	fe.fe_fsr |= fe.fe_cx << FSR_AEXC_SHIFT; \
56 	__asm __volatile("ldx %0, %%fsr" : : "m" (fe.fe_fsr)); \
57 }
58 
59 #define	_QP_TTOQ(qname, fname, ntype, signpos, atype, ...) \
60 void _Qp_ ## qname ## toq(u_int *c, ntype n); \
61 void \
62 _Qp_ ## qname ## toq(u_int *c, ntype n) \
63 { \
64 	struct fpemu fe; \
65 	union { atype a[2]; ntype n; } u = { .n = n }; \
66 	__asm __volatile("stx %%fsr, %0" : "=m" (fe.fe_fsr) :); \
67 	fe.fe_cx = 0; \
68 	fe.fe_f1.fp_sign = (signpos >= 0) ? u.a[0] >> signpos : 0; \
69 	fe.fe_f1.fp_sticky = 0; \
70 	fe.fe_f1.fp_class = __fpu_ ## fname ## tof(&fe.fe_f1, __VA_ARGS__); \
71 	c[0] = __fpu_ftoq(&fe, &fe.fe_f1, c); \
72 	fe.fe_fsr |= fe.fe_cx << FSR_AEXC_SHIFT; \
73 	__asm __volatile("ldx %0, %%fsr" : : "m" (fe.fe_fsr)); \
74 }
75 
76 #define	_QP_QTOT(qname, fname, type, ...) \
77 type _Qp_qto ## qname(u_int *c); \
78 type \
79 _Qp_qto ## qname(u_int *c) \
80 { \
81 	struct fpemu fe; \
82 	union { u_int a; type n; } u; \
83 	__asm __volatile("stx %%fsr, %0" : "=m" (fe.fe_fsr) :); \
84 	fe.fe_cx = 0; \
85 	fe.fe_f1.fp_sign = c[0] >> 31; \
86 	fe.fe_f1.fp_sticky = 0; \
87 	fe.fe_f1.fp_class = __fpu_qtof(&fe.fe_f1, c[0], c[1], c[2], c[3]); \
88 	u.a = __fpu_fto ## fname(&fe, &fe.fe_f1, ## __VA_ARGS__); \
89 	fe.fe_fsr |= fe.fe_cx << FSR_AEXC_SHIFT; \
90 	__asm __volatile("ldx %0, %%fsr" : : "m" (fe.fe_fsr)); \
91 	return (u.n); \
92 }
93 
94 #define	FCC_EQ(fcc)	((fcc) == FSR_CC_EQ)
95 #define	FCC_GE(fcc)	((fcc) == FSR_CC_EQ || (fcc) == FSR_CC_GT)
96 #define	FCC_GT(fcc)	((fcc) == FSR_CC_GT)
97 #define	FCC_LE(fcc)	((fcc) == FSR_CC_EQ || (fcc) == FSR_CC_LT)
98 #define	FCC_LT(fcc)	((fcc) == FSR_CC_LT)
99 #define	FCC_NE(fcc)	((fcc) != FSR_CC_EQ)
100 #define	FCC_ID(fcc)	(fcc)
101 
102 #define	_QP_CMP(name, cmpe, test) \
103 int _Qp_ ## name(u_int *a, u_int *b) ; \
104 int \
105 _Qp_ ## name(u_int *a, u_int *b) \
106 { \
107 	struct fpemu fe; \
108 	__asm __volatile("stx %%fsr, %0" : "=m" (fe.fe_fsr) :); \
109 	fe.fe_cx = 0; \
110 	fe.fe_f1.fp_sign = a[0] >> 31; \
111 	fe.fe_f1.fp_sticky = 0; \
112 	fe.fe_f1.fp_class = __fpu_qtof(&fe.fe_f1, a[0], a[1], a[2], a[3]); \
113 	fe.fe_f2.fp_sign = b[0] >> 31; \
114 	fe.fe_f2.fp_sticky = 0; \
115 	fe.fe_f2.fp_class = __fpu_qtof(&fe.fe_f2, b[0], b[1], b[2], b[3]); \
116 	__fpu_compare(&fe, cmpe, 0); \
117 	fe.fe_fsr |= fe.fe_cx << FSR_AEXC_SHIFT; \
118 	__asm __volatile("ldx %0, %%fsr" : : "m" (fe.fe_fsr)); \
119 	return (test(FSR_GET_FCC0(fe.fe_fsr))); \
120 }
121 
122 void _Qp_sqrt(u_int *c, u_int *a);
123 void
_Qp_sqrt(u_int * c,u_int * a)124 _Qp_sqrt(u_int *c, u_int *a)
125 {
126 	struct fpemu fe;
127 	struct fpn *r;
128 	__asm __volatile("stx %%fsr, %0" : "=m" (fe.fe_fsr) :);
129 	fe.fe_cx = 0;
130 	fe.fe_f1.fp_sign = a[0] >> 31;
131 	fe.fe_f1.fp_sticky = 0;
132 	fe.fe_f1.fp_class = __fpu_qtof(&fe.fe_f1, a[0], a[1], a[2], a[3]);
133 	r = __fpu_sqrt(&fe);
134 	c[0] = __fpu_ftoq(&fe, r, c);
135 	fe.fe_fsr |= fe.fe_cx << FSR_AEXC_SHIFT;
136 	__asm __volatile("ldx %0, %%fsr" : : "m" (fe.fe_fsr));
137 }
138 
139 _QP_OP(add)
140 _QP_OP(div)
141 _QP_OP(mul)
142 _QP_OP(sub)
143 
144 _QP_TTOQ(d,	d,	double,	31, u_int,  u.a[0], u.a[1])
145 _QP_TTOQ(i,	i,	int,	31, u_int,  u.a[0])
146 _QP_TTOQ(s,	s,	float,	31, u_int,  u.a[0])
147 _QP_TTOQ(x,	x,	long,	63, u_long, u.a[0])
148 _QP_TTOQ(ui,	i,	u_int,	-1, u_int,  u.a[0])
149 _QP_TTOQ(ux,	x,	u_long,	-1, u_long, u.a[0])
150 
151 _QP_QTOT(d,	d,	double,	&u.a)
152 _QP_QTOT(i,	i,	int)
153 _QP_QTOT(s,	s,	float)
154 _QP_QTOT(x,	x,	long,	&u.a)
155 _QP_QTOT(ui,	i,	u_int)
156 _QP_QTOT(ux,	x,	u_long,	&u.a)
157 
158 _QP_CMP(feq,	0,	FCC_EQ)
159 _QP_CMP(fge,	1,	FCC_GE)
160 _QP_CMP(fgt,	1,	FCC_GT)
161 _QP_CMP(fle,	1,	FCC_LE)
162 _QP_CMP(flt,	1,	FCC_LT)
163 _QP_CMP(fne,	0, 	FCC_NE)
164 _QP_CMP(cmp,	0, 	FCC_ID)
165 _QP_CMP(cmpe,	1, 	FCC_ID)
166