xref: /illumos-gate/usr/src/lib/libc/sparc/fp/_Q_scl.c (revision 1da57d55)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #if !defined(sparc) && !defined(__sparc)
28*7c478bd9Sstevel@tonic-gate #error This code is for SPARC only
29*7c478bd9Sstevel@tonic-gate #endif
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate /*
32*7c478bd9Sstevel@tonic-gate  * _Q_scl(x, n) sets *x = *x * 2^n.
33*7c478bd9Sstevel@tonic-gate  *
34*7c478bd9Sstevel@tonic-gate  * This routine tacitly assumes the result will be either zero
35*7c478bd9Sstevel@tonic-gate  * or normal, so there is no need to raise any exceptions.
36*7c478bd9Sstevel@tonic-gate  */
37*7c478bd9Sstevel@tonic-gate void
_Q_scl(long double * x,int n)38*7c478bd9Sstevel@tonic-gate _Q_scl(long double *x, int n)
39*7c478bd9Sstevel@tonic-gate {
40*7c478bd9Sstevel@tonic-gate 	union {
41*7c478bd9Sstevel@tonic-gate 		unsigned int	i[4];
42*7c478bd9Sstevel@tonic-gate 		long double	q;
43*7c478bd9Sstevel@tonic-gate 	} xx;
44*7c478bd9Sstevel@tonic-gate 	int	hx;
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate 	xx.q = *x;
47*7c478bd9Sstevel@tonic-gate 	hx = xx.i[0] & ~0x80000000;
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate 	if (hx < 0x10000) { /* x is zero or subnormal */
50*7c478bd9Sstevel@tonic-gate 		if ((hx | xx.i[1] | xx.i[2] | xx.i[3]) == 0)
51*7c478bd9Sstevel@tonic-gate 			return;
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate 		/* normalize x */
54*7c478bd9Sstevel@tonic-gate 		while (hx == 0 && xx.i[1] < 0x10000) {
55*7c478bd9Sstevel@tonic-gate 			hx = xx.i[1];
56*7c478bd9Sstevel@tonic-gate 			xx.i[1] = xx.i[2];
57*7c478bd9Sstevel@tonic-gate 			xx.i[2] = xx.i[3];
58*7c478bd9Sstevel@tonic-gate 			xx.i[3] = 0;
59*7c478bd9Sstevel@tonic-gate 			n -= 32;
60*7c478bd9Sstevel@tonic-gate 		}
61*7c478bd9Sstevel@tonic-gate 		while (hx < 0x10000) {
62*7c478bd9Sstevel@tonic-gate 			hx = (hx << 1) | (xx.i[1] >> 31);
63*7c478bd9Sstevel@tonic-gate 			xx.i[1] = (xx.i[1] << 1) | (xx.i[2] >> 31);
64*7c478bd9Sstevel@tonic-gate 			xx.i[2] = (xx.i[2] << 1) | (xx.i[3] >> 31);
65*7c478bd9Sstevel@tonic-gate 			xx.i[3] <<= 1;
66*7c478bd9Sstevel@tonic-gate 			n--;
67*7c478bd9Sstevel@tonic-gate 		}
68*7c478bd9Sstevel@tonic-gate 		xx.i[0] = hx | (xx.i[0] & 0x80000000);
69*7c478bd9Sstevel@tonic-gate 	}
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 	if ((hx >> 16) + n < 1) {
72*7c478bd9Sstevel@tonic-gate 		/* for subnormal result, just deliver zero */
73*7c478bd9Sstevel@tonic-gate 		xx.i[0] = xx.i[0] & 0x80000000;
74*7c478bd9Sstevel@tonic-gate 		xx.i[1] = xx.i[2] = xx.i[3] = 0;
75*7c478bd9Sstevel@tonic-gate 	} else
76*7c478bd9Sstevel@tonic-gate 		xx.i[0] += (n << 16);
77*7c478bd9Sstevel@tonic-gate 	*x = xx.q;
78*7c478bd9Sstevel@tonic-gate }
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate static const union {
81*7c478bd9Sstevel@tonic-gate 	int		i[4];
82*7c478bd9Sstevel@tonic-gate 	long double	q;
83*7c478bd9Sstevel@tonic-gate } consts[2] = {
84*7c478bd9Sstevel@tonic-gate 	{ 0x7ffe0000, 0, 0, 0 },
85*7c478bd9Sstevel@tonic-gate 	{ 0x00010000, 0, 0, 0 }
86*7c478bd9Sstevel@tonic-gate };
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate /*
89*7c478bd9Sstevel@tonic-gate  * _Q_scle(x, n) sets *x = *x * 2^n, raising overflow or underflow
90*7c478bd9Sstevel@tonic-gate  * as appropriate.
91*7c478bd9Sstevel@tonic-gate  *
92*7c478bd9Sstevel@tonic-gate  * This routine tacitly assumes the argument is either zero or normal.
93*7c478bd9Sstevel@tonic-gate  */
94*7c478bd9Sstevel@tonic-gate void
_Q_scle(long double * x,int n)95*7c478bd9Sstevel@tonic-gate _Q_scle(long double *x, int n)
96*7c478bd9Sstevel@tonic-gate {
97*7c478bd9Sstevel@tonic-gate 	union {
98*7c478bd9Sstevel@tonic-gate 		unsigned int	i[4];
99*7c478bd9Sstevel@tonic-gate 		long double	q;
100*7c478bd9Sstevel@tonic-gate 	} xx;
101*7c478bd9Sstevel@tonic-gate 	int	hx;
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 	xx.q = *x;
104*7c478bd9Sstevel@tonic-gate 	hx = (xx.i[0] >> 16) & 0x7fff;
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	if (hx == 0) /* x must be zero */
107*7c478bd9Sstevel@tonic-gate 		return;
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	hx += n;
110*7c478bd9Sstevel@tonic-gate 	if (hx >= 0x7fff) { /* overflow */
111*7c478bd9Sstevel@tonic-gate 		xx.i[0] = 0x7ffe0000 | (xx.i[0] & 0x80000000);
112*7c478bd9Sstevel@tonic-gate 		xx.i[1] = xx.i[2] = xx.i[3] = 0;
113*7c478bd9Sstevel@tonic-gate 		xx.q *= consts[0].q;
114*7c478bd9Sstevel@tonic-gate 	} else if (hx < 1) { /* possible underflow */
115*7c478bd9Sstevel@tonic-gate 		if (hx < -112) {
116*7c478bd9Sstevel@tonic-gate 			xx.i[0] = 0x00010000 | (xx.i[0] & 0x80000000);
117*7c478bd9Sstevel@tonic-gate 			xx.i[1] = xx.i[2] = xx.i[3] = 0;
118*7c478bd9Sstevel@tonic-gate 		} else {
119*7c478bd9Sstevel@tonic-gate 			xx.i[0] = (0x3ffe0000 + (hx << 16)) |
120*7c478bd9Sstevel@tonic-gate 				(xx.i[0] & 0x8000ffff);
121*7c478bd9Sstevel@tonic-gate 		}
122*7c478bd9Sstevel@tonic-gate 		xx.q *= consts[1].q;
123*7c478bd9Sstevel@tonic-gate 	} else
124*7c478bd9Sstevel@tonic-gate 		xx.i[0] += (n << 16);
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	*x = xx.q;
127*7c478bd9Sstevel@tonic-gate }
128