1/*-
2 * Copyright (c) 2004-2008 David Schultz <das@FreeBSD.ORG>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD$");
29
30#include <float.h>
31#include <limits.h>
32#include <math.h>
33
34#include "../stdio/floatio.h"
35#include "fpmath.h"
36#include "gdtoaimp.h"
37
38/* Strings values used by dtoa() */
39#define	INFSTR	"Infinity"
40#define	NANSTR	"NaN"
41
42#define	DBL_ADJ	(DBL_MAX_EXP - 2)
43#define	SIGFIGS	((DBL_MANT_DIG + 3) / 4 + 1)
44
45static const float one[] = { 1.0f, -1.0f };
46
47/*
48 * This procedure converts a double-precision number in IEEE format
49 * into a string of hexadecimal digits and an exponent of 2.  Its
50 * behavior is bug-for-bug compatible with dtoa() in mode 2, with the
51 * following exceptions:
52 *
53 * - An ndigits < 0 causes it to use as many digits as necessary to
54 *   represent the number exactly.
55 * - The additional xdigs argument should point to either the string
56 *   "0123456789ABCDEF" or the string "0123456789abcdef", depending on
57 *   which case is desired.
58 * - This routine does not repeat dtoa's mistake of setting decpt
59 *   to 9999 in the case of an infinity or NaN.  INT_MAX is used
60 *   for this purpose instead.
61 *
62 * Note that the C99 standard does not specify what the leading digit
63 * should be for non-zero numbers.  For instance, 0x1.3p3 is the same
64 * as 0x2.6p2 is the same as 0x4.cp3.  This implementation always makes
65 * the leading digit a 1. This ensures that the exponent printed is the
66 * actual base-2 exponent, i.e., ilogb(d).
67 *
68 * Inputs:	d, xdigs, ndigits
69 * Outputs:	decpt, sign, rve
70 */
71char *
72__hdtoa(double d, const char *xdigs, int ndigits, int *decpt, int *sign,
73    char **rve)
74{
75	union IEEEd2bits u;
76	char *s, *s0;
77	int bufsize;
78	uint32_t manh, manl;
79
80	u.d = d;
81	*sign = u.bits.sign;
82
83	switch (fpclassify(d)) {
84	case FP_NORMAL:
85		*decpt = u.bits.exp - DBL_ADJ;
86		break;
87	case FP_ZERO:
88		*decpt = 1;
89		return (nrv_alloc("0", rve, 1));
90	case FP_SUBNORMAL:
91		u.d *= 0x1p514;
92		*decpt = u.bits.exp - (514 + DBL_ADJ);
93		break;
94	case FP_INFINITE:
95		*decpt = INT_MAX;
96		return (nrv_alloc(INFSTR, rve, sizeof(INFSTR) - 1));
97	default:	/* FP_NAN or unrecognized */
98		*decpt = INT_MAX;
99		return (nrv_alloc(NANSTR, rve, sizeof(NANSTR) - 1));
100	}
101
102	/* FP_NORMAL or FP_SUBNORMAL */
103
104	if (ndigits == 0)		/* dtoa() compatibility */
105		ndigits = 1;
106
107	/*
108	 * If ndigits < 0, we are expected to auto-size, so we allocate
109	 * enough space for all the digits.
110	 */
111	bufsize = (ndigits > 0) ? ndigits : SIGFIGS;
112	s0 = rv_alloc(bufsize);
113
114	/* Round to the desired number of digits. */
115	if (SIGFIGS > ndigits && ndigits > 0) {
116		float redux = one[u.bits.sign];
117		int offset = 4 * ndigits + DBL_MAX_EXP - 4 - DBL_MANT_DIG;
118		u.bits.exp = offset;
119		u.d += redux;
120		u.d -= redux;
121		*decpt += u.bits.exp - offset;
122	}
123
124	manh = u.bits.manh;
125	manl = u.bits.manl;
126	*s0 = '1';
127	for (s = s0 + 1; s < s0 + bufsize; s++) {
128		*s = xdigs[(manh >> (DBL_MANH_SIZE - 4)) & 0xf];
129		manh = (manh << 4) | (manl >> (DBL_MANL_SIZE - 4));
130		manl <<= 4;
131	}
132
133	/* If ndigits < 0, we are expected to auto-size the precision. */
134	if (ndigits < 0) {
135		for (ndigits = SIGFIGS; s0[ndigits - 1] == '0'; ndigits--)
136			;
137	}
138
139	s = s0 + ndigits;
140	*s = '\0';
141	if (rve != NULL)
142		*rve = s;
143	return (s0);
144}
145