Skip to main content

twos_complement_unmasked

Function twos_complement_unmasked 

Source
pub fn twos_complement_unmasked(value: u64, width: u32) -> Option<i64>
Expand description

Sign extension for the one case where the value can be wider than the field it came from.

After a leastSignificantByteFirst swap of a field whose width is not a whole number of bytes, the value carries bits above width: a twelve-bit 0x0AB comes back as 0xAB00. The reference does not mask those away. It tests bit width - 1 and subtracts 2^width if it is set, leaving the high bits in place — so a twelve-bit field can report 43274.

twos_complement masks, and for every value that fits its width the two agree exactly. This is a separate function rather than a change to that one because the masking form is the right answer everywhere else, and because it is branchless.

Returns None when the reference’s answer does not fit an i64. That needs a width between 57 and 63 bits, not a whole number of bytes, whose swap widened it past 2^63 — where the reference’s arbitrary-precision integers can hold a number this cannot.