1 /*	$OpenBSD: ldtoa.c,v 1.4 2016/03/09 16:28:47 deraadt Exp $	*/
2 /*-
3  * Copyright (c) 2003 David Schultz <das@FreeBSD.ORG>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <newlib.h>
29 #include <sys/config.h>
30 
31 #ifdef _USE_GDTOA
32 #include <sys/types.h>
33 #include <machine/ieee.h>
34 #include <float.h>
35 #include <stdint.h>
36 #include <limits.h>
37 #include <math.h>
38 #include <stdlib.h>
39 #include "gdtoaimp.h"
40 
41 #if (LDBL_MANT_DIG > DBL_MANT_DIG)
42 
43 /*
44  * ldtoa() is a wrapper for gdtoa() that makes it smell like dtoa(),
45  * except that the floating point argument is passed by reference.
46  * When dtoa() is passed a NaN or infinity, it sets expt to 9999.
47  * However, a long double could have a valid exponent of 9999, so we
48  * use INT_MAX in ldtoa() instead.
49  */
50 char *
_ldtoa_r(struct _reent * ptr,long double ld,int mode,int ndigits,int * decpt,int * sign,char ** rve)51 _ldtoa_r(struct _reent *ptr,
52     long double ld, int mode, int ndigits, int *decpt, int *sign, char **rve)
53 {
54 	FPI fpi = {
55 		LDBL_MANT_DIG,			/* nbits */
56 		LDBL_MIN_EXP - LDBL_MANT_DIG,	/* emin */
57 		LDBL_MAX_EXP - LDBL_MANT_DIG,	/* emax */
58 		FLT_ROUNDS,	       		/* rounding */
59 #ifdef Sudden_Underflow	/* unused, but correct anyway */
60 		1
61 #else
62 		0
63 #endif
64 	};
65 	int be, kind;
66 	char *ret;
67 	struct ieee_ext *p = (struct ieee_ext *)&ld;
68 	uint32_t bits[(LDBL_MANT_DIG + 31) / 32];
69 	void *vbits = bits;
70 
71 	_REENT_CHECK_MP (ptr);
72 
73 	/* reentrancy addition to use mprec storage pool */
74 	if (_REENT_MP_RESULT (ptr)) {
75 		_REENT_MP_RESULT (ptr)->_k = _REENT_MP_RESULT_K (ptr);
76 		_REENT_MP_RESULT (ptr)->_maxwds = 1 << _REENT_MP_RESULT_K (ptr);
77 		Bfree (ptr, _REENT_MP_RESULT (ptr));
78 		_REENT_MP_RESULT (ptr) = 0;
79 	}
80 
81 	/*
82 	 * gdtoa doesn't know anything about the sign of the number, so
83 	 * if the number is negative, we need to swap rounding modes of
84 	 * 2 (upwards) and 3 (downwards).
85 	 */
86 	*sign = p->ext_sign;
87 	fpi.rounding ^= (fpi.rounding >> 1) & p->ext_sign;
88 
89 	be = p->ext_exp - (LDBL_MAX_EXP - 1) - (LDBL_MANT_DIG - 1);
90 	EXT_TO_ARRAY32(p, bits);
91 
92 	switch (fpclassify(ld)) {
93 	case FP_NORMAL:
94 		kind = STRTOG_Normal;
95 #ifdef EXT_IMPLICIT_NBIT
96 		bits[LDBL_MANT_DIG / 32] |= 1 << ((LDBL_MANT_DIG - 1) % 32);
97 #endif /* EXT_IMPLICIT_NBIT */
98 		break;
99 	case FP_ZERO:
100 		kind = STRTOG_Zero;
101 		break;
102 	case FP_SUBNORMAL:
103 		kind = STRTOG_Denormal;
104 		be++;
105 		break;
106 	case FP_INFINITE:
107 		kind = STRTOG_Infinite;
108 		break;
109 	case FP_NAN:
110 		kind = STRTOG_NaN;
111 		break;
112 	default:
113 		abort();
114 	}
115 
116 	ret = gdtoa(ptr, &fpi, be, vbits, &kind, mode, ndigits, decpt, rve);
117 	if (*decpt == -32768)
118 		*decpt = INT_MAX;
119 	return ret;
120 }
121 DEF_STRONG(_ldtoa_r);
122 
123 #else   /* (LDBL_MANT_DIG == DBL_MANT_DIG) */
124 
125 char *
_ldtoa_r(struct _reent * ptr,long double ld,int mode,int ndigits,int * decpt,int * sign,char ** rve)126 _ldtoa_r(struct _reent *ptr,
127     long double ld, int mode, int ndigits, int *decpt, int *sign,
128     char **rve)
129 {
130 	char *ret;
131 
132 	ret = _dtoa_r(ptr, (double)ld, mode, ndigits, decpt, sign, rve);
133 	if (*decpt == 9999)
134 		*decpt = INT_MAX;
135 	return ret;
136 }
137 DEF_STRONG(_ldtoa_r);
138 
139 #endif  /* (LDBL_MANT_DIG == DBL_MANT_DIG) */
140 
141 /* Routine used to tell if long double is NaN or Infinity or regular number.
142    Returns:  0 = regular number
143              1 = Nan
144              2 = Infinity
145 */
146 int
_ldcheck(long double * d)147 _ldcheck (long double *d)
148 {
149 	switch (fpclassify(*d)) {
150 	case FP_NAN:
151 		return 1;
152 	case FP_INFINITE:
153 		return 2;
154 	default:
155 		return 0;
156 	}
157 }
158 
159 #endif /* _USE_GDTOA */
160