| author | |
| committer | |
| log | 2f11de4f514d9775c7713313f2dcfa7343cd2bf2 |
| tree | e3c3bcc77fe3933fb48cc345537aa14901c249d1 |
| parent | e5d76176b9b18eefd8136f51c0188e5c57631e62 |
Previously, this would set len to 1 but fail to initialize any limbs.2 files changed, 14 insertions(+), 2 deletions(-)
lib/std/math/big/int.zig+6-2| ... | ... | @@ -238,12 +238,14 @@ pub const Mutable = struct { |
| 238 | 238 | self.limbs[0] = w_value; |
| 239 | 239 | } else { |
| 240 | 240 | var i: usize = 0; |
| 241 | while (w_value != 0) : (i += 1) { | |
| 241 | while (true) : (i += 1) { | |
| 242 | 242 | self.limbs[i] = @truncate(Limb, w_value); |
| 243 | 243 | |
| 244 | 244 | // TODO: shift == 64 at compile-time fails. Fails on u128 limbs. |
| 245 | 245 | w_value >>= limb_bits / 2; |
| 246 | 246 | w_value >>= limb_bits / 2; |
| 247 | ||
| 248 | if (w_value == 0) break; | |
| 247 | 249 | } |
| 248 | 250 | } |
| 249 | 251 | }, |
| ... | ... | @@ -256,11 +258,13 @@ pub const Mutable = struct { |
| 256 | 258 | const mask = (1 << limb_bits) - 1; |
| 257 | 259 | |
| 258 | 260 | comptime var i = 0; |
| 259 | inline while (w_value != 0) : (i += 1) { | |
| 261 | inline while (true) : (i += 1) { | |
| 260 | 262 | self.limbs[i] = w_value & mask; |
| 261 | 263 | |
| 262 | 264 | w_value >>= limb_bits / 2; |
| 263 | 265 | w_value >>= limb_bits / 2; |
| 266 | ||
| 267 | if (w_value == 0) break; | |
| 264 | 268 | } |
| 265 | 269 | } |
| 266 | 270 | }, |
lib/std/math/big/int_test.zig+8| ... | ... | @@ -69,6 +69,14 @@ test "big.int set negative minimum" { |
| 69 | 69 | try testing.expect((try a.to(i64)) == minInt(i64)); |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | test "big.int set double-width maximum then zero" { | |
| 73 | var a = try Managed.initSet(testing.allocator, maxInt(DoubleLimb)); | |
| 74 | defer a.deinit(); | |
| 75 | try a.set(@as(DoubleLimb, 0)); | |
| 76 | ||
| 77 | try testing.expectEqual(@as(DoubleLimb, 0), try a.to(DoubleLimb)); | |
| 78 | } | |
| 79 | ||
| 72 | 80 | test "big.int to target too small error" { |
| 73 | 81 | var a = try Managed.initSet(testing.allocator, 0xffffffff); |
| 74 | 82 | defer a.deinit(); |