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