Skip to content

Commit

Permalink
optimize highestOne256 (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
Trinidadec authored Oct 27, 2023
1 parent 4fe48c1 commit ccf8cdc
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions subtree.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,20 @@ const nodeHeight = 2

/** @type {(bu256: bigint) => bigint} */
const highestOne256 = bu256 => {
let result = 0n
let index = 256n
while (bu256 !== 0n) {
index >>= 1n
if (index === 0n) {
return result + 1n
}
if (bu256 === 0n) { return 0n }
let result = 1n
let index = 128n
while (true) {
const high = bu256 >> index
if (high === 0n) {
bu256 &= (1n << index) - 1n
} else {
bu256 = high
result += index
}
if (index === 1n) { return result }
index >>= 1n
}
return result
}

/** @type {(bu256: bigint) => bigint} */
Expand Down

0 comments on commit ccf8cdc

Please sign in to comment.