-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Branchless wide-integer comparisons and sign extension #109474
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
Merged
Algunenano
merged 7 commits into
ClickHouse:master
from
raimannma:wide-integer-branchless-comparison
Jul 7, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6f671cd
Use native __int128 comparison in wide::integer operator_less / opera…
raimannma c19cebd
Address review nits: comment placement in wide_integer_impl.h and per…
raimannma 9cb4bb0
Add correctness test for 128-bit comparison fast path
raimannma 2e51487
Merge branch 'master' into wide-integer-branchless-comparison
alexey-milovidov 7e5b16a
Replace the native __int128 fast path with a generic branchless limb …
raimannma 79a6d8b
Branchless sign extension in wide::integer conversions
raimannma 8d36d93
Cover 256-bit comparisons whose decisive limb is above bit 127
raimannma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| <test> | ||
| <!-- Comparison of 128-bit integer columns. Data shapes are chosen to exercise branch-prediction-hostile | ||
| patterns: fully random values (random sign for Int128), values that fit in the low 64 bits, | ||
| and values where half of the rows share the high 64 bits. --> | ||
|
|
||
| <create_query> | ||
| CREATE TABLE cmp128 | ||
| ( | ||
| u1 UInt128, u2 UInt128, | ||
| i1 Int128, i2 Int128, | ||
| small_u1 UInt128, small_u2 UInt128, | ||
| mixed_u1 UInt128, mixed_u2 UInt128 | ||
| ) ENGINE = Memory | ||
| </create_query> | ||
| <fill_query> | ||
| INSERT INTO cmp128 SELECT | ||
| bitShiftLeft(toUInt128(cityHash64(number, 1)), 64) + cityHash64(number, 2), | ||
| bitShiftLeft(toUInt128(cityHash64(number, 3)), 64) + cityHash64(number, 4), | ||
| toInt128(bitShiftLeft(toUInt128(cityHash64(number, 5)), 64) + cityHash64(number, 6)), | ||
| toInt128(bitShiftLeft(toUInt128(cityHash64(number, 7)), 64) + cityHash64(number, 8)), | ||
| toUInt128(cityHash64(number, 9)), | ||
| toUInt128(cityHash64(number, 10)), | ||
| bitShiftLeft(toUInt128(cityHash64(number, 11)), 64) + cityHash64(number, 12), | ||
| bitShiftLeft(toUInt128(cityHash64(number, if(number % 2 = 0, 11, 13))), 64) + cityHash64(number, 14) | ||
| FROM numbers(10000000) | ||
| </fill_query> | ||
|
|
||
| <settings> | ||
| <max_threads>1</max_threads> | ||
| </settings> | ||
|
|
||
| <query>SELECT count() FROM cmp128 WHERE u1 < u2</query> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @raimannma should performance tests for the <= and >= operators be added?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| <query>SELECT count() FROM cmp128 WHERE i1 < i2</query> | ||
| <query>SELECT count() FROM cmp128 WHERE small_u1 < small_u2</query> | ||
| <query>SELECT count() FROM cmp128 WHERE mixed_u1 < mixed_u2</query> | ||
| <query>SELECT count() FROM cmp128 WHERE u1 > u2</query> | ||
| <query>SELECT count() FROM cmp128 WHERE i1 >= i2</query> | ||
| <query>SELECT count() FROM cmp128 WHERE u1 < bitShiftLeft(toUInt128(1), 127)</query> | ||
| <query>SELECT i1 FROM cmp128 ORDER BY i1 LIMIT 10</query> | ||
| <query>SELECT u1 FROM cmp128 ORDER BY u1 DESC LIMIT 10</query> | ||
|
|
||
| <drop_query>DROP TABLE IF EXISTS cmp128</drop_query> | ||
| </test> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <test> | ||
| <!-- Conversion of native integers to 256-bit integers and widening of 128-bit to 256-bit integers. | ||
| Random 64-bit values give a random sign for the signed cases, which is hostile to a branchy | ||
| sign extension. The unsigned and 128-bit target queries guard the already fast paths. --> | ||
|
|
||
| <settings> | ||
| <max_threads>1</max_threads> | ||
| </settings> | ||
|
|
||
| <query>SELECT count() FROM numbers(50000000) WHERE NOT ignore(toInt256(toInt64(cityHash64(number))))</query> | ||
| <query>SELECT count() FROM numbers(50000000) WHERE NOT ignore(toInt256(toInt64(bitAnd(cityHash64(number), 4611686018427387903))))</query> | ||
| <query>SELECT count() FROM numbers(50000000) WHERE NOT ignore(toUInt256(cityHash64(number)))</query> | ||
| <query>SELECT count() FROM numbers(50000000) WHERE NOT ignore(toInt256(toInt128(toInt64(cityHash64(number)))))</query> | ||
| <query>SELECT count() FROM numbers(50000000) WHERE NOT ignore(toInt128(toInt64(cityHash64(number))))</query> | ||
| </test> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.