mpn_set_str_bits

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


Ciao,

Il 2020-09-30 19:37 nisse at lysator.liu.se ha scritto:
> Marco Bodrato <bodrato at mail.dm.unipi.it> writes:
> 
>>   limb = (unsigned int) sp[sn] >> (bits - shift);
> 
> That's easier to read than what I proposed.

> Maybe worth a comment mentioning the problem case: mp_limb_t

Thanks Niels!

So, here is the current proposed rewrite:

static mp_size_t
mpn_set_str_bits (mp_ptr rp, const unsigned char *sp, size_t sn,
		  unsigned bits)
{
   mp_size_t rn;
   mp_limb_t limb;
   unsigned shift;

   for (limb = 0, rn = 0, shift = 0; sn-- > 0; )
     {
       limb |= (mp_limb_t) sp[sn] << shift;
       shift += bits;
       if (shift >= GMP_LIMB_BITS)
         {
           shift -= GMP_LIMB_BITS;
           rp[rn++] = limb;
           /* Next line is correct also if shift == 0,
              bits == 8, and mp_limb_t == unsigned char. */
           limb = (unsigned int) sp[sn] >> (bits - shift);
         }
     }
   if (limb != 0)
     rp[rn++] = limb;
   else
     rn = mpn_normalized_size (rp, rn);
   return rn;
}

It seems simple enough. Any further comment?

Ĝis,
m


More information about the gmp-devel mailing list