1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright © 2021 Keith Packard
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  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above
14  *    copyright notice, this list of conditions and the following
15  *    disclaimer in the documentation and/or other materials provided
16  *    with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33  * OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #define _NEED_IO_LONG_DOUBLE
37 
38 #include "dtoa.h"
39 
40 #ifdef _NEED_IO_FLOAT_LARGE
41 
42 #define max(a, b) ({\
43 		__typeof(a) _a = a;\
44 		__typeof(b) _b = b;\
45 		_a > _b ? _a : _b; })
46 
47 #define min(a, b) ({\
48 		__typeof(a) _a = a;\
49 		__typeof(b) _b = b;\
50 		_a < _b ? _a : _b; })
51 
52 int
__ldtoa_engine(long double x,struct dtoa * dtoa,int max_digits,bool fmode,int max_decimals)53 __ldtoa_engine(long double x, struct dtoa *dtoa, int max_digits, bool fmode, int max_decimals)
54 {
55     int i;
56 
57     dtoa->flags = 0;
58 
59     if (signbit(x)) {
60         dtoa->flags |= DTOA_MINUS;
61         x = -x;
62     }
63     if (isnan(x)) {
64         dtoa->flags |= DTOA_NAN;
65         return max_digits;
66     } else if (isinf(x)) {
67         dtoa->flags |= DTOA_INF;
68         return max_digits;
69     }
70 
71     int expo;
72     int decexp = 0;
73 
74     x = frexpl(x, &expo);
75 
76     /* move into [1-2) range */
77     x *= 2.0l;
78     expo--;
79 
80     while (expo <= -2) {
81 
82         /* Make room to allow multiplication by 5 without overflow */
83         do {
84             expo++;
85             x *= 0.5l;
86         } while (x >= 2.0l);
87 
88         x *= 5.0l;
89         expo++;
90         decexp--;
91     }
92 
93     while (expo > 0) {
94         /* divide by 10 */
95         x *= 0.2l;
96         expo--;
97         decexp++;
98 
99         /* get the value back in range */
100         do {
101             x *= 2.0l;
102             expo--;
103         } while (x < 1.0l);
104     }
105 
106     while (expo < 0) {
107         x *= 0.5l;
108         expo++;
109     }
110 
111     if (x < 1.0l) {
112         x *= 10.0l;
113         decexp--;
114     }
115 
116     /* x is now in the range 1 <= x < 10 */
117 
118     int save_max_digits = max_digits;
119 
120     if (fmode)
121         max_digits = min(max_digits, max(1, max_decimals + decexp + 1));
122 
123     int decimals = max_digits;
124 
125     long double round = 0.5l;
126     while (decimals--) {
127         round *= 0.1l;
128     }
129     x += round;
130 
131     if (x >= 10.0l) {
132         x *= 0.1l;
133         decexp++;
134         max_digits = min(save_max_digits, max(1, max_decimals + decexp + 1));
135     }
136 
137     /* convert to decimal */
138 
139     for (i = 0; i < max_digits; i++) {
140         int digit = (int) x;
141         dtoa->digits[i] = '0' + digit;
142         x -= digit;
143         x *= 10.0l;
144     }
145 
146     dtoa->exp = decexp;
147 
148     return max_digits;
149 }
150 
151 #endif
152