-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Replace VarInt.getLength with Protobuf's branchless implementation #36959
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
8a82c18
07b1ee1
f859813
76fc879
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -136,16 +136,19 @@ public static long decodeLong(InputStream stream) throws IOException { | |
|
|
||
| /** Returns the length of the encoding of the given value (in bytes). */ | ||
| public static int getLength(int v) { | ||
| return getLength(convertIntToLongNoSignExtend(v)); | ||
| // log2(v) / 7 + 1 rewritten as multiplication by 9/64 instead of a division by 7. | ||
| // Log2 is performed using a bit counting instruction. | ||
| // Multiplication by 9 is performed using a 3-bit left shift and add. | ||
| // Division by 64 is performed using a 6-bit right shift. | ||
| return ((Integer.SIZE * 9 + (1 << 6)) - (Integer.numberOfLeadingZeros(v) * 9)) >>> 6; | ||
|
||
| } | ||
|
|
||
| /** Returns the length of the encoding of the given value (in bytes). */ | ||
| public static int getLength(long v) { | ||
| int result = 0; | ||
| do { | ||
| result++; | ||
| v >>>= 7; | ||
| } while (v != 0); | ||
| return result; | ||
| // log2(v) / 7 + 1 rewritten as multiplication by 9/64 instead of a division by 7. | ||
| // Log2 is performed using a bit counting instruction. | ||
| // Multiplication by 9 is performed using a 3-bit left shift and add. | ||
| // Division by 64 is performed using a 6-bit right shift. | ||
| return ((Long.SIZE * 9 + (1 << 6)) - (Long.numberOfLeadingZeros(v) * 9)) >>> 6; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.