xref: /onnv-gate/usr/src/uts/sparc/fpu/mul.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 1988 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  */
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* SunOS-4.1 1.8 88/12/06 */
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate #include <sys/fpu/fpu_simulator.h>
29*0Sstevel@tonic-gate #include <sys/fpu/globals.h>
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate void
_fp_mul(pfpsd,px,py,pz)32*0Sstevel@tonic-gate _fp_mul(pfpsd, px, py, pz)
33*0Sstevel@tonic-gate 	fp_simd_type	*pfpsd;
34*0Sstevel@tonic-gate 	unpacked	*px, *py, *pz;
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate {
37*0Sstevel@tonic-gate 	unpacked	*pt;
38*0Sstevel@tonic-gate 	unsigned	acc[4];		/* Product accumulator. */
39*0Sstevel@tonic-gate 	unsigned	j, y, *x, s, r, c;
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate 	if ((int) px->fpclass <= (int) py->fpclass) {
42*0Sstevel@tonic-gate 		pt = px;
43*0Sstevel@tonic-gate 		px = py;
44*0Sstevel@tonic-gate 		py = pt;
45*0Sstevel@tonic-gate 	}
46*0Sstevel@tonic-gate 	/* Now class(x) >= class(y).  */
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate 	*pz = *px;
49*0Sstevel@tonic-gate 	if (pz->fpclass < fp_quiet)
50*0Sstevel@tonic-gate 		pz->sign = px->sign ^ py->sign;
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate 	switch (px->fpclass) {
53*0Sstevel@tonic-gate 	case fp_quiet:
54*0Sstevel@tonic-gate 	case fp_signaling:
55*0Sstevel@tonic-gate 	case fp_zero:
56*0Sstevel@tonic-gate 		return;
57*0Sstevel@tonic-gate 	case fp_infinity:
58*0Sstevel@tonic-gate 		if (py->fpclass == fp_zero) {
59*0Sstevel@tonic-gate 			fpu_error_nan(pfpsd, pz);
60*0Sstevel@tonic-gate 			pz->fpclass = fp_quiet;
61*0Sstevel@tonic-gate 		}
62*0Sstevel@tonic-gate 		return;
63*0Sstevel@tonic-gate 	case fp_normal:
64*0Sstevel@tonic-gate 		if (py->fpclass == fp_zero) {
65*0Sstevel@tonic-gate 			pz->fpclass = fp_zero;
66*0Sstevel@tonic-gate 			return;
67*0Sstevel@tonic-gate 		}
68*0Sstevel@tonic-gate 	}
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 	/* Now x and y are both normal or subnormal. */
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 	x = px->significand;	/* save typing */
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 	/* intialize acc to zero */
75*0Sstevel@tonic-gate 	s = r = acc[0] = acc[1] = acc[2] = acc[3] = 0;
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate 	y = py->significand[3];		/* py->significand[3] * x */
78*0Sstevel@tonic-gate 	if (y != 0) {
79*0Sstevel@tonic-gate 	    j = 1;
80*0Sstevel@tonic-gate 	    do {
81*0Sstevel@tonic-gate 		s |= r;		/* shift acc right one bit */
82*0Sstevel@tonic-gate 		r  = acc[3]&1;
83*0Sstevel@tonic-gate 		acc[3] = ((acc[2]&1)<<31)|(acc[3]>>1);
84*0Sstevel@tonic-gate 		acc[2] = ((acc[1]&1)<<31)|(acc[2]>>1);
85*0Sstevel@tonic-gate 		acc[1] = ((acc[0]&1)<<31)|(acc[1]>>1);
86*0Sstevel@tonic-gate 		acc[0] = (acc[0]>>1);
87*0Sstevel@tonic-gate 		if (j&y) {		/* bit i of y != 0, add x to acc */
88*0Sstevel@tonic-gate 			c = 0;
89*0Sstevel@tonic-gate 			c = fpu_add3wc(&acc[3], acc[3], x[3], c);
90*0Sstevel@tonic-gate 			c = fpu_add3wc(&acc[2], acc[2], x[2], c);
91*0Sstevel@tonic-gate 			c = fpu_add3wc(&acc[1], acc[1], x[1], c);
92*0Sstevel@tonic-gate 			c = fpu_add3wc(&acc[0], acc[0], x[0], c);
93*0Sstevel@tonic-gate 		}
94*0Sstevel@tonic-gate 		j += j;
95*0Sstevel@tonic-gate 	    } while (j != 0);
96*0Sstevel@tonic-gate 	}
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 	y = py->significand[2];		/* py->significand[2] * x */
99*0Sstevel@tonic-gate 	if (y != 0) {
100*0Sstevel@tonic-gate 	    j = 1;
101*0Sstevel@tonic-gate 	    do {
102*0Sstevel@tonic-gate 		s |= r;		/* shift acc right one bit */
103*0Sstevel@tonic-gate 		r  = acc[3]&1;
104*0Sstevel@tonic-gate 		acc[3] = ((acc[2]&1)<<31)|(acc[3]>>1);
105*0Sstevel@tonic-gate 		acc[2] = ((acc[1]&1)<<31)|(acc[2]>>1);
106*0Sstevel@tonic-gate 		acc[1] = ((acc[0]&1)<<31)|(acc[1]>>1);
107*0Sstevel@tonic-gate 		acc[0] = (acc[0]>>1);
108*0Sstevel@tonic-gate 		if (j&y) {		/* bit i of y != 0, add x to acc */
109*0Sstevel@tonic-gate 			c = 0;
110*0Sstevel@tonic-gate 			c = fpu_add3wc(&acc[3], acc[3], x[3], c);
111*0Sstevel@tonic-gate 			c = fpu_add3wc(&acc[2], acc[2], x[2], c);
112*0Sstevel@tonic-gate 			c = fpu_add3wc(&acc[1], acc[1], x[1], c);
113*0Sstevel@tonic-gate 			c = fpu_add3wc(&acc[0], acc[0], x[0], c);
114*0Sstevel@tonic-gate 		}
115*0Sstevel@tonic-gate 		j += j;
116*0Sstevel@tonic-gate 	    } while (j != 0);
117*0Sstevel@tonic-gate 	} else {
118*0Sstevel@tonic-gate 		s |= r|(acc[3]&0x7fffffff);
119*0Sstevel@tonic-gate 		r  = (acc[3]&0x80000000)>>31;
120*0Sstevel@tonic-gate 		acc[3] = acc[2]; acc[2] = acc[1]; acc[1] = acc[0]; acc[0] = 0;
121*0Sstevel@tonic-gate 	}
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 	y = py->significand[1];		/* py->significand[1] * x */
124*0Sstevel@tonic-gate 	if (y != 0) {
125*0Sstevel@tonic-gate 	    j = 1;
126*0Sstevel@tonic-gate 	    do {
127*0Sstevel@tonic-gate 		s |= r;		/* shift acc right one bit */
128*0Sstevel@tonic-gate 		r  = acc[3]&1;
129*0Sstevel@tonic-gate 		acc[3] = ((acc[2]&1)<<31)|(acc[3]>>1);
130*0Sstevel@tonic-gate 		acc[2] = ((acc[1]&1)<<31)|(acc[2]>>1);
131*0Sstevel@tonic-gate 		acc[1] = ((acc[0]&1)<<31)|(acc[1]>>1);
132*0Sstevel@tonic-gate 		acc[0] = (acc[0]>>1);
133*0Sstevel@tonic-gate 		if (j&y) {		/* bit i of y != 0, add x to acc */
134*0Sstevel@tonic-gate 			c = 0;
135*0Sstevel@tonic-gate 			c = fpu_add3wc(&acc[3], acc[3], x[3], c);
136*0Sstevel@tonic-gate 			c = fpu_add3wc(&acc[2], acc[2], x[2], c);
137*0Sstevel@tonic-gate 			c = fpu_add3wc(&acc[1], acc[1], x[1], c);
138*0Sstevel@tonic-gate 			c = fpu_add3wc(&acc[0], acc[0], x[0], c);
139*0Sstevel@tonic-gate 		}
140*0Sstevel@tonic-gate 		j += j;
141*0Sstevel@tonic-gate 	    } while (j != 0);
142*0Sstevel@tonic-gate 	} else {
143*0Sstevel@tonic-gate 		s |= r|(acc[3]&0x7fffffff);
144*0Sstevel@tonic-gate 		r  = (acc[3]&0x80000000)>>31;
145*0Sstevel@tonic-gate 		acc[3] = acc[2]; acc[2] = acc[1]; acc[1] = acc[0]; acc[0] = 0;
146*0Sstevel@tonic-gate 	}
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 					/* py->significand[0] * x */
149*0Sstevel@tonic-gate 	y = py->significand[0];		/* y is of form 0x0001???? */
150*0Sstevel@tonic-gate 	j = 1;
151*0Sstevel@tonic-gate 	do {
152*0Sstevel@tonic-gate 		s |= r;		/* shift acc right one bit */
153*0Sstevel@tonic-gate 		r  = acc[3]&1;
154*0Sstevel@tonic-gate 		acc[3] = ((acc[2]&1)<<31)|(acc[3]>>1);
155*0Sstevel@tonic-gate 		acc[2] = ((acc[1]&1)<<31)|(acc[2]>>1);
156*0Sstevel@tonic-gate 		acc[1] = ((acc[0]&1)<<31)|(acc[1]>>1);
157*0Sstevel@tonic-gate 		acc[0] = (acc[0]>>1);
158*0Sstevel@tonic-gate 		if (j&y) {		/* bit i of y != 0, add x to acc */
159*0Sstevel@tonic-gate 			c = 0;
160*0Sstevel@tonic-gate 			c = fpu_add3wc(&acc[3], acc[3], x[3], c);
161*0Sstevel@tonic-gate 			c = fpu_add3wc(&acc[2], acc[2], x[2], c);
162*0Sstevel@tonic-gate 			c = fpu_add3wc(&acc[1], acc[1], x[1], c);
163*0Sstevel@tonic-gate 			c = fpu_add3wc(&acc[0], acc[0], x[0], c);
164*0Sstevel@tonic-gate 		}
165*0Sstevel@tonic-gate 		j += j;
166*0Sstevel@tonic-gate 	} while (j <= y);
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 	if (acc[0] >= 0x20000) {	/* right shift one bit to normalize */
169*0Sstevel@tonic-gate 		pz->exponent = px->exponent + py->exponent + 1;
170*0Sstevel@tonic-gate 		pz->sticky = s|r;
171*0Sstevel@tonic-gate 		pz->rounded = acc[3]&1;
172*0Sstevel@tonic-gate 		pz->significand[3] = ((acc[2]&1)<<31)|(acc[3]>>1);
173*0Sstevel@tonic-gate 		pz->significand[2] = ((acc[1]&1)<<31)|(acc[2]>>1);
174*0Sstevel@tonic-gate 		pz->significand[1] = ((acc[0]&1)<<31)|(acc[1]>>1);
175*0Sstevel@tonic-gate 		pz->significand[0] = (acc[0]>>1);
176*0Sstevel@tonic-gate 	} else {
177*0Sstevel@tonic-gate 		pz->exponent = px->exponent + py->exponent;
178*0Sstevel@tonic-gate 		pz->sticky = s;
179*0Sstevel@tonic-gate 		pz->rounded = r;
180*0Sstevel@tonic-gate 		pz->significand[3] = acc[3];
181*0Sstevel@tonic-gate 		pz->significand[2] = acc[2];
182*0Sstevel@tonic-gate 		pz->significand[1] = acc[1];
183*0Sstevel@tonic-gate 		pz->significand[0] = acc[0];
184*0Sstevel@tonic-gate 	}
185*0Sstevel@tonic-gate }
186