janestreet / base

Standard library for OCaml
MIT License
848 stars 124 forks source link

int_math_stub.c should use BitScanReverse for clz (and BitScanForward for ctz) #130

Closed mlemerre closed 2 years ago

mlemerre commented 2 years ago

Hello,

While googling for implementing clz in OCaml I stepped on your code and on the Microsoft documentation for the clz/ctz functions equivalent:

https://docs.microsoft.com/en-us/cpp/intrinsics/bitscanreverse-bitscanreverse64?view=msvc-170

The documentation says for BitScanReverse "Search the mask data from most significant bit (MSB) to least significant bit (LSB) for a set bit (1)", so it seems for me that it corresponds to count leading zeroes.

Hence this issue so that you can check that.

Arguably BitScanReverse is not a very good name, so it is easy to get confused!

Bests Matthieu Lemerre

bcc32 commented 2 years ago

Thanks for reporting this! I've started a feature internally to switch the two functions around. This issue will be closed automatically once that gets pushed.