Closed dvdhrm closed 5 years ago
I thought about using
std::mem::size_of()
, but that does not sound right, as it includes possible state-tracking data.
That would be fine for the actual primitive integers, but I suppose generically you're right that an arbitrary PrimInt
could have other data.
For generic T: PrimInt
, I think you could use T::zero().count_zeros()
. While this isn't exactly a compile-time constant, the optimizer will probably reduce that to a constant anyway.
For generic
T: PrimInt
, I think you could useT::zero().count_zeros()
. While this isn't exactly a compile-time constant, the optimizer will probably reduce that to a constant anyway.
This is what I ended up doing.
If you want, you can close this and I will comment on #11 that zero().count_zeros()
is a viable alternative?
Thanks!
OK, closing. I'm not sure how this is an "alternative" to #11 documentation though...
Nah, I meant I will comment on #11 that zero().count_zeros()
is an alternative to std::mem::size_of()
;)
I think this should be reopened. Neither zero().count_zeros()
nor std::mem::size_of(x) * 8
are particularly obvious, and it's not obvious that they would be optimised to a constant (though I verified that both are).
Hi!
Is there any way to get the total number of bits in an
PrimInt
? That is, the compile-time equivalent of:I am implementing floored/ceiled logarithms on
PrimInt
and these make use of the leading/trailing counters. These need the inverse of the respective functions, so I need to calculate the total bitlength of a number.I thought about using
std::mem::size_of()
, but that does not sound right, as it includes possible state-tracking data. Or isPrimInt
meant to allow the use ofsizeof()
?Thanks! David