1*9a9a4122Sriastradh /* $NetBSD: ldtoa.c,v 1.6 2019/08/01 02:27:43 riastradh Exp $ */
2482b8dd6Schristos
3482b8dd6Schristos /*-
4482b8dd6Schristos * Copyright (c) 2003 David Schultz <das@FreeBSD.ORG>
5482b8dd6Schristos * All rights reserved.
6482b8dd6Schristos *
7482b8dd6Schristos * Redistribution and use in source and binary forms, with or without
8482b8dd6Schristos * modification, are permitted provided that the following conditions
9482b8dd6Schristos * are met:
10482b8dd6Schristos * 1. Redistributions of source code must retain the above copyright
11482b8dd6Schristos * notice, this list of conditions and the following disclaimer.
12482b8dd6Schristos * 2. Redistributions in binary form must reproduce the above copyright
13482b8dd6Schristos * notice, this list of conditions and the following disclaimer in the
14482b8dd6Schristos * documentation and/or other materials provided with the distribution.
15482b8dd6Schristos *
16482b8dd6Schristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17482b8dd6Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18482b8dd6Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19482b8dd6Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20482b8dd6Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21482b8dd6Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22482b8dd6Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23482b8dd6Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24482b8dd6Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25482b8dd6Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26482b8dd6Schristos * SUCH DAMAGE.
27482b8dd6Schristos */
28482b8dd6Schristos
29482b8dd6Schristos #include <sys/cdefs.h>
30482b8dd6Schristos #if 0
31482b8dd6Schristos __FBSDID("$FreeBSD: src/lib/libc/gdtoa/_ldtoa.c,v 1.2 2004/01/18 07:53:49 das Exp $");
32482b8dd6Schristos #else
33*9a9a4122Sriastradh __RCSID("$NetBSD: ldtoa.c,v 1.6 2019/08/01 02:27:43 riastradh Exp $");
34482b8dd6Schristos #endif
35482b8dd6Schristos
36482b8dd6Schristos #include <float.h>
37482b8dd6Schristos #include <inttypes.h>
38482b8dd6Schristos #include <limits.h>
39482b8dd6Schristos #include <math.h>
40482b8dd6Schristos #include <stdlib.h>
41aa221fafSchristos #ifndef __vax__
42482b8dd6Schristos #include <machine/ieee.h>
43aa221fafSchristos #endif
44482b8dd6Schristos #include "gdtoaimp.h"
45482b8dd6Schristos
46482b8dd6Schristos /*
47482b8dd6Schristos * ldtoa() is a wrapper for gdtoa() that makes it smell like dtoa(),
48482b8dd6Schristos * except that the floating point argument is passed by reference.
49482b8dd6Schristos * When dtoa() is passed a NaN or infinity, it sets expt to 9999.
50482b8dd6Schristos * However, a long double could have a valid exponent of 9999, so we
51482b8dd6Schristos * use INT_MAX in ldtoa() instead.
52482b8dd6Schristos */
53482b8dd6Schristos char *
ldtoa(long double * ld,int mode,int ndigits,int * decpt,int * sign,char ** rve)54482b8dd6Schristos ldtoa(long double *ld, int mode, int ndigits, int *decpt, int *sign, char **rve)
55482b8dd6Schristos {
56482b8dd6Schristos #ifdef EXT_EXPBITS
57*9a9a4122Sriastradh static CONST FPI fpi = {
58482b8dd6Schristos LDBL_MANT_DIG, /* nbits */
59482b8dd6Schristos LDBL_MIN_EXP - LDBL_MANT_DIG, /* emin */
60482b8dd6Schristos LDBL_MAX_EXP - LDBL_MANT_DIG, /* emax */
61482b8dd6Schristos FPI_Round_near, /* rounding */
62482b8dd6Schristos #ifdef Sudden_Underflow /* unused, but correct anyway */
63482b8dd6Schristos 1
64482b8dd6Schristos #else
65482b8dd6Schristos 0
66482b8dd6Schristos #endif
67482b8dd6Schristos };
68482b8dd6Schristos int be, kind;
69482b8dd6Schristos char *ret;
70482b8dd6Schristos union ieee_ext_u u;
71482b8dd6Schristos uint32_t bits[(LDBL_MANT_DIG + 31) / 32];
72482b8dd6Schristos
73482b8dd6Schristos u.extu_ld = *ld;
74482b8dd6Schristos *sign = u.extu_ext.ext_sign;
75482b8dd6Schristos be = u.extu_ext.ext_exp - (LDBL_MAX_EXP - 1) - (LDBL_MANT_DIG - 1);
76482b8dd6Schristos EXT_TO_ARRAY32(u, bits);
77482b8dd6Schristos
78482b8dd6Schristos switch (fpclassify(u.extu_ld)) {
79482b8dd6Schristos case FP_NORMAL:
80482b8dd6Schristos kind = STRTOG_Normal;
81482b8dd6Schristos #ifdef LDBL_IMPLICIT_NBIT
82482b8dd6Schristos bits[LDBL_MANT_DIG / 32] |= 1 << ((LDBL_MANT_DIG - 1) % 32);
83482b8dd6Schristos #endif /* LDBL_IMPLICIT_NBIT */
84482b8dd6Schristos break;
85482b8dd6Schristos case FP_ZERO:
86482b8dd6Schristos kind = STRTOG_Zero;
87482b8dd6Schristos break;
88482b8dd6Schristos case FP_SUBNORMAL:
89482b8dd6Schristos kind = STRTOG_Denormal;
90482b8dd6Schristos be++;
91482b8dd6Schristos break;
92482b8dd6Schristos case FP_INFINITE:
93482b8dd6Schristos kind = STRTOG_Infinite;
94482b8dd6Schristos break;
95482b8dd6Schristos case FP_NAN:
96482b8dd6Schristos kind = STRTOG_NaN;
97482b8dd6Schristos break;
98482b8dd6Schristos default:
99482b8dd6Schristos abort();
100482b8dd6Schristos }
101482b8dd6Schristos
102482b8dd6Schristos ret = gdtoa(&fpi, be, (ULong *)bits, &kind, mode, ndigits, decpt, rve);
103482b8dd6Schristos if (*decpt == -32768)
104482b8dd6Schristos *decpt = INT_MAX;
105482b8dd6Schristos return ret;
106482b8dd6Schristos #else
1073bc8e006Suwe return dtoa((double)*ld, mode, ndigits, decpt, sign, rve);
108482b8dd6Schristos #endif
109482b8dd6Schristos }
110