186d7f5d3SJohn Marino /* mpn_sizeinbase -- approximation to chars required for an mpn.
286d7f5d3SJohn Marino
386d7f5d3SJohn Marino THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY. THEY'RE ALMOST
486d7f5d3SJohn Marino CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
586d7f5d3SJohn Marino FUTURE GNU MP RELEASES.
686d7f5d3SJohn Marino
786d7f5d3SJohn Marino Copyright 1991, 1993, 1994, 1995, 2001, 2002 Free Software Foundation, Inc.
886d7f5d3SJohn Marino
986d7f5d3SJohn Marino This file is part of the GNU MP Library.
1086d7f5d3SJohn Marino
1186d7f5d3SJohn Marino The GNU MP Library is free software; you can redistribute it and/or modify
1286d7f5d3SJohn Marino it under the terms of the GNU Lesser General Public License as published by
1386d7f5d3SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
1486d7f5d3SJohn Marino option) any later version.
1586d7f5d3SJohn Marino
1686d7f5d3SJohn Marino The GNU MP Library is distributed in the hope that it will be useful, but
1786d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1886d7f5d3SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
1986d7f5d3SJohn Marino License for more details.
2086d7f5d3SJohn Marino
2186d7f5d3SJohn Marino You should have received a copy of the GNU Lesser General Public License
2286d7f5d3SJohn Marino along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
2386d7f5d3SJohn Marino
2486d7f5d3SJohn Marino #include "gmp.h"
2586d7f5d3SJohn Marino #include "gmp-impl.h"
2686d7f5d3SJohn Marino #include "longlong.h"
2786d7f5d3SJohn Marino
2886d7f5d3SJohn Marino
2986d7f5d3SJohn Marino /* Same as mpz_sizeinbase, meaning exact for power-of-2 bases, and either
3086d7f5d3SJohn Marino exact or 1 too big for other bases. */
3186d7f5d3SJohn Marino
3286d7f5d3SJohn Marino size_t
mpn_sizeinbase(mp_srcptr xp,mp_size_t xsize,int base)3386d7f5d3SJohn Marino mpn_sizeinbase (mp_srcptr xp, mp_size_t xsize, int base)
3486d7f5d3SJohn Marino {
3586d7f5d3SJohn Marino int lb_base, cnt;
3686d7f5d3SJohn Marino mp_size_t totbits;
3786d7f5d3SJohn Marino
3886d7f5d3SJohn Marino ASSERT (xsize >= 0);
3986d7f5d3SJohn Marino ASSERT (base >= 2);
4086d7f5d3SJohn Marino ASSERT (base < numberof (mp_bases));
4186d7f5d3SJohn Marino
4286d7f5d3SJohn Marino /* Special case for X == 0. */
4386d7f5d3SJohn Marino if (xsize == 0)
4486d7f5d3SJohn Marino return 1;
4586d7f5d3SJohn Marino
4686d7f5d3SJohn Marino /* Calculate the total number of significant bits of X. */
4786d7f5d3SJohn Marino count_leading_zeros (cnt, xp[xsize-1]);
4886d7f5d3SJohn Marino totbits = xsize * GMP_LIMB_BITS - cnt;
4986d7f5d3SJohn Marino
5086d7f5d3SJohn Marino if (POW2_P (base))
5186d7f5d3SJohn Marino {
5286d7f5d3SJohn Marino /* Special case for powers of 2, giving exact result. */
5386d7f5d3SJohn Marino lb_base = mp_bases[base].big_base;
5486d7f5d3SJohn Marino return (totbits + lb_base - 1) / lb_base;
5586d7f5d3SJohn Marino }
5686d7f5d3SJohn Marino else
5786d7f5d3SJohn Marino return (size_t) (totbits * mp_bases[base].chars_per_bit_exactly) + 1;
5886d7f5d3SJohn Marino }
59