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