1 /* mpn_sizeinbase -- approximation to chars required for an mpn. 2 3 THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY. THEY'RE ALMOST 4 CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN 5 FUTURE GNU MP RELEASES. 6 7 Copyright 1991, 1993, 1994, 1995, 2001, 2002 Free Software Foundation, Inc. 8 9 This file is part of the GNU MP Library. 10 11 The GNU MP Library is free software; you can redistribute it and/or modify 12 it under the terms of the GNU Lesser General Public License as published by 13 the Free Software Foundation; either version 3 of the License, or (at your 14 option) any later version. 15 16 The GNU MP Library is distributed in the hope that it will be useful, but 17 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 18 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 19 License for more details. 20 21 You should have received a copy of the GNU Lesser General Public License 22 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */ 23 24 #include "gmp.h" 25 #include "gmp-impl.h" 26 #include "longlong.h" 27 28 29 /* Same as mpz_sizeinbase, meaning exact for power-of-2 bases, and either 30 exact or 1 too big for other bases. */ 31 32 size_t 33 mpn_sizeinbase (mp_srcptr xp, mp_size_t xsize, int base) 34 { 35 int lb_base, cnt; 36 mp_size_t totbits; 37 38 ASSERT (xsize >= 0); 39 ASSERT (base >= 2); 40 ASSERT (base < numberof (__mp_bases)); 41 42 /* Special case for X == 0. */ 43 if (xsize == 0) 44 return 1; 45 46 /* Calculate the total number of significant bits of X. */ 47 count_leading_zeros (cnt, xp[xsize-1]); 48 totbits = xsize * BITS_PER_MP_LIMB - cnt; 49 50 if (POW2_P (base)) 51 { 52 /* Special case for powers of 2, giving exact result. */ 53 lb_base = __mp_bases[base].big_base; 54 return (totbits + lb_base - 1) / lb_base; 55 } 56 else 57 return (size_t) (totbits * __mp_bases[base].chars_per_bit_exactly) + 1; 58 } 59