Missed off <ctype.h>\!
[u/mdw/catacomb] / mptext-len.c
CommitLineData
0cbfe12e 1/* -*-c-*-
2 *
3 * $Id: mptext-len.c,v 1.1 2002/10/15 22:58:29 mdw Exp $
4 *
5 * Work out length of a number's string representation
6 *
7 * (c) 2002 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: mptext-len.c,v $
33 * Revision 1.1 2002/10/15 22:58:29 mdw
34 * Fast estimation of number representation lengths.
35 *
36 */
37
38/*----- Header files ------------------------------------------------------*/
39
40#include "mp.h"
41#include "mptext.h"
42
43/*----- Main code ---------------------------------------------------------*/
44
45/* --- @mptext_len@ --- *
46 *
47 * Arguments: @mp *x@ = number to work on
48 * @int r@ = radix the number will be expressed in
49 *
50 * Returns: The number of digits needed to represent the number in the
51 * given base. This will not include space for a leading sign
52 * (use @MP_ISNEG@ to check that, or just add one on for luck);
53 * neither will it add space for a terminating null. In general
54 * the answer will be an overestimate.
55 */
56
57size_t mptext_len(mp *x, int r)
58{
59 unsigned long b = mp_bits(x);
60 int s, ss = 2;
61 size_t n;
62 unsigned d = 0;
63
64 /* --- Huh? --- *
65 *
66 * The number of digits is at most %$\lceil b \log 2/\log r \rceil$%. We
67 * produce an underestimate of %$\log_2 r = \log r/\log 2$% and divide by
68 * that. How? By linear interpolation between known points on the curve.
69 * The known points are precisely the powers of 2, so we can find a pair
70 * efficiently by doubling up. The log curve is convex, so linear
71 * interpolation between points on the curve is always an underestimate.
72 *
73 * The integer maths here is a bit weird, so here's how it works. If
74 * %$s = 2^d$% is the power of 2 below %$r$% then we want to compute
75 * %$\lceil b/(d + (r - s)/s) \rceil = \lceil (b s)/(s(d - 1) + r \rceil$%
76 * which is %$\lfloor (r + s (b + d - 1) - 1)/(r + s(d - 1)) \rfloor$%.
77 * Gluing the whole computation together like this makes the code hard to
78 * read, but means that there are fewer possibilities for rounding errors
79 * and thus we get a tighter bound.
80 */
81
82 /* --- Find the right pair of points --- */
83
84 do {
85 s = ss;
86 d++;
87 if (r == s) {
88 n = (b + (d - 1))/d;
89 goto done;
90 }
91 ss = s << 1;
92 } while (ss <= r);
93
94 /* --- Do the interpolation --- */
95
96 n = (r + s*(b + d - 1) - 1)/(r + s*(d - 1));
97
98 /* --- Fixups --- */
99
100done:
101 if (!n)
102 n = 1;
103 return (n);
104}
105
106/*----- That's all, folks -------------------------------------------------*/