1124667Sdas/*-
2178140Sdas * Copyright (c) 2004-2008 David Schultz <das@FreeBSD.ORG>
3124667Sdas * All rights reserved.
4124667Sdas *
5124667Sdas * Redistribution and use in source and binary forms, with or without
6124667Sdas * modification, are permitted provided that the following conditions
7124667Sdas * are met:
8124667Sdas * 1. Redistributions of source code must retain the above copyright
9124667Sdas *    notice, this list of conditions and the following disclaimer.
10124667Sdas * 2. Redistributions in binary form must reproduce the above copyright
11124667Sdas *    notice, this list of conditions and the following disclaimer in the
12124667Sdas *    documentation and/or other materials provided with the distribution.
13124667Sdas *
14124667Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15124667Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16124667Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17124667Sdas * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18124667Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19124667Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20124667Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21124667Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22124667Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23124667Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24124667Sdas * SUCH DAMAGE.
25124667Sdas */
26124667Sdas
27124667Sdas#include <sys/cdefs.h>
28124667Sdas__FBSDID("$FreeBSD$");
29124667Sdas
30124667Sdas#include <float.h>
31124667Sdas#include <limits.h>
32124667Sdas#include <math.h>
33178154Sdas
34178154Sdas#include "../stdio/floatio.h"
35124667Sdas#include "fpmath.h"
36124667Sdas#include "gdtoaimp.h"
37124667Sdas
38124667Sdas/* Strings values used by dtoa() */
39124667Sdas#define	INFSTR	"Infinity"
40124667Sdas#define	NANSTR	"NaN"
41124667Sdas
42178140Sdas#define	DBL_ADJ	(DBL_MAX_EXP - 2)
43178140Sdas#define	SIGFIGS	((DBL_MANT_DIG + 3) / 4 + 1)
44124667Sdas
45178140Sdasstatic const float one[] = { 1.0f, -1.0f };
46124667Sdas
47124667Sdas/*
48124667Sdas * This procedure converts a double-precision number in IEEE format
49124667Sdas * into a string of hexadecimal digits and an exponent of 2.  Its
50124667Sdas * behavior is bug-for-bug compatible with dtoa() in mode 2, with the
51124667Sdas * following exceptions:
52124667Sdas *
53124667Sdas * - An ndigits < 0 causes it to use as many digits as necessary to
54124667Sdas *   represent the number exactly.
55124667Sdas * - The additional xdigs argument should point to either the string
56124667Sdas *   "0123456789ABCDEF" or the string "0123456789abcdef", depending on
57124667Sdas *   which case is desired.
58124667Sdas * - This routine does not repeat dtoa's mistake of setting decpt
59124667Sdas *   to 9999 in the case of an infinity or NaN.  INT_MAX is used
60124667Sdas *   for this purpose instead.
61124667Sdas *
62124667Sdas * Note that the C99 standard does not specify what the leading digit
63124667Sdas * should be for non-zero numbers.  For instance, 0x1.3p3 is the same
64178140Sdas * as 0x2.6p2 is the same as 0x4.cp3.  This implementation always makes
65178140Sdas * the leading digit a 1. This ensures that the exponent printed is the
66178140Sdas * actual base-2 exponent, i.e., ilogb(d).
67124667Sdas *
68124667Sdas * Inputs:	d, xdigs, ndigits
69124667Sdas * Outputs:	decpt, sign, rve
70124667Sdas */
71124667Sdaschar *
72124667Sdas__hdtoa(double d, const char *xdigs, int ndigits, int *decpt, int *sign,
73124667Sdas    char **rve)
74124667Sdas{
75124667Sdas	union IEEEd2bits u;
76124667Sdas	char *s, *s0;
77124667Sdas	int bufsize;
78178140Sdas	uint32_t manh, manl;
79124667Sdas
80124667Sdas	u.d = d;
81124667Sdas	*sign = u.bits.sign;
82124667Sdas
83124667Sdas	switch (fpclassify(d)) {
84124667Sdas	case FP_NORMAL:
85140430Sdas		*decpt = u.bits.exp - DBL_ADJ;
86124667Sdas		break;
87124667Sdas	case FP_ZERO:
88124667Sdas		*decpt = 1;
89124667Sdas		return (nrv_alloc("0", rve, 1));
90124667Sdas	case FP_SUBNORMAL:
91140430Sdas		u.d *= 0x1p514;
92140430Sdas		*decpt = u.bits.exp - (514 + DBL_ADJ);
93124667Sdas		break;
94124667Sdas	case FP_INFINITE:
95124667Sdas		*decpt = INT_MAX;
96124667Sdas		return (nrv_alloc(INFSTR, rve, sizeof(INFSTR) - 1));
97178140Sdas	default:	/* FP_NAN or unrecognized */
98124667Sdas		*decpt = INT_MAX;
99124667Sdas		return (nrv_alloc(NANSTR, rve, sizeof(NANSTR) - 1));
100124667Sdas	}
101124667Sdas
102124667Sdas	/* FP_NORMAL or FP_SUBNORMAL */
103124667Sdas
104124667Sdas	if (ndigits == 0)		/* dtoa() compatibility */
105124667Sdas		ndigits = 1;
106124667Sdas
107124667Sdas	/*
108178140Sdas	 * If ndigits < 0, we are expected to auto-size, so we allocate
109178140Sdas	 * enough space for all the digits.
110124667Sdas	 */
111178140Sdas	bufsize = (ndigits > 0) ? ndigits : SIGFIGS;
112124667Sdas	s0 = rv_alloc(bufsize);
113124667Sdas
114178140Sdas	/* Round to the desired number of digits. */
115178140Sdas	if (SIGFIGS > ndigits && ndigits > 0) {
116178140Sdas		float redux = one[u.bits.sign];
117178140Sdas		int offset = 4 * ndigits + DBL_MAX_EXP - 4 - DBL_MANT_DIG;
118178140Sdas		u.bits.exp = offset;
119178140Sdas		u.d += redux;
120178140Sdas		u.d -= redux;
121178140Sdas		*decpt += u.bits.exp - offset;
122124667Sdas	}
123124667Sdas
124178140Sdas	manh = u.bits.manh;
125178140Sdas	manl = u.bits.manl;
126178140Sdas	*s0 = '1';
127178140Sdas	for (s = s0 + 1; s < s0 + bufsize; s++) {
128178140Sdas		*s = xdigs[(manh >> (DBL_MANH_SIZE - 4)) & 0xf];
129178140Sdas		manh = (manh << 4) | (manl >> (DBL_MANL_SIZE - 4));
130178140Sdas		manl <<= 4;
131124667Sdas	}
132124667Sdas
133124667Sdas	/* If ndigits < 0, we are expected to auto-size the precision. */
134124667Sdas	if (ndigits < 0) {
135178140Sdas		for (ndigits = SIGFIGS; s0[ndigits - 1] == '0'; ndigits--)
136124667Sdas			;
137124667Sdas	}
138124667Sdas
139124667Sdas	s = s0 + ndigits;
140178140Sdas	*s = '\0';
141124667Sdas	if (rve != NULL)
142124667Sdas		*rve = s;
143124667Sdas	return (s0);
144124667Sdas}
145