Skip to content

Commit 85017c4

Browse files
committed
Shortened count leading ones logic
1 parent 1888cae commit 85017c4

File tree

2 files changed

+2
-7
lines changed

2 files changed

+2
-7
lines changed

dist/lib/flex-int.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,7 @@ exports.makeValueBuffer = makeValueBuffer;
7474
* @return The length of the `flexInt`
7575
*/
7676
function getByteCount(firstByte) {
77-
assert_1.default.byteUnsignedInteger(firstByte);
78-
let leadingOnes;
79-
for (leadingOnes = 0; firstByte & (1 << 7); leadingOnes++)
80-
firstByte <<= 1;
77+
const leadingOnes = Math.clz32(~firstByte << 24);
8178
const bytes = NUMBER_OF_BYTES.get(leadingOnes);
8279
assert_1.default(bytes !== undefined, 'Invalid number of bytes');
8380
return bytes;

lib/flex-int.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ export function makeValueBuffer(value: number): ArrayBuffer {
7171
* @return The length of the `flexInt`
7272
*/
7373
export function getByteCount(firstByte: number): number {
74-
assert.byteUnsignedInteger(firstByte)
75-
let leadingOnes: number
76-
for (leadingOnes = 0; firstByte & (1 << 7); leadingOnes++) firstByte <<= 1
74+
const leadingOnes = Math.clz32(~firstByte << 24)
7775
const bytes = NUMBER_OF_BYTES.get(leadingOnes)
7876
assert(bytes !== undefined, 'Invalid number of bytes')
7977
return bytes!

0 commit comments

Comments
 (0)