mpn_set_str_bits

Marco Bodrato bodrato at mail.dm.unipi.it
Tue Sep 29 18:49:55 UTC 2020


Ciao,

Il 2020-09-29 12:15 Raphael Rieu-Helft ha scritto:
> The attached patch slightly improves the mini-gmp function
> mpn_set_str_bits. The invariants are also a bit clearer (shift is the

The loop in mpz_import uses another strategy, a temporary limb.
This reduces the number of write operations into memory.

May I propose an alternative rewrite?
What do you think about it?

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)
	{
	  rp[rn++] = limb;
	  shift -= GMP_LIMB_BITS;
	  limb = sp[sn];
	  if (GMP_LIMB_BITS > CHAR_BIT || shift > 0)
	    limb >>= bits - shift;
	  else
	    limb = 0;
	}
     }
   if (limb != 0)
     rp[rn++] = limb;
   else
     rn = mpn_normalized_size (rp, rn);
   return rn;
}

Ĝis,
m


More information about the gmp-devel mailing list