bn_mp_div_2.c

Go to the documentation of this file.
00001 #include <tommath.h>
00002 #ifdef BN_MP_DIV_2_C
00003 /* LibTomMath, multiple-precision integer library -- Tom St Denis
00004  *
00005  * LibTomMath is a library that provides multiple-precision
00006  * integer arithmetic as well as number theoretic functionality.
00007  *
00008  * The library was designed directly after the MPI library by
00009  * Michael Fromberger but has been written from scratch with
00010  * additional optimizations in place.
00011  *
00012  * The library is free for all purposes without any express
00013  * guarantee it works.
00014  *
00015  * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
00016  */
00017 
00018 /* b = a/2 */
00019 int mp_div_2(mp_int * a, mp_int * b)
00020 {
00021   int     x, res, oldused;
00022 
00023   /* copy */
00024   if (b->alloc < a->used) {
00025     if ((res = mp_grow (b, a->used)) != MP_OKAY) {
00026       return res;
00027     }
00028   }
00029 
00030   oldused = b->used;
00031   b->used = a->used;
00032   {
00033     register mp_digit r, rr, *tmpa, *tmpb;
00034 
00035     /* source alias */
00036     tmpa = a->dp + b->used - 1;
00037 
00038     /* dest alias */
00039     tmpb = b->dp + b->used - 1;
00040 
00041     /* carry */
00042     r = 0;
00043     for (x = b->used - 1; x >= 0; x--) {
00044       /* get the carry for the next iteration */
00045       rr = *tmpa & 1;
00046 
00047       /* shift the current digit, add in carry and store */
00048       *tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1));
00049 
00050       /* forward carry to next iteration */
00051       r = rr;
00052     }
00053 
00054     /* zero excess digits */
00055     tmpb = b->dp + b->used;
00056     for (x = b->used; x < oldused; x++) {
00057       *tmpb++ = 0;
00058     }
00059   }
00060   b->sign = a->sign;
00061   mp_clamp (b);
00062   return MP_OKAY;
00063 }
00064 #endif
00065 
00066 /* $Source: /cvsroot/tcl/libtommath/bn_mp_div_2.c,v $ */
00067 /* $Revision: 1.1.1.3 $ */
00068 /* $Date: 2006/12/01 00:08:11 $ */



Generated on Wed Mar 12 12:18:24 2008 by  doxygen 1.5.1