fpu_qp.c revision 147519
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: head/lib/libc/sparc64/fpu/fpu_qp.c 147519 2005-06-21 21:13:07Z stefanf $");
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_f1.fp_sign = a[0] >> 31; \
45	fe.fe_f1.fp_sticky = 0; \
46	fe.fe_f1.fp_class = __fpu_qtof(&fe.fe_f1, a[0], a[1], a[2], a[3]); \
47	fe.fe_f2.fp_sign = b[0] >> 31; \
48	fe.fe_f2.fp_sticky = 0; \
49	fe.fe_f2.fp_class = __fpu_qtof(&fe.fe_f2, b[0], b[1], b[2], b[3]); \
50	r = __fpu_ ## op(&fe); \
51	c[0] = __fpu_ftoq(&fe, r, c); \
52}
53
54#define	_QP_TTOQ(qname, fname, ntype, signpos, atype, ...) \
55void _Qp_ ## qname ## toq(u_int *c, ntype n); \
56void \
57_Qp_ ## qname ## toq(u_int *c, ntype n) \
58{ \
59	struct fpemu fe; \
60	union { atype a[2]; ntype n; } u = { .n = n }; \
61	__asm __volatile("stx %%fsr, %0" : "=m" (fe.fe_fsr) :); \
62	fe.fe_f1.fp_sign = (signpos >= 0) ? u.a[0] >> signpos : 0; \
63	fe.fe_f1.fp_sticky = 0; \
64	fe.fe_f1.fp_class = __fpu_ ## fname ## tof(&fe.fe_f1, __VA_ARGS__); \
65	c[0] = __fpu_ftoq(&fe, &fe.fe_f1, c); \
66}
67
68#define	_QP_QTOT(qname, fname, type, ...) \
69type _Qp_qto ## qname(u_int *c); \
70type \
71_Qp_qto ## qname(u_int *c) \
72{ \
73	struct fpemu fe; \
74	union { u_int a; type n; } u; \
75	__asm __volatile("stx %%fsr, %0" : "=m" (fe.fe_fsr) :); \
76	fe.fe_f1.fp_sign = c[0] >> 31; \
77	fe.fe_f1.fp_sticky = 0; \
78	fe.fe_f1.fp_class = __fpu_qtof(&fe.fe_f1, c[0], c[1], c[2], c[3]); \
79	u.a = __fpu_fto ## fname(&fe, &fe.fe_f1, ## __VA_ARGS__); \
80	return (u.n); \
81}
82
83#define	FCC_EQ(fcc)	((fcc) == FSR_CC_EQ)
84#define	FCC_GE(fcc)	((fcc) == FSR_CC_EQ || (fcc) == FSR_CC_GT)
85#define	FCC_GT(fcc)	((fcc) == FSR_CC_GT)
86#define	FCC_LE(fcc)	((fcc) == FSR_CC_EQ || (fcc) == FSR_CC_LT)
87#define	FCC_LT(fcc)	((fcc) == FSR_CC_LT)
88#define	FCC_NE(fcc)	((fcc) != FSR_CC_EQ)
89#define	FCC_ID(fcc)	(fcc)
90
91#define	_QP_CMP(name, cmpe, test) \
92int _Qp_ ## name(u_int *a, u_int *b) ; \
93int \
94_Qp_ ## name(u_int *a, u_int *b) \
95{ \
96	struct fpemu fe; \
97	__asm __volatile("stx %%fsr, %0" : "=m" (fe.fe_fsr) :); \
98	fe.fe_f1.fp_sign = a[0] >> 31; \
99	fe.fe_f1.fp_sticky = 0; \
100	fe.fe_f1.fp_class = __fpu_qtof(&fe.fe_f1, a[0], a[1], a[2], a[3]); \
101	fe.fe_f2.fp_sign = b[0] >> 31; \
102	fe.fe_f2.fp_sticky = 0; \
103	fe.fe_f2.fp_class = __fpu_qtof(&fe.fe_f2, b[0], b[1], b[2], b[3]); \
104	__fpu_compare(&fe, cmpe, 0); \
105	return (test(FSR_GET_FCC0(fe.fe_fsr))); \
106}
107
108void _Qp_sqrt(u_int *c, u_int *a);
109void
110_Qp_sqrt(u_int *c, u_int *a)
111{
112	struct fpemu fe;
113	struct fpn *r;
114	__asm __volatile("stx %%fsr, %0" : "=m" (fe.fe_fsr) :);
115	fe.fe_f1.fp_sign = a[0] >> 31;
116	fe.fe_f1.fp_sticky = 0;
117	fe.fe_f1.fp_class = __fpu_qtof(&fe.fe_f1, a[0], a[1], a[2], a[3]);
118	r = __fpu_sqrt(&fe);
119	c[0] = __fpu_ftoq(&fe, r, c);
120}
121
122_QP_OP(add)
123_QP_OP(div)
124_QP_OP(mul)
125_QP_OP(sub)
126
127_QP_TTOQ(d,	d,	double,	31, u_int,  u.a[0], u.a[1])
128_QP_TTOQ(i,	i,	int,	31, u_int,  u.a[0])
129_QP_TTOQ(s,	s,	float,	31, u_int,  u.a[0])
130_QP_TTOQ(x,	x,	long,	63, u_long, u.a[0])
131_QP_TTOQ(ui,	i,	u_int,	-1, u_int,  u.a[0])
132_QP_TTOQ(ux,	x,	u_long,	-1, u_long, u.a[0])
133
134_QP_QTOT(d,	d,	double,	&u.a)
135_QP_QTOT(i,	i,	int)
136_QP_QTOT(s,	s,	float)
137_QP_QTOT(x,	x,	long,	&u.a)
138_QP_QTOT(ui,	i,	u_int)
139_QP_QTOT(ux,	x,	u_long,	&u.a)
140
141_QP_CMP(feq,	0,	FCC_EQ)
142_QP_CMP(fge,	0,	FCC_GE)
143_QP_CMP(fgt,	0,	FCC_GT)
144_QP_CMP(fle,	0,	FCC_LE)
145_QP_CMP(flt,	0,	FCC_LT)
146_QP_CMP(fne,	0, 	FCC_NE)
147_QP_CMP(cmp,	0, 	FCC_ID)
148_QP_CMP(cmpe,	1, 	FCC_ID)
149