Home | History | Annotate | Line # | Download | only in gdtoa
hdtoa.c revision 1.3
      1 /*	$NetBSD: hdtoa.c,v 1.3 2007/02/03 18:09:20 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2004, 2005 David Schultz <das (at) FreeBSD.ORG>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 #if 0
     31 __FBSDID("$FreeBSD: src/lib/libc/gdtoa/_hdtoa.c,v 1.4 2007/01/03 04:57:58 das Exp $");
     32 #else
     33 __RCSID("$NetBSD: hdtoa.c,v 1.3 2007/02/03 18:09:20 christos Exp $");
     34 #endif
     35 
     36 #include <float.h>
     37 #include <limits.h>
     38 #include <math.h>
     39 #include <machine/ieee.h>
     40 #include "gdtoaimp.h"
     41 
     42 /* Strings values used by dtoa() */
     43 #define	INFSTR	"Infinity"
     44 #define	NANSTR	"NaN"
     45 
     46 #define	DBL_ADJ		(DBL_MAX_EXP - 2 + ((DBL_MANT_DIG - 1) % 4))
     47 #define	LDBL_ADJ	(LDBL_MAX_EXP - 2 + ((LDBL_MANT_DIG - 1) % 4))
     48 
     49 /*
     50  * Round up the given digit string.  If the digit string is fff...f,
     51  * this procedure sets it to 100...0 and returns 1 to indicate that
     52  * the exponent needs to be bumped.  Otherwise, 0 is returned.
     53  */
     54 static int
     55 roundup(char *s0, int ndigits)
     56 {
     57 	char *s;
     58 
     59 	for (s = s0 + ndigits - 1; *s == 0xf; s--) {
     60 		if (s == s0) {
     61 			*s = 1;
     62 			return (1);
     63 		}
     64 		*s = 0;
     65 	}
     66 	++*s;
     67 	return (0);
     68 }
     69 
     70 /*
     71  * Round the given digit string to ndigits digits according to the
     72  * current rounding mode.  Note that this could produce a string whose
     73  * value is not representable in the corresponding floating-point
     74  * type.  The exponent pointed to by decpt is adjusted if necessary.
     75  */
     76 static void
     77 dorounding(char *s0, int ndigits, int sign, int *decpt)
     78 {
     79 	int adjust = 0;	/* do we need to adjust the exponent? */
     80 
     81 	switch (FLT_ROUNDS) {
     82 	case 0:		/* toward zero */
     83 	default:	/* implementation-defined */
     84 		break;
     85 	case 1:		/* to nearest, halfway rounds to even */
     86 		if ((s0[ndigits] > 8) ||
     87 		    (s0[ndigits] == 8 && s0[ndigits - 1] & 1))
     88 			adjust = roundup(s0, ndigits);
     89 		break;
     90 	case 2:		/* toward +inf */
     91 		if (sign == 0)
     92 			adjust = roundup(s0, ndigits);
     93 		break;
     94 	case 3:		/* toward -inf */
     95 		if (sign != 0)
     96 			adjust = roundup(s0, ndigits);
     97 		break;
     98 	}
     99 
    100 	if (adjust)
    101 		*decpt += 4;
    102 }
    103 
    104 /*
    105  * This procedure converts a double-precision number in IEEE format
    106  * into a string of hexadecimal digits and an exponent of 2.  Its
    107  * behavior is bug-for-bug compatible with dtoa() in mode 2, with the
    108  * following exceptions:
    109  *
    110  * - An ndigits < 0 causes it to use as many digits as necessary to
    111  *   represent the number exactly.
    112  * - The additional xdigs argument should point to either the string
    113  *   "0123456789ABCDEF" or the string "0123456789abcdef", depending on
    114  *   which case is desired.
    115  * - This routine does not repeat dtoa's mistake of setting decpt
    116  *   to 9999 in the case of an infinity or NaN.  INT_MAX is used
    117  *   for this purpose instead.
    118  *
    119  * Note that the C99 standard does not specify what the leading digit
    120  * should be for non-zero numbers.  For instance, 0x1.3p3 is the same
    121  * as 0x2.6p2 is the same as 0x4.cp3.  This implementation chooses the
    122  * first digit so that subsequent digits are aligned on nibble
    123  * boundaries (before rounding).
    124  *
    125  * Inputs:	d, xdigs, ndigits
    126  * Outputs:	decpt, sign, rve
    127  */
    128 char *
    129 hdtoa(double d, const char *xdigs, int ndigits, int *decpt, int *sign,
    130     char **rve)
    131 {
    132 	static const int sigfigs = (DBL_MANT_DIG + 3) / 4;
    133 	union ieee_double_u u;
    134 	char *s, *s0;
    135 	size_t bufsize;
    136 
    137 	u.dblu_d = d;
    138 	*sign = u.dblu_dbl.dbl_sign;
    139 
    140 	switch (fpclassify(d)) {
    141 	case FP_NORMAL:
    142 		*decpt = u.dblu_dbl.dbl_exp - DBL_ADJ;
    143 		break;
    144 	case FP_ZERO:
    145 		*decpt = 1;
    146 		return (nrv_alloc("0", rve, 1));
    147 	case FP_SUBNORMAL:
    148 		u.dblu_d *= 0x1p514;
    149 		*decpt = u.dblu_dbl.dbl_exp - (514 + DBL_ADJ);
    150 		break;
    151 	case FP_INFINITE:
    152 		*decpt = INT_MAX;
    153 		return (nrv_alloc(INFSTR, rve, sizeof(INFSTR) - 1));
    154 	case FP_NAN:
    155 		*decpt = INT_MAX;
    156 		return (nrv_alloc(NANSTR, rve, sizeof(NANSTR) - 1));
    157 	default:
    158 		abort();
    159 	}
    160 
    161 	/* FP_NORMAL or FP_SUBNORMAL */
    162 
    163 	if (ndigits == 0)		/* dtoa() compatibility */
    164 		ndigits = 1;
    165 
    166 	/*
    167 	 * For simplicity, we generate all the digits even if the
    168 	 * caller has requested fewer.
    169 	 */
    170 	bufsize = (sigfigs > ndigits) ? sigfigs : ndigits;
    171 	s0 = rv_alloc(bufsize);
    172 
    173 	/*
    174 	 * We work from right to left, first adding any requested zero
    175 	 * padding, then the least significant portion of the
    176 	 * mantissa, followed by the most significant.  The buffer is
    177 	 * filled with the byte values 0x0 through 0xf, which are
    178 	 * converted to xdigs[0x0] through xdigs[0xf] after the
    179 	 * rounding phase.
    180 	 */
    181 	for (s = s0 + bufsize - 1; s > s0 + sigfigs - 1; s--)
    182 		*s = 0;
    183 	for (; s > s0 + sigfigs - (DBL_FRACLBITS / 4) - 1 && s > s0; s--) {
    184 		*s = u.dblu_dbl.dbl_fracl & 0xf;
    185 		u.dblu_dbl.dbl_fracl >>= 4;
    186 	}
    187 	for (; s > s0; s--) {
    188 		*s = u.dblu_dbl.dbl_frach & 0xf;
    189 		u.dblu_dbl.dbl_frach >>= 4;
    190 	}
    191 
    192 	/*
    193 	 * At this point, we have snarfed all the bits in the
    194 	 * mantissa, with the possible exception of the highest-order
    195 	 * (partial) nibble, which is dealt with by the next
    196 	 * statement.  We also tack on the implicit normalization bit.
    197 	 */
    198 	*s = u.dblu_dbl.dbl_frach | (1U << ((DBL_MANT_DIG - 1) % 4));
    199 
    200 	/* If ndigits < 0, we are expected to auto-size the precision. */
    201 	if (ndigits < 0) {
    202 		for (ndigits = sigfigs; s0[ndigits - 1] == 0; ndigits--)
    203 			continue;
    204 	}
    205 
    206 	if (sigfigs > ndigits && s0[ndigits] != 0)
    207 		dorounding(s0, ndigits, u.dblu_dbl.dbl_sign, decpt);
    208 
    209 	s = s0 + ndigits;
    210 	if (rve != NULL)
    211 		*rve = s;
    212 	*s-- = '\0';
    213 	for (; s >= s0; s--)
    214 		*s = xdigs[(unsigned int)*s];
    215 
    216 	return (s0);
    217 }
    218 
    219 #if (LDBL_MANT_DIG > DBL_MANT_DIG)
    220 
    221 /*
    222  * This is the long double version of hdtoa().
    223  */
    224 char *
    225 hldtoa(long double e, const char *xdigs, int ndigits, int *decpt, int *sign,
    226     char **rve)
    227 {
    228 	static const int sigfigs = (LDBL_MANT_DIG + 3) / 4;
    229 	union ieee_ext_u u;
    230 	char *s, *s0;
    231 	size_t bufsize;
    232 
    233 	u.extu_ld = e;
    234 	*sign = u.extu_ext.ext_sign;
    235 
    236 	switch (fpclassify(e)) {
    237 	case FP_NORMAL:
    238 		*decpt = u.extu_ext.ext_exp - LDBL_ADJ;
    239 		break;
    240 	case FP_ZERO:
    241 		*decpt = 1;
    242 		return (nrv_alloc("0", rve, 1));
    243 	case FP_SUBNORMAL:
    244 		u.extu_ld *= 0x1p514L;
    245 		*decpt = u.extu_ext.ext_exp - (514 + LDBL_ADJ);
    246 		break;
    247 	case FP_INFINITE:
    248 		*decpt = INT_MAX;
    249 		return (nrv_alloc(INFSTR, rve, sizeof(INFSTR) - 1));
    250 	case FP_NAN:
    251 		*decpt = INT_MAX;
    252 		return (nrv_alloc(NANSTR, rve, sizeof(NANSTR) - 1));
    253 	default:
    254 		abort();
    255 	}
    256 
    257 	/* FP_NORMAL or FP_SUBNORMAL */
    258 
    259 	if (ndigits == 0)		/* dtoa() compatibility */
    260 		ndigits = 1;
    261 
    262 	/*
    263 	 * For simplicity, we generate all the digits even if the
    264 	 * caller has requested fewer.
    265 	 */
    266 	bufsize = (sigfigs > ndigits) ? sigfigs : ndigits;
    267 	s0 = rv_alloc(bufsize);
    268 
    269 	/*
    270 	 * We work from right to left, first adding any requested zero
    271 	 * padding, then the least significant portion of the
    272 	 * mantissa, followed by the most significant.  The buffer is
    273 	 * filled with the byte values 0x0 through 0xf, which are
    274 	 * converted to xdigs[0x0] through xdigs[0xf] after the
    275 	 * rounding phase.
    276 	 */
    277 	for (s = s0 + bufsize - 1; s > s0 + sigfigs - 1; s--)
    278 		*s = 0;
    279 	for (; s > s0 + sigfigs - (EXT_FRACLBITS / 4) - 1 && s > s0; s--) {
    280 		*s = u.extu_ext.ext_fracl & 0xf;
    281 		u.extu_ext.ext_fracl >>= 4;
    282 	}
    283 #ifdef EXT_FRACHMBITS
    284 	for (; s > s0; s--) {
    285 		*s = u.extu_ext.ext_frachm & 0xf;
    286 		u.extu_ext.ext_frachm >>= 4;
    287 	}
    288 #endif
    289 #ifdef EXT_FRACLMBITS
    290 	for (; s > s0; s--) {
    291 		*s = u.extu_ext.ext_fraclm & 0xf;
    292 		u.extu_ext.ext_fraclm >>= 4;
    293 	}
    294 #endif
    295 	for (; s > s0; s--) {
    296 		*s = u.extu_ext.ext_frach & 0xf;
    297 		u.extu_ext.ext_frach >>= 4;
    298 	}
    299 
    300 	/*
    301 	 * At this point, we have snarfed all the bits in the
    302 	 * mantissa, with the possible exception of the highest-order
    303 	 * (partial) nibble, which is dealt with by the next
    304 	 * statement.  We also tack on the implicit normalization bit.
    305 	 */
    306 	*s = u.extu_ext.ext_frach | (1U << ((LDBL_MANT_DIG - 1) % 4));
    307 
    308 	/* If ndigits < 0, we are expected to auto-size the precision. */
    309 	if (ndigits < 0) {
    310 		for (ndigits = sigfigs; s0[ndigits - 1] == 0; ndigits--)
    311 			continue;
    312 	}
    313 
    314 	if (sigfigs > ndigits && s0[ndigits] != 0)
    315 		dorounding(s0, ndigits, u.extu_ext.ext_sign, decpt);
    316 
    317 	s = s0 + ndigits;
    318 	if (rve != NULL)
    319 		*rve = s;
    320 	*s-- = '\0';
    321 	for (; s >= s0; s--)
    322 		*s = xdigs[(unsigned int)*s];
    323 
    324 	return (s0);
    325 }
    326 
    327 #else	/* (LDBL_MANT_DIG == DBL_MANT_DIG) */
    328 
    329 char *
    330 hldtoa(long double e, const char *xdigs, int ndigits, int *decpt, int *sign,
    331     char **rve)
    332 {
    333 
    334 	return (hdtoa((double)e, xdigs, ndigits, decpt, sign, rve));
    335 }
    336 
    337 #endif	/* (LDBL_MANT_DIG == DBL_MANT_DIG) */
    338