Mention BZ 14075 and 14112
[glibc.git] / stdio-common / _itoa.c
blob12d69541b9b7d0f4a447b5b1af8b378d69991f82
1 /* Internal function for converting integers to ASCII.
2 Copyright (C) 1994, 1995, 1996, 1999, 2000, 2002, 2003, 2004, 2007
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Torbjorn Granlund <tege@matematik.su.se>
6 and Ulrich Drepper <drepper@gnu.org>.
8 The GNU C Library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
13 The GNU C Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with the GNU C Library; if not, see
20 <http://www.gnu.org/licenses/>. */
22 #include <gmp-mparam.h>
23 #include <gmp.h>
24 #include <limits.h>
25 #include <stdlib/gmp-impl.h>
26 #include <stdlib/longlong.h>
28 #include <_itoa.h>
31 /* Canonize environment. For some architectures not all values might
32 be defined in the GMP header files. */
33 #ifndef UMUL_TIME
34 # define UMUL_TIME 1
35 #endif
36 #ifndef UDIV_TIME
37 # define UDIV_TIME 3
38 #endif
40 /* Control memory layout. */
41 #ifdef PACK
42 # undef PACK
43 # define PACK __attribute__ ((packed))
44 #else
45 # define PACK
46 #endif
49 /* Declare local types. */
50 struct base_table_t
52 #if (UDIV_TIME > 2 * UMUL_TIME)
53 mp_limb_t base_multiplier;
54 #endif
55 char flag;
56 char post_shift;
57 #if BITS_PER_MP_LIMB == 32
58 struct
60 char normalization_steps;
61 char ndigits;
62 mp_limb_t base PACK;
63 #if UDIV_TIME > 2 * UMUL_TIME
64 mp_limb_t base_ninv PACK;
65 #endif
66 } big;
67 #endif
70 /* To reduce the memory needed we include some fields of the tables
71 only conditionally. */
72 #if UDIV_TIME > 2 * UMUL_TIME
73 # define SEL1(X) X,
74 # define SEL2(X) ,X
75 #else
76 # define SEL1(X)
77 # define SEL2(X)
78 #endif
81 /* We do not compile _itoa if we always can use _itoa_word. */
82 #if _ITOA_NEEDED
83 /* Local variables. */
84 const struct base_table_t _itoa_base_table[] attribute_hidden =
86 # if BITS_PER_MP_LIMB == 64
87 /* 2 */ {SEL1(0ull) 1, 1},
88 /* 3 */ {SEL1(0xaaaaaaaaaaaaaaabull) 0, 1},
89 /* 4 */ {SEL1(0ull) 1, 2},
90 /* 5 */ {SEL1(0xcccccccccccccccdull) 0, 2},
91 /* 6 */ {SEL1(0xaaaaaaaaaaaaaaabull) 0, 2},
92 /* 7 */ {SEL1(0x2492492492492493ull) 1, 3},
93 /* 8 */ {SEL1(0ull) 1, 3},
94 /* 9 */ {SEL1(0xe38e38e38e38e38full) 0, 3},
95 /* 10 */ {SEL1(0xcccccccccccccccdull) 0, 3},
96 /* 11 */ {SEL1(0x2e8ba2e8ba2e8ba3ull) 0, 1},
97 /* 12 */ {SEL1(0xaaaaaaaaaaaaaaabull) 0, 3},
98 /* 13 */ {SEL1(0x4ec4ec4ec4ec4ec5ull) 0, 2},
99 /* 14 */ {SEL1(0x2492492492492493ull) 1, 4},
100 /* 15 */ {SEL1(0x8888888888888889ull) 0, 3},
101 /* 16 */ {SEL1(0ull) 1, 4},
102 /* 17 */ {SEL1(0xf0f0f0f0f0f0f0f1ull) 0, 4},
103 /* 18 */ {SEL1(0xe38e38e38e38e38full) 0, 4},
104 /* 19 */ {SEL1(0xd79435e50d79435full) 0, 4},
105 /* 20 */ {SEL1(0xcccccccccccccccdull) 0, 4},
106 /* 21 */ {SEL1(0x8618618618618619ull) 1, 5},
107 /* 22 */ {SEL1(0x2e8ba2e8ba2e8ba3ull) 0, 2},
108 /* 23 */ {SEL1(0x642c8590b21642c9ull) 1, 5},
109 /* 24 */ {SEL1(0xaaaaaaaaaaaaaaabull) 0, 4},
110 /* 25 */ {SEL1(0x47ae147ae147ae15ull) 1, 5},
111 /* 26 */ {SEL1(0x4ec4ec4ec4ec4ec5ull) 0, 3},
112 /* 27 */ {SEL1(0x97b425ed097b425full) 0, 4},
113 /* 28 */ {SEL1(0x2492492492492493ull) 1, 5},
114 /* 29 */ {SEL1(0x1a7b9611a7b9611bull) 1, 5},
115 /* 30 */ {SEL1(0x8888888888888889ull) 0, 4},
116 /* 31 */ {SEL1(0x0842108421084211ull) 1, 5},
117 /* 32 */ {SEL1(0ull) 1, 5},
118 /* 33 */ {SEL1(0x0f83e0f83e0f83e1ull) 0, 1},
119 /* 34 */ {SEL1(0xf0f0f0f0f0f0f0f1ull) 0, 5},
120 /* 35 */ {SEL1(0xea0ea0ea0ea0ea0full) 0, 5},
121 /* 36 */ {SEL1(0xe38e38e38e38e38full) 0, 5}
122 # endif
123 # if BITS_PER_MP_LIMB == 32
124 /* 2 */ {SEL1(0ul) 1, 1, {0, 31, 0x80000000ul SEL2(0xfffffffful)}},
125 /* 3 */ {SEL1(0xaaaaaaabul) 0, 1, {0, 20, 0xcfd41b91ul SEL2(0x3b563c24ul)}},
126 /* 4 */ {SEL1(0ul) 1, 2, {1, 15, 0x40000000ul SEL2(0xfffffffful)}},
127 /* 5 */ {SEL1(0xcccccccdul) 0, 2, {1, 13, 0x48c27395ul SEL2(0xc25c2684ul)}},
128 /* 6 */ {SEL1(0xaaaaaaabul) 0, 2, {0, 12, 0x81bf1000ul SEL2(0xf91bd1b6ul)}},
129 /* 7 */ {SEL1(0x24924925ul) 1, 3, {1, 11, 0x75db9c97ul SEL2(0x1607a2cbul)}},
130 /* 8 */ {SEL1(0ul) 1, 3, {1, 10, 0x40000000ul SEL2(0xfffffffful)}},
131 /* 9 */ {SEL1(0x38e38e39ul) 0, 1, {0, 10, 0xcfd41b91ul SEL2(0x3b563c24ul)}},
132 /* 10 */ {SEL1(0xcccccccdul) 0, 3, {2, 9, 0x3b9aca00ul SEL2(0x12e0be82ul)}},
133 /* 11 */ {SEL1(0xba2e8ba3ul) 0, 3, {0, 9, 0x8c8b6d2bul SEL2(0xd24cde04ul)}},
134 /* 12 */ {SEL1(0xaaaaaaabul) 0, 3, {3, 8, 0x19a10000ul SEL2(0x3fa39ab5ul)}},
135 /* 13 */ {SEL1(0x4ec4ec4ful) 0, 2, {2, 8, 0x309f1021ul SEL2(0x50f8ac5ful)}},
136 /* 14 */ {SEL1(0x24924925ul) 1, 4, {1, 8, 0x57f6c100ul SEL2(0x74843b1eul)}},
137 /* 15 */ {SEL1(0x88888889ul) 0, 3, {0, 8, 0x98c29b81ul SEL2(0xad0326c2ul)}},
138 /* 16 */ {SEL1(0ul) 1, 4, {3, 7, 0x10000000ul SEL2(0xfffffffful)}},
139 /* 17 */ {SEL1(0xf0f0f0f1ul) 0, 4, {3, 7, 0x18754571ul SEL2(0x4ef0b6bdul)}},
140 /* 18 */ {SEL1(0x38e38e39ul) 0, 2, {2, 7, 0x247dbc80ul SEL2(0xc0fc48a1ul)}},
141 /* 19 */ {SEL1(0xaf286bcbul) 1, 5, {2, 7, 0x3547667bul SEL2(0x33838942ul)}},
142 /* 20 */ {SEL1(0xcccccccdul) 0, 4, {1, 7, 0x4c4b4000ul SEL2(0xad7f29abul)}},
143 /* 21 */ {SEL1(0x86186187ul) 1, 5, {1, 7, 0x6b5a6e1dul SEL2(0x313c3d15ul)}},
144 /* 22 */ {SEL1(0xba2e8ba3ul) 0, 4, {0, 7, 0x94ace180ul SEL2(0xb8cca9e0ul)}},
145 /* 23 */ {SEL1(0xb21642c9ul) 0, 4, {0, 7, 0xcaf18367ul SEL2(0x42ed6de9ul)}},
146 /* 24 */ {SEL1(0xaaaaaaabul) 0, 4, {4, 6, 0x0b640000ul SEL2(0x67980e0bul)}},
147 /* 25 */ {SEL1(0x51eb851ful) 0, 3, {4, 6, 0x0e8d4a51ul SEL2(0x19799812ul)}},
148 /* 26 */ {SEL1(0x4ec4ec4ful) 0, 3, {3, 6, 0x1269ae40ul SEL2(0xbce85396ul)}},
149 /* 27 */ {SEL1(0x2f684bdbul) 1, 5, {3, 6, 0x17179149ul SEL2(0x62c103a9ul)}},
150 /* 28 */ {SEL1(0x24924925ul) 1, 5, {3, 6, 0x1cb91000ul SEL2(0x1d353d43ul)}},
151 /* 29 */ {SEL1(0x8d3dcb09ul) 0, 4, {2, 6, 0x23744899ul SEL2(0xce1deceaul)}},
152 /* 30 */ {SEL1(0x88888889ul) 0, 4, {2, 6, 0x2b73a840ul SEL2(0x790fc511ul)}},
153 /* 31 */ {SEL1(0x08421085ul) 1, 5, {2, 6, 0x34e63b41ul SEL2(0x35b865a0ul)}},
154 /* 32 */ {SEL1(0ul) 1, 5, {1, 6, 0x40000000ul SEL2(0xfffffffful)}},
155 /* 33 */ {SEL1(0x3e0f83e1ul) 0, 3, {1, 6, 0x4cfa3cc1ul SEL2(0xa9aed1b3ul)}},
156 /* 34 */ {SEL1(0xf0f0f0f1ul) 0, 5, {1, 6, 0x5c13d840ul SEL2(0x63dfc229ul)}},
157 /* 35 */ {SEL1(0xd41d41d5ul) 1, 6, {1, 6, 0x6d91b519ul SEL2(0x2b0fee30ul)}},
158 /* 36 */ {SEL1(0x38e38e39ul) 0, 3, {0, 6, 0x81bf1000ul SEL2(0xf91bd1b6ul)}}
159 # endif
161 #endif
163 /* Lower-case digits. */
164 extern const char _itoa_lower_digits[];
165 extern const char _itoa_lower_digits_internal[] attribute_hidden;
166 /* Upper-case digits. */
167 extern const char _itoa_upper_digits[];
168 extern const char _itoa_upper_digits_internal[] attribute_hidden;
171 char *
172 _itoa_word (_ITOA_WORD_TYPE value, char *buflim,
173 unsigned int base, int upper_case)
175 const char *digits = (upper_case
176 #if !defined NOT_IN_libc || defined IS_IN_rtld
177 ? INTUSE(_itoa_upper_digits)
178 : INTUSE(_itoa_lower_digits)
179 #else
180 ? _itoa_upper_digits
181 : _itoa_lower_digits
182 #endif
185 switch (base)
187 #define SPECIAL(Base) \
188 case Base: \
189 do \
190 *--buflim = digits[value % Base]; \
191 while ((value /= Base) != 0); \
192 break
194 SPECIAL (10);
195 SPECIAL (16);
196 SPECIAL (8);
197 default:
199 *--buflim = digits[value % base];
200 while ((value /= base) != 0);
202 return buflim;
204 #undef SPECIAL
207 #if _ITOA_NEEDED
208 char *
209 _itoa (value, buflim, base, upper_case)
210 unsigned long long int value;
211 char *buflim;
212 unsigned int base;
213 int upper_case;
215 const char *digits = (upper_case
216 ? INTUSE(_itoa_upper_digits)
217 : INTUSE(_itoa_lower_digits));
218 const struct base_table_t *brec = &_itoa_base_table[base - 2];
220 switch (base)
222 # define RUN_2N(BITS) \
223 do \
225 /* `unsigned long long int' always has 64 bits. */ \
226 mp_limb_t work_hi = value >> (64 - BITS_PER_MP_LIMB); \
228 if (BITS_PER_MP_LIMB == 32) \
230 if (work_hi != 0) \
232 mp_limb_t work_lo; \
233 int cnt; \
235 work_lo = value & 0xfffffffful; \
236 for (cnt = BITS_PER_MP_LIMB / BITS; cnt > 0; --cnt) \
238 *--buflim = digits[work_lo & ((1ul << BITS) - 1)]; \
239 work_lo >>= BITS; \
241 if (BITS_PER_MP_LIMB % BITS != 0) \
243 work_lo \
244 |= ((work_hi \
245 & ((1 << (BITS - BITS_PER_MP_LIMB%BITS)) \
246 - 1)) \
247 << BITS_PER_MP_LIMB % BITS); \
248 work_hi >>= BITS - BITS_PER_MP_LIMB % BITS; \
249 if (work_hi == 0) \
250 work_hi = work_lo; \
251 else \
252 *--buflim = digits[work_lo]; \
255 else \
256 work_hi = value & 0xfffffffful; \
258 do \
260 *--buflim = digits[work_hi & ((1 << BITS) - 1)]; \
261 work_hi >>= BITS; \
263 while (work_hi != 0); \
265 while (0)
266 case 8:
267 RUN_2N (3);
268 break;
270 case 16:
271 RUN_2N (4);
272 break;
274 default:
276 char *bufend = buflim;
277 # if BITS_PER_MP_LIMB == 64
278 mp_limb_t base_multiplier = brec->base_multiplier;
279 if (brec->flag)
280 while (value != 0)
282 mp_limb_t quo, rem, x, dummy;
284 umul_ppmm (x, dummy, value, base_multiplier);
285 quo = (x + ((value - x) >> 1)) >> (brec->post_shift - 1);
286 rem = value - quo * base;
287 *--buflim = digits[rem];
288 value = quo;
290 else
291 while (value != 0)
293 mp_limb_t quo, rem, x, dummy;
295 umul_ppmm (x, dummy, value, base_multiplier);
296 quo = x >> brec->post_shift;
297 rem = value - quo * base;
298 *--buflim = digits[rem];
299 value = quo;
301 # endif
302 # if BITS_PER_MP_LIMB == 32
303 mp_limb_t t[3];
304 int n;
306 /* First convert x0 to 1-3 words in base s->big.base.
307 Optimize for frequent cases of 32 bit numbers. */
308 if ((mp_limb_t) (value >> 32) >= 1)
310 # if UDIV_TIME > 2 * UMUL_TIME || UDIV_NEEDS_NORMALIZATION
311 int big_normalization_steps = brec->big.normalization_steps;
312 mp_limb_t big_base_norm
313 = brec->big.base << big_normalization_steps;
314 # endif
315 if ((mp_limb_t) (value >> 32) >= brec->big.base)
317 mp_limb_t x1hi, x1lo, r;
318 /* If you want to optimize this, take advantage of
319 that the quotient in the first udiv_qrnnd will
320 always be very small. It might be faster just to
321 subtract in a tight loop. */
323 # if UDIV_TIME > 2 * UMUL_TIME
324 mp_limb_t x, xh, xl;
326 if (big_normalization_steps == 0)
327 xh = 0;
328 else
329 xh = (mp_limb_t) (value >> (64 - big_normalization_steps));
330 xl = (mp_limb_t) (value >> (32 - big_normalization_steps));
331 udiv_qrnnd_preinv (x1hi, r, xh, xl, big_base_norm,
332 brec->big.base_ninv);
334 xl = ((mp_limb_t) value) << big_normalization_steps;
335 udiv_qrnnd_preinv (x1lo, x, r, xl, big_base_norm,
336 brec->big.base_ninv);
337 t[2] = x >> big_normalization_steps;
339 if (big_normalization_steps == 0)
340 xh = x1hi;
341 else
342 xh = ((x1hi << big_normalization_steps)
343 | (x1lo >> (32 - big_normalization_steps)));
344 xl = x1lo << big_normalization_steps;
345 udiv_qrnnd_preinv (t[0], x, xh, xl, big_base_norm,
346 brec->big.base_ninv);
347 t[1] = x >> big_normalization_steps;
348 # elif UDIV_NEEDS_NORMALIZATION
349 mp_limb_t x, xh, xl;
351 if (big_normalization_steps == 0)
352 xh = 0;
353 else
354 xh = (mp_limb_t) (value >> 64 - big_normalization_steps);
355 xl = (mp_limb_t) (value >> 32 - big_normalization_steps);
356 udiv_qrnnd (x1hi, r, xh, xl, big_base_norm);
358 xl = ((mp_limb_t) value) << big_normalization_steps;
359 udiv_qrnnd (x1lo, x, r, xl, big_base_norm);
360 t[2] = x >> big_normalization_steps;
362 if (big_normalization_steps == 0)
363 xh = x1hi;
364 else
365 xh = ((x1hi << big_normalization_steps)
366 | (x1lo >> 32 - big_normalization_steps));
367 xl = x1lo << big_normalization_steps;
368 udiv_qrnnd (t[0], x, xh, xl, big_base_norm);
369 t[1] = x >> big_normalization_steps;
370 # else
371 udiv_qrnnd (x1hi, r, 0, (mp_limb_t) (value >> 32),
372 brec->big.base);
373 udiv_qrnnd (x1lo, t[2], r, (mp_limb_t) value, brec->big.base);
374 udiv_qrnnd (t[0], t[1], x1hi, x1lo, brec->big.base);
375 # endif
376 n = 3;
378 else
380 # if UDIV_TIME > 2 * UMUL_TIME
381 mp_limb_t x;
383 value <<= brec->big.normalization_steps;
384 udiv_qrnnd_preinv (t[0], x, (mp_limb_t) (value >> 32),
385 (mp_limb_t) value, big_base_norm,
386 brec->big.base_ninv);
387 t[1] = x >> brec->big.normalization_steps;
388 # elif UDIV_NEEDS_NORMALIZATION
389 mp_limb_t x;
391 value <<= big_normalization_steps;
392 udiv_qrnnd (t[0], x, (mp_limb_t) (value >> 32),
393 (mp_limb_t) value, big_base_norm);
394 t[1] = x >> big_normalization_steps;
395 # else
396 udiv_qrnnd (t[0], t[1], (mp_limb_t) (value >> 32),
397 (mp_limb_t) value, brec->big.base);
398 # endif
399 n = 2;
402 else
404 t[0] = value;
405 n = 1;
408 /* Convert the 1-3 words in t[], word by word, to ASCII. */
411 mp_limb_t ti = t[--n];
412 int ndig_for_this_limb = 0;
414 # if UDIV_TIME > 2 * UMUL_TIME
415 mp_limb_t base_multiplier = brec->base_multiplier;
416 if (brec->flag)
417 while (ti != 0)
419 mp_limb_t quo, rem, x, dummy;
421 umul_ppmm (x, dummy, ti, base_multiplier);
422 quo = (x + ((ti - x) >> 1)) >> (brec->post_shift - 1);
423 rem = ti - quo * base;
424 *--buflim = digits[rem];
425 ti = quo;
426 ++ndig_for_this_limb;
428 else
429 while (ti != 0)
431 mp_limb_t quo, rem, x, dummy;
433 umul_ppmm (x, dummy, ti, base_multiplier);
434 quo = x >> brec->post_shift;
435 rem = ti - quo * base;
436 *--buflim = digits[rem];
437 ti = quo;
438 ++ndig_for_this_limb;
440 # else
441 while (ti != 0)
443 mp_limb_t quo, rem;
445 quo = ti / base;
446 rem = ti % base;
447 *--buflim = digits[rem];
448 ti = quo;
449 ++ndig_for_this_limb;
451 # endif
452 /* If this wasn't the most significant word, pad with zeros. */
453 if (n != 0)
454 while (ndig_for_this_limb < brec->big.ndigits)
456 *--buflim = '0';
457 ++ndig_for_this_limb;
460 while (n != 0);
461 # endif
462 if (buflim == bufend)
463 *--buflim = '0';
465 break;
468 return buflim;
470 #endif
472 char *
473 _fitoa_word (_ITOA_WORD_TYPE value, char *buf, unsigned int base,
474 int upper_case)
476 char tmpbuf[sizeof (value) * 4]; /* Worst case length: base 2. */
477 char *cp = _itoa_word (value, tmpbuf + sizeof (value) * 4, base, upper_case);
478 while (cp < tmpbuf + sizeof (value) * 4)
479 *buf++ = *cp++;
480 return buf;
483 #if _ITOA_NEEDED
484 char *
485 _fitoa (unsigned long long value, char *buf, unsigned int base, int upper_case)
487 char tmpbuf[sizeof (value) * 4]; /* Worst case length: base 2. */
488 char *cp = _itoa (value, tmpbuf + sizeof (value) * 4, base, upper_case);
489 while (cp < tmpbuf + sizeof (value) * 4)
490 *buf++ = *cp++;
491 return buf;
493 #endif