-
Notifications
You must be signed in to change notification settings - Fork 473
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
[WIP] Fast isValidCell #496
Draft
ajfriend
wants to merge
24
commits into
uber:master
Choose a base branch
from
ajfriend:fast_isValidCell
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
196b271
initial commit
ajfriend 5125e7b
use NB_
ajfriend aa0e572
m7s
ajfriend 744c023
benchmarks running
ajfriend ea83ff9
actual ones running
ajfriend 1b63946
just do the pentagon
ajfriend 0ca2c28
two tests
ajfriend e898d63
science!
ajfriend 983cb3d
docs
ajfriend 5bcc2ee
cleaning up
ajfriend 3e912d8
fix benchmark bug
ajfriend bef505f
three implementations
ajfriend 63962ed
comments for benchmarking
ajfriend 0f18b18
Merge branch 'master' into fast_isValidCell_try_merge
ajfriend c7a32f2
moving stuff around
ajfriend c22fa9f
checking that the end is all 7's
ajfriend 47b6584
wicked fast method
ajfriend 2afecd6
good times
ajfriend dde10bb
shifting around shifts
ajfriend 9edb39a
just make the 0 test explicit
ajfriend 5e18135
tables for the fancy bitwise ops
ajfriend 5d68649
Adding some notes
ajfriend 65a5e89
bad news bits
ajfriend 5a70747
better tests, better macros, better comments
ajfriend 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 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 |
---|---|---|
|
@@ -344,18 +344,31 @@ int _isValidCell_const(const H3Index h) { | |
// Pentagon cells start with a sequence of 0's (CENTER_DIGIT's). | ||
// The first nonzero digit can't be a 1 (i.e., "deleted subsequence", | ||
// PENTAGON_SKIPPED_DIGIT, or K_AXES_DIGIT). | ||
// Test for pentagon base cell first to avoid this loop if possible. | ||
if (isBaseCellPentagonArr[bc]) { | ||
H3Index g = h << 19; | ||
H3Index g = h; | ||
g <<= 19; | ||
g >>= 19; // at this point, g < 2^45 - 1 | ||
|
||
for (int r = 1; r <= res; r++) { | ||
uint64_t d = GT(g, 3); | ||
g <<= 3; | ||
if (g == 0) return true; // all zeros (res 15 pentagon) | ||
|
||
if (d == 0) continue; | ||
if (d == 1) return false; | ||
if (d >= 2) break; | ||
} | ||
// Converting g to a double essentially puts log2(g) into the | ||
// exponent bits of f (with an offset). | ||
double f = g; | ||
|
||
// reinterpret the double bits as uint64_t | ||
g = *(uint64_t *)&f; | ||
Comment on lines
+360
to
+361
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. I'm unclear if this is safe - it may be necessary to use a different implementation on a platform like ARM (we can probably test this on Raspberry Pi). We should check if UBSan complains about this. |
||
|
||
// Take the 11 exponent bits and the 1 sign bit. | ||
// The sign bit is guaranteed to be 0 in our case. | ||
g = GT(g, 12); | ||
|
||
// Subtract the exponent bias. | ||
g -= 1023; | ||
|
||
// g now holds the index of (its previous) first nonzero bit. | ||
// The first nonzero digit is a 1 (and thus invalid) if the | ||
// first nonzero bit's position is divisible by 3. | ||
if (g % 3 == 0) return false; | ||
} | ||
|
||
// If no flaws were identified above, then the index is a valid H3 cell. | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not replace this with a simple
&
mask that has0
s for the top 19 bits and1
s for the lower 45 bits? That should be much faster than shifting up and down 19 times each.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, agreed!