Open fsareshwala opened 10 months ago
Are you asking for a POPCOUNT function + a way to reinterpret_cast
a bits
to UInt
, or something that would actually analyze the bits
and only pay attention to known Flag
fields?
I was asking more for a std::popcount
type approach.
Adding $popcount
should be straightforward: a 1-adic function that takes an integer argument. Min value is 0, max value is ciel(log2(input max val)), +1 if the input can be negative. Would need some #if
s in the C++ implementation to use std::popcount()
or __builtin_popcount()
where available, and fallback to a C++11 native implementation otherwise.
Internally, a bits
field and a UInt
field (also Flag
, Int
, Float
, etc.) are treated similarly, so some equivalent of reinterpret_cast
that basically says "pretend this field is this other type and read it out" should be doable. We would have to figure out a readable syntax -- there aren't many languages that let you cast pointers that way, so $reinterpret_cast<UInt>(field)
might be the least-bad option.
Suppose you have a
bits
type like the following:A packet contains a bitset like the one above with the number of flags enabled determining the size of a variable length array.
The calculation of
num_features
can become quite a bit of toil. We have to physically write out the bits we wish to check. It would be easy to forget to add here if another needed to be checked in the future. It would be much easier to simply call out to a built in function which can do the counting for us.Something like this would be so much better: