]> pd.if.org Git - zpackage/blob - tomsfastmath/src/bin/fp_radix_size.c
commit files needed for zpm-fetchurl
[zpackage] / tomsfastmath / src / bin / fp_radix_size.c
1 /* TomsFastMath, a fast ISO C bignum library.
2  *
3  * This project is meant to fill in where LibTomMath
4  * falls short.  That is speed ;-)
5  *
6  * This project is public domain and free for all purposes.
7  *
8  * Tom St Denis, tomstdenis@gmail.com
9  */
10 #include <tfm_private.h>
11
12 int fp_radix_size(fp_int *a, int radix, int *size)
13 {
14   fp_int  t;
15   fp_digit d;
16
17   *size = 0;
18
19   /* check range of the radix */
20   if (radix < 2 || radix > 64) {
21     return FP_VAL;
22   }
23
24   /* quick out if its zero */
25   if (fp_iszero(a) == 1) {
26      *size = 2;
27      return FP_OKAY;
28   }
29
30   fp_init_copy(&t, a);
31
32   /* if it is negative output a - */
33   if (t.sign == FP_NEG) {
34     (*size)++;
35     t.sign = FP_ZPOS;
36   }
37
38   while (fp_iszero (&t) == FP_NO) {
39     fp_div_d (&t, (fp_digit) radix, &t, &d);
40     (*size)++;
41   }
42
43   /* append a NULL so the string is properly terminated */
44   (*size)++;
45   return FP_OKAY;
46
47 }
48
49 /* $Source$ */
50 /* $Revision$ */
51 /* $Date$ */