xref: /titanic_50/usr/src/lib/libm/common/m9x/frexp.c (revision 1ec68d336ba97cd53f46053ac10401d16014d075)
1*25c28e83SPiotr Jasiukajtis /*
2*25c28e83SPiotr Jasiukajtis  * CDDL HEADER START
3*25c28e83SPiotr Jasiukajtis  *
4*25c28e83SPiotr Jasiukajtis  * The contents of this file are subject to the terms of the
5*25c28e83SPiotr Jasiukajtis  * Common Development and Distribution License (the "License").
6*25c28e83SPiotr Jasiukajtis  * You may not use this file except in compliance with the License.
7*25c28e83SPiotr Jasiukajtis  *
8*25c28e83SPiotr Jasiukajtis  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*25c28e83SPiotr Jasiukajtis  * or http://www.opensolaris.org/os/licensing.
10*25c28e83SPiotr Jasiukajtis  * See the License for the specific language governing permissions
11*25c28e83SPiotr Jasiukajtis  * and limitations under the License.
12*25c28e83SPiotr Jasiukajtis  *
13*25c28e83SPiotr Jasiukajtis  * When distributing Covered Code, include this CDDL HEADER in each
14*25c28e83SPiotr Jasiukajtis  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*25c28e83SPiotr Jasiukajtis  * If applicable, add the following below this CDDL HEADER, with the
16*25c28e83SPiotr Jasiukajtis  * fields enclosed by brackets "[]" replaced with your own identifying
17*25c28e83SPiotr Jasiukajtis  * information: Portions Copyright [yyyy] [name of copyright owner]
18*25c28e83SPiotr Jasiukajtis  *
19*25c28e83SPiotr Jasiukajtis  * CDDL HEADER END
20*25c28e83SPiotr Jasiukajtis  */
21*25c28e83SPiotr Jasiukajtis 
22*25c28e83SPiotr Jasiukajtis /*
23*25c28e83SPiotr Jasiukajtis  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
24*25c28e83SPiotr Jasiukajtis  */
25*25c28e83SPiotr Jasiukajtis /*
26*25c28e83SPiotr Jasiukajtis  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
27*25c28e83SPiotr Jasiukajtis  * Use is subject to license terms.
28*25c28e83SPiotr Jasiukajtis  */
29*25c28e83SPiotr Jasiukajtis 
30*25c28e83SPiotr Jasiukajtis #pragma weak frexp = __frexp
31*25c28e83SPiotr Jasiukajtis 
32*25c28e83SPiotr Jasiukajtis /*
33*25c28e83SPiotr Jasiukajtis  * frexp(x, exp) returns the normalized significand of x and sets
34*25c28e83SPiotr Jasiukajtis  * *exp so that x = r*2^(*exp) where r is the return value.  If x
35*25c28e83SPiotr Jasiukajtis  * is finite and nonzero, 1/2 <= |r| < 1.
36*25c28e83SPiotr Jasiukajtis  *
37*25c28e83SPiotr Jasiukajtis  * If x is zero, infinite or NaN, frexp returns x and sets *exp = 0.
38*25c28e83SPiotr Jasiukajtis  * (The relevant standards do not specify *exp when x is infinite or
39*25c28e83SPiotr Jasiukajtis  * NaN, but this code sets it anyway.)
40*25c28e83SPiotr Jasiukajtis  *
41*25c28e83SPiotr Jasiukajtis  * If x is a signaling NaN, this code returns x without attempting
42*25c28e83SPiotr Jasiukajtis  * to raise the invalid operation exception.  If x is subnormal,
43*25c28e83SPiotr Jasiukajtis  * this code treats it as nonzero regardless of nonstandard mode.
44*25c28e83SPiotr Jasiukajtis  */
45*25c28e83SPiotr Jasiukajtis 
46*25c28e83SPiotr Jasiukajtis #include "libm.h"
47*25c28e83SPiotr Jasiukajtis 
48*25c28e83SPiotr Jasiukajtis double
__frexp(double x,int * exp)49*25c28e83SPiotr Jasiukajtis __frexp(double x, int *exp) {
50*25c28e83SPiotr Jasiukajtis 	union {
51*25c28e83SPiotr Jasiukajtis 		unsigned i[2];
52*25c28e83SPiotr Jasiukajtis 		double d;
53*25c28e83SPiotr Jasiukajtis 	} xx, yy;
54*25c28e83SPiotr Jasiukajtis 	double t;
55*25c28e83SPiotr Jasiukajtis 	unsigned hx;
56*25c28e83SPiotr Jasiukajtis 	int e;
57*25c28e83SPiotr Jasiukajtis 
58*25c28e83SPiotr Jasiukajtis 	xx.d = x;
59*25c28e83SPiotr Jasiukajtis 	hx = xx.i[HIWORD] & ~0x80000000;
60*25c28e83SPiotr Jasiukajtis 
61*25c28e83SPiotr Jasiukajtis 	if (hx >= 0x7ff00000) { /* x is infinite or NaN */
62*25c28e83SPiotr Jasiukajtis 		*exp = 0;
63*25c28e83SPiotr Jasiukajtis 		return (x);
64*25c28e83SPiotr Jasiukajtis 	}
65*25c28e83SPiotr Jasiukajtis 
66*25c28e83SPiotr Jasiukajtis 	e = 0;
67*25c28e83SPiotr Jasiukajtis 	if (hx < 0x00100000) { /* x is subnormal or zero */
68*25c28e83SPiotr Jasiukajtis 		if ((hx | xx.i[LOWORD]) == 0) {
69*25c28e83SPiotr Jasiukajtis 			*exp = 0;
70*25c28e83SPiotr Jasiukajtis 			return (x);
71*25c28e83SPiotr Jasiukajtis 		}
72*25c28e83SPiotr Jasiukajtis 
73*25c28e83SPiotr Jasiukajtis 		/*
74*25c28e83SPiotr Jasiukajtis 		 * normalize x by regarding it as an integer
75*25c28e83SPiotr Jasiukajtis 		 *
76*25c28e83SPiotr Jasiukajtis 		 * Here we use 32-bit integer arithmetic to avoid trapping
77*25c28e83SPiotr Jasiukajtis 		 * or emulating 64-bit arithmetic.  If 64-bit arithmetic is
78*25c28e83SPiotr Jasiukajtis 		 * available (e.g., in SPARC V9), do this instead:
79*25c28e83SPiotr Jasiukajtis 		 *
80*25c28e83SPiotr Jasiukajtis 		 *  long lx = ((long) hx << 32) | xx.i[LOWORD];
81*25c28e83SPiotr Jasiukajtis 		 *  xx.d = (xx.i[HIWORD] < 0)? -lx : lx;
82*25c28e83SPiotr Jasiukajtis 		 *
83*25c28e83SPiotr Jasiukajtis 		 * If subnormal arithmetic doesn't trap, just multiply x by
84*25c28e83SPiotr Jasiukajtis 		 * a power of two.
85*25c28e83SPiotr Jasiukajtis 		 */
86*25c28e83SPiotr Jasiukajtis 		yy.i[HIWORD] = 0x43300000 | hx;
87*25c28e83SPiotr Jasiukajtis 		yy.i[LOWORD] = xx.i[LOWORD];
88*25c28e83SPiotr Jasiukajtis 		t = yy.d;
89*25c28e83SPiotr Jasiukajtis 		yy.i[HIWORD] = 0x43300000;
90*25c28e83SPiotr Jasiukajtis 		yy.i[LOWORD] = 0;
91*25c28e83SPiotr Jasiukajtis 		t -= yy.d; /* t = |x| scaled */
92*25c28e83SPiotr Jasiukajtis 		xx.d = ((int)xx.i[HIWORD] < 0)? -t : t;
93*25c28e83SPiotr Jasiukajtis 		hx = xx.i[HIWORD] & ~0x80000000;
94*25c28e83SPiotr Jasiukajtis 		e = -1074;
95*25c28e83SPiotr Jasiukajtis 	}
96*25c28e83SPiotr Jasiukajtis 
97*25c28e83SPiotr Jasiukajtis 	/* now xx.d is normal */
98*25c28e83SPiotr Jasiukajtis 	xx.i[HIWORD] = (xx.i[HIWORD] & ~0x7ff00000) | 0x3fe00000;
99*25c28e83SPiotr Jasiukajtis 	*exp = e + (hx >> 20) - 0x3fe;
100*25c28e83SPiotr Jasiukajtis 	return (xx.d);
101*25c28e83SPiotr Jasiukajtis }
102