1 /* mpz_sizeinbase(x, base) -- return an approximation to the number of
2 character the integer X would have printed in base BASE. The
3 approximation is never too small.
5 Copyright (C) 1991 Free Software Foundation, Inc.
7 This file is part of the GNU MP Library.
9 The GNU MP Library is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 The GNU MP Library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with the GNU MP Library; see the file COPYING. If not, write to
21 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
29 mpz_sizeinbase (const MP_INT *x, int base)
31 mpz_sizeinbase (x, base)
36 mp_size size = ABS (x->size);
40 /* Special case for X == 0. */
44 /* Calculate the total number of significant bits of X. */
45 count_leading_zeros (cnt, x->d[size - 1]);
46 totbits = size * BITS_PER_MP_LIMB - cnt;
48 if ((base & (base - 1)) == 0)
50 /* Special case for powers of 2, giving exact result. */
52 count_leading_zeros (lb_base, base);
53 lb_base = BITS_PER_MP_LIMB - lb_base - 1;
55 return (totbits + lb_base - 1) / lb_base;
58 return (size_t) (totbits * __mp_bases[base].chars_per_bit_exactly) + 1;