1*f202adf7SPeter Avalos /*-
2*f202adf7SPeter Avalos * Copyright (c) 2004-2008 David Schultz <das@FreeBSD.ORG>
3*f202adf7SPeter Avalos * All rights reserved.
4*f202adf7SPeter Avalos *
5*f202adf7SPeter Avalos * Redistribution and use in source and binary forms, with or without
6*f202adf7SPeter Avalos * modification, are permitted provided that the following conditions
7*f202adf7SPeter Avalos * are met:
8*f202adf7SPeter Avalos * 1. Redistributions of source code must retain the above copyright
9*f202adf7SPeter Avalos * notice, this list of conditions and the following disclaimer.
10*f202adf7SPeter Avalos * 2. Redistributions in binary form must reproduce the above copyright
11*f202adf7SPeter Avalos * notice, this list of conditions and the following disclaimer in the
12*f202adf7SPeter Avalos * documentation and/or other materials provided with the distribution.
13*f202adf7SPeter Avalos *
14*f202adf7SPeter Avalos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*f202adf7SPeter Avalos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*f202adf7SPeter Avalos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*f202adf7SPeter Avalos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*f202adf7SPeter Avalos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*f202adf7SPeter Avalos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*f202adf7SPeter Avalos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*f202adf7SPeter Avalos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*f202adf7SPeter Avalos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*f202adf7SPeter Avalos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*f202adf7SPeter Avalos * SUCH DAMAGE.
25*f202adf7SPeter Avalos *
26*f202adf7SPeter Avalos * $FreeBSD: src/lib/libc/gdtoa/_hldtoa.c,v 1.2 2008/04/12 14:53:52 das Exp $
27*f202adf7SPeter Avalos */
28*f202adf7SPeter Avalos
29*f202adf7SPeter Avalos #include <float.h>
30*f202adf7SPeter Avalos #include <limits.h>
31*f202adf7SPeter Avalos #include <math.h>
32*f202adf7SPeter Avalos #include <stdint.h>
33*f202adf7SPeter Avalos
34*f202adf7SPeter Avalos #include "../stdio/floatio.h"
35*f202adf7SPeter Avalos #include "fpmath.h"
36*f202adf7SPeter Avalos #include "gdtoaimp.h"
37*f202adf7SPeter Avalos
38*f202adf7SPeter Avalos #if (LDBL_MANT_DIG > DBL_MANT_DIG)
39*f202adf7SPeter Avalos
40*f202adf7SPeter Avalos /* Strings values used by dtoa() */
41*f202adf7SPeter Avalos #define INFSTR "Infinity"
42*f202adf7SPeter Avalos #define NANSTR "NaN"
43*f202adf7SPeter Avalos
44*f202adf7SPeter Avalos #ifdef LDBL_IMPLICIT_NBIT
45*f202adf7SPeter Avalos #define MANH_SIZE LDBL_MANH_SIZE
46*f202adf7SPeter Avalos #else
47*f202adf7SPeter Avalos #define MANH_SIZE (LDBL_MANH_SIZE - 1)
48*f202adf7SPeter Avalos #endif
49*f202adf7SPeter Avalos
50*f202adf7SPeter Avalos #if MANH_SIZE > 32
51*f202adf7SPeter Avalos typedef uint64_t manh_t;
52*f202adf7SPeter Avalos #else
53*f202adf7SPeter Avalos typedef uint32_t manh_t;
54*f202adf7SPeter Avalos #endif
55*f202adf7SPeter Avalos
56*f202adf7SPeter Avalos #if LDBL_MANL_SIZE > 32
57*f202adf7SPeter Avalos typedef uint64_t manl_t;
58*f202adf7SPeter Avalos #else
59*f202adf7SPeter Avalos typedef uint32_t manl_t;
60*f202adf7SPeter Avalos #endif
61*f202adf7SPeter Avalos
62*f202adf7SPeter Avalos #define LDBL_ADJ (LDBL_MAX_EXP - 2)
63*f202adf7SPeter Avalos #define SIGFIGS ((LDBL_MANT_DIG + 3) / 4 + 1)
64*f202adf7SPeter Avalos
65*f202adf7SPeter Avalos static const float one[] = { 1.0f, -1.0f };
66*f202adf7SPeter Avalos
67*f202adf7SPeter Avalos /*
68*f202adf7SPeter Avalos * This is the long double version of __hdtoa().
69*f202adf7SPeter Avalos */
70*f202adf7SPeter Avalos char *
__hldtoa(long double e,const char * xdigs,int ndigits,int * decpt,int * sign,char ** rve)71*f202adf7SPeter Avalos __hldtoa(long double e, const char *xdigs, int ndigits, int *decpt, int *sign,
72*f202adf7SPeter Avalos char **rve)
73*f202adf7SPeter Avalos {
74*f202adf7SPeter Avalos union IEEEl2bits u;
75*f202adf7SPeter Avalos char *s, *s0;
76*f202adf7SPeter Avalos manh_t manh;
77*f202adf7SPeter Avalos manl_t manl;
78*f202adf7SPeter Avalos int bufsize;
79*f202adf7SPeter Avalos
80*f202adf7SPeter Avalos u.e = e;
81*f202adf7SPeter Avalos *sign = u.bits.sign;
82*f202adf7SPeter Avalos
83*f202adf7SPeter Avalos switch (fpclassify(e)) {
84*f202adf7SPeter Avalos case FP_NORMAL:
85*f202adf7SPeter Avalos *decpt = u.bits.exp - LDBL_ADJ;
86*f202adf7SPeter Avalos break;
87*f202adf7SPeter Avalos case FP_ZERO:
88*f202adf7SPeter Avalos *decpt = 1;
89*f202adf7SPeter Avalos return (nrv_alloc("0", rve, 1));
90*f202adf7SPeter Avalos case FP_SUBNORMAL:
91*f202adf7SPeter Avalos u.e *= 0x1p514L;
92*f202adf7SPeter Avalos *decpt = u.bits.exp - (514 + LDBL_ADJ);
93*f202adf7SPeter Avalos break;
94*f202adf7SPeter Avalos case FP_INFINITE:
95*f202adf7SPeter Avalos *decpt = INT_MAX;
96*f202adf7SPeter Avalos return (nrv_alloc(INFSTR, rve, sizeof(INFSTR) - 1));
97*f202adf7SPeter Avalos default: /* FP_NAN or unrecognized */
98*f202adf7SPeter Avalos *decpt = INT_MAX;
99*f202adf7SPeter Avalos return (nrv_alloc(NANSTR, rve, sizeof(NANSTR) - 1));
100*f202adf7SPeter Avalos }
101*f202adf7SPeter Avalos
102*f202adf7SPeter Avalos /* FP_NORMAL or FP_SUBNORMAL */
103*f202adf7SPeter Avalos
104*f202adf7SPeter Avalos if (ndigits == 0) /* dtoa() compatibility */
105*f202adf7SPeter Avalos ndigits = 1;
106*f202adf7SPeter Avalos
107*f202adf7SPeter Avalos /*
108*f202adf7SPeter Avalos * If ndigits < 0, we are expected to auto-size, so we allocate
109*f202adf7SPeter Avalos * enough space for all the digits.
110*f202adf7SPeter Avalos */
111*f202adf7SPeter Avalos bufsize = (ndigits > 0) ? ndigits : SIGFIGS;
112*f202adf7SPeter Avalos s0 = rv_alloc(bufsize);
113*f202adf7SPeter Avalos
114*f202adf7SPeter Avalos /* Round to the desired number of digits. */
115*f202adf7SPeter Avalos if (SIGFIGS > ndigits && ndigits > 0) {
116*f202adf7SPeter Avalos float redux = one[u.bits.sign];
117*f202adf7SPeter Avalos int offset = 4 * ndigits + LDBL_MAX_EXP - 4 - LDBL_MANT_DIG;
118*f202adf7SPeter Avalos u.bits.exp = offset;
119*f202adf7SPeter Avalos u.e += redux;
120*f202adf7SPeter Avalos u.e -= redux;
121*f202adf7SPeter Avalos *decpt += u.bits.exp - offset;
122*f202adf7SPeter Avalos }
123*f202adf7SPeter Avalos
124*f202adf7SPeter Avalos mask_nbit_l(u);
125*f202adf7SPeter Avalos manh = u.bits.manh;
126*f202adf7SPeter Avalos manl = u.bits.manl;
127*f202adf7SPeter Avalos *s0 = '1';
128*f202adf7SPeter Avalos for (s = s0 + 1; s < s0 + bufsize; s++) {
129*f202adf7SPeter Avalos *s = xdigs[(manh >> (MANH_SIZE - 4)) & 0xf];
130*f202adf7SPeter Avalos manh = (manh << 4) | (manl >> (LDBL_MANL_SIZE - 4));
131*f202adf7SPeter Avalos manl <<= 4;
132*f202adf7SPeter Avalos }
133*f202adf7SPeter Avalos
134*f202adf7SPeter Avalos /* If ndigits < 0, we are expected to auto-size the precision. */
135*f202adf7SPeter Avalos if (ndigits < 0) {
136*f202adf7SPeter Avalos for (ndigits = SIGFIGS; s0[ndigits - 1] == '0'; ndigits--)
137*f202adf7SPeter Avalos ;
138*f202adf7SPeter Avalos }
139*f202adf7SPeter Avalos
140*f202adf7SPeter Avalos s = s0 + ndigits;
141*f202adf7SPeter Avalos *s = '\0';
142*f202adf7SPeter Avalos if (rve != NULL)
143*f202adf7SPeter Avalos *rve = s;
144*f202adf7SPeter Avalos return (s0);
145*f202adf7SPeter Avalos }
146*f202adf7SPeter Avalos
147*f202adf7SPeter Avalos #else /* (LDBL_MANT_DIG == DBL_MANT_DIG) */
148*f202adf7SPeter Avalos
149*f202adf7SPeter Avalos char *
__hldtoa(long double e,const char * xdigs,int ndigits,int * decpt,int * sign,char ** rve)150*f202adf7SPeter Avalos __hldtoa(long double e, const char *xdigs, int ndigits, int *decpt, int *sign,
151*f202adf7SPeter Avalos char **rve)
152*f202adf7SPeter Avalos {
153*f202adf7SPeter Avalos
154*f202adf7SPeter Avalos return (__hdtoa((double)e, xdigs, ndigits, decpt, sign, rve));
155*f202adf7SPeter Avalos }
156*f202adf7SPeter Avalos
157*f202adf7SPeter Avalos #endif /* (LDBL_MANT_DIG == DBL_MANT_DIG) */
158