1 /* __mpn_get_str -- Convert a MSIZE long limb vector pointed to by MPTR
2 to a printable string in STR in base BASE.
4 Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
7 This file is part of the GNU C Library. Its master source is NOT part of
8 the C library, however. This file is in fact copied from the GNU MP
9 Library and its source lives there.
11 The GNU C Library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Library General Public License as
13 published by the Free Software Foundation; either version 2 of the
14 License, or (at your option) any later version.
16 The GNU C Library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Library General Public License for more details.
21 You should have received a copy of the GNU Library General Public
22 License along with the GNU C Library; see the file COPYING.LIB. If
23 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
24 Cambridge, MA 02139, USA. */
29 /* Convert the limb vector pointed to by MPTR and MSIZE long to a
30 char array, using base BASE for the result array. Store the
31 result in the character array STR. STR must point to an array with
32 space for the largest possible number represented by a MSIZE long
33 limb vector + 1 extra character.
35 The result is NOT in Ascii, to convert it to printable format, add
36 '0' or 'A' depending on the base and range.
38 Return the number of digits in the result string.
39 This may include some leading zeros.
41 The limb vector pointed to by MPTR is clobbered. */
44 __mpn_get_str (str
, base
, mptr
, msize
)
51 #if UDIV_NEEDS_NORMALIZATION || UDIV_TIME > 2 * UMUL_TIME
52 int normalization_steps
;
54 #if UDIV_TIME > 2 * UMUL_TIME
55 mp_limb big_base_inverted
;
57 unsigned int dig_per_u
;
59 register unsigned char *s
;
61 big_base
= __mp_bases
[base
].big_base
;
65 /* Special case zero, as the code below doesn't handle it. */
72 if ((base
& (base
- 1)) == 0)
74 /* The base is a power of 2. Make conversion from most
77 register int bits_per_digit
= big_base
;
83 count_leading_zeros (x
, n1
);
85 /* BIT_POS should be R when input ends in least sign. nibble,
86 R + bits_per_digit * n when input ends in n:th least significant
92 bits
= BITS_PER_MP_LIMB
* msize
- x
;
93 x
= bits
% bits_per_digit
;
95 bits
+= bits_per_digit
- x
;
96 bit_pos
= bits
- (msize
- 1) * BITS_PER_MP_LIMB
;
99 /* Fast loop for bit output. */
103 bit_pos
-= bits_per_digit
;
106 *s
++ = (n1
>> bit_pos
) & ((1 << bits_per_digit
) - 1);
107 bit_pos
-= bits_per_digit
;
112 n0
= (n1
<< -bit_pos
) & ((1 << bits_per_digit
) - 1);
114 bit_pos
+= BITS_PER_MP_LIMB
;
115 *s
++ = n0
| (n1
>> bit_pos
);
124 /* General case. The base is not a power of 2. Make conversion
125 from least significant end. */
127 /* If udiv_qrnnd only handles divisors with the most significant bit
128 set, prepare BIG_BASE for being a divisor by shifting it to the
129 left exactly enough to set the most significant bit. */
130 #if UDIV_NEEDS_NORMALIZATION || UDIV_TIME > 2 * UMUL_TIME
131 count_leading_zeros (normalization_steps
, big_base
);
132 big_base
<<= normalization_steps
;
133 #if UDIV_TIME > 2 * UMUL_TIME
134 /* Get the fixed-point approximation to 1/(BIG_BASE << NORMALIZATION_STEPS). */
135 big_base_inverted
= __mp_bases
[base
].big_base_inverted
;
139 dig_per_u
= __mp_bases
[base
].chars_per_limb
;
140 out_len
= ((size_t) msize
* BITS_PER_MP_LIMB
141 * __mp_bases
[base
].chars_per_bit_exactly
) + 1;
149 #if UDIV_NEEDS_NORMALIZATION || UDIV_TIME > 2 * UMUL_TIME
150 /* If we shifted BIG_BASE above, shift the dividend too, to get
151 the right quotient. We need to do this every loop,
152 since the intermediate quotients are OK, but the quotient from
153 one turn in the loop is going to be the dividend in the
154 next turn, and the dividend needs to be up-shifted. */
155 if (normalization_steps
!= 0)
157 n0
= __mpn_lshift (mptr
, mptr
, msize
, normalization_steps
);
159 /* If the shifting gave a carry out limb, store it and
160 increase the length. */
169 /* Divide the number at TP with BIG_BASE to get a quotient and a
170 remainder. The remainder is our new digit in base BIG_BASE. */
185 #if UDIV_TIME > 2 * UMUL_TIME
186 udiv_qrnnd_preinv (mptr
[i
], n1
, n1
, n0
, big_base
, big_base_inverted
);
188 udiv_qrnnd (mptr
[i
], n1
, n1
, n0
, big_base
);
192 #if UDIV_NEEDS_NORMALIZATION || UDIV_TIME > 2 * UMUL_TIME
193 /* If we shifted above (at previous UDIV_NEEDS_NORMALIZATION tests)
194 the remainder will be up-shifted here. Compensate. */
195 n1
>>= normalization_steps
;
198 /* Convert N1 from BIG_BASE to a string of digits in BASE
199 using single precision operations. */
200 for (i
= dig_per_u
- 1; i
>= 0; i
--)
204 if (n1
== 0 && msize
== 0)