mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-30 16:28:30 +00:00
Polyfill the 32-bit _bsr() function
This commit is contained in:
parent
c776a32f75
commit
ef6387ee5e
3 changed files with 47 additions and 68 deletions
|
@ -17,40 +17,7 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/intrin/bsr.h"
|
||||
|
||||
static const char kDebruijn[64] = {
|
||||
0, 47, 1, 56, 48, 27, 2, 60, 57, 49, 41, 37, 28, 16, 3, 61,
|
||||
54, 58, 35, 52, 50, 42, 21, 44, 38, 32, 29, 23, 17, 11, 4, 62,
|
||||
46, 55, 26, 59, 40, 36, 15, 53, 34, 51, 20, 43, 31, 22, 10, 45,
|
||||
25, 39, 14, 33, 19, 30, 9, 24, 13, 18, 8, 12, 7, 6, 5, 63,
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns binary logarithm of 𝑥.
|
||||
*
|
||||
* ctz(𝑥) 31^clz(𝑥) clz(𝑥)
|
||||
* uint32 𝑥 _bsf(𝑥) tzcnt(𝑥) ffs(𝑥) _bsr(𝑥) lzcnt(𝑥)
|
||||
* 0x00000000 wut 32 0 wut 32
|
||||
* 0x00000001 0 0 1 0 31
|
||||
* 0x80000001 0 0 1 31 0
|
||||
* 0x80000000 31 31 32 31 0
|
||||
* 0x00000010 4 4 5 4 27
|
||||
* 0x08000010 4 4 5 27 4
|
||||
* 0x08000000 27 27 28 27 4
|
||||
* 0xffffffff 0 0 1 31 0
|
||||
*
|
||||
* @param x is a 64-bit integer
|
||||
* @return number in range 0..63 or undefined if 𝑥 is 0
|
||||
*/
|
||||
int(_bsrl)(long x) {
|
||||
x |= x >> 1;
|
||||
x |= x >> 2;
|
||||
x |= x >> 4;
|
||||
x |= x >> 8;
|
||||
x |= x >> 16;
|
||||
x |= x >> 32;
|
||||
return kDebruijn[(x * 0x03f79d71b4cb0a89) >> 58];
|
||||
}
|
||||
// clang-format off
|
||||
|
||||
/**
|
||||
* Returns binary logarithm of 𝑥.
|
||||
|
@ -70,7 +37,11 @@ int(_bsrl)(long x) {
|
|||
* @return number in range 0..31 or undefined if 𝑥 is 0
|
||||
*/
|
||||
int(_bsr)(int x) {
|
||||
return _bsrl((unsigned)x);
|
||||
int r = 0;
|
||||
if(x & 0xFFFF0000u) { x >>= 16; r |= 16; }
|
||||
if(x & 0xFF00) { x >>= 8; r |= 8; }
|
||||
if(x & 0xF0) { x >>= 4; r |= 4; }
|
||||
if(x & 0xC) { x >>= 2; r |= 2; }
|
||||
if(x & 0x2) { r |= 1; }
|
||||
return r;
|
||||
}
|
||||
|
||||
__weak_reference(_bsrl, _bsrll);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue