mpn_set_str_bits

Marco Bodrato bodrato at mail.dm.unipi.it
Wed Sep 30 16:47:26 UTC 2020


Ciao!

Il 2020-09-30 09:03 nisse at lysator.liu.se ha scritto:
> Marco Bodrato <bodrato at mail.dm.unipi.it> writes:
>> 	  limb = sp[sn];
>> 	  if (GMP_LIMB_BITS > CHAR_BIT || shift > 0)
>> 	    limb >>= bits - shift;
>> 	  else
>> 	    limb = 0;

> Do we really need to support bits == GMP_LIMB_BITS here? If not, the

About mpn_set_str, the manual reads "base can vary from 2 to 256". 
Moreover we are fool enough to (unofficially) support "typedef unsigned 
char mp_limb_t;" in mini-gmp...

> above 5 lines could be simplified to just

>   limb = (sp[sn] >> 1) >> (bits - 1 - shift);
> 
> it should be safe in all cases.

I agree.

Anyway, there may be a problem only if we shift sp[sn] (unsigned char) 
or limb (possibly the same type) by 8 bits (if bits=8 and shift=0).

An even simpler solution could be to cast to unsigned int (at least 16 
bits, right?) so:

   limb = (unsigned int) sp[sn] >> (bits - shift);

Maybe the cast is unnecessary, because the C standard says that the left 
operand of a shift operation is always promoted to (unsigned) int if it 
is smaller, correct? But it shouldn't be dangerous.

Ĝis,
m


More information about the gmp-devel mailing list