(n: bigint)
| 45 | |
| 46 | /** Bit length of |n| (the number of bits in its binary representation). */ |
| 47 | export function bitLength(n: bigint): number { |
| 48 | if (n < 0n) n = -n; |
| 49 | if (n === 0n) return 0; |
| 50 | let bits = 0; |
| 51 | // Doubling search to bracket the bit length. |
| 52 | let high = 1; |
| 53 | while (n >> BigInt(high) > 0n) high *= 2; |
| 54 | // Binary search within [0, high]. |
| 55 | for (let shift = high >> 1; shift >= 1; shift >>= 1) { |
| 56 | if (n >> BigInt(shift) > 0n) { |
| 57 | bits += shift; |
| 58 | n >>= BigInt(shift); |
| 59 | } |
| 60 | } |
| 61 | return bits + 1; |
| 62 | } |
| 63 | |
| 64 | /** Fixed-point multiply on the base-2 grid: (a * b) >> bits */ |
| 65 | export function fpmul(a: bigint, b: bigint, bits: number): bigint { |
no outgoing calls
no test coverage detected