Skip to content
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

Add Net.*Addr#is_cidr method #68

Open
1 of 2 tasks
rye opened this issue Oct 20, 2019 · 3 comments
Open
1 of 2 tasks

Add Net.*Addr#is_cidr method #68

rye opened this issue Oct 20, 2019 · 3 comments
Assignees

Comments

@rye
Copy link
Owner

rye commented Oct 20, 2019

@rye rye self-assigned this Oct 20, 2019
@rye
Copy link
Owner Author

rye commented Nov 18, 2019

It turns out that I'm doing this already, who knew?

The basic algorithm: take the mask as a u32, count the ones in the mask (up to 32), then let cidr_mask = u32::max_value().checked_shl(32 - ones).unwrap_or(0). If cidr_mask == mask, then the mask is a standard CIDR mask and you're done.

The problem is that the time to finish the check here is the same regardless of whether or not the mask is CIDR; ideally I'd like CIDR masks to take a lot less time to return a result than non-CIDR masks, if that's possible.

@rye
Copy link
Owner Author

rye commented Nov 18, 2019

Looking deeper into what count_ones actually does, it calls the llvm.ctpop intrinsic—as a result, I'm pretty sure it's target-specific as to what the performance characteristics look like.

Regardless, probably safe to just use that in an impl, and if a target architecture somehow includes bit-counting, then we get that speed. It looks like -C target-cpu=native might optimize it down to popcnt, but by default it's just a shift-add chain.

Remember to use the result in the Display traits and anywhere else where CIDR checking is necessary.

@rye
Copy link
Owner Author

rye commented Feb 26, 2020

Triage: still not done. Re-titling and adding some information to the OP.

@rye rye changed the title Add methods to determine if a netaddr is cidr Add Net.*Addr#is_cidr method Feb 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant