Doing fast leftshift

Décio Luiz Gazzoni Filho decio at decpp.net
Fri Nov 11 17:22:34 CET 2005


On Nov 11, 2005, at 12:47 PM, stiank at ii.uib.no wrote:

>
> Hi. I'm working on a very time-critical application witch includes  
> calculations
> with big integers (128bits, unsigned). Using GMP boosted the speed  
> alot, so I
> were really happy when I discovered it!
>
> However; I have a problem. I'm not able to do shifting of numbers  
> in a efficient
> way. As I couldn't find any methods for doing leftshift directly  
> (including
> wrapping around the edge) I created my own function for doing this  
> – using
> GMP-functions.
>
> It goes like this :
>
> //shifting number given # positions and putting the result into  
> result.
> void leftShift(mpz_t result, mpz_t number, int positions){
>   //The shifting:
>   mpz_mul_2exp(result, number, positions);
>   //And the wrapping:
>   for(int i=0;i<positions;i++){
>     if(mpz_tstbit(result, 128+i){
>       mpz_clrbit(result, 128+i);
>       mpz_setbit(result, i);
>     }
>   }
> }
>
> Any ideas about what's not good here? Are there any functions witch  
> does this
> directly witch I've missed?

http://www.swox.com/gmp/manual/Integer-Arithmetic.html#Integer% 
20Arithmetic

void mpz_mul_2exp (mpz_t rop, mpz_t op1, unsigned long int op2)
Set rop to op1 times 2 raised to op2. This operation can also be  
defined as a left shift by op2 bits.

Décio
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 186 bytes
Desc: This is a digitally signed message part
Url : http://gmplib.org/list-archives/gmp-discuss/attachments/20051111/2309b24d/PGP.bin


More information about the gmp-discuss mailing list