(__pthread_initial_thread): Update initializer. (__pthread_manager_thread): Likewise...
[glibc.git] / stdio-common / _i18n_itowa.c
blob357f8a383be0ee0e254008af7e9ee7aaec65a638
1 /* Internal function for converting integers to string using locale
2 specific digits.
3 Copyright (C) 2000 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 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 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include <gmp-mparam.h>
23 #include <stdlib/gmp.h>
24 #include <stdlib/gmp-impl.h>
25 #include <stdlib/longlong.h>
27 #include "_i18n_itowa.h"
30 /* Canonize environment. For some architectures not all values might
31 be defined in the GMP header files. */
32 #ifndef UMUL_TIME
33 # define UMUL_TIME 1
34 #endif
35 #ifndef UDIV_TIME
36 # define UDIV_TIME 3
37 #endif
39 /* Control memory layout. */
40 #ifdef PACK
41 # undef PACK
42 # define PACK __attribute__ ((packed))
43 #else
44 # define PACK
45 #endif
48 /* Declare local types. */
49 struct base_table_t
51 #if (UDIV_TIME > 2 * UMUL_TIME)
52 mp_limb_t base_multiplier;
53 #endif
54 char flag;
55 char post_shift;
56 #if BITS_PER_MP_LIMB == 32
57 struct
59 char normalization_steps;
60 char ndigits;
61 mp_limb_t base PACK;
62 #if UDIV_TIME > 2 * UMUL_TIME
63 mp_limb_t base_ninv PACK;
64 #endif
65 } big;
66 #endif
70 /* Variable in other file. */
71 extern const struct base_table_t _itoa_base_table[];
74 wchar_t *
75 _i18n_itowa (value, buflim)
76 unsigned long long int value;
77 wchar_t *buflim;
79 const struct base_table_t *brec = &_itoa_base_table[8];
81 #if BITS_PER_MP_LIMB == 64
82 mp_limb_t base_multiplier = brec->base_multiplier;
83 if (brec->flag) while (value != 0)
85 mp_limb_t quo, rem, x, dummy;
87 umul_ppmm (x, dummy, value, base_multiplier);
88 quo = (x + ((value - x) >> 1)) >> (brec->post_shift - 1);
89 rem = value - quo * 10;
90 *--buflim = outdigitwc_value (rem);
91 value = quo;
93 else
94 while (value != 0)
96 mp_limb_t quo, rem, x, dummy;
98 umul_ppmm (x, dummy, value, base_multiplier);
99 quo = x >> brec->post_shift;
100 rem = value - quo * 10;
101 *--buflim = outdigitwc_value (rem);
102 value = quo;
104 #endif
105 #if BITS_PER_MP_LIMB == 32
106 mp_limb_t t[3];
107 int n;
109 /* First convert x0 to 1-3 words in base s->big.base.
110 Optimize for frequent cases of 32 bit numbers. */
111 if ((mp_limb_t) (value >> 32) >= 1)
113 #if UDIV_TIME > 2 * UMUL_TIME || UDIV_NEEDS_NORMALIZATION
114 int big_normalization_steps = brec->big.normalization_steps;
115 mp_limb_t big_base_norm
116 = brec->big.base << big_normalization_steps;
117 #endif
118 if ((mp_limb_t) (value >> 32) >= brec->big.base)
120 mp_limb_t x1hi, x1lo, r;
121 /* If you want to optimize this, take advantage of
122 that the quotient in the first udiv_qrnnd will
123 always be very small. It might be faster just to
124 subtract in a tight loop. */
126 #if UDIV_TIME > 2 * UMUL_TIME
127 mp_limb_t x, xh, xl;
129 if (big_normalization_steps == 0)
130 xh = 0;
131 else
132 xh = (mp_limb_t) (value >> (64 - big_normalization_steps));
133 xl = (mp_limb_t) (value >> (32 - big_normalization_steps));
134 udiv_qrnnd_preinv (x1hi, r, xh, xl, big_base_norm,
135 brec->big.base_ninv);
137 xl = ((mp_limb_t) value) << big_normalization_steps;
138 udiv_qrnnd_preinv (x1lo, x, r, xl, big_base_norm,
139 brec->big.base_ninv);
140 t[2] = x >> big_normalization_steps;
142 if (big_normalization_steps == 0)
143 xh = x1hi;
144 else
145 xh = ((x1hi << big_normalization_steps)
146 | (x1lo >> (32 - big_normalization_steps)));
147 xl = x1lo << big_normalization_steps;
148 udiv_qrnnd_preinv (t[0], x, xh, xl, big_base_norm,
149 brec->big.base_ninv);
150 t[1] = x >> big_normalization_steps;
151 #elif UDIV_NEEDS_NORMALIZATION
152 mp_limb_t x, xh, xl;
154 if (big_normalization_steps == 0)
155 xh = 0;
156 else
157 xh = (mp_limb_t) (value >> 64 - big_normalization_steps);
158 xl = (mp_limb_t) (value >> 32 - big_normalization_steps);
159 udiv_qrnnd (x1hi, r, xh, xl, big_base_norm);
161 xl = ((mp_limb_t) value) << big_normalization_steps;
162 udiv_qrnnd (x1lo, x, r, xl, big_base_norm);
163 t[2] = x >> big_normalization_steps;
165 if (big_normalization_steps == 0)
166 xh = x1hi;
167 else
168 xh = ((x1hi << big_normalization_steps)
169 | (x1lo >> 32 - big_normalization_steps));
170 xl = x1lo << big_normalization_steps;
171 udiv_qrnnd (t[0], x, xh, xl, big_base_norm);
172 t[1] = x >> big_normalization_steps;
173 #else
174 udiv_qrnnd (x1hi, r, 0, (mp_limb_t) (value >> 32),
175 brec->big.base);
176 udiv_qrnnd (x1lo, t[2], r, (mp_limb_t) value, brec->big.base);
177 udiv_qrnnd (t[0], t[1], x1hi, x1lo, brec->big.base);
178 #endif
179 n = 3;
181 else
183 #if (UDIV_TIME > 2 * UMUL_TIME)
184 mp_limb_t x;
186 value <<= brec->big.normalization_steps;
187 udiv_qrnnd_preinv (t[0], x, (mp_limb_t) (value >> 32),
188 (mp_limb_t) value, big_base_norm,
189 brec->big.base_ninv);
190 t[1] = x >> brec->big.normalization_steps;
191 #elif UDIV_NEEDS_NORMALIZATION
192 mp_limb_t x;
194 value <<= big_normalization_steps;
195 udiv_qrnnd (t[0], x, (mp_limb_t) (value >> 32),
196 (mp_limb_t) value, big_base_norm);
197 t[1] = x >> big_normalization_steps;
198 #else
199 udiv_qrnnd (t[0], t[1], (mp_limb_t) (value >> 32),
200 (mp_limb_t) value, brec->big.base);
201 #endif
202 n = 2;
205 else
207 t[0] = value;
208 n = 1;
211 /* Convert the 1-3 words in t[], word by word, to ASCII. */
214 mp_limb_t ti = t[--n];
215 int ndig_for_this_limb = 0;
217 #if UDIV_TIME > 2 * UMUL_TIME
218 mp_limb_t base_multiplier = brec->base_multiplier;
219 if (brec->flag)
220 while (ti != 0)
222 mp_limb_t quo, rem, x, dummy;
224 umul_ppmm (x, dummy, ti, base_multiplier);
225 quo = (x + ((ti - x) >> 1)) >> (brec->post_shift - 1);
226 rem = ti - quo * 10;
227 *--buflim = outdigitwc_value (rem);
228 ti = quo;
229 ++ndig_for_this_limb;
231 else
232 while (ti != 0)
234 mp_limb_t quo, rem, x, dummy;
236 umul_ppmm (x, dummy, ti, base_multiplier);
237 quo = x >> brec->post_shift;
238 rem = ti - quo * 10;
239 *--buflim = outdigitwc_value (rem);
240 ti = quo;
241 ++ndig_for_this_limb;
243 #else
244 while (ti != 0)
246 mp_limb_t quo, rem;
248 quo = ti / 10;
249 rem = ti % 10;
250 *--buflim = outdigitwc_value (rem);
251 ti = quo;
252 ++ndig_for_this_limb;
254 #endif
255 /* If this wasn't the most significant word, pad with zeros. */
256 if (n != 0)
257 while (ndig_for_this_limb < brec->big.ndigits)
259 *--buflim = '0';
260 ++ndig_for_this_limb;
263 while (n != 0);
264 #endif
266 return buflim;