Fix aux cache handling in ldconfig with chroot.
[glibc.git] / stdio-common / _itowa.c
blob09a961dfafa2d3f55b5dfe4e54fbae6878357e4d
1 /* Internal function for converting integers to ASCII.
2 Copyright (C) 1994-1996,1999,2000,2002,2007 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Torbjorn Granlund <tege@matematik.su.se>
5 and Ulrich Drepper <drepper@gnu.org>.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA. */
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 "_itowa.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
80 /* Factor table for the different bases. */
81 extern const struct base_table_t _itoa_base_table[] attribute_hidden;
83 /* Lower-case digits. */
84 extern const wchar_t _itowa_lower_digits[] attribute_hidden;
85 /* Upper-case digits. */
86 extern const wchar_t _itowa_upper_digits[] attribute_hidden;
89 #if LLONG_MAX != LONG_MAX
90 wchar_t *
91 _itowa (value, buflim, base, upper_case)
92 unsigned long long int value;
93 wchar_t *buflim;
94 unsigned int base;
95 int upper_case;
97 const wchar_t *digits = (upper_case
98 ? _itowa_upper_digits : _itowa_lower_digits);
99 wchar_t *bp = buflim;
100 const struct base_table_t *brec = &_itoa_base_table[base - 2];
102 switch (base)
104 # define RUN_2N(BITS) \
105 do \
107 /* `unsigned long long int' always has 64 bits. */ \
108 mp_limb_t work_hi = value >> (64 - BITS_PER_MP_LIMB); \
110 if (BITS_PER_MP_LIMB == 32) \
112 if (work_hi != 0) \
114 mp_limb_t work_lo; \
115 int cnt; \
117 work_lo = value & 0xfffffffful; \
118 for (cnt = BITS_PER_MP_LIMB / BITS; cnt > 0; --cnt) \
120 *--bp = digits[work_lo & ((1ul << BITS) - 1)]; \
121 work_lo >>= BITS; \
123 if (BITS_PER_MP_LIMB % BITS != 0) \
125 work_lo \
126 |= ((work_hi \
127 & ((1 << (BITS - BITS_PER_MP_LIMB%BITS)) \
128 - 1)) \
129 << BITS_PER_MP_LIMB % BITS); \
130 work_hi >>= BITS - BITS_PER_MP_LIMB % BITS; \
131 if (work_hi == 0) \
132 work_hi = work_lo; \
133 else \
134 *--bp = digits[work_lo]; \
137 else \
138 work_hi = value & 0xfffffffful; \
140 do \
142 *--bp = digits[work_hi & ((1 << BITS) - 1)]; \
143 work_hi >>= BITS; \
145 while (work_hi != 0); \
147 while (0)
148 case 8:
149 RUN_2N (3);
150 break;
152 case 16:
153 RUN_2N (4);
154 break;
156 default:
158 # if BITS_PER_MP_LIMB == 64
159 mp_limb_t base_multiplier = brec->base_multiplier;
160 if (brec->flag)
161 while (value != 0)
163 mp_limb_t quo, rem, x, dummy;
165 umul_ppmm (x, dummy, value, base_multiplier);
166 quo = (x + ((value - x) >> 1)) >> (brec->post_shift - 1);
167 rem = value - quo * base;
168 *--bp = digits[rem];
169 value = quo;
171 else
172 while (value != 0)
174 mp_limb_t quo, rem, x, dummy;
176 umul_ppmm (x, dummy, value, base_multiplier);
177 quo = x >> brec->post_shift;
178 rem = value - quo * base;
179 *--bp = digits[rem];
180 value = quo;
182 # endif
183 # if BITS_PER_MP_LIMB == 32
184 mp_limb_t t[3];
185 int n;
187 /* First convert x0 to 1-3 words in base s->big.base.
188 Optimize for frequent cases of 32 bit numbers. */
189 if ((mp_limb_t) (value >> 32) >= 1)
191 # if UDIV_TIME > 2 * UMUL_TIME || UDIV_NEEDS_NORMALIZATION
192 int big_normalization_steps = brec->big.normalization_steps;
193 mp_limb_t big_base_norm
194 = brec->big.base << big_normalization_steps;
195 # endif
196 if ((mp_limb_t) (value >> 32) >= brec->big.base)
198 mp_limb_t x1hi, x1lo, r;
199 /* If you want to optimize this, take advantage of
200 that the quotient in the first udiv_qrnnd will
201 always be very small. It might be faster just to
202 subtract in a tight loop. */
204 # if UDIV_TIME > 2 * UMUL_TIME
205 mp_limb_t x, xh, xl;
207 if (big_normalization_steps == 0)
208 xh = 0;
209 else
210 xh = (mp_limb_t) (value >> (64 - big_normalization_steps));
211 xl = (mp_limb_t) (value >> (32 - big_normalization_steps));
212 udiv_qrnnd_preinv (x1hi, r, xh, xl, big_base_norm,
213 brec->big.base_ninv);
215 xl = ((mp_limb_t) value) << big_normalization_steps;
216 udiv_qrnnd_preinv (x1lo, x, r, xl, big_base_norm,
217 brec->big.base_ninv);
218 t[2] = x >> big_normalization_steps;
220 if (big_normalization_steps == 0)
221 xh = x1hi;
222 else
223 xh = ((x1hi << big_normalization_steps)
224 | (x1lo >> (32 - big_normalization_steps)));
225 xl = x1lo << big_normalization_steps;
226 udiv_qrnnd_preinv (t[0], x, xh, xl, big_base_norm,
227 brec->big.base_ninv);
228 t[1] = x >> big_normalization_steps;
229 # elif UDIV_NEEDS_NORMALIZATION
230 mp_limb_t x, xh, xl;
232 if (big_normalization_steps == 0)
233 xh = 0;
234 else
235 xh = (mp_limb_t) (value >> 64 - big_normalization_steps);
236 xl = (mp_limb_t) (value >> 32 - big_normalization_steps);
237 udiv_qrnnd (x1hi, r, xh, xl, big_base_norm);
239 xl = ((mp_limb_t) value) << big_normalization_steps;
240 udiv_qrnnd (x1lo, x, r, xl, big_base_norm);
241 t[2] = x >> big_normalization_steps;
243 if (big_normalization_steps == 0)
244 xh = x1hi;
245 else
246 xh = ((x1hi << big_normalization_steps)
247 | (x1lo >> 32 - big_normalization_steps));
248 xl = x1lo << big_normalization_steps;
249 udiv_qrnnd (t[0], x, xh, xl, big_base_norm);
250 t[1] = x >> big_normalization_steps;
251 # else
252 udiv_qrnnd (x1hi, r, 0, (mp_limb_t) (value >> 32),
253 brec->big.base);
254 udiv_qrnnd (x1lo, t[2], r, (mp_limb_t) value, brec->big.base);
255 udiv_qrnnd (t[0], t[1], x1hi, x1lo, brec->big.base);
256 # endif
257 n = 3;
259 else
261 # if UDIV_TIME > 2 * UMUL_TIME
262 mp_limb_t x;
264 value <<= brec->big.normalization_steps;
265 udiv_qrnnd_preinv (t[0], x, (mp_limb_t) (value >> 32),
266 (mp_limb_t) value, big_base_norm,
267 brec->big.base_ninv);
268 t[1] = x >> brec->big.normalization_steps;
269 # elif UDIV_NEEDS_NORMALIZATION
270 mp_limb_t x;
272 value <<= big_normalization_steps;
273 udiv_qrnnd (t[0], x, (mp_limb_t) (value >> 32),
274 (mp_limb_t) value, big_base_norm);
275 t[1] = x >> big_normalization_steps;
276 # else
277 udiv_qrnnd (t[0], t[1], (mp_limb_t) (value >> 32),
278 (mp_limb_t) value, brec->big.base);
279 # endif
280 n = 2;
283 else
285 t[0] = value;
286 n = 1;
289 /* Convert the 1-3 words in t[], word by word, to ASCII. */
292 mp_limb_t ti = t[--n];
293 int ndig_for_this_limb = 0;
295 # if UDIV_TIME > 2 * UMUL_TIME
296 mp_limb_t base_multiplier = brec->base_multiplier;
297 if (brec->flag)
298 while (ti != 0)
300 mp_limb_t quo, rem, x, dummy;
302 umul_ppmm (x, dummy, ti, base_multiplier);
303 quo = (x + ((ti - x) >> 1)) >> (brec->post_shift - 1);
304 rem = ti - quo * base;
305 *--bp = digits[rem];
306 ti = quo;
307 ++ndig_for_this_limb;
309 else
310 while (ti != 0)
312 mp_limb_t quo, rem, x, dummy;
314 umul_ppmm (x, dummy, ti, base_multiplier);
315 quo = x >> brec->post_shift;
316 rem = ti - quo * base;
317 *--bp = digits[rem];
318 ti = quo;
319 ++ndig_for_this_limb;
321 # else
322 while (ti != 0)
324 mp_limb_t quo, rem;
326 quo = ti / base;
327 rem = ti % base;
328 *--bp = digits[rem];
329 ti = quo;
330 ++ndig_for_this_limb;
332 # endif
333 /* If this wasn't the most significant word, pad with zeros. */
334 if (n != 0)
335 while (ndig_for_this_limb < brec->big.ndigits)
337 *--bp = '0';
338 ++ndig_for_this_limb;
341 while (n != 0);
342 # endif
344 break;
347 return bp;
349 #endif