summaryrefslogtreecommitdiff
path: root/usr/src/lib/libm/common/m9x/frexpl.c
blob: d2639abc077ad6d40de2e471ce2f7af1cdf6d6ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
 */
/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma weak frexpl = __frexpl

#include "libm.h"

#if defined(__sparc)

long double
__frexpl(long double x, int *exp) {
	union {
		unsigned i[4];
		long double q;
	} xx;
	unsigned hx;
	int e, s;

	xx.q = x;
	hx = xx.i[0] & ~0x80000000;

	if (hx >= 0x7fff0000) {	/* x is infinite or NaN */
		*exp = 0;
		return (x);
	}

	e = 0;
	if (hx < 0x00010000) {	/* x is subnormal or zero */
		if ((hx | xx.i[1] | xx.i[2] | xx.i[3]) == 0) {
			*exp = 0;
			return (x);
		}

		/* normalize x */
		s = xx.i[0] & 0x80000000;
		while ((hx | (xx.i[1] & 0xffff0000)) == 0) {
			hx = xx.i[1];
			xx.i[1] = xx.i[2];
			xx.i[2] = xx.i[3];
			xx.i[3] = 0;
			e -= 32;
		}
		while (hx < 0x10000) {
			hx = (hx << 1) | (xx.i[1] >> 31);
			xx.i[1] = (xx.i[1] << 1) | (xx.i[2] >> 31);
			xx.i[2] = (xx.i[2] << 1) | (xx.i[3] >> 31);
			xx.i[3] <<= 1;
			e--;
		}
		xx.i[0] = s | hx;
	}

	/* now xx.q is normal */
	xx.i[0] = (xx.i[0] & ~0x7fff0000) | 0x3ffe0000;
	*exp = e + (hx >> 16) - 0x3ffe;
	return (xx.q);
}

#elif defined(__x86)

long double
__frexpl(long double x, int *exp) {
	union {
		unsigned i[3];
		long double e;
	} xx;
	unsigned hx;
	int e;

	xx.e = x;
	hx = xx.i[2] & 0x7fff;

	if (hx >= 0x7fff) {	/* x is infinite or NaN */
		*exp = 0;
		return (x);
	}

	e = 0;
	if (hx < 0x0001) {	/* x is subnormal or zero */
		if ((xx.i[0] | xx.i[1]) == 0) {
			*exp = 0;
			return (x);
		}

		/* normalize x */
		xx.e *= 18446744073709551616.0L;	/* 2^64 */
		hx = xx.i[2] & 0x7fff;
		e = -64;
	}

	/* now xx.e is normal */
	xx.i[2] = (xx.i[2] & 0x8000) | 0x3ffe;
	*exp = e + hx - 0x3ffe;
	return (xx.e);
}

#else
#error Unknown architecture
#endif