DoxigAlpha

set

Sets the Mutable to value. Value must be an primitive integer type. Asserts the value fits within the limbs buffer. Note: calcLimbLen can be used to figure out how big the limbs buffer needs to be to store a specific value.

Function parameters

Parameters

#
self:*Mutable
value:anytype

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 set(self: *Mutable, value: anytype) void {
    const T = @TypeOf(value);
    const needed_limbs = calcLimbLen(value);
    assert(needed_limbs <= self.limbs.len); // value too big

    self.len = needed_limbs;
    self.positive = value >= 0;

    switch (@typeInfo(T)) {
        .int => |info| {
            var w_value = @abs(value);

            if (info.bits <= limb_bits) {
                self.limbs[0] = w_value;
            } else {
                var i: usize = 0;
                while (true) : (i += 1) {
                    self.limbs[i] = @as(Limb, @truncate(w_value));
                    w_value >>= limb_bits;

                    if (w_value == 0) break;
                }
            }
        },
        .comptime_int => {
            comptime var w_value = @abs(value);

            if (w_value <= maxInt(Limb)) {
                self.limbs[0] = w_value;
            } else {
                const mask = (1 << limb_bits) - 1;

                comptime var i = 0;
                inline while (true) : (i += 1) {
                    self.limbs[i] = w_value & mask;
                    w_value >>= limb_bits;

                    if (w_value == 0) break;
                }
            }
        },
        else => @compileError("cannot set Mutable using type " ++ @typeName(T)),
    }
}