| 2175 | */ |
| 2176 | |
| 2177 | static char *dtoa(double dd, int mode, int ndigits, int *decpt, int *sign, |
| 2178 | char **rve, char *buf, size_t buf_size) |
| 2179 | { |
| 2180 | /* |
| 2181 | Arguments ndigits, decpt, sign are similar to those |
| 2182 | of ecvt and fcvt; trailing zeros are suppressed from |
| 2183 | the returned string. If not null, *rve is set to point |
| 2184 | to the end of the return value. If d is +-Infinity or NaN, |
| 2185 | then *decpt is set to DTOA_OVERFLOW. |
| 2186 | |
| 2187 | mode: |
| 2188 | 0 ==> shortest string that yields d when read in |
| 2189 | and rounded to nearest. |
| 2190 | 1 ==> like 0, but with Steele & White stopping rule; |
| 2191 | e.g. with IEEE P754 arithmetic , mode 0 gives |
| 2192 | 1e23 whereas mode 1 gives 9.999999999999999e22. |
| 2193 | 2 ==> max(1,ndigits) significant digits. This gives a |
| 2194 | return value similar to that of ecvt, except |
| 2195 | that trailing zeros are suppressed. |
| 2196 | 3 ==> through ndigits past the decimal point. This |
| 2197 | gives a return value similar to that from fcvt, |
| 2198 | except that trailing zeros are suppressed, and |
| 2199 | ndigits can be negative. |
| 2200 | 4,5 ==> similar to 2 and 3, respectively, but (in |
| 2201 | round-nearest mode) with the tests of mode 0 to |
| 2202 | possibly return a shorter string that rounds to d. |
| 2203 | With IEEE arithmetic and compilation with |
| 2204 | -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same |
| 2205 | as modes 2 and 3 when FLT_ROUNDS != 1. |
| 2206 | 6-9 ==> Debugging modes similar to mode - 4: don't try |
| 2207 | fast floating-point estimate (if applicable). |
| 2208 | |
| 2209 | Values of mode other than 0-9 are treated as mode 0. |
| 2210 | |
| 2211 | Sufficient space is allocated to the return value |
| 2212 | to hold the suppressed trailing zeros. |
| 2213 | */ |
| 2214 | |
| 2215 | int bbits, b2, b5, be, dig, i, ieps, UNINIT_VAR(ilim), ilim0, |
| 2216 | UNINIT_VAR(ilim1), j, j1, k, k0, k_check, leftright, m2, m5, s2, s5, |
| 2217 | spec_case, try_quick; |
| 2218 | Long L; |
| 2219 | int denorm; |
| 2220 | ULong x; |
| 2221 | Bigint *b, *b1, *delta, *mlo, *mhi, *S; |
| 2222 | U d2, eps, u; |
| 2223 | double ds; |
| 2224 | char *s, *s0; |
| 2225 | #ifdef Honor_FLT_ROUNDS |
| 2226 | int rounding; |
| 2227 | #endif |
| 2228 | Stack_alloc alloc; |
| 2229 | |
| 2230 | alloc.begin= alloc.free= buf; |
| 2231 | alloc.end= buf + buf_size; |
| 2232 | memset(alloc.freelist, 0, sizeof(alloc.freelist)); |
| 2233 | |
| 2234 | u.d= dd; |