summaryrefslogtreecommitdiff
path: root/usr/src/lib/libc/port/fp/hex_bin.c
blob: 752d385b7efae09538773ae167824d6db04cb3e2 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * 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 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include "lint.h"
#include "base_conversion.h"

/* conversion from hex chars to hex values */
#define	HEXVAL(c)	(('0' <= c && c <= '9')? c - '0' : \
			10 + (('a' <= c && c <= 'f')? c - 'a' : c - 'A'))

/*
 * Convert a hexadecimal record in *pd to unpacked form in *pu.
 *
 * Up to 30 hexadecimal digits from pd->ds are converted to a binary
 * value in px->significand, which is then normalized so that the most
 * significant bit is 1.  If there are additional, unused digits in
 * pd->ds, the least significant bit of px->significand will be set.
 */
static void
__hex_to_unpacked(decimal_record *pd, unpacked *pu)
{
	int	i, n;

	pu->sign = pd->sign;
	pu->fpclass = pd->fpclass;

	/*
	 * Adjust the (base two) exponent to reflect the fact that the
	 * radix point in *pd lies to the right of the last (base sixteen)
	 * digit while the radix point in *pu lies to the right of the
	 * most significant bit.
	 */
	pu->exponent = pd->exponent + (pd->ndigits << 2) - 1;

	/* fill in the significand */
	for (i = 0; i < 5; i++)
		pu->significand[i] = 0;

	n = pd->ndigits;
	if (n > 30)
		n = 30;
	for (i = 0; i < n; i++) {
		pu->significand[i >> 3] |= HEXVAL(pd->ds[i]) <<
		    ((7 - (i & 7)) << 2);
	}

	/* sanity check */
	if (pu->significand[0] == 0) {
		pu->fpclass = fp_zero;
		return;
	}

	/* normalize so the most significant bit is set */
	while (pu->significand[0] < 0x80000000u) {
		pu->significand[0] = (pu->significand[0] << 1) |
		    (pu->significand[1] >> 31);
		pu->significand[1] = (pu->significand[1] << 1) |
		    (pu->significand[2] >> 31);
		pu->significand[2] = (pu->significand[2] << 1) |
		    (pu->significand[3] >> 31);
		pu->significand[3] <<= 1;
		pu->exponent--;
	}

	/* if there are any unused digits, set a sticky bit */
	if (pd->ndigits > 30 || pd->more)
		pu->significand[4] = 1;
}

/*
 * The following routines convert the hexadecimal value encoded in the
 * decimal record *pd to a floating point value *px observing the round-
 * ing mode specified in rd and passing back any exceptions raised via
 * *ps.
 *
 * These routines assume pd->fpclass is either fp_zero or fp_normal.
 * If pd->fpclass is fp_zero, *px is set to zero with the sign indicated
 * by pd->sign and no exceptions are raised.  Otherwise, pd->ds must
 * contain a string of hexadecimal digits of length pd->ndigits > 0, and
 * the first digit must be nonzero.  Let m be the integer represented by
 * this string.  Then *px is set to a correctly rounded approximation to
 *
 *  (-1)^(pd->sign) * m * 2^(pd->exponent)
 *
 * with inexact, underflow, and/or overflow raised as appropriate.
 */

void
__hex_to_single(decimal_record *pd, enum fp_direction_type rd, single *px,
    fp_exception_field_type *ps)
{
	single_equivalence	kluge;
	unpacked		u;

	*ps = 0;
	if (pd->fpclass == fp_zero) {
		kluge.f.msw.sign = pd->sign? 1 : 0;
		kluge.f.msw.exponent = 0;
		kluge.f.msw.significand = 0;
		*px = kluge.x;
	} else {
		__hex_to_unpacked(pd, &u);
		__pack_single(&u, px, rd, ps);
		if (*ps != 0)
			__base_conversion_set_exception(*ps);
	}
}

void
__hex_to_double(decimal_record *pd, enum fp_direction_type rd, double *px,
    fp_exception_field_type *ps)
{
	double_equivalence	kluge;
	unpacked		u;

	*ps = 0;
	if (pd->fpclass == fp_zero) {
		kluge.f.msw.sign = pd->sign? 1 : 0;
		kluge.f.msw.exponent = 0;
		kluge.f.msw.significand = 0;
		kluge.f.significand2 = 0;
		*px = kluge.x;
	} else {
		__hex_to_unpacked(pd, &u);
		__pack_double(&u, px, rd, ps);
		if (*ps != 0)
			__base_conversion_set_exception(*ps);
	}
}

#if defined(__sparc)

void
__hex_to_quadruple(decimal_record *pd, enum fp_direction_type rd, quadruple *px,
    fp_exception_field_type *ps)
{
	quadruple_equivalence	kluge;
	unpacked		u;

	*ps = 0;
	if (pd->fpclass == fp_zero) {
		kluge.f.msw.sign = pd->sign? 1 : 0;
		kluge.f.msw.exponent = 0;
		kluge.f.msw.significand = 0;
		kluge.f.significand2 = 0;
		kluge.f.significand3 = 0;
		kluge.f.significand4 = 0;
		*px = kluge.x;
	} else {
		__hex_to_unpacked(pd, &u);
		__pack_quadruple(&u, px, rd, ps);
		if (*ps != 0)
			__base_conversion_set_exception(*ps);
	}
}

#elif defined(__i386) || defined(__amd64)

void
__hex_to_extended(decimal_record *pd, enum fp_direction_type rd, extended *px,
    fp_exception_field_type *ps)
{
	extended_equivalence	kluge;
	unpacked		u;

	*ps = 0;
	if (pd->fpclass == fp_zero) {
		kluge.f.msw.sign = pd->sign? 1 : 0;
		kluge.f.msw.exponent = 0;
		kluge.f.significand = 0;
		kluge.f.significand2 = 0;
		(*px)[0] = kluge.x[0];
		(*px)[1] = kluge.x[1];
		(*px)[2] = kluge.x[2];
	} else {
		__hex_to_unpacked(pd, &u);
		__pack_extended(&u, px, rd, ps);
		if (*ps != 0)
			__base_conversion_set_exception(*ps);
	}
}

#else
#error Unknown architecture
#endif