Ciao,

Il 2020-09-30 19:37 ni...@lysator.liu.se ha scritto:
Marco Bodrato <bodr...@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
_______________________________________________
gmp-devel mailing list
gmp-devel@gmplib.org
https://gmplib.org/mailman/listinfo/gmp-devel

Reply via email to