A more general bsr/bsf implementation
Johan Engelen via Digitalmars-d
digitalmars-d at puremagic.com
Sun Apr 12 04:53:39 PDT 2015
Currently, druntime only supports bsf/bsf, i.e. finding the first
or last bit set, on size_t [1]; bsf(size_t) and bsr(size_t) are
compiler intrinsics.
Because only size_t is supported, it is cumbersome to write
generic code using bsf/bsr. For example, for 64-bit code
bsr(long(...)) will work, but not for 32-bit code. bsr(cent(...))
will not work either.
To improve std.math.ilogb, I need a generic bsr. So I figured I
add that to druntime or Phobos.
I wrote more generic bsr/bsf but I stumbled across an unexpected
result:
Currently, bsr(byte(-1))) is equal to 31 instead of 7. My general
bsr honors the width of the input type, and returns 7 for this
example. The problem now of course is that replacing the old bsr
with my bsr would potentially break existing code (the existing
code may then already be broken when considering 32-bit and
64-bit mode).
My questions:
1) Is it OK to put a more general bsf/bsr in druntime or in
Phobos? (if Phobos: in which package to put it?)
2) Is the current sign-extend up to size_t's width really
intended behavior?
Thanks a bunch,
Johan
[1] http://dlang.org/phobos/core_bitop.html#.bsr
More information about the Digitalmars-d
mailing list