14bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes/*	$OpenBSD: hdtoa.c,v 1.2 2009/10/16 12:15:03 martynas Exp $	*/
24bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes/*-
34bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * Copyright (c) 2004, 2005 David Schultz <das@FreeBSD.ORG>
44bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * All rights reserved.
54bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes *
64bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * Redistribution and use in source and binary forms, with or without
74bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * modification, are permitted provided that the following conditions
84bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * are met:
94bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * 1. Redistributions of source code must retain the above copyright
104bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes *    notice, this list of conditions and the following disclaimer.
114bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * 2. Redistributions in binary form must reproduce the above copyright
124bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes *    notice, this list of conditions and the following disclaimer in the
134bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes *    documentation and/or other materials provided with the distribution.
144bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes *
154bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
164bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
174bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
184bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
194bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
204bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
214bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
224bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
234bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
244bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
254bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * SUCH DAMAGE.
264bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes */
274bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
284bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes#include <sys/types.h>
294bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes#include <machine/ieee.h>
304bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes#include <float.h>
314bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes#include <limits.h>
324bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes#include <math.h>
334bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
344bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes#include "gdtoaimp.h"
354bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
364bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes/* Strings values used by dtoa() */
374bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes#define	INFSTR	"Infinity"
384bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes#define	NANSTR	"NaN"
394bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
404bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes#define	DBL_ADJ		(DBL_MAX_EXP - 2 + ((DBL_MANT_DIG - 1) % 4))
414bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes#define	LDBL_ADJ	(LDBL_MAX_EXP - 2 + ((LDBL_MANT_DIG - 1) % 4))
424bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
434bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes/*
444bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * Round up the given digit string.  If the digit string is fff...f,
454bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * this procedure sets it to 100...0 and returns 1 to indicate that
464bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * the exponent needs to be bumped.  Otherwise, 0 is returned.
474bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes */
484bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughesstatic int
494bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughesroundup(char *s0, int ndigits)
504bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes{
514bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	char *s;
524bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
534bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	for (s = s0 + ndigits - 1; *s == 0xf; s--) {
544bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		if (s == s0) {
554bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes			*s = 1;
564bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes			return (1);
574bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		}
584bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		*s = 0;
594bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	}
604bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	++*s;
614bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	return (0);
624bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes}
634bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
644bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes/*
654bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * Round the given digit string to ndigits digits according to the
664bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * current rounding mode.  Note that this could produce a string whose
674bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * value is not representable in the corresponding floating-point
684bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * type.  The exponent pointed to by decpt is adjusted if necessary.
694bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes */
704bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughesstatic void
714bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughesdorounding(char *s0, int ndigits, int sign, int *decpt)
724bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes{
734bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	int adjust = 0;	/* do we need to adjust the exponent? */
744bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
754bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	switch (FLT_ROUNDS) {
764bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	case 0:		/* toward zero */
774bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	default:	/* implementation-defined */
784bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		break;
794bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	case 1:		/* to nearest, halfway rounds to even */
804bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		if ((s0[ndigits] > 8) ||
814bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		    (s0[ndigits] == 8 && s0[ndigits + 1] & 1))
824bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes			adjust = roundup(s0, ndigits);
834bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		break;
844bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	case 2:		/* toward +inf */
854bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		if (sign == 0)
864bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes			adjust = roundup(s0, ndigits);
874bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		break;
884bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	case 3:		/* toward -inf */
894bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		if (sign != 0)
904bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes			adjust = roundup(s0, ndigits);
914bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		break;
924bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	}
934bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
944bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	if (adjust)
954bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		*decpt += 4;
964bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes}
974bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
984bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes/*
994bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * This procedure converts a double-precision number in IEEE format
1004bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * into a string of hexadecimal digits and an exponent of 2.  Its
1014bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * behavior is bug-for-bug compatible with dtoa() in mode 2, with the
1024bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * following exceptions:
1034bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes *
1044bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * - An ndigits < 0 causes it to use as many digits as necessary to
1054bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes *   represent the number exactly.
1064bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * - The additional xdigs argument should point to either the string
1074bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes *   "0123456789ABCDEF" or the string "0123456789abcdef", depending on
1084bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes *   which case is desired.
1094bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * - This routine does not repeat dtoa's mistake of setting decpt
1104bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes *   to 9999 in the case of an infinity or NaN.  INT_MAX is used
1114bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes *   for this purpose instead.
1124bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes *
1134bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * Note that the C99 standard does not specify what the leading digit
1144bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * should be for non-zero numbers.  For instance, 0x1.3p3 is the same
1154bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * as 0x2.6p2 is the same as 0x4.cp3.  This implementation chooses the
1164bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * first digit so that subsequent digits are aligned on nibble
1174bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * boundaries (before rounding).
1184bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes *
1194bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * Inputs:	d, xdigs, ndigits
1204bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * Outputs:	decpt, sign, rve
1214bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes */
1224bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hugheschar *
1234bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes__hdtoa(double d, const char *xdigs, int ndigits, int *decpt, int *sign,
1244bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes    char **rve)
1254bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes{
1264bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	static const int sigfigs = (DBL_MANT_DIG + 3) / 4;
1274bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	struct ieee_double *p = (struct ieee_double *)&d;
1284bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	char *s, *s0;
1294bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	int bufsize;
1304bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
1314bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	*sign = p->dbl_sign;
1324bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
1334bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	switch (fpclassify(d)) {
1344bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	case FP_NORMAL:
1354bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		*decpt = p->dbl_exp - DBL_ADJ;
1364bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		break;
1374bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	case FP_ZERO:
1384bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		*decpt = 1;
1394bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		return (nrv_alloc("0", rve, 1));
1404bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	case FP_SUBNORMAL:
1414bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		d *= 0x1p514;
1424bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		*decpt = p->dbl_exp - (514 + DBL_ADJ);
1434bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		break;
1444bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	case FP_INFINITE:
1454bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		*decpt = INT_MAX;
1464bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		return (nrv_alloc(INFSTR, rve, sizeof(INFSTR) - 1));
1474bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	case FP_NAN:
1484bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		*decpt = INT_MAX;
1494bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		return (nrv_alloc(NANSTR, rve, sizeof(NANSTR) - 1));
1504bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	default:
1514bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		abort();
1524bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	}
1534bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
1544bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	/* FP_NORMAL or FP_SUBNORMAL */
1554bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
1564bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	if (ndigits == 0)		/* dtoa() compatibility */
1574bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		ndigits = 1;
1584bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
1594bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	/*
1604bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 * For simplicity, we generate all the digits even if the
1614bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 * caller has requested fewer.
1624bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 */
1634bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	bufsize = (sigfigs > ndigits) ? sigfigs : ndigits;
1644bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	s0 = rv_alloc(bufsize);
1654bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	if (s0 == NULL)
1664bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		return (NULL);
1674bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
1684bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	/*
1694bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 * We work from right to left, first adding any requested zero
1704bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 * padding, then the least significant portion of the
1714bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 * mantissa, followed by the most significant.  The buffer is
1724bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 * filled with the byte values 0x0 through 0xf, which are
1734bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 * converted to xdigs[0x0] through xdigs[0xf] after the
1744bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 * rounding phase.
1754bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 */
1764bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	for (s = s0 + bufsize - 1; s > s0 + sigfigs - 1; s--)
1774bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		*s = 0;
1784bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	for (; s > s0 + sigfigs - (DBL_FRACLBITS / 4) - 1 && s > s0; s--) {
1794bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		*s = p->dbl_fracl & 0xf;
1804bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		p->dbl_fracl >>= 4;
1814bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	}
1824bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	for (; s > s0; s--) {
1834bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		*s = p->dbl_frach & 0xf;
1844bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		p->dbl_frach >>= 4;
1854bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	}
1864bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
1874bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	/*
1884bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 * At this point, we have snarfed all the bits in the
1894bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 * mantissa, with the possible exception of the highest-order
1904bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 * (partial) nibble, which is dealt with by the next
1914bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 * statement.  We also tack on the implicit normalization bit.
1924bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 */
1934bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	*s = p->dbl_frach | (1U << ((DBL_MANT_DIG - 1) % 4));
1944bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
1954bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	/* If ndigits < 0, we are expected to auto-size the precision. */
1964bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	if (ndigits < 0) {
1974bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		for (ndigits = sigfigs; s0[ndigits - 1] == 0; ndigits--)
1984bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes			;
1994bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	}
2004bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
2014bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	if (sigfigs > ndigits && s0[ndigits] != 0)
2024bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		dorounding(s0, ndigits, p->dbl_sign, decpt);
2034bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
2044bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	s = s0 + ndigits;
2054bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	if (rve != NULL)
2064bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		*rve = s;
2074bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	*s-- = '\0';
2084bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	for (; s >= s0; s--)
2094bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		*s = xdigs[(unsigned int)*s];
2104bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
2114bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	return (s0);
2124bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes}
2134bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
2144bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes#if (LDBL_MANT_DIG > DBL_MANT_DIG)
2154bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
2164bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes/*
2174bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * This is the long double version of __hdtoa().
2184bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes */
2194bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hugheschar *
2204bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes__hldtoa(long double e, const char *xdigs, int ndigits, int *decpt, int *sign,
2214bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes    char **rve)
2224bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes{
2234bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	static const int sigfigs = (LDBL_MANT_DIG + 3) / 4;
2244bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	struct ieee_ext *p = (struct ieee_ext *)&e;
2254bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	char *s, *s0;
2264bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	int bufsize;
2274bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
2284bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	*sign = p->ext_sign;
2294bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
2304bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	switch (fpclassify(e)) {
2314bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	case FP_NORMAL:
2324bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		*decpt = p->ext_exp - LDBL_ADJ;
2334bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		break;
2344bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	case FP_ZERO:
2354bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		*decpt = 1;
2364bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		return (nrv_alloc("0", rve, 1));
2374bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	case FP_SUBNORMAL:
2384bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		e *= 0x1p514L;
2394bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		*decpt = p->ext_exp - (514 + LDBL_ADJ);
2404bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		break;
2414bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	case FP_INFINITE:
2424bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		*decpt = INT_MAX;
2434bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		return (nrv_alloc(INFSTR, rve, sizeof(INFSTR) - 1));
2444bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	case FP_NAN:
2454bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		*decpt = INT_MAX;
2464bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		return (nrv_alloc(NANSTR, rve, sizeof(NANSTR) - 1));
2474bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	default:
2484bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		abort();
2494bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	}
2504bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
2514bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	/* FP_NORMAL or FP_SUBNORMAL */
2524bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
2534bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	if (ndigits == 0)		/* dtoa() compatibility */
2544bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		ndigits = 1;
2554bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
2564bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	/*
2574bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 * For simplicity, we generate all the digits even if the
2584bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 * caller has requested fewer.
2594bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 */
2604bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	bufsize = (sigfigs > ndigits) ? sigfigs : ndigits;
2614bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	s0 = rv_alloc(bufsize);
2624bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	if (s0 == NULL)
2634bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		return (NULL);
2644bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
2654bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	/*
2664bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 * We work from right to left, first adding any requested zero
2674bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 * padding, then the least significant portion of the
2684bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 * mantissa, followed by the most significant.  The buffer is
2694bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 * filled with the byte values 0x0 through 0xf, which are
2704bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 * converted to xdigs[0x0] through xdigs[0xf] after the
2714bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 * rounding phase.
2724bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 */
2734bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	for (s = s0 + bufsize - 1; s > s0 + sigfigs - 1; s--)
2744bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		*s = 0;
2754bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	for (; s > s0 + sigfigs - (EXT_FRACLBITS / 4) - 1 && s > s0; s--) {
2764bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		*s = p->ext_fracl & 0xf;
2774bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		p->ext_fracl >>= 4;
2784bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	}
2794bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes#ifdef EXT_FRACHMBITS
2804bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	for (; s > s0; s--) {
2814bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		*s = p->ext_frachm & 0xf;
2824bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		p->ext_frachm >>= 4;
2834bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	}
2844bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes#endif
2854bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes#ifdef EXT_FRACLMBITS
2864bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	for (; s > s0; s--) {
2874bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		*s = p->ext_fraclm & 0xf;
2884bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		p->ext_fraclm >>= 4;
2894bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	}
2904bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes#endif
2914bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	for (; s > s0; s--) {
2924bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		*s = p->ext_frach & 0xf;
2934bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		p->ext_frach >>= 4;
2944bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	}
2954bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
2964bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	/*
2974bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 * At this point, we have snarfed all the bits in the
2984bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 * mantissa, with the possible exception of the highest-order
2994bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 * (partial) nibble, which is dealt with by the next
3004bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 * statement.  We also tack on the implicit normalization bit.
3014bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	 */
3024bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	*s = p->ext_frach | (1U << ((LDBL_MANT_DIG - 1) % 4));
3034bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
3044bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	/* If ndigits < 0, we are expected to auto-size the precision. */
3054bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	if (ndigits < 0) {
3064bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		for (ndigits = sigfigs; s0[ndigits - 1] == 0; ndigits--)
3074bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes			;
3084bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	}
3094bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
3104bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	if (sigfigs > ndigits && s0[ndigits] != 0)
3114bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		dorounding(s0, ndigits, p->ext_sign, decpt);
3124bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
3134bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	s = s0 + ndigits;
3144bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	if (rve != NULL)
3154bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		*rve = s;
3164bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	*s-- = '\0';
3174bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	for (; s >= s0; s--)
3184bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes		*s = xdigs[(unsigned int)*s];
3194bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
3204bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	return (s0);
3214bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes}
3224bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
3234bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes#else	/* (LDBL_MANT_DIG == DBL_MANT_DIG) */
3244bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
3254bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hugheschar *
3264bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes__hldtoa(long double e, const char *xdigs, int ndigits, int *decpt, int *sign,
3274bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes    char **rve)
3284bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes{
3294bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes	return (__hdtoa((double)e, xdigs, ndigits, decpt, sign, rve));
3304bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes}
3314bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes
3324bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes#endif	/* (LDBL_MANT_DIG == DBL_MANT_DIG) */
333