Mercurial > dropbear
comparison tomsfastmath/src/bin/fp_toradix.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_toradix(fp_int *a, char *str, int radix) | |
13 { | |
14 int digs; | |
15 fp_int t; | |
16 fp_digit d; | |
17 char *_s = str; | |
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 *str++ = '0'; | |
27 *str = '\0'; | |
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 ++_s; | |
36 *str++ = '-'; | |
37 t.sign = FP_ZPOS; | |
38 } | |
39 | |
40 digs = 0; | |
41 while (fp_iszero (&t) == FP_NO) { | |
42 fp_div_d (&t, (fp_digit) radix, &t, &d); | |
43 *str++ = fp_s_rmap[d]; | |
44 ++digs; | |
45 } | |
46 | |
47 /* reverse the digits of the string. In this case _s points | |
48 * to the first digit [exluding the sign] of the number] | |
49 */ | |
50 fp_reverse ((unsigned char *)_s, digs); | |
51 | |
52 /* append a NULL so the string is properly terminated */ | |
53 *str = '\0'; | |
54 return FP_OKAY; | |
55 } | |
56 | |
57 /* $Source$ */ | |
58 /* $Revision$ */ | |
59 /* $Date$ */ |