2N/A * The contents of this file are subject to the terms of the 2N/A * Common Development and Distribution License (the "License"). 2N/A * You may not use this file except in compliance with the License. 2N/A * See the License for the specific language governing permissions 2N/A * and limitations under the License. 2N/A * When distributing Covered Code, include this CDDL HEADER in each 2N/A * If applicable, add the following below this CDDL HEADER, with the 2N/A * fields enclosed by brackets "[]" replaced with your own identifying 2N/A * information: Portions Copyright [yyyy] [name of copyright owner] 2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 2N/A * Use is subject to license terms. 2N/A/* translation table from hex values to hex chars */ 2N/A * Convert arg to a hexadecimal string. 2N/A * If arg is finite and nonzero, buf is filled with ndigits hexadecimal 2N/A * digits, representing the significand of arg, followed by a null byte 2N/A * (so ndigits must be at least 1 and buf must be large enough to hold 2N/A * ndigits + 1 characters). If ndigits is large enough, the representa- 2N/A * tion is exact; otherwise, the value is rounded according to the pre- 2N/A * vailing rounding mode to fit the requested number of digits. Either 2N/A * way, the result is normalized so that the first digit is '1'. The 2N/A * corresponding base two exponent is passed back in *exp. 2N/A * If arg is zero, buf is filled with ndigits zeros followed by a null, 2N/A * and *exp is set to zero. If arg is infinite or NaN, __infnanstring 2N/A * is called to place an appropriate string in buf, and *exp is set to 2N/A * Regardless of the value of arg, its sign bit is stored in *sign. 2N/A ha = a.i[0] & ~
0x80000000;
2N/A /* check for infinity or nan */ 2N/A /* check for subnormal or zero */ 2N/A if ((
ha | a.i[
1]) == 0) {
2N/A * Normalize. It would be much simpler if we could just 2N/A * multiply by a power of two here, but some SPARC imple- 2N/A * mentations would flush the subnormal operand to zero 2N/A * when nonstandard mode is enabled. 2N/A ha = a.i[0] & ~
0x80000000;
2N/A * Round the significand at the appropriate bit by adding 2N/A * and subtracting a power of two. This will also raise 2N/A * the inexact exception if anything is rounded off. 2N/A c.i[0] = (
0x43700000 | (s <<
31)) - (
ndigits <<
22);
2N/A a.i[0] = (a.i[0] &
0x800fffff) |
0x3ff00000;
2N/A a.d = (a.d + c.d) - c.d;
2N/A ha = a.i[0] & ~
0x80000000;
2N/A /* convert to hex digits */ 2N/A unsigned int b, r, d;
2N/A ha = a.i[0] &= ~
0x80000000;
2N/A /* check for infinity or nan */ 2N/A /* check for subnormal or zero */ 2N/A if ((
ha | a.i[
1] | a.i[
2] | a.i[
3]) == 0) {
2N/A while ((a.i[0] | (a.i[
1] &
0xffff0000)) == 0) {
2N/A while ((a.i[0] &
0x7fff0000) == 0) {
2N/A a.i[0] = (a.i[0] <<
1) | (a.i[
1] >>
31);
2N/A a.i[
1] = (a.i[
1] <<
1) | (a.i[
2] >>
31);
2N/A a.i[
2] = (a.i[
2] <<
1) | (a.i[
3] >>
31);
2N/A * Round the significand at the appropriate bit using 2N/A * integer arithmetic. Explicitly raise the inexact 2N/A * exception if anything is rounded off. 2N/A a.i[0] = (a.i[0] &
0xffff) |
0x10000;
2N/A * i and b are the index and bit position in a.i[] 2N/A * of the last bit to be retained. r holds the bits 2N/A * to be rounded off, left-adjusted and sticky. 2N/A r = ((a.i[0] <<
1) << (
31 - s)) | (a.i[
1] >> s);
2N/A if ((a.i[
1] & (b -
1)) | a.i[
2] | a.i[
3])
2N/A a.i[
1] = a.i[
2] = a.i[
3] = 0;
2N/A r = ((a.i[
1] <<
1) << (
31 - s)) | (a.i[
2] >> s);
2N/A if ((a.i[
2] & (b -
1)) | a.i[
3])
2N/A a.i[
2] = a.i[
3] = 0;
2N/A r = ((a.i[
2] <<
1) << (
31 - s)) | (a.i[
3] >> s);
2N/A if (a.i[
3] & (b -
1))
2N/A r = (a.i[
3] <<
1) << (
31 - s);
2N/A /* conversion is inexact if r is not zero */ 2N/A /* massage the rounding direction based on the sign */ 2N/A /* decide whether to round up */ 2N/A (r >
0x80000000u || (r ==
0x80000000u &&
2N/A if (a.i[0] >=
0x20000)
2N/A /* convert to hex digits */ 2N/A * The following code assumes the rounding precision mode is set 2N/A * to the default (round to 64 bits). 2N/A /* check for infinity or nan */ 2N/A /* check for subnormal or zero */ 2N/A if ((a.i[
1] | a.i[0]) == 0) {
2N/A a.x *=
18446744073709551616.0;
/* 2^64 */ 2N/A * Round the significand at the appropriate bit by adding 2N/A * and subtracting a power of two. This will also raise 2N/A * the inexact exception if anything is rounded off. 2N/A c.i[
1] =
0x80000000;
2N/A a.i[
2] =
0x3fff | (s <<
15);
2N/A a.x = (a.x + c.x) - c.x;
2N/A /* convert to hex digits */ 2N/A d = (a.i[
1] <<
1) | (a.i[0] >>
31);
2N/A /* avoid raising invalid operation exception for signaling nan */ 2N/A ha = a.i[
1] & ~
0x80000000;
2N/A if (
ha >
0x7ff00000 || (
ha ==
0x7ff00000 && a.i[0] != 0))
2N/A a.i[
1] |=
0x80000;
/* make nan quiet */