comparison tomsfastmath/src/bin/fp_radix_size.c @ 643:a362b62d38b2 dropbear-tfm

Add tomsfastmath from git rev bfa4582842bc3bab42e4be4aed5703437049502a with Makefile.in renamed
author Matt Johnston <matt@ucc.asn.au>
date Wed, 23 Nov 2011 18:10:20 +0700
parents
children
comparison
equal deleted inserted replaced
642:33fd2f3499d2 643:a362b62d38b2
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, [email protected]
9 */
10 #include <tfm.h>
11
12 int fp_radix_size(fp_int *a, int radix, int *size)
13 {
14 int digs;
15 fp_int t;
16 fp_digit d;
17
18 *size = 0;
19
20 /* check range of the radix */
21 if (radix < 2 || radix > 64) {
22 return FP_VAL;
23 }
24
25 /* quick out if its zero */
26 if (fp_iszero(a) == 1) {
27 *size = 2;
28 return FP_OKAY;
29 }
30
31 fp_init_copy(&t, a);
32
33 /* if it is negative output a - */
34 if (t.sign == FP_NEG) {
35 (*size)++;
36 t.sign = FP_ZPOS;
37 }
38
39 digs = 0;
40 while (fp_iszero (&t) == FP_NO) {
41 fp_div_d (&t, (fp_digit) radix, &t, &d);
42 (*size)++;
43 }
44
45 /* append a NULL so the string is properly terminated */
46 (*size)++;
47 return FP_OKAY;
48
49 }
50
51 /* $Source$ */
52 /* $Revision$ */
53 /* $Date$ */