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