request for bit-level manipulation

Torbjorn Granlund tg at gmplib.org
Sat Nov 14 19:42:58 CET 2009


luc bruant <non.deterministic.finite.organism at gmail.com> writes:

   * set/reset/flip a single bit (I can do that with some assumptions on
  internal data representation but that might be screwed by nails or
  endianness so it's not satisfying)

These exist at the mpz level.

   * insert/remove a signle bit (I can do that with ugly combinations of
  shifts and bitmasking but with the same drawbacks as above and an even worse
  performance
  overhead...)
  
Thse cannot be done efficiently.

  So I would be extremely grateful if such bit-level manipulation was added to
  the low-level interface of GMP in a future release.
  
At the mpn level, I think you should do these yourself using the
existing primitives.  For setting and clearing bits, you need to check
GMP_NAIL_BITS if you worry it might have been enabled.  But it will be
oneliners anyway.

  Another function that would prove useful in my use case (and possibly in a
  lot of others) : "reverse" bit scan. mpn_scan0 and mpn_scan1 start from the
  least significant
  bits whereas I would need the opposite traversal and am forced to emulate it
  using a loop which induces size and time overhead.
  
Again, this is a sort of manipulation that you should do yourself, since
it is too simple to warrant mpn primtives.

This is perhaps not the answer you hoped for, but once at the mpn level,
you should not expect to have a compete set of functions.  GMP provides
time critical functions only, plus some extremely common simpler
functions.

-- 
Torbjörn


More information about the gmp-discuss mailing list