xref: /dragonfly/lib/libc/gdtoa/_ldtoa.c (revision 86d7f5d3)
1*86d7f5d3SJohn Marino /*-
2*86d7f5d3SJohn Marino  * Copyright (c) 2003 David Schultz <das@FreeBSD.ORG>
3*86d7f5d3SJohn Marino  * All rights reserved.
4*86d7f5d3SJohn Marino  *
5*86d7f5d3SJohn Marino  * Redistribution and use in source and binary forms, with or without
6*86d7f5d3SJohn Marino  * modification, are permitted provided that the following conditions
7*86d7f5d3SJohn Marino  * are met:
8*86d7f5d3SJohn Marino  * 1. Redistributions of source code must retain the above copyright
9*86d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer.
10*86d7f5d3SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
11*86d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
12*86d7f5d3SJohn Marino  *    documentation and/or other materials provided with the distribution.
13*86d7f5d3SJohn Marino  *
14*86d7f5d3SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*86d7f5d3SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*86d7f5d3SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*86d7f5d3SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*86d7f5d3SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*86d7f5d3SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*86d7f5d3SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*86d7f5d3SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*86d7f5d3SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*86d7f5d3SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*86d7f5d3SJohn Marino  * SUCH DAMAGE.
25*86d7f5d3SJohn Marino  *
26*86d7f5d3SJohn Marino  * $FreeBSD: src/lib/libc/gdtoa/_ldtoa.c,v 1.5 2007/12/09 19:48:57 das Exp $
27*86d7f5d3SJohn Marino  */
28*86d7f5d3SJohn Marino 
29*86d7f5d3SJohn Marino #include <float.h>
30*86d7f5d3SJohn Marino #include <inttypes.h>
31*86d7f5d3SJohn Marino #include <limits.h>
32*86d7f5d3SJohn Marino #include <math.h>
33*86d7f5d3SJohn Marino #include <stdlib.h>
34*86d7f5d3SJohn Marino #include "fpmath.h"
35*86d7f5d3SJohn Marino #include "gdtoaimp.h"
36*86d7f5d3SJohn Marino 
37*86d7f5d3SJohn Marino /*
38*86d7f5d3SJohn Marino  * ldtoa() is a wrapper for gdtoa() that makes it smell like dtoa(),
39*86d7f5d3SJohn Marino  * except that the floating point argument is passed by reference.
40*86d7f5d3SJohn Marino  * When dtoa() is passed a NaN or infinity, it sets expt to 9999.
41*86d7f5d3SJohn Marino  * However, a long double could have a valid exponent of 9999, so we
42*86d7f5d3SJohn Marino  * use INT_MAX in ldtoa() instead.
43*86d7f5d3SJohn Marino  */
44*86d7f5d3SJohn Marino char *
__ldtoa(long double * ld,int mode,int ndigits,int * decpt,int * sign,char ** rve)45*86d7f5d3SJohn Marino __ldtoa(long double *ld, int mode, int ndigits, int *decpt, int *sign,
46*86d7f5d3SJohn Marino     char **rve)
47*86d7f5d3SJohn Marino {
48*86d7f5d3SJohn Marino 	FPI fpi = {
49*86d7f5d3SJohn Marino 		LDBL_MANT_DIG,			/* nbits */
50*86d7f5d3SJohn Marino 		LDBL_MIN_EXP - LDBL_MANT_DIG,	/* emin */
51*86d7f5d3SJohn Marino 		LDBL_MAX_EXP - LDBL_MANT_DIG,	/* emax */
52*86d7f5d3SJohn Marino 		FLT_ROUNDS,	       		/* rounding */
53*86d7f5d3SJohn Marino #ifdef Sudden_Underflow	/* unused, but correct anyway */
54*86d7f5d3SJohn Marino 		1
55*86d7f5d3SJohn Marino #else
56*86d7f5d3SJohn Marino 		0
57*86d7f5d3SJohn Marino #endif
58*86d7f5d3SJohn Marino 	};
59*86d7f5d3SJohn Marino 	int be, kind;
60*86d7f5d3SJohn Marino 	char *ret;
61*86d7f5d3SJohn Marino 	union IEEEl2bits u;
62*86d7f5d3SJohn Marino 	uint32_t bits[(LDBL_MANT_DIG + 31) / 32];
63*86d7f5d3SJohn Marino 	void *vbits = bits;
64*86d7f5d3SJohn Marino 
65*86d7f5d3SJohn Marino 	u.e = *ld;
66*86d7f5d3SJohn Marino 
67*86d7f5d3SJohn Marino 	/*
68*86d7f5d3SJohn Marino 	 * gdtoa doesn't know anything about the sign of the number, so
69*86d7f5d3SJohn Marino 	 * if the number is negative, we need to swap rounding modes of
70*86d7f5d3SJohn Marino 	 * 2 (upwards) and 3 (downwards).
71*86d7f5d3SJohn Marino 	 */
72*86d7f5d3SJohn Marino 	*sign = u.bits.sign;
73*86d7f5d3SJohn Marino 	fpi.rounding ^= (fpi.rounding >> 1) & u.bits.sign;
74*86d7f5d3SJohn Marino 
75*86d7f5d3SJohn Marino 	be = u.bits.exp - (LDBL_MAX_EXP - 1) - (LDBL_MANT_DIG - 1);
76*86d7f5d3SJohn Marino 	LDBL_TO_ARRAY32(u, bits);
77*86d7f5d3SJohn Marino 
78*86d7f5d3SJohn Marino 	switch (fpclassify(u.e)) {
79*86d7f5d3SJohn Marino 	case FP_NORMAL:
80*86d7f5d3SJohn Marino 		kind = STRTOG_Normal;
81*86d7f5d3SJohn Marino #ifdef	LDBL_IMPLICIT_NBIT
82*86d7f5d3SJohn Marino 		bits[LDBL_MANT_DIG / 32] |= 1 << ((LDBL_MANT_DIG - 1) % 32);
83*86d7f5d3SJohn Marino #endif /* LDBL_IMPLICIT_NBIT */
84*86d7f5d3SJohn Marino 		break;
85*86d7f5d3SJohn Marino 	case FP_ZERO:
86*86d7f5d3SJohn Marino 		kind = STRTOG_Zero;
87*86d7f5d3SJohn Marino 		break;
88*86d7f5d3SJohn Marino 	case FP_SUBNORMAL:
89*86d7f5d3SJohn Marino 		kind = STRTOG_Denormal;
90*86d7f5d3SJohn Marino 		be++;
91*86d7f5d3SJohn Marino 		break;
92*86d7f5d3SJohn Marino 	case FP_INFINITE:
93*86d7f5d3SJohn Marino 		kind = STRTOG_Infinite;
94*86d7f5d3SJohn Marino 		break;
95*86d7f5d3SJohn Marino 	case FP_NAN:
96*86d7f5d3SJohn Marino 		kind = STRTOG_NaN;
97*86d7f5d3SJohn Marino 		break;
98*86d7f5d3SJohn Marino 	default:
99*86d7f5d3SJohn Marino 		abort();
100*86d7f5d3SJohn Marino 	}
101*86d7f5d3SJohn Marino 
102*86d7f5d3SJohn Marino 	ret = gdtoa(&fpi, be, vbits, &kind, mode, ndigits, decpt, rve);
103*86d7f5d3SJohn Marino 	if (*decpt == -32768)
104*86d7f5d3SJohn Marino 		*decpt = INT_MAX;
105*86d7f5d3SJohn Marino 	return ret;
106*86d7f5d3SJohn Marino }
107