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_read_unsigned_bin.c Source File

fp_read_unsigned_bin.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 void fp_read_unsigned_bin(fp_int *a, unsigned char *b, int c)
00013 {
00014   /* zero the int */
00015   fp_zero (a);
00016 
00017   /* If we know the endianness of this architecture, and we're using
00018      32-bit fp_digits, we can optimize this */
00019 #if (defined(ENDIAN_LITTLE) || defined(ENDIAN_BIG)) && !defined(FP_64BIT)
00020   /* But not for both simultaneously */
00021 #if defined(ENDIAN_LITTLE) && defined(ENDIAN_BIG)
00022 #error Both ENDIAN_LITTLE and ENDIAN_BIG defined.
00023 #endif
00024   {
00025      unsigned char *pd = (unsigned char *)a->dp;
00026 
00027      if ((unsigned)c > (FP_SIZE * sizeof(fp_digit))) {
00028         int excess = c - (FP_SIZE * sizeof(fp_digit));
00029         c -= excess;
00030         b += excess;
00031      }
00032      a->used = (c + sizeof(fp_digit) - 1)/sizeof(fp_digit);
00033      /* read the bytes in */
00034 #ifdef ENDIAN_BIG
00035      {
00036        /* Use Duff's device to unroll the loop. */
00037        int idx = (c - 1) & ~3;
00038        switch (c % 4) {
00039        case 0:  do { pd[idx+0] = *b++;
00040        case 3:       pd[idx+1] = *b++;
00041        case 2:       pd[idx+2] = *b++;
00042        case 1:       pd[idx+3] = *b++;
00043                      idx -= 4;
00044                 } while ((c -= 4) > 0);
00045        }
00046      }
00047 #else
00048      for (c -= 1; c >= 0; c -= 1) {
00049        pd[c] = *b++;
00050      }
00051 #endif
00052   }
00053 #else
00054   /* read the bytes in */
00055   for (; c > 0; c--) {
00056      fp_mul_2d (a, 8, a);
00057      a->dp[0] |= *b++;
00058      a->used += 1;
00059   }
00060 #endif
00061   fp_clamp (a);
00062 }
00063 
00064 /* $Source: /cvs/libtom/tomsfastmath/src/bin/fp_read_unsigned_bin.c,v $ */
00065 /* $Revision: 1.2 $ */
00066 /* $Date: 2007/02/17 02:58:19 $ */