std::math::bigint: Fixed init_with_u128 and init_with_array

This commit is contained in:
Jonas Quinten
2025-02-21 18:00:06 +01:00
committed by Christoffer Lerno
parent b50e6bd0e4
commit e34d56327a

View File

@@ -44,17 +44,16 @@ fn BigInt* BigInt.init(&self, int128 value)
fn BigInt* BigInt.init_with_u128(&self, uint128 value)
{
self.data[..] = 0;
int128 tmp = value;
uint128 tmp = value;
uint len = 0;
while (tmp != 0 && len < MAX_LEN)
while (tmp != 0)
{
self.data[len] = (uint)(tmp & 0xFFFFFFFF);
tmp >>= 32;
len++;
}
self.len = len;
assert(value == 0 || tmp == 0 || !self.is_negative());
self.len = max(len, 1);
assert(!self.is_negative());
self.len = max(len ?: 1);
return self;
}
@@ -68,7 +67,7 @@ fn BigInt* BigInt.init_with_array(&self, uint[] values)
foreach_r(i, val : values)
{
values[values.len - 1 - i] = val;
self.data[values.len - 1 - i] = val;
}
while (self.len > 1 && self.data[self.len - 1] == 0)
{