xref: /freebsd-src/lib/libc/gdtoa/_hdtoa.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
18f592773SDavid Schultz /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni  *
476303a97SDavid Schultz  * Copyright (c) 2004-2008 David Schultz <das@FreeBSD.ORG>
58f592773SDavid Schultz  * All rights reserved.
68f592773SDavid Schultz  *
78f592773SDavid Schultz  * Redistribution and use in source and binary forms, with or without
88f592773SDavid Schultz  * modification, are permitted provided that the following conditions
98f592773SDavid Schultz  * are met:
108f592773SDavid Schultz  * 1. Redistributions of source code must retain the above copyright
118f592773SDavid Schultz  *    notice, this list of conditions and the following disclaimer.
128f592773SDavid Schultz  * 2. Redistributions in binary form must reproduce the above copyright
138f592773SDavid Schultz  *    notice, this list of conditions and the following disclaimer in the
148f592773SDavid Schultz  *    documentation and/or other materials provided with the distribution.
158f592773SDavid Schultz  *
168f592773SDavid Schultz  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
178f592773SDavid Schultz  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
188f592773SDavid Schultz  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
198f592773SDavid Schultz  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
208f592773SDavid Schultz  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
218f592773SDavid Schultz  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
228f592773SDavid Schultz  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
238f592773SDavid Schultz  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
248f592773SDavid Schultz  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
258f592773SDavid Schultz  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
268f592773SDavid Schultz  * SUCH DAMAGE.
278f592773SDavid Schultz  */
288f592773SDavid Schultz 
298f592773SDavid Schultz #include <float.h>
308f592773SDavid Schultz #include <limits.h>
318f592773SDavid Schultz #include <math.h>
3277fab5a8SDavid Schultz 
3377fab5a8SDavid Schultz #include "../stdio/floatio.h"
348f592773SDavid Schultz #include "fpmath.h"
358f592773SDavid Schultz #include "gdtoaimp.h"
368f592773SDavid Schultz 
378f592773SDavid Schultz /* Strings values used by dtoa() */
388f592773SDavid Schultz #define	INFSTR	"Infinity"
398f592773SDavid Schultz #define	NANSTR	"NaN"
408f592773SDavid Schultz 
4176303a97SDavid Schultz #define	DBL_ADJ	(DBL_MAX_EXP - 2)
4276303a97SDavid Schultz #define	SIGFIGS	((DBL_MANT_DIG + 3) / 4 + 1)
438f592773SDavid Schultz 
4476303a97SDavid Schultz static const float one[] = { 1.0f, -1.0f };
458f592773SDavid Schultz 
468f592773SDavid Schultz /*
478f592773SDavid Schultz  * This procedure converts a double-precision number in IEEE format
488f592773SDavid Schultz  * into a string of hexadecimal digits and an exponent of 2.  Its
498f592773SDavid Schultz  * behavior is bug-for-bug compatible with dtoa() in mode 2, with the
508f592773SDavid Schultz  * following exceptions:
518f592773SDavid Schultz  *
528f592773SDavid Schultz  * - An ndigits < 0 causes it to use as many digits as necessary to
538f592773SDavid Schultz  *   represent the number exactly.
548f592773SDavid Schultz  * - The additional xdigs argument should point to either the string
558f592773SDavid Schultz  *   "0123456789ABCDEF" or the string "0123456789abcdef", depending on
568f592773SDavid Schultz  *   which case is desired.
578f592773SDavid Schultz  * - This routine does not repeat dtoa's mistake of setting decpt
588f592773SDavid Schultz  *   to 9999 in the case of an infinity or NaN.  INT_MAX is used
598f592773SDavid Schultz  *   for this purpose instead.
608f592773SDavid Schultz  *
618f592773SDavid Schultz  * Note that the C99 standard does not specify what the leading digit
628f592773SDavid Schultz  * should be for non-zero numbers.  For instance, 0x1.3p3 is the same
6376303a97SDavid Schultz  * as 0x2.6p2 is the same as 0x4.cp3.  This implementation always makes
6476303a97SDavid Schultz  * the leading digit a 1. This ensures that the exponent printed is the
6576303a97SDavid Schultz  * actual base-2 exponent, i.e., ilogb(d).
668f592773SDavid Schultz  *
678f592773SDavid Schultz  * Inputs:	d, xdigs, ndigits
688f592773SDavid Schultz  * Outputs:	decpt, sign, rve
698f592773SDavid Schultz  */
708f592773SDavid Schultz char *
__hdtoa(double d,const char * xdigs,int ndigits,int * decpt,int * sign,char ** rve)718f592773SDavid Schultz __hdtoa(double d, const char *xdigs, int ndigits, int *decpt, int *sign,
728f592773SDavid Schultz     char **rve)
738f592773SDavid Schultz {
748f592773SDavid Schultz 	union IEEEd2bits u;
758f592773SDavid Schultz 	char *s, *s0;
768f592773SDavid Schultz 	int bufsize;
7776303a97SDavid Schultz 	uint32_t manh, manl;
788f592773SDavid Schultz 
798f592773SDavid Schultz 	u.d = d;
808f592773SDavid Schultz 	*sign = u.bits.sign;
818f592773SDavid Schultz 
828f592773SDavid Schultz 	switch (fpclassify(d)) {
838f592773SDavid Schultz 	case FP_NORMAL:
849fd7a48dSDavid Schultz 		*decpt = u.bits.exp - DBL_ADJ;
858f592773SDavid Schultz 		break;
868f592773SDavid Schultz 	case FP_ZERO:
878f592773SDavid Schultz 		*decpt = 1;
888f592773SDavid Schultz 		return (nrv_alloc("0", rve, 1));
898f592773SDavid Schultz 	case FP_SUBNORMAL:
909fd7a48dSDavid Schultz 		u.d *= 0x1p514;
919fd7a48dSDavid Schultz 		*decpt = u.bits.exp - (514 + DBL_ADJ);
928f592773SDavid Schultz 		break;
938f592773SDavid Schultz 	case FP_INFINITE:
948f592773SDavid Schultz 		*decpt = INT_MAX;
958f592773SDavid Schultz 		return (nrv_alloc(INFSTR, rve, sizeof(INFSTR) - 1));
9676303a97SDavid Schultz 	default:	/* FP_NAN or unrecognized */
978f592773SDavid Schultz 		*decpt = INT_MAX;
988f592773SDavid Schultz 		return (nrv_alloc(NANSTR, rve, sizeof(NANSTR) - 1));
998f592773SDavid Schultz 	}
1008f592773SDavid Schultz 
1018f592773SDavid Schultz 	/* FP_NORMAL or FP_SUBNORMAL */
1028f592773SDavid Schultz 
1038f592773SDavid Schultz 	if (ndigits == 0)		/* dtoa() compatibility */
1048f592773SDavid Schultz 		ndigits = 1;
1058f592773SDavid Schultz 
1068f592773SDavid Schultz 	/*
10776303a97SDavid Schultz 	 * If ndigits < 0, we are expected to auto-size, so we allocate
10876303a97SDavid Schultz 	 * enough space for all the digits.
1098f592773SDavid Schultz 	 */
11076303a97SDavid Schultz 	bufsize = (ndigits > 0) ? ndigits : SIGFIGS;
1118f592773SDavid Schultz 	s0 = rv_alloc(bufsize);
1128f592773SDavid Schultz 
11376303a97SDavid Schultz 	/* Round to the desired number of digits. */
11476303a97SDavid Schultz 	if (SIGFIGS > ndigits && ndigits > 0) {
11576303a97SDavid Schultz 		float redux = one[u.bits.sign];
11676303a97SDavid Schultz 		int offset = 4 * ndigits + DBL_MAX_EXP - 4 - DBL_MANT_DIG;
11776303a97SDavid Schultz 		u.bits.exp = offset;
11876303a97SDavid Schultz 		u.d += redux;
11976303a97SDavid Schultz 		u.d -= redux;
12076303a97SDavid Schultz 		*decpt += u.bits.exp - offset;
1218f592773SDavid Schultz 	}
1228f592773SDavid Schultz 
12376303a97SDavid Schultz 	manh = u.bits.manh;
12476303a97SDavid Schultz 	manl = u.bits.manl;
12576303a97SDavid Schultz 	*s0 = '1';
12676303a97SDavid Schultz 	for (s = s0 + 1; s < s0 + bufsize; s++) {
12776303a97SDavid Schultz 		*s = xdigs[(manh >> (DBL_MANH_SIZE - 4)) & 0xf];
12876303a97SDavid Schultz 		manh = (manh << 4) | (manl >> (DBL_MANL_SIZE - 4));
12976303a97SDavid Schultz 		manl <<= 4;
13076303a97SDavid Schultz 	}
1318f592773SDavid Schultz 
1328f592773SDavid Schultz 	/* If ndigits < 0, we are expected to auto-size the precision. */
1338f592773SDavid Schultz 	if (ndigits < 0) {
13476303a97SDavid Schultz 		for (ndigits = SIGFIGS; s0[ndigits - 1] == '0'; ndigits--)
1358f592773SDavid Schultz 			;
1368f592773SDavid Schultz 	}
1378f592773SDavid Schultz 
1388f592773SDavid Schultz 	s = s0 + ndigits;
13976303a97SDavid Schultz 	*s = '\0';
1408f592773SDavid Schultz 	if (rve != NULL)
1418f592773SDavid Schultz 		*rve = s;
1428f592773SDavid Schultz 	return (s0);
1438f592773SDavid Schultz }
144