1 /* Internal function for converting integers to ASCII.
2 Copyright (C) 1994-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <gmp-mparam.h>
22 #include <stdlib/gmp-impl.h>
23 #include <stdlib/longlong.h>
28 /* Canonize environment. For some architectures not all values might
29 be defined in the GMP header files. */
37 /* Control memory layout. */
40 # define PACK __attribute__ ((packed))
46 /* Declare local types. */
49 #if (UDIV_TIME > 2 * UMUL_TIME)
50 mp_limb_t base_multiplier
;
54 #if BITS_PER_MP_LIMB == 32
57 char normalization_steps
;
60 #if UDIV_TIME > 2 * UMUL_TIME
61 mp_limb_t base_ninv PACK
;
67 /* To reduce the memory needed we include some fields of the tables
68 only conditionally. */
69 #if UDIV_TIME > 2 * UMUL_TIME
77 /* Factor table for the different bases. */
78 extern const struct base_table_t _itoa_base_table
[] attribute_hidden
;
80 /* Lower-case digits. */
81 extern const wchar_t _itowa_lower_digits
[] attribute_hidden
;
82 /* Upper-case digits. */
83 extern const wchar_t _itowa_upper_digits
[] attribute_hidden
;
88 _itowa (unsigned long long int value
, wchar_t *buflim
, unsigned int base
,
91 const wchar_t *digits
= (upper_case
92 ? _itowa_upper_digits
: _itowa_lower_digits
);
94 const struct base_table_t
*brec
= &_itoa_base_table
[base
- 2];
98 # define RUN_2N(BITS) \
101 /* `unsigned long long int' always has 64 bits. */ \
102 mp_limb_t work_hi = value >> (64 - BITS_PER_MP_LIMB); \
104 if (BITS_PER_MP_LIMB == 32) \
111 work_lo = value & 0xfffffffful; \
112 for (cnt = BITS_PER_MP_LIMB / BITS; cnt > 0; --cnt) \
114 *--bp = digits[work_lo & ((1ul << BITS) - 1)]; \
117 if (BITS_PER_MP_LIMB % BITS != 0) \
121 & ((1 << (BITS - BITS_PER_MP_LIMB%BITS)) \
123 << BITS_PER_MP_LIMB % BITS); \
124 work_hi >>= BITS - BITS_PER_MP_LIMB % BITS; \
128 *--bp = digits[work_lo]; \
132 work_hi = value & 0xfffffffful; \
136 *--bp = digits[work_hi & ((1 << BITS) - 1)]; \
139 while (work_hi != 0); \
152 # if BITS_PER_MP_LIMB == 64
153 mp_limb_t base_multiplier
= brec
->base_multiplier
;
157 mp_limb_t quo
, rem
, x
;
158 mp_limb_t dummy
__attribute__ ((unused
));
160 umul_ppmm (x
, dummy
, value
, base_multiplier
);
161 quo
= (x
+ ((value
- x
) >> 1)) >> (brec
->post_shift
- 1);
162 rem
= value
- quo
* base
;
169 mp_limb_t quo
, rem
, x
;
170 mp_limb_t dummy
__attribute__ ((unused
));
172 umul_ppmm (x
, dummy
, value
, base_multiplier
);
173 quo
= x
>> brec
->post_shift
;
174 rem
= value
- quo
* base
;
179 # if BITS_PER_MP_LIMB == 32
183 /* First convert x0 to 1-3 words in base s->big.base.
184 Optimize for frequent cases of 32 bit numbers. */
185 if ((mp_limb_t
) (value
>> 32) >= 1)
187 # if UDIV_TIME > 2 * UMUL_TIME || UDIV_NEEDS_NORMALIZATION
188 int big_normalization_steps
= brec
->big
.normalization_steps
;
189 mp_limb_t big_base_norm
190 = brec
->big
.base
<< big_normalization_steps
;
192 if ((mp_limb_t
) (value
>> 32) >= brec
->big
.base
)
194 mp_limb_t x1hi
, x1lo
, r
;
195 /* If you want to optimize this, take advantage of
196 that the quotient in the first udiv_qrnnd will
197 always be very small. It might be faster just to
198 subtract in a tight loop. */
200 # if UDIV_TIME > 2 * UMUL_TIME
203 if (big_normalization_steps
== 0)
206 xh
= (mp_limb_t
) (value
>> (64 - big_normalization_steps
));
207 xl
= (mp_limb_t
) (value
>> (32 - big_normalization_steps
));
208 udiv_qrnnd_preinv (x1hi
, r
, xh
, xl
, big_base_norm
,
209 brec
->big
.base_ninv
);
211 xl
= ((mp_limb_t
) value
) << big_normalization_steps
;
212 udiv_qrnnd_preinv (x1lo
, x
, r
, xl
, big_base_norm
,
213 brec
->big
.base_ninv
);
214 t
[2] = x
>> big_normalization_steps
;
216 if (big_normalization_steps
== 0)
219 xh
= ((x1hi
<< big_normalization_steps
)
220 | (x1lo
>> (32 - big_normalization_steps
)));
221 xl
= x1lo
<< big_normalization_steps
;
222 udiv_qrnnd_preinv (t
[0], x
, xh
, xl
, big_base_norm
,
223 brec
->big
.base_ninv
);
224 t
[1] = x
>> big_normalization_steps
;
225 # elif UDIV_NEEDS_NORMALIZATION
228 if (big_normalization_steps
== 0)
231 xh
= (mp_limb_t
) (value
>> 64 - big_normalization_steps
);
232 xl
= (mp_limb_t
) (value
>> 32 - big_normalization_steps
);
233 udiv_qrnnd (x1hi
, r
, xh
, xl
, big_base_norm
);
235 xl
= ((mp_limb_t
) value
) << big_normalization_steps
;
236 udiv_qrnnd (x1lo
, x
, r
, xl
, big_base_norm
);
237 t
[2] = x
>> big_normalization_steps
;
239 if (big_normalization_steps
== 0)
242 xh
= ((x1hi
<< big_normalization_steps
)
243 | (x1lo
>> 32 - big_normalization_steps
));
244 xl
= x1lo
<< big_normalization_steps
;
245 udiv_qrnnd (t
[0], x
, xh
, xl
, big_base_norm
);
246 t
[1] = x
>> big_normalization_steps
;
248 udiv_qrnnd (x1hi
, r
, 0, (mp_limb_t
) (value
>> 32),
250 udiv_qrnnd (x1lo
, t
[2], r
, (mp_limb_t
) value
, brec
->big
.base
);
251 udiv_qrnnd (t
[0], t
[1], x1hi
, x1lo
, brec
->big
.base
);
257 # if UDIV_TIME > 2 * UMUL_TIME
260 value
<<= brec
->big
.normalization_steps
;
261 udiv_qrnnd_preinv (t
[0], x
, (mp_limb_t
) (value
>> 32),
262 (mp_limb_t
) value
, big_base_norm
,
263 brec
->big
.base_ninv
);
264 t
[1] = x
>> brec
->big
.normalization_steps
;
265 # elif UDIV_NEEDS_NORMALIZATION
268 value
<<= big_normalization_steps
;
269 udiv_qrnnd (t
[0], x
, (mp_limb_t
) (value
>> 32),
270 (mp_limb_t
) value
, big_base_norm
);
271 t
[1] = x
>> big_normalization_steps
;
273 udiv_qrnnd (t
[0], t
[1], (mp_limb_t
) (value
>> 32),
274 (mp_limb_t
) value
, brec
->big
.base
);
285 /* Convert the 1-3 words in t[], word by word, to ASCII. */
288 mp_limb_t ti
= t
[--n
];
289 int ndig_for_this_limb
= 0;
291 # if UDIV_TIME > 2 * UMUL_TIME
292 mp_limb_t base_multiplier
= brec
->base_multiplier
;
296 mp_limb_t quo
, rem
, x
;
297 mp_limb_t dummy
__attribute__ ((unused
));
299 umul_ppmm (x
, dummy
, ti
, base_multiplier
);
300 quo
= (x
+ ((ti
- x
) >> 1)) >> (brec
->post_shift
- 1);
301 rem
= ti
- quo
* base
;
304 ++ndig_for_this_limb
;
309 mp_limb_t quo
, rem
, x
;
310 mp_limb_t dummy
__attribute__ ((unused
));
312 umul_ppmm (x
, dummy
, ti
, base_multiplier
);
313 quo
= x
>> brec
->post_shift
;
314 rem
= ti
- quo
* base
;
317 ++ndig_for_this_limb
;
328 ++ndig_for_this_limb
;
331 /* If this wasn't the most significant word, pad with zeros. */
333 while (ndig_for_this_limb
< brec
->big
.ndigits
)
336 ++ndig_for_this_limb
;