fpu_qp.c revision 127091
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 127091 2004-03-16 20:42:02Z des $");
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, 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 = u.a[0] >> 31; \
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
90#define	_QP_CMP(name, cmpe, test) \
91int _Qp_f ## name(u_int *a, u_int *b) ; \
92int \
93_Qp_f ## name(u_int *a, u_int *b) \
94{ \
95	struct fpemu fe; \
96	__asm __volatile("stx %%fsr, %0" : "=m" (fe.fe_fsr) :); \
97	fe.fe_f1.fp_sign = a[0] >> 31; \
98	fe.fe_f1.fp_sticky = 0; \
99	fe.fe_f1.fp_class = __fpu_qtof(&fe.fe_f1, a[0], a[1], a[2], a[3]); \
100	fe.fe_f2.fp_sign = b[0] >> 31; \
101	fe.fe_f2.fp_sticky = 0; \
102	fe.fe_f2.fp_class = __fpu_qtof(&fe.fe_f2, b[0], b[1], b[2], b[3]); \
103	__fpu_compare(&fe, cmpe, 0); \
104	return (test(FSR_GET_FCC0(fe.fe_fsr))); \
105}
106
107void _Qp_sqrt(u_int *c, u_int *a);
108void
109_Qp_sqrt(u_int *c, u_int *a)
110{
111	struct fpemu fe;
112	struct fpn *r;
113	__asm __volatile("stx %%fsr, %0" : "=m" (fe.fe_fsr) :);
114	fe.fe_f1.fp_sign = a[0] >> 31;
115	fe.fe_f1.fp_sticky = 0;
116	fe.fe_f1.fp_class = __fpu_qtof(&fe.fe_f1, a[0], a[1], a[2], a[3]);
117	r = __fpu_sqrt(&fe);
118	c[0] = __fpu_ftoq(&fe, r, c);
119}
120
121_QP_OP(add)
122_QP_OP(div)
123_QP_OP(mul)
124_QP_OP(sub)
125
126_QP_TTOQ(d,	d,	double,	u_int,	u.a[0], u.a[1])
127_QP_TTOQ(i,	i,	int,	u_int,	u.a[0])
128_QP_TTOQ(s,	s,	float,	u_int,	u.a[0])
129_QP_TTOQ(x,	x,	long,	u_long,	u.a[0])
130_QP_TTOQ(ui,	i,	u_int,	u_int,	u.a[0])
131_QP_TTOQ(ux,	x,	u_long,	u_long,	u.a[0])
132
133_QP_QTOT(d,	d,	double,	&u.a)
134_QP_QTOT(i,	i,	int)
135_QP_QTOT(s,	s,	float)
136_QP_QTOT(x,	x,	long,	&u.a)
137_QP_QTOT(ui,	i,	u_int)
138_QP_QTOT(ux,	x,	u_long,	&u.a)
139
140_QP_CMP(eq,	0,	FCC_EQ)
141_QP_CMP(ge,	0,	FCC_GE)
142_QP_CMP(gt,	0,	FCC_GT)
143_QP_CMP(le,	0,	FCC_LE)
144_QP_CMP(lt,	0,	FCC_LT)
145_QP_CMP(ne,	0, 	FCC_NE)
146