DoxigAlpha

shiftRight

r = a >> shift r and a may alias.

Asserts there is enough memory to fit the result. The upper bound Limb count is a.limbs.len - (shift / (@bitSizeOf(Limb))).

Function parameters

Parameters

#
r:*Mutable
shift:usize

Used to indicate either limit of a 2s-complement integer.

Types

#
TwosCompIntLimit
Used to indicate either limit of a 2s-complement integer.
Mutable
A arbitrary-precision big integer, with a fixed set of mutable limbs.
Const
A arbitrary-precision big integer, with a fixed set of immutable limbs.
Managed
An arbitrary-precision big integer along with an allocator which manages the memory.

Returns the number of limbs needed to store `scalar`, which must be a

Functions

#
calcLimbLen
Returns the number of limbs needed to store `scalar`, which must be a
calcSetStringLimbCount
Assumes `string_len` doesn't account for minus signs if the number is negative.
calcNonZeroTwosCompLimbCount
Compute the number of limbs required to store a 2s-complement number of `bit_count` bits.
calcTwosCompLimbCount
Compute the number of limbs required to store a 2s-complement number of `bit_count` bits.
addMulLimbWithCarry
a + b * c + *carry, sets carry to the overflow bits
llcmp
Returns -1, 0, 1 if |a| < |b|, |a| == |b| or |a| > |b| respectively for limbs.

Source

Implementation

#
pub fn shiftRight(r: *Mutable, a: Const, shift: usize) void {
    const full_limbs_shifted_out = shift / limb_bits;
    const remaining_bits_shifted_out = shift % limb_bits;
    if (a.limbs.len <= full_limbs_shifted_out) {
        // Shifting negative numbers converges to -1 instead of 0
        if (a.positive) {
            r.len = 1;
            r.positive = true;
            r.limbs[0] = 0;
        } else {
            r.len = 1;
            r.positive = false;
            r.limbs[0] = 1;
        }
        return;
    }
    const nonzero_negative_shiftout = if (a.positive) false else nonzero: {
        for (a.limbs[0..full_limbs_shifted_out]) |x| {
            if (x != 0)
                break :nonzero true;
        }
        if (remaining_bits_shifted_out == 0)
            break :nonzero false;
        const not_covered: Log2Limb = @intCast(limb_bits - remaining_bits_shifted_out);
        break :nonzero a.limbs[full_limbs_shifted_out] << not_covered != 0;
    };

    const new_len = llshr(r.limbs, a.limbs, shift);

    r.len = new_len;
    r.positive = a.positive;
    if (nonzero_negative_shiftout) r.addScalar(r.toConst(), -1);
    r.normalize(r.len);
}