ARM Shanghai IoT Team (Internal) / newMiniTLS-GPL

Fork of MiniTLS-GPL by Donatien Garnier

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers fp_radix_size.c Source File

fp_radix_size.c

00001 /* TomsFastMath, a fast ISO C bignum library.
00002  * 
00003  * This project is meant to fill in where LibTomMath
00004  * falls short.  That is speed ;-)
00005  *
00006  * This project is public domain and free for all purposes.
00007  * 
00008  * Tom St Denis, tomstdenis@gmail.com
00009  */
00010 #include <tfm.h>
00011 
00012 int fp_radix_size(fp_int *a, int radix, int *size)
00013 {
00014   int     digs;
00015   fp_int  t;
00016   fp_digit d;
00017    
00018   *size = 0;
00019 
00020   /* check range of the radix */
00021   if (radix < 2 || radix > 64) {
00022     return FP_VAL;
00023   }
00024 
00025   /* quick out if its zero */
00026   if (fp_iszero(a) == 1) {
00027      *size = 2;
00028      return FP_OKAY;
00029   }
00030 
00031   fp_init_copy(&t, a);
00032 
00033   /* if it is negative output a - */
00034   if (t.sign == FP_NEG) {
00035     (*size)++;
00036     t.sign = FP_ZPOS;
00037   }
00038 
00039   digs = 0;
00040   while (fp_iszero (&t) == FP_NO) {
00041     fp_div_d (&t, (fp_digit) radix, &t, &d);
00042     (*size)++;
00043   }
00044 
00045   /* append a NULL so the string is properly terminated */
00046   (*size)++;
00047   return FP_OKAY;
00048 
00049 }
00050 
00051 /* $Source: /cvs/libtom/tomsfastmath/src/bin/fp_radix_size.c,v $ */
00052 /* $Revision: 1.1 $ */
00053 /* $Date: 2006/12/31 21:25:53 $ */