Open rye opened 4 years ago
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.
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.
Triage: still not done. Re-titling and adding some information to the OP.
Display
trait to give/
-notation./
is<max> - count_ones
.