]> git.ipfire.org Git - thirdparty/glibc.git/blame - stdio-common/_itoa.c
.
[thirdparty/glibc.git] / stdio-common / _itoa.c
CommitLineData
28f540f4 1/* Internal function for converting integers to ASCII.
32c075e1 2 Copyright (C) 1994, 1995, 1996, 1999, 2000, 2002, 2003, 2004, 2007
d8a5edc2 3 Free Software Foundation, Inc.
6d52618b
UD
4 This file is part of the GNU C Library.
5 Contributed by Torbjorn Granlund <tege@matematik.su.se>
d112ae30 6 and Ulrich Drepper <drepper@gnu.org>.
28f540f4 7
6d52618b 8 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
28f540f4 12
6d52618b
UD
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
41bdb6e2 16 Lesser General Public License for more details.
28f540f4 17
41bdb6e2
AJ
18 You should have received a copy of the GNU Lesser General Public
19 License along with the GNU C Library; if not, write to the Free
20 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 02111-1307 USA. */
28f540f4 22
8f5ca04b 23#include <gmp-mparam.h>
48896b9d 24#include <gmp.h>
ca34d7a7
UD
25#include <stdlib/gmp-impl.h>
26#include <stdlib/longlong.h>
8f5ca04b
RM
27
28#include "_itoa.h"
29
30
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
5ae3e846 37# define UDIV_TIME 3
8f5ca04b
RM
38#endif
39
40/* Control memory layout. */
41#ifdef PACK
42# undef PACK
43# define PACK __attribute__ ((packed))
44#else
45# define PACK
46#endif
47
48
49/* Declare local types. */
50struct base_table_t
51{
52#if (UDIV_TIME > 2 * UMUL_TIME)
0e3426bb 53 mp_limb_t base_multiplier;
8f5ca04b
RM
54#endif
55 char flag;
56 char post_shift;
57#if BITS_PER_MP_LIMB == 32
58 struct
59 {
60 char normalization_steps;
61 char ndigits;
0e3426bb 62 mp_limb_t base PACK;
8f5ca04b 63#if UDIV_TIME > 2 * UMUL_TIME
0e3426bb 64 mp_limb_t base_ninv PACK;
8f5ca04b
RM
65#endif
66 } big;
67#endif
68};
69
70/* To reduce the memory needed we include some fields of the tables
6d52618b 71 only conditionally. */
5ae3e846
UD
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)
8f5ca04b
RM
78#endif
79
80
81/* Local variables. */
aa32f798 82const struct base_table_t _itoa_base_table[] attribute_hidden =
8f5ca04b
RM
83{
84#if BITS_PER_MP_LIMB == 64
d8a5edc2
RM
85 /* 2 */ {SEL1(0ull) 1, 1},
86 /* 3 */ {SEL1(0xaaaaaaaaaaaaaaabull) 0, 1},
87 /* 4 */ {SEL1(0ull) 1, 2},
88 /* 5 */ {SEL1(0xcccccccccccccccdull) 0, 2},
89 /* 6 */ {SEL1(0xaaaaaaaaaaaaaaabull) 0, 2},
90 /* 7 */ {SEL1(0x2492492492492493ull) 1, 3},
91 /* 8 */ {SEL1(0ull) 1, 3},
92 /* 9 */ {SEL1(0xe38e38e38e38e38full) 0, 3},
93 /* 10 */ {SEL1(0xcccccccccccccccdull) 0, 3},
94 /* 11 */ {SEL1(0x2e8ba2e8ba2e8ba3ull) 0, 1},
95 /* 12 */ {SEL1(0xaaaaaaaaaaaaaaabull) 0, 3},
96 /* 13 */ {SEL1(0x4ec4ec4ec4ec4ec5ull) 0, 2},
97 /* 14 */ {SEL1(0x2492492492492493ull) 1, 4},
98 /* 15 */ {SEL1(0x8888888888888889ull) 0, 3},
99 /* 16 */ {SEL1(0ull) 1, 4},
100 /* 17 */ {SEL1(0xf0f0f0f0f0f0f0f1ull) 0, 4},
101 /* 18 */ {SEL1(0xe38e38e38e38e38full) 0, 4},
102 /* 19 */ {SEL1(0xd79435e50d79435full) 0, 4},
103 /* 20 */ {SEL1(0xcccccccccccccccdull) 0, 4},
104 /* 21 */ {SEL1(0x8618618618618619ull) 1, 5},
105 /* 22 */ {SEL1(0x2e8ba2e8ba2e8ba3ull) 0, 2},
106 /* 23 */ {SEL1(0x642c8590b21642c9ull) 1, 5},
107 /* 24 */ {SEL1(0xaaaaaaaaaaaaaaabull) 0, 4},
108 /* 25 */ {SEL1(0x47ae147ae147ae15ull) 1, 5},
109 /* 26 */ {SEL1(0x4ec4ec4ec4ec4ec5ull) 0, 3},
110 /* 27 */ {SEL1(0x97b425ed097b425full) 0, 4},
111 /* 28 */ {SEL1(0x2492492492492493ull) 1, 5},
112 /* 29 */ {SEL1(0x1a7b9611a7b9611bull) 1, 5},
113 /* 30 */ {SEL1(0x8888888888888889ull) 0, 4},
114 /* 31 */ {SEL1(0x0842108421084211ull) 1, 5},
115 /* 32 */ {SEL1(0ull) 1, 5},
116 /* 33 */ {SEL1(0x0f83e0f83e0f83e1ull) 0, 1},
117 /* 34 */ {SEL1(0xf0f0f0f0f0f0f0f1ull) 0, 5},
118 /* 35 */ {SEL1(0xea0ea0ea0ea0ea0full) 0, 5},
119 /* 36 */ {SEL1(0xe38e38e38e38e38full) 0, 5}
8f5ca04b
RM
120#endif
121#if BITS_PER_MP_LIMB == 32
122 /* 2 */ {SEL1(0ul) 1, 1, {0, 31, 0x80000000ul SEL2(0xfffffffful)}},
123 /* 3 */ {SEL1(0xaaaaaaabul) 0, 1, {0, 20, 0xcfd41b91ul SEL2(0x3b563c24ul)}},
124 /* 4 */ {SEL1(0ul) 1, 2, {1, 15, 0x40000000ul SEL2(0xfffffffful)}},
125 /* 5 */ {SEL1(0xcccccccdul) 0, 2, {1, 13, 0x48c27395ul SEL2(0xc25c2684ul)}},
126 /* 6 */ {SEL1(0xaaaaaaabul) 0, 2, {0, 12, 0x81bf1000ul SEL2(0xf91bd1b6ul)}},
127 /* 7 */ {SEL1(0x24924925ul) 1, 3, {1, 11, 0x75db9c97ul SEL2(0x1607a2cbul)}},
128 /* 8 */ {SEL1(0ul) 1, 3, {1, 10, 0x40000000ul SEL2(0xfffffffful)}},
129 /* 9 */ {SEL1(0x38e38e39ul) 0, 1, {0, 10, 0xcfd41b91ul SEL2(0x3b563c24ul)}},
130 /* 10 */ {SEL1(0xcccccccdul) 0, 3, {2, 9, 0x3b9aca00ul SEL2(0x12e0be82ul)}},
131 /* 11 */ {SEL1(0xba2e8ba3ul) 0, 3, {0, 9, 0x8c8b6d2bul SEL2(0xd24cde04ul)}},
132 /* 12 */ {SEL1(0xaaaaaaabul) 0, 3, {3, 8, 0x19a10000ul SEL2(0x3fa39ab5ul)}},
133 /* 13 */ {SEL1(0x4ec4ec4ful) 0, 2, {2, 8, 0x309f1021ul SEL2(0x50f8ac5ful)}},
134 /* 14 */ {SEL1(0x24924925ul) 1, 4, {1, 8, 0x57f6c100ul SEL2(0x74843b1eul)}},
135 /* 15 */ {SEL1(0x88888889ul) 0, 3, {0, 8, 0x98c29b81ul SEL2(0xad0326c2ul)}},
136 /* 16 */ {SEL1(0ul) 1, 4, {3, 7, 0x10000000ul SEL2(0xfffffffful)}},
137 /* 17 */ {SEL1(0xf0f0f0f1ul) 0, 4, {3, 7, 0x18754571ul SEL2(0x4ef0b6bdul)}},
138 /* 18 */ {SEL1(0x38e38e39ul) 0, 2, {2, 7, 0x247dbc80ul SEL2(0xc0fc48a1ul)}},
139 /* 19 */ {SEL1(0xaf286bcbul) 1, 5, {2, 7, 0x3547667bul SEL2(0x33838942ul)}},
140 /* 20 */ {SEL1(0xcccccccdul) 0, 4, {1, 7, 0x4c4b4000ul SEL2(0xad7f29abul)}},
141 /* 21 */ {SEL1(0x86186187ul) 1, 5, {1, 7, 0x6b5a6e1dul SEL2(0x313c3d15ul)}},
142 /* 22 */ {SEL1(0xba2e8ba3ul) 0, 4, {0, 7, 0x94ace180ul SEL2(0xb8cca9e0ul)}},
143 /* 23 */ {SEL1(0xb21642c9ul) 0, 4, {0, 7, 0xcaf18367ul SEL2(0x42ed6de9ul)}},
144 /* 24 */ {SEL1(0xaaaaaaabul) 0, 4, {4, 6, 0x0b640000ul SEL2(0x67980e0bul)}},
145 /* 25 */ {SEL1(0x51eb851ful) 0, 3, {4, 6, 0x0e8d4a51ul SEL2(0x19799812ul)}},
146 /* 26 */ {SEL1(0x4ec4ec4ful) 0, 3, {3, 6, 0x1269ae40ul SEL2(0xbce85396ul)}},
147 /* 27 */ {SEL1(0x2f684bdbul) 1, 5, {3, 6, 0x17179149ul SEL2(0x62c103a9ul)}},
148 /* 28 */ {SEL1(0x24924925ul) 1, 5, {3, 6, 0x1cb91000ul SEL2(0x1d353d43ul)}},
149 /* 29 */ {SEL1(0x8d3dcb09ul) 0, 4, {2, 6, 0x23744899ul SEL2(0xce1deceaul)}},
150 /* 30 */ {SEL1(0x88888889ul) 0, 4, {2, 6, 0x2b73a840ul SEL2(0x790fc511ul)}},
151 /* 31 */ {SEL1(0x08421085ul) 1, 5, {2, 6, 0x34e63b41ul SEL2(0x35b865a0ul)}},
152 /* 32 */ {SEL1(0ul) 1, 5, {1, 6, 0x40000000ul SEL2(0xfffffffful)}},
153 /* 33 */ {SEL1(0x3e0f83e1ul) 0, 3, {1, 6, 0x4cfa3cc1ul SEL2(0xa9aed1b3ul)}},
154 /* 34 */ {SEL1(0xf0f0f0f1ul) 0, 5, {1, 6, 0x5c13d840ul SEL2(0x63dfc229ul)}},
155 /* 35 */ {SEL1(0xd41d41d5ul) 1, 6, {1, 6, 0x6d91b519ul SEL2(0x2b0fee30ul)}},
156 /* 36 */ {SEL1(0x38e38e39ul) 0, 3, {0, 6, 0x81bf1000ul SEL2(0xf91bd1b6ul)}}
157#endif
158};
159
28f540f4 160/* Lower-case digits. */
af6f3906 161extern const char _itoa_lower_digits[];
aa32f798 162extern const char _itoa_lower_digits_internal[] attribute_hidden;
28f540f4 163/* Upper-case digits. */
af6f3906 164extern const char _itoa_upper_digits[];
aa32f798 165extern const char _itoa_upper_digits_internal[] attribute_hidden;
28f540f4 166
28f540f4 167
40c38b6c
UD
168char *
169_itoa_word (unsigned long value, char *buflim,
170 unsigned int base, int upper_case)
171{
172 const char *digits = (upper_case
173#if !defined NOT_IN_libc || defined IS_IN_rtld
174 ? INTUSE(_itoa_upper_digits)
175 : INTUSE(_itoa_lower_digits)
176#else
177 ? _itoa_upper_digits
178 : _itoa_lower_digits
179#endif
180 );
181
182 switch (base)
183 {
184#define SPECIAL(Base) \
185 case Base: \
186 do \
187 *--buflim = digits[value % Base]; \
188 while ((value /= Base) != 0); \
189 break
190
191 SPECIAL (10);
192 SPECIAL (16);
193 SPECIAL (8);
194 default:
195 do
196 *--buflim = digits[value % base];
197 while ((value /= base) != 0);
198 }
199 return buflim;
200}
201#undef SPECIAL
202
203
8f5ca04b
RM
204char *
205_itoa (value, buflim, base, upper_case)
206 unsigned long long int value;
207 char *buflim;
208 unsigned int base;
209 int upper_case;
210{
aa32f798
UD
211 const char *digits = (upper_case
212 ? INTUSE(_itoa_upper_digits)
213 : INTUSE(_itoa_lower_digits));
d64b6ad0 214 const struct base_table_t *brec = &_itoa_base_table[base - 2];
8f5ca04b
RM
215
216 switch (base)
217 {
4f8dbcb1
UD
218#define RUN_2N(BITS) \
219 do \
220 { \
221 /* `unsigned long long int' always has 64 bits. */ \
222 mp_limb_t work_hi = value >> (64 - BITS_PER_MP_LIMB); \
223 \
224 if (BITS_PER_MP_LIMB == 32) \
225 { \
226 if (work_hi != 0) \
227 { \
228 mp_limb_t work_lo; \
229 int cnt; \
230 \
231 work_lo = value & 0xfffffffful; \
232 for (cnt = BITS_PER_MP_LIMB / BITS; cnt > 0; --cnt) \
233 { \
9710f75d 234 *--buflim = digits[work_lo & ((1ul << BITS) - 1)]; \
4f8dbcb1
UD
235 work_lo >>= BITS; \
236 } \
237 if (BITS_PER_MP_LIMB % BITS != 0) \
238 { \
239 work_lo \
240 |= ((work_hi \
241 & ((1 << (BITS - BITS_PER_MP_LIMB%BITS)) \
242 - 1)) \
243 << BITS_PER_MP_LIMB % BITS); \
244 work_hi >>= BITS - BITS_PER_MP_LIMB % BITS; \
245 if (work_hi == 0) \
246 work_hi = work_lo; \
247 else \
9710f75d 248 *--buflim = digits[work_lo]; \
4f8dbcb1
UD
249 } \
250 } \
251 else \
252 work_hi = value & 0xfffffffful; \
253 } \
254 do \
255 { \
9710f75d 256 *--buflim = digits[work_hi & ((1 << BITS) - 1)]; \
4f8dbcb1
UD
257 work_hi >>= BITS; \
258 } \
259 while (work_hi != 0); \
260 } \
8f5ca04b
RM
261 while (0)
262 case 8:
263 RUN_2N (3);
264 break;
265
266 case 16:
267 RUN_2N (4);
268 break;
269
270 default:
271 {
32c075e1 272 char *bufend = buflim;
8f5ca04b 273#if BITS_PER_MP_LIMB == 64
0e3426bb 274 mp_limb_t base_multiplier = brec->base_multiplier;
8f5ca04b
RM
275 if (brec->flag)
276 while (value != 0)
277 {
0e3426bb 278 mp_limb_t quo, rem, x, dummy;
8f5ca04b
RM
279
280 umul_ppmm (x, dummy, value, base_multiplier);
281 quo = (x + ((value - x) >> 1)) >> (brec->post_shift - 1);
282 rem = value - quo * base;
9710f75d 283 *--buflim = digits[rem];
8f5ca04b
RM
284 value = quo;
285 }
286 else
287 while (value != 0)
288 {
0e3426bb 289 mp_limb_t quo, rem, x, dummy;
8f5ca04b
RM
290
291 umul_ppmm (x, dummy, value, base_multiplier);
292 quo = x >> brec->post_shift;
293 rem = value - quo * base;
9710f75d 294 *--buflim = digits[rem];
8f5ca04b
RM
295 value = quo;
296 }
297#endif
298#if BITS_PER_MP_LIMB == 32
0e3426bb 299 mp_limb_t t[3];
8f5ca04b
RM
300 int n;
301
302 /* First convert x0 to 1-3 words in base s->big.base.
303 Optimize for frequent cases of 32 bit numbers. */
0e3426bb 304 if ((mp_limb_t) (value >> 32) >= 1)
8f5ca04b 305 {
7c713e28 306#if UDIV_TIME > 2 * UMUL_TIME || UDIV_NEEDS_NORMALIZATION
8f5ca04b 307 int big_normalization_steps = brec->big.normalization_steps;
7c713e28
RM
308 mp_limb_t big_base_norm
309 = brec->big.base << big_normalization_steps;
310#endif
0e3426bb 311 if ((mp_limb_t) (value >> 32) >= brec->big.base)
8f5ca04b 312 {
0e3426bb 313 mp_limb_t x1hi, x1lo, r;
8f5ca04b
RM
314 /* If you want to optimize this, take advantage of
315 that the quotient in the first udiv_qrnnd will
316 always be very small. It might be faster just to
317 subtract in a tight loop. */
318
319#if UDIV_TIME > 2 * UMUL_TIME
0e3426bb 320 mp_limb_t x, xh, xl;
8f5ca04b
RM
321
322 if (big_normalization_steps == 0)
323 xh = 0;
324 else
cae88996
UD
325 xh = (mp_limb_t) (value >> (64 - big_normalization_steps));
326 xl = (mp_limb_t) (value >> (32 - big_normalization_steps));
8f5ca04b
RM
327 udiv_qrnnd_preinv (x1hi, r, xh, xl, big_base_norm,
328 brec->big.base_ninv);
329
0e3426bb 330 xl = ((mp_limb_t) value) << big_normalization_steps;
8f5ca04b 331 udiv_qrnnd_preinv (x1lo, x, r, xl, big_base_norm,
d112ae30 332 brec->big.base_ninv);
8f5ca04b
RM
333 t[2] = x >> big_normalization_steps;
334
335 if (big_normalization_steps == 0)
336 xh = x1hi;
337 else
338 xh = ((x1hi << big_normalization_steps)
cae88996 339 | (x1lo >> (32 - big_normalization_steps)));
8f5ca04b
RM
340 xl = x1lo << big_normalization_steps;
341 udiv_qrnnd_preinv (t[0], x, xh, xl, big_base_norm,
d112ae30 342 brec->big.base_ninv);
8f5ca04b
RM
343 t[1] = x >> big_normalization_steps;
344#elif UDIV_NEEDS_NORMALIZATION
0e3426bb 345 mp_limb_t x, xh, xl;
8f5ca04b
RM
346
347 if (big_normalization_steps == 0)
348 xh = 0;
349 else
0e3426bb
RM
350 xh = (mp_limb_t) (value >> 64 - big_normalization_steps);
351 xl = (mp_limb_t) (value >> 32 - big_normalization_steps);
8f5ca04b
RM
352 udiv_qrnnd (x1hi, r, xh, xl, big_base_norm);
353
0e3426bb 354 xl = ((mp_limb_t) value) << big_normalization_steps;
8f5ca04b
RM
355 udiv_qrnnd (x1lo, x, r, xl, big_base_norm);
356 t[2] = x >> big_normalization_steps;
357
358 if (big_normalization_steps == 0)
359 xh = x1hi;
360 else
361 xh = ((x1hi << big_normalization_steps)
362 | (x1lo >> 32 - big_normalization_steps));
363 xl = x1lo << big_normalization_steps;
364 udiv_qrnnd (t[0], x, xh, xl, big_base_norm);
365 t[1] = x >> big_normalization_steps;
366#else
0e3426bb 367 udiv_qrnnd (x1hi, r, 0, (mp_limb_t) (value >> 32),
8f5ca04b 368 brec->big.base);
0e3426bb 369 udiv_qrnnd (x1lo, t[2], r, (mp_limb_t) value, brec->big.base);
8f5ca04b
RM
370 udiv_qrnnd (t[0], t[1], x1hi, x1lo, brec->big.base);
371#endif
372 n = 3;
373 }
374 else
375 {
376#if (UDIV_TIME > 2 * UMUL_TIME)
0e3426bb 377 mp_limb_t x;
8f5ca04b
RM
378
379 value <<= brec->big.normalization_steps;
0e3426bb
RM
380 udiv_qrnnd_preinv (t[0], x, (mp_limb_t) (value >> 32),
381 (mp_limb_t) value, big_base_norm,
8f5ca04b
RM
382 brec->big.base_ninv);
383 t[1] = x >> brec->big.normalization_steps;
384#elif UDIV_NEEDS_NORMALIZATION
0e3426bb 385 mp_limb_t x;
8f5ca04b
RM
386
387 value <<= big_normalization_steps;
0e3426bb
RM
388 udiv_qrnnd (t[0], x, (mp_limb_t) (value >> 32),
389 (mp_limb_t) value, big_base_norm);
8f5ca04b
RM
390 t[1] = x >> big_normalization_steps;
391#else
0e3426bb
RM
392 udiv_qrnnd (t[0], t[1], (mp_limb_t) (value >> 32),
393 (mp_limb_t) value, brec->big.base);
8f5ca04b
RM
394#endif
395 n = 2;
396 }
397 }
398 else
399 {
400 t[0] = value;
401 n = 1;
402 }
403
404 /* Convert the 1-3 words in t[], word by word, to ASCII. */
405 do
406 {
0e3426bb 407 mp_limb_t ti = t[--n];
8f5ca04b
RM
408 int ndig_for_this_limb = 0;
409
410#if UDIV_TIME > 2 * UMUL_TIME
0e3426bb 411 mp_limb_t base_multiplier = brec->base_multiplier;
8f5ca04b
RM
412 if (brec->flag)
413 while (ti != 0)
414 {
0e3426bb 415 mp_limb_t quo, rem, x, dummy;
8f5ca04b
RM
416
417 umul_ppmm (x, dummy, ti, base_multiplier);
418 quo = (x + ((ti - x) >> 1)) >> (brec->post_shift - 1);
419 rem = ti - quo * base;
9710f75d 420 *--buflim = digits[rem];
8f5ca04b
RM
421 ti = quo;
422 ++ndig_for_this_limb;
423 }
424 else
425 while (ti != 0)
426 {
0e3426bb 427 mp_limb_t quo, rem, x, dummy;
8f5ca04b
RM
428
429 umul_ppmm (x, dummy, ti, base_multiplier);
430 quo = x >> brec->post_shift;
431 rem = ti - quo * base;
9710f75d 432 *--buflim = digits[rem];
8f5ca04b
RM
433 ti = quo;
434 ++ndig_for_this_limb;
435 }
436#else
437 while (ti != 0)
438 {
0e3426bb 439 mp_limb_t quo, rem;
8f5ca04b
RM
440
441 quo = ti / base;
442 rem = ti % base;
9710f75d 443 *--buflim = digits[rem];
8f5ca04b
RM
444 ti = quo;
445 ++ndig_for_this_limb;
446 }
447#endif
448 /* If this wasn't the most significant word, pad with zeros. */
449 if (n != 0)
450 while (ndig_for_this_limb < brec->big.ndigits)
451 {
9710f75d 452 *--buflim = '0';
8f5ca04b
RM
453 ++ndig_for_this_limb;
454 }
455 }
456 while (n != 0);
457#endif
32c075e1
JJ
458 if (buflim == bufend)
459 *--buflim = '0';
8f5ca04b
RM
460 }
461 break;
462 }
463
9710f75d 464 return buflim;
8f5ca04b 465}
9c7ff11a
UD
466
467char *
468_fitoa_word (unsigned long value, char *buf, unsigned int base, int upper_case)
469{
470 char tmpbuf[sizeof (value) * 4]; /* Worst case length: base 2. */
471 char *cp = _itoa_word (value, tmpbuf + sizeof (value) * 4, base, upper_case);
472 while (cp < tmpbuf + sizeof (value) * 4)
473 *buf++ = *cp++;
474 return buf;
475}
476
477char *
478_fitoa (unsigned long long value, char *buf, unsigned int base, int upper_case)
479{
480 char tmpbuf[sizeof (value) * 4]; /* Worst case length: base 2. */
481 char *cp = _itoa (value, tmpbuf + sizeof (value) * 4, base, upper_case);
482 while (cp < tmpbuf + sizeof (value) * 4)
483 *buf++ = *cp++;
484 return buf;
485}