| ... | ... | @@ -22,13 +22,18 @@ comptime { |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | pub const Int = struct { |
| 25 | | allocator: *Allocator, |
| 26 | | positive: bool, |
| 25 | const sign_bit: usize = 1 << (usize.bit_count - 1); |
| 26 | |
| 27 | allocator: ?*Allocator, |
| 27 | 28 | // - little-endian ordered |
| 28 | 29 | // - len >= 1 always |
| 29 | 30 | // - zero value -> len == 1 with limbs[0] == 0 |
| 30 | 31 | limbs: []Limb, |
| 31 | | len: usize, |
| 32 | // High bit is the sign bit. 1 is negative, 0 positive. |
| 33 | // Remaining bits indicate the number of used limbs. |
| 34 | // |
| 35 | // If Zig gets smarter about packing data, this can be rewritten as a u1 and usize - 1 field. |
| 36 | metadata: usize, |
| 32 | 37 | |
| 33 | 38 | const default_capacity = 4; |
| 34 | 39 | |
| ... | ... | @@ -45,54 +50,98 @@ pub const Int = struct { |
| 45 | 50 | pub fn initCapacity(allocator: *Allocator, capacity: usize) !Int { |
| 46 | 51 | return Int{ |
| 47 | 52 | .allocator = allocator, |
| 48 | | .positive = true, |
| 53 | .metadata = 1, |
| 49 | 54 | .limbs = block: { |
| 50 | 55 | var limbs = try allocator.alloc(Limb, math.max(default_capacity, capacity)); |
| 51 | 56 | limbs[0] = 0; |
| 52 | 57 | break :block limbs; |
| 53 | 58 | }, |
| 54 | | .len = 1, |
| 55 | 59 | }; |
| 56 | 60 | } |
| 57 | 61 | |
| 62 | pub fn len(self: Int) usize { |
| 63 | return self.metadata & ~sign_bit; |
| 64 | } |
| 65 | |
| 66 | pub fn isPositive(self: Int) bool { |
| 67 | return self.metadata & sign_bit == 0; |
| 68 | } |
| 69 | |
| 70 | pub fn setSign(self: *Int, positive: bool) void { |
| 71 | if (positive) { |
| 72 | self.metadata &= ~sign_bit; |
| 73 | } else { |
| 74 | self.metadata |= sign_bit; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | pub fn setLen(self: *Int, new_len: usize) void { |
| 79 | self.metadata &= sign_bit; |
| 80 | self.metadata |= new_len; |
| 81 | } |
| 82 | |
| 83 | // Initialize an Int directly from a fixed set of limb values. This is considered read-only |
| 84 | // and cannot be used as a receiver argument to any functions. If this tries to allocate |
| 85 | // at any point a panic will occur due to the null allocator. |
| 86 | pub fn initFixed(limbs: []const Limb) Int { |
| 87 | var self = Int{ |
| 88 | .allocator = null, |
| 89 | .metadata = limbs.len, |
| 90 | // Cast away the const, invalid use to pass as a pointer argument. |
| 91 | .limbs = @intToPtr([*]Limb, @ptrToInt(limbs.ptr))[0..limbs.len], |
| 92 | }; |
| 93 | |
| 94 | self.normalize(limbs.len); |
| 95 | return self; |
| 96 | } |
| 97 | |
| 58 | 98 | pub fn ensureCapacity(self: *Int, capacity: usize) !void { |
| 99 | self.assertWritable(); |
| 59 | 100 | if (capacity <= self.limbs.len) { |
| 60 | 101 | return; |
| 61 | 102 | } |
| 62 | 103 | |
| 63 | | self.limbs = try self.allocator.realloc(self.limbs, capacity); |
| 104 | self.limbs = try self.allocator.?.realloc(self.limbs, capacity); |
| 105 | } |
| 106 | |
| 107 | fn assertWritable(self: Int) void { |
| 108 | if (self.allocator == null) { |
| 109 | @panic("provided Int value is read-only but must be writable"); |
| 110 | } |
| 64 | 111 | } |
| 65 | 112 | |
| 66 | 113 | pub fn deinit(self: *Int) void { |
| 67 | | self.allocator.free(self.limbs); |
| 114 | self.assertWritable(); |
| 115 | self.allocator.?.free(self.limbs); |
| 68 | 116 | self.* = undefined; |
| 69 | 117 | } |
| 70 | 118 | |
| 71 | 119 | pub fn clone(other: Int) !Int { |
| 120 | other.assertWritable(); |
| 72 | 121 | return Int{ |
| 73 | 122 | .allocator = other.allocator, |
| 74 | | .positive = other.positive, |
| 123 | .metadata = other.metadata, |
| 75 | 124 | .limbs = block: { |
| 76 | | var limbs = try other.allocator.alloc(Limb, other.len); |
| 77 | | mem.copy(Limb, limbs[0..], other.limbs[0..other.len]); |
| 125 | var limbs = try other.allocator.?.alloc(Limb, other.len()); |
| 126 | mem.copy(Limb, limbs[0..], other.limbs[0..other.len()]); |
| 78 | 127 | break :block limbs; |
| 79 | 128 | }, |
| 80 | | .len = other.len, |
| 81 | 129 | }; |
| 82 | 130 | } |
| 83 | 131 | |
| 84 | 132 | pub fn copy(self: *Int, other: Int) !void { |
| 85 | | if (self == &other) { |
| 133 | self.assertWritable(); |
| 134 | if (self.limbs.ptr == other.limbs.ptr) { |
| 86 | 135 | return; |
| 87 | 136 | } |
| 88 | 137 | |
| 89 | | self.positive = other.positive; |
| 90 | | try self.ensureCapacity(other.len); |
| 91 | | mem.copy(Limb, self.limbs[0..], other.limbs[0..other.len]); |
| 92 | | self.len = other.len; |
| 138 | try self.ensureCapacity(other.len()); |
| 139 | mem.copy(Limb, self.limbs[0..], other.limbs[0..other.len()]); |
| 140 | self.metadata = other.metadata; |
| 93 | 141 | } |
| 94 | 142 | |
| 95 | 143 | pub fn swap(self: *Int, other: *Int) void { |
| 144 | self.assertWritable(); |
| 96 | 145 | mem.swap(Int, self, other); |
| 97 | 146 | } |
| 98 | 147 | |
| ... | ... | @@ -103,25 +152,25 @@ pub const Int = struct { |
| 103 | 152 | debug.warn("\n"); |
| 104 | 153 | } |
| 105 | 154 | |
| 106 | | pub fn negate(r: *Int) void { |
| 107 | | r.positive = !r.positive; |
| 155 | pub fn negate(self: *Int) void { |
| 156 | self.metadata ^= sign_bit; |
| 108 | 157 | } |
| 109 | 158 | |
| 110 | | pub fn abs(r: *Int) void { |
| 111 | | r.positive = true; |
| 159 | pub fn abs(self: *Int) void { |
| 160 | self.metadata &= ~sign_bit; |
| 112 | 161 | } |
| 113 | 162 | |
| 114 | | pub fn isOdd(r: Int) bool { |
| 115 | | return r.limbs[0] & 1 != 0; |
| 163 | pub fn isOdd(self: Int) bool { |
| 164 | return self.limbs[0] & 1 != 0; |
| 116 | 165 | } |
| 117 | 166 | |
| 118 | | pub fn isEven(r: Int) bool { |
| 119 | | return !r.isOdd(); |
| 167 | pub fn isEven(self: Int) bool { |
| 168 | return !self.isOdd(); |
| 120 | 169 | } |
| 121 | 170 | |
| 122 | 171 | // Returns the number of bits required to represent the absolute value of self. |
| 123 | 172 | fn bitCountAbs(self: Int) usize { |
| 124 | | return (self.len - 1) * Limb.bit_count + (Limb.bit_count - @clz(self.limbs[self.len - 1])); |
| 173 | return (self.len() - 1) * Limb.bit_count + (Limb.bit_count - @clz(self.limbs[self.len() - 1])); |
| 125 | 174 | } |
| 126 | 175 | |
| 127 | 176 | // Returns the number of bits required to represent the integer in twos-complement form. |
| ... | ... | @@ -137,11 +186,11 @@ pub const Int = struct { |
| 137 | 186 | |
| 138 | 187 | // If the entire value has only one bit set (e.g. 0b100000000) then the negation in twos |
| 139 | 188 | // complement requires one less bit. |
| 140 | | if (!self.positive) block: { |
| 189 | if (!self.isPositive()) block: { |
| 141 | 190 | bits += 1; |
| 142 | 191 | |
| 143 | | if (@popCount(self.limbs[self.len - 1]) == 1) { |
| 144 | | for (self.limbs[0 .. self.len - 1]) |limb| { |
| 192 | if (@popCount(self.limbs[self.len() - 1]) == 1) { |
| 193 | for (self.limbs[0 .. self.len() - 1]) |limb| { |
| 145 | 194 | if (@popCount(limb) != 0) { |
| 146 | 195 | break :block; |
| 147 | 196 | } |
| ... | ... | @@ -158,11 +207,11 @@ pub const Int = struct { |
| 158 | 207 | if (self.eqZero()) { |
| 159 | 208 | return true; |
| 160 | 209 | } |
| 161 | | if (!is_signed and !self.positive) { |
| 210 | if (!is_signed and !self.isPositive()) { |
| 162 | 211 | return false; |
| 163 | 212 | } |
| 164 | 213 | |
| 165 | | const req_bits = self.bitCountTwosComp() + @boolToInt(self.positive and is_signed); |
| 214 | const req_bits = self.bitCountTwosComp() + @boolToInt(self.isPositive() and is_signed); |
| 166 | 215 | return bit_count >= req_bits; |
| 167 | 216 | } |
| 168 | 217 | |
| ... | ... | @@ -174,11 +223,12 @@ pub const Int = struct { |
| 174 | 223 | // the minus sign. This is used for determining the number of characters needed to print the |
| 175 | 224 | // value. It is inexact and will exceed the given value by 1-2 digits. |
| 176 | 225 | pub fn sizeInBase(self: Int, base: usize) usize { |
| 177 | | const bit_count = usize(@boolToInt(!self.positive)) + self.bitCountAbs(); |
| 226 | const bit_count = usize(@boolToInt(!self.isPositive())) + self.bitCountAbs(); |
| 178 | 227 | return (bit_count / math.log2(base)) + 1; |
| 179 | 228 | } |
| 180 | 229 | |
| 181 | 230 | pub fn set(self: *Int, value: var) Allocator.Error!void { |
| 231 | self.assertWritable(); |
| 182 | 232 | const T = @typeOf(value); |
| 183 | 233 | |
| 184 | 234 | switch (@typeInfo(T)) { |
| ... | ... | @@ -186,19 +236,19 @@ pub const Int = struct { |
| 186 | 236 | const UT = if (T.is_signed) @IntType(false, T.bit_count - 1) else T; |
| 187 | 237 | |
| 188 | 238 | try self.ensureCapacity(@sizeOf(UT) / @sizeOf(Limb)); |
| 189 | | self.positive = value >= 0; |
| 190 | | self.len = 0; |
| 239 | self.metadata = 0; |
| 240 | self.setSign(value >= 0); |
| 191 | 241 | |
| 192 | 242 | var w_value: UT = if (value < 0) @intCast(UT, -value) else @intCast(UT, value); |
| 193 | 243 | |
| 194 | 244 | if (info.bits <= Limb.bit_count) { |
| 195 | 245 | self.limbs[0] = Limb(w_value); |
| 196 | | self.len = 1; |
| 246 | self.metadata += 1; |
| 197 | 247 | } else { |
| 198 | 248 | var i: usize = 0; |
| 199 | 249 | while (w_value != 0) : (i += 1) { |
| 200 | 250 | self.limbs[i] = @truncate(Limb, w_value); |
| 201 | | self.len += 1; |
| 251 | self.metadata += 1; |
| 202 | 252 | |
| 203 | 253 | // TODO: shift == 64 at compile-time fails. Fails on u128 limbs. |
| 204 | 254 | w_value >>= Limb.bit_count / 2; |
| ... | ... | @@ -212,8 +262,8 @@ pub const Int = struct { |
| 212 | 262 | const req_limbs = @divFloor(math.log2(w_value), Limb.bit_count) + 1; |
| 213 | 263 | try self.ensureCapacity(req_limbs); |
| 214 | 264 | |
| 215 | | self.positive = value >= 0; |
| 216 | | self.len = req_limbs; |
| 265 | self.metadata = req_limbs; |
| 266 | self.setSign(value >= 0); |
| 217 | 267 | |
| 218 | 268 | if (w_value <= maxInt(Limb)) { |
| 219 | 269 | self.limbs[0] = w_value; |
| ... | ... | @@ -254,17 +304,17 @@ pub const Int = struct { |
| 254 | 304 | if (@sizeOf(UT) <= @sizeOf(Limb)) { |
| 255 | 305 | r = @intCast(UT, self.limbs[0]); |
| 256 | 306 | } else { |
| 257 | | for (self.limbs[0..self.len]) |_, ri| { |
| 258 | | const limb = self.limbs[self.len - ri - 1]; |
| 307 | for (self.limbs[0..self.len()]) |_, ri| { |
| 308 | const limb = self.limbs[self.len() - ri - 1]; |
| 259 | 309 | r <<= Limb.bit_count; |
| 260 | 310 | r |= limb; |
| 261 | 311 | } |
| 262 | 312 | } |
| 263 | 313 | |
| 264 | 314 | if (!T.is_signed) { |
| 265 | | return if (self.positive) @intCast(T, r) else error.NegativeIntoUnsigned; |
| 315 | return if (self.isPositive()) @intCast(T, r) else error.NegativeIntoUnsigned; |
| 266 | 316 | } else { |
| 267 | | if (self.positive) { |
| 317 | if (self.isPositive()) { |
| 268 | 318 | return @intCast(T, r); |
| 269 | 319 | } else { |
| 270 | 320 | if (math.cast(T, r)) |ok| { |
| ... | ... | @@ -304,6 +354,7 @@ pub const Int = struct { |
| 304 | 354 | } |
| 305 | 355 | |
| 306 | 356 | pub fn setString(self: *Int, base: u8, value: []const u8) !void { |
| 357 | self.assertWritable(); |
| 307 | 358 | if (base < 2 or base > 16) { |
| 308 | 359 | return error.InvalidBase; |
| 309 | 360 | } |
| ... | ... | @@ -315,25 +366,18 @@ pub const Int = struct { |
| 315 | 366 | i += 1; |
| 316 | 367 | } |
| 317 | 368 | |
| 318 | | // TODO values less than limb size should guarantee non allocating |
| 319 | | var base_buffer: [512]u8 = undefined; |
| 320 | | const base_al = &std.heap.FixedBufferAllocator.init(base_buffer[0..]).allocator; |
| 321 | | const base_ap = try Int.initSet(base_al, base); |
| 322 | | |
| 323 | | var d_buffer: [512]u8 = undefined; |
| 324 | | var d_fba = std.heap.FixedBufferAllocator.init(d_buffer[0..]); |
| 325 | | const d_al = &d_fba.allocator; |
| 326 | | |
| 369 | const ap_base = Int.initFixed(([]Limb{base})[0..]); |
| 327 | 370 | try self.set(0); |
| 371 | |
| 328 | 372 | for (value[i..]) |ch| { |
| 329 | 373 | const d = try charToDigit(ch, base); |
| 330 | | d_fba.end_index = 0; |
| 331 | | const d_ap = try Int.initSet(d_al, d); |
| 332 | 374 | |
| 333 | | try self.mul(self.*, base_ap); |
| 334 | | try self.add(self.*, d_ap); |
| 375 | const ap_d = Int.initFixed(([]Limb{d})[0..]); |
| 376 | |
| 377 | try self.mul(self.*, ap_base); |
| 378 | try self.add(self.*, ap_d); |
| 335 | 379 | } |
| 336 | | self.positive = positive; |
| 380 | self.setSign(positive); |
| 337 | 381 | } |
| 338 | 382 | |
| 339 | 383 | /// TODO make this call format instead of the other way around |
| ... | ... | @@ -355,7 +399,7 @@ pub const Int = struct { |
| 355 | 399 | if (base & (base - 1) == 0) { |
| 356 | 400 | const base_shift = math.log2_int(Limb, base); |
| 357 | 401 | |
| 358 | | for (self.limbs[0..self.len]) |limb| { |
| 402 | for (self.limbs[0..self.len()]) |limb| { |
| 359 | 403 | var shift: usize = 0; |
| 360 | 404 | while (shift < Limb.bit_count) : (shift += base_shift) { |
| 361 | 405 | const r = @intCast(u8, (limb >> @intCast(Log2Limb, shift)) & Limb(base - 1)); |
| ... | ... | @@ -382,11 +426,11 @@ pub const Int = struct { |
| 382 | 426 | } |
| 383 | 427 | |
| 384 | 428 | var q = try self.clone(); |
| 385 | | q.positive = true; |
| 429 | q.abs(); |
| 386 | 430 | var r = try Int.init(allocator); |
| 387 | 431 | var b = try Int.initSet(allocator, limb_base); |
| 388 | 432 | |
| 389 | | while (q.len >= 2) { |
| 433 | while (q.len() >= 2) { |
| 390 | 434 | try Int.divTrunc(&q, &r, q, b); |
| 391 | 435 | |
| 392 | 436 | var r_word = r.limbs[0]; |
| ... | ... | @@ -399,7 +443,7 @@ pub const Int = struct { |
| 399 | 443 | } |
| 400 | 444 | |
| 401 | 445 | { |
| 402 | | debug.assert(q.len == 1); |
| 446 | debug.assert(q.len() == 1); |
| 403 | 447 | |
| 404 | 448 | var r_word = q.limbs[0]; |
| 405 | 449 | while (r_word != 0) { |
| ... | ... | @@ -410,7 +454,7 @@ pub const Int = struct { |
| 410 | 454 | } |
| 411 | 455 | } |
| 412 | 456 | |
| 413 | | if (!self.positive) { |
| 457 | if (!self.isPositive()) { |
| 414 | 458 | try digits.append('-'); |
| 415 | 459 | } |
| 416 | 460 | |
| ... | ... | @@ -428,22 +472,24 @@ pub const Int = struct { |
| 428 | 472 | comptime FmtError: type, |
| 429 | 473 | output: fn (@typeOf(context), []const u8) FmtError!void, |
| 430 | 474 | ) FmtError!void { |
| 475 | self.assertWritable(); |
| 431 | 476 | // TODO look at fmt and support other bases |
| 432 | | const str = self.toString(self.allocator, 10) catch @panic("TODO make this non allocating"); |
| 433 | | defer self.allocator.free(str); |
| 477 | // TODO support read-only fixed integers |
| 478 | const str = self.toString(self.allocator.?, 10) catch @panic("TODO make this non allocating"); |
| 479 | defer self.allocator.?.free(str); |
| 434 | 480 | return output(context, str); |
| 435 | 481 | } |
| 436 | 482 | |
| 437 | 483 | // returns -1, 0, 1 if |a| < |b|, |a| == |b| or |a| > |b| respectively. |
| 438 | 484 | pub fn cmpAbs(a: Int, b: Int) i8 { |
| 439 | | if (a.len < b.len) { |
| 485 | if (a.len() < b.len()) { |
| 440 | 486 | return -1; |
| 441 | 487 | } |
| 442 | | if (a.len > b.len) { |
| 488 | if (a.len() > b.len()) { |
| 443 | 489 | return 1; |
| 444 | 490 | } |
| 445 | 491 | |
| 446 | | var i: usize = a.len - 1; |
| 492 | var i: usize = a.len() - 1; |
| 447 | 493 | while (i != 0) : (i -= 1) { |
| 448 | 494 | if (a.limbs[i] != b.limbs[i]) { |
| 449 | 495 | break; |
| ... | ... | @@ -461,17 +507,17 @@ pub const Int = struct { |
| 461 | 507 | |
| 462 | 508 | // returns -1, 0, 1 if a < b, a == b or a > b respectively. |
| 463 | 509 | pub fn cmp(a: Int, b: Int) i8 { |
| 464 | | if (a.positive != b.positive) { |
| 465 | | return if (a.positive) i8(1) else -1; |
| 510 | if (a.isPositive() != b.isPositive()) { |
| 511 | return if (a.isPositive()) i8(1) else -1; |
| 466 | 512 | } else { |
| 467 | 513 | const r = cmpAbs(a, b); |
| 468 | | return if (a.positive) r else -r; |
| 514 | return if (a.isPositive()) r else -r; |
| 469 | 515 | } |
| 470 | 516 | } |
| 471 | 517 | |
| 472 | 518 | // if a == 0 |
| 473 | 519 | pub fn eqZero(a: Int) bool { |
| 474 | | return a.len == 1 and a.limbs[0] == 0; |
| 520 | return a.len() == 1 and a.limbs[0] == 0; |
| 475 | 521 | } |
| 476 | 522 | |
| 477 | 523 | // if |a| == |b| |
| ... | ... | @@ -484,28 +530,12 @@ pub const Int = struct { |
| 484 | 530 | return cmp(a, b) == 0; |
| 485 | 531 | } |
| 486 | 532 | |
| 487 | | // Normalize for a possible single carry digit. |
| 488 | | // |
| 489 | | // [1, 2, 3, 4, 0] -> [1, 2, 3, 4] |
| 490 | | // [1, 2, 3, 4, 5] -> [1, 2, 3, 4, 5] |
| 491 | | // [0] -> [0] |
| 492 | | fn norm1(r: *Int, length: usize) void { |
| 493 | | debug.assert(length > 0); |
| 494 | | debug.assert(length <= r.limbs.len); |
| 495 | | |
| 496 | | if (r.limbs[length - 1] == 0) { |
| 497 | | r.len = if (length > 1) length - 1 else 1; |
| 498 | | } else { |
| 499 | | r.len = length; |
| 500 | | } |
| 501 | | } |
| 502 | | |
| 503 | 533 | // Normalize a possible sequence of leading zeros. |
| 504 | 534 | // |
| 505 | 535 | // [1, 2, 3, 4, 0] -> [1, 2, 3, 4] |
| 506 | 536 | // [1, 2, 0, 0, 0] -> [1, 2] |
| 507 | 537 | // [0, 0, 0, 0, 0] -> [0] |
| 508 | | fn normN(r: *Int, length: usize) void { |
| 538 | fn normalize(r: *Int, length: usize) void { |
| 509 | 539 | debug.assert(length > 0); |
| 510 | 540 | debug.assert(length <= r.limbs.len); |
| 511 | 541 | |
| ... | ... | @@ -517,11 +547,21 @@ pub const Int = struct { |
| 517 | 547 | } |
| 518 | 548 | |
| 519 | 549 | // Handle zero |
| 520 | | r.len = if (j != 0) j else 1; |
| 550 | r.setLen(if (j != 0) j else 1); |
| 551 | } |
| 552 | |
| 553 | // Cannot be used as a result argument to any function. |
| 554 | fn readOnlyPositive(a: Int) Int { |
| 555 | return Int{ |
| 556 | .allocator = null, |
| 557 | .metadata = a.len(), |
| 558 | .limbs = a.limbs, |
| 559 | }; |
| 521 | 560 | } |
| 522 | 561 | |
| 523 | 562 | // r = a + b |
| 524 | 563 | pub fn add(r: *Int, a: Int, b: Int) Allocator.Error!void { |
| 564 | r.assertWritable(); |
| 525 | 565 | if (a.eqZero()) { |
| 526 | 566 | try r.copy(b); |
| 527 | 567 | return; |
| ... | ... | @@ -530,38 +570,26 @@ pub const Int = struct { |
| 530 | 570 | return; |
| 531 | 571 | } |
| 532 | 572 | |
| 533 | | if (a.positive != b.positive) { |
| 534 | | if (a.positive) { |
| 573 | if (a.isPositive() != b.isPositive()) { |
| 574 | if (a.isPositive()) { |
| 535 | 575 | // (a) + (-b) => a - b |
| 536 | | const bp = Int{ |
| 537 | | .allocator = undefined, |
| 538 | | .positive = true, |
| 539 | | .limbs = b.limbs, |
| 540 | | .len = b.len, |
| 541 | | }; |
| 542 | | try r.sub(a, bp); |
| 576 | try r.sub(a, readOnlyPositive(b)); |
| 543 | 577 | } else { |
| 544 | 578 | // (-a) + (b) => b - a |
| 545 | | const ap = Int{ |
| 546 | | .allocator = undefined, |
| 547 | | .positive = true, |
| 548 | | .limbs = a.limbs, |
| 549 | | .len = a.len, |
| 550 | | }; |
| 551 | | try r.sub(b, ap); |
| 579 | try r.sub(b, readOnlyPositive(a)); |
| 552 | 580 | } |
| 553 | 581 | } else { |
| 554 | | if (a.len >= b.len) { |
| 555 | | try r.ensureCapacity(a.len + 1); |
| 556 | | lladd(r.limbs[0..], a.limbs[0..a.len], b.limbs[0..b.len]); |
| 557 | | r.norm1(a.len + 1); |
| 582 | if (a.len() >= b.len()) { |
| 583 | try r.ensureCapacity(a.len() + 1); |
| 584 | lladd(r.limbs[0..], a.limbs[0..a.len()], b.limbs[0..b.len()]); |
| 585 | r.normalize(a.len() + 1); |
| 558 | 586 | } else { |
| 559 | | try r.ensureCapacity(b.len + 1); |
| 560 | | lladd(r.limbs[0..], b.limbs[0..b.len], a.limbs[0..a.len]); |
| 561 | | r.norm1(b.len + 1); |
| 587 | try r.ensureCapacity(b.len() + 1); |
| 588 | lladd(r.limbs[0..], b.limbs[0..b.len()], a.limbs[0..a.len()]); |
| 589 | r.normalize(b.len() + 1); |
| 562 | 590 | } |
| 563 | 591 | |
| 564 | | r.positive = a.positive; |
| 592 | r.setSign(a.isPositive()); |
| 565 | 593 | } |
| 566 | 594 | } |
| 567 | 595 | |
| ... | ... | @@ -591,53 +619,42 @@ pub const Int = struct { |
| 591 | 619 | |
| 592 | 620 | // r = a - b |
| 593 | 621 | pub fn sub(r: *Int, a: Int, b: Int) !void { |
| 594 | | if (a.positive != b.positive) { |
| 595 | | if (a.positive) { |
| 622 | r.assertWritable(); |
| 623 | if (a.isPositive() != b.isPositive()) { |
| 624 | if (a.isPositive()) { |
| 596 | 625 | // (a) - (-b) => a + b |
| 597 | | const bp = Int{ |
| 598 | | .allocator = undefined, |
| 599 | | .positive = true, |
| 600 | | .limbs = b.limbs, |
| 601 | | .len = b.len, |
| 602 | | }; |
| 603 | | try r.add(a, bp); |
| 626 | try r.add(a, readOnlyPositive(b)); |
| 604 | 627 | } else { |
| 605 | 628 | // (-a) - (b) => -(a + b) |
| 606 | | const ap = Int{ |
| 607 | | .allocator = undefined, |
| 608 | | .positive = true, |
| 609 | | .limbs = a.limbs, |
| 610 | | .len = a.len, |
| 611 | | }; |
| 612 | | try r.add(ap, b); |
| 613 | | r.positive = false; |
| 629 | try r.add(readOnlyPositive(a), b); |
| 630 | r.setSign(false); |
| 614 | 631 | } |
| 615 | 632 | } else { |
| 616 | | if (a.positive) { |
| 633 | if (a.isPositive()) { |
| 617 | 634 | // (a) - (b) => a - b |
| 618 | 635 | if (a.cmp(b) >= 0) { |
| 619 | | try r.ensureCapacity(a.len + 1); |
| 620 | | llsub(r.limbs[0..], a.limbs[0..a.len], b.limbs[0..b.len]); |
| 621 | | r.normN(a.len); |
| 622 | | r.positive = true; |
| 636 | try r.ensureCapacity(a.len() + 1); |
| 637 | llsub(r.limbs[0..], a.limbs[0..a.len()], b.limbs[0..b.len()]); |
| 638 | r.normalize(a.len()); |
| 639 | r.setSign(true); |
| 623 | 640 | } else { |
| 624 | | try r.ensureCapacity(b.len + 1); |
| 625 | | llsub(r.limbs[0..], b.limbs[0..b.len], a.limbs[0..a.len]); |
| 626 | | r.normN(b.len); |
| 627 | | r.positive = false; |
| 641 | try r.ensureCapacity(b.len() + 1); |
| 642 | llsub(r.limbs[0..], b.limbs[0..b.len()], a.limbs[0..a.len()]); |
| 643 | r.normalize(b.len()); |
| 644 | r.setSign(false); |
| 628 | 645 | } |
| 629 | 646 | } else { |
| 630 | 647 | // (-a) - (-b) => -(a - b) |
| 631 | 648 | if (a.cmp(b) < 0) { |
| 632 | | try r.ensureCapacity(a.len + 1); |
| 633 | | llsub(r.limbs[0..], a.limbs[0..a.len], b.limbs[0..b.len]); |
| 634 | | r.normN(a.len); |
| 635 | | r.positive = false; |
| 649 | try r.ensureCapacity(a.len() + 1); |
| 650 | llsub(r.limbs[0..], a.limbs[0..a.len()], b.limbs[0..b.len()]); |
| 651 | r.normalize(a.len()); |
| 652 | r.setSign(false); |
| 636 | 653 | } else { |
| 637 | | try r.ensureCapacity(b.len + 1); |
| 638 | | llsub(r.limbs[0..], b.limbs[0..b.len], a.limbs[0..a.len]); |
| 639 | | r.normN(b.len); |
| 640 | | r.positive = true; |
| 654 | try r.ensureCapacity(b.len() + 1); |
| 655 | llsub(r.limbs[0..], b.limbs[0..b.len()], a.limbs[0..a.len()]); |
| 656 | r.normalize(b.len()); |
| 657 | r.setSign(true); |
| 641 | 658 | } |
| 642 | 659 | } |
| 643 | 660 | } |
| ... | ... | @@ -671,12 +688,14 @@ pub const Int = struct { |
| 671 | 688 | // |
| 672 | 689 | // For greatest efficiency, ensure rma does not alias a or b. |
| 673 | 690 | pub fn mul(rma: *Int, a: Int, b: Int) !void { |
| 691 | rma.assertWritable(); |
| 692 | |
| 674 | 693 | var r = rma; |
| 675 | 694 | var aliased = rma.limbs.ptr == a.limbs.ptr or rma.limbs.ptr == b.limbs.ptr; |
| 676 | 695 | |
| 677 | 696 | var sr: Int = undefined; |
| 678 | 697 | if (aliased) { |
| 679 | | sr = try Int.initCapacity(rma.allocator, a.len + b.len); |
| 698 | sr = try Int.initCapacity(rma.allocator.?, a.len() + b.len()); |
| 680 | 699 | r = &sr; |
| 681 | 700 | aliased = true; |
| 682 | 701 | } |
| ... | ... | @@ -685,16 +704,16 @@ pub const Int = struct { |
| 685 | 704 | r.deinit(); |
| 686 | 705 | }; |
| 687 | 706 | |
| 688 | | try r.ensureCapacity(a.len + b.len); |
| 707 | try r.ensureCapacity(a.len() + b.len()); |
| 689 | 708 | |
| 690 | | if (a.len >= b.len) { |
| 691 | | llmul(r.limbs, a.limbs[0..a.len], b.limbs[0..b.len]); |
| 709 | if (a.len() >= b.len()) { |
| 710 | llmul(r.limbs, a.limbs[0..a.len()], b.limbs[0..b.len()]); |
| 692 | 711 | } else { |
| 693 | | llmul(r.limbs, b.limbs[0..b.len], a.limbs[0..a.len]); |
| 712 | llmul(r.limbs, b.limbs[0..b.len()], a.limbs[0..a.len()]); |
| 694 | 713 | } |
| 695 | 714 | |
| 696 | | r.positive = a.positive == b.positive; |
| 697 | | r.normN(a.len + b.len); |
| 715 | r.normalize(a.len() + b.len()); |
| 716 | r.setSign(a.isPositive() == b.isPositive()); |
| 698 | 717 | } |
| 699 | 718 | |
| 700 | 719 | // a + b * c + *carry, sets carry to the overflow bits |
| ... | ... | @@ -744,25 +763,24 @@ pub const Int = struct { |
| 744 | 763 | try div(q, r, a, b); |
| 745 | 764 | |
| 746 | 765 | // Trunc -> Floor. |
| 747 | | if (!q.positive) { |
| 748 | | // TODO values less than limb size should guarantee non allocating |
| 749 | | var one_buffer: [512]u8 = undefined; |
| 750 | | const one_al = &std.heap.FixedBufferAllocator.init(one_buffer[0..]).allocator; |
| 751 | | const one_ap = try Int.initSet(one_al, 1); |
| 752 | | |
| 753 | | try q.sub(q.*, one_ap); |
| 754 | | try r.add(q.*, one_ap); |
| 766 | if (!q.isPositive()) { |
| 767 | const one = Int.initFixed(([]Limb{1})[0..]); |
| 768 | try q.sub(q.*, one); |
| 769 | try r.add(q.*, one); |
| 755 | 770 | } |
| 756 | | r.positive = b.positive; |
| 771 | r.setSign(b.isPositive()); |
| 757 | 772 | } |
| 758 | 773 | |
| 759 | 774 | pub fn divTrunc(q: *Int, r: *Int, a: Int, b: Int) !void { |
| 760 | 775 | try div(q, r, a, b); |
| 761 | | r.positive = a.positive; |
| 776 | r.setSign(a.isPositive()); |
| 762 | 777 | } |
| 763 | 778 | |
| 764 | 779 | // Truncates by default. |
| 765 | 780 | fn div(quo: *Int, rem: *Int, a: Int, b: Int) !void { |
| 781 | quo.assertWritable(); |
| 782 | rem.assertWritable(); |
| 783 | |
| 766 | 784 | if (b.eqZero()) { |
| 767 | 785 | @panic("division by zero"); |
| 768 | 786 | } |
| ... | ... | @@ -773,36 +791,67 @@ pub const Int = struct { |
| 773 | 791 | if (a.cmpAbs(b) < 0) { |
| 774 | 792 | // quo may alias a so handle rem first |
| 775 | 793 | try rem.copy(a); |
| 776 | | rem.positive = a.positive == b.positive; |
| 794 | rem.setSign(a.isPositive() == b.isPositive()); |
| 777 | 795 | |
| 778 | | quo.positive = true; |
| 779 | | quo.len = 1; |
| 796 | quo.metadata = 1; |
| 780 | 797 | quo.limbs[0] = 0; |
| 781 | 798 | return; |
| 782 | 799 | } |
| 783 | 800 | |
| 784 | | if (b.len == 1) { |
| 785 | | try quo.ensureCapacity(a.len); |
| 801 | // Handle trailing zero-words of divisor/dividend. These are not handled in the following |
| 802 | // algorithms. |
| 803 | const a_zero_limb_count = blk: { |
| 804 | var i: usize = 0; |
| 805 | while (i < a.len()) : (i += 1) { |
| 806 | if (a.limbs[i] != 0) break; |
| 807 | } |
| 808 | break :blk i; |
| 809 | }; |
| 810 | const b_zero_limb_count = blk: { |
| 811 | var i: usize = 0; |
| 812 | while (i < b.len()) : (i += 1) { |
| 813 | if (b.limbs[i] != 0) break; |
| 814 | } |
| 815 | break :blk i; |
| 816 | }; |
| 817 | |
| 818 | const ab_zero_limb_count = std.math.min(a_zero_limb_count, b_zero_limb_count); |
| 819 | |
| 820 | if (b.len() - ab_zero_limb_count == 1) { |
| 821 | try quo.ensureCapacity(a.len()); |
| 786 | 822 | |
| 787 | | lldiv1(quo.limbs[0..], &rem.limbs[0], a.limbs[0..a.len], b.limbs[0]); |
| 788 | | quo.norm1(a.len); |
| 789 | | quo.positive = a.positive == b.positive; |
| 823 | lldiv1(quo.limbs[0..], &rem.limbs[0], a.limbs[ab_zero_limb_count..a.len()], b.limbs[b.len() - 1]); |
| 824 | quo.normalize(a.len() - ab_zero_limb_count); |
| 825 | quo.setSign(a.isPositive() == b.isPositive()); |
| 790 | 826 | |
| 791 | | rem.len = 1; |
| 792 | | rem.positive = true; |
| 827 | rem.metadata = 1; |
| 793 | 828 | } else { |
| 794 | 829 | // x and y are modified during division |
| 795 | | var x = try a.clone(); |
| 830 | var x = try Int.initCapacity(quo.allocator.?, a.len()); |
| 796 | 831 | defer x.deinit(); |
| 832 | try x.copy(a); |
| 797 | 833 | |
| 798 | | var y = try b.clone(); |
| 834 | var y = try Int.initCapacity(quo.allocator.?, b.len()); |
| 799 | 835 | defer y.deinit(); |
| 836 | try y.copy(b); |
| 800 | 837 | |
| 801 | 838 | // x may grow one limb during normalization |
| 802 | | try quo.ensureCapacity(a.len + y.len); |
| 803 | | try divN(quo.allocator, quo, rem, &x, &y); |
| 839 | try quo.ensureCapacity(a.len() + y.len()); |
| 840 | |
| 841 | // Shrink x, y such that the trailing zero limbs shared between are removed. |
| 842 | if (ab_zero_limb_count != 0) { |
| 843 | std.mem.copy(Limb, x.limbs[0..], x.limbs[ab_zero_limb_count..]); |
| 844 | std.mem.copy(Limb, y.limbs[0..], y.limbs[ab_zero_limb_count..]); |
| 845 | x.metadata -= ab_zero_limb_count; |
| 846 | y.metadata -= ab_zero_limb_count; |
| 847 | } |
| 848 | |
| 849 | try divN(quo.allocator.?, quo, rem, &x, &y); |
| 850 | quo.setSign(a.isPositive() == b.isPositive()); |
| 851 | } |
| 804 | 852 | |
| 805 | | quo.positive = a.positive == b.positive; |
| 853 | if (ab_zero_limb_count != 0) { |
| 854 | try rem.shiftLeft(rem.*, ab_zero_limb_count * Limb.bit_count); |
| 806 | 855 | } |
| 807 | 856 | } |
| 808 | 857 | |
| ... | ... | @@ -837,25 +886,28 @@ pub const Int = struct { |
| 837 | 886 | // |
| 838 | 887 | // x = qy + r where 0 <= r < y |
| 839 | 888 | fn divN(allocator: *Allocator, q: *Int, r: *Int, x: *Int, y: *Int) !void { |
| 840 | | debug.assert(y.len >= 2); |
| 841 | | debug.assert(x.len >= y.len); |
| 842 | | debug.assert(q.limbs.len >= x.len + y.len - 1); |
| 889 | debug.assert(y.len() >= 2); |
| 890 | debug.assert(x.len() >= y.len()); |
| 891 | debug.assert(q.limbs.len >= x.len() + y.len() - 1); |
| 843 | 892 | debug.assert(default_capacity >= 3); // see 3.2 |
| 844 | 893 | |
| 845 | 894 | var tmp = try Int.init(allocator); |
| 846 | 895 | defer tmp.deinit(); |
| 847 | 896 | |
| 848 | | // Normalize so y > Limb.bit_count / 2 (i.e. leading bit is set) |
| 849 | | const norm_shift = @clz(y.limbs[y.len - 1]); |
| 897 | // Normalize so y > Limb.bit_count / 2 (i.e. leading bit is set) and even |
| 898 | var norm_shift = @clz(y.limbs[y.len() - 1]); |
| 899 | if (norm_shift == 0 and y.isOdd()) { |
| 900 | norm_shift = Limb.bit_count; |
| 901 | } |
| 850 | 902 | try x.shiftLeft(x.*, norm_shift); |
| 851 | 903 | try y.shiftLeft(y.*, norm_shift); |
| 852 | 904 | |
| 853 | | const n = x.len - 1; |
| 854 | | const t = y.len - 1; |
| 905 | const n = x.len() - 1; |
| 906 | const t = y.len() - 1; |
| 855 | 907 | |
| 856 | 908 | // 1. |
| 857 | | q.len = n - t + 1; |
| 858 | | mem.set(Limb, q.limbs[0..q.len], 0); |
| 909 | q.metadata = n - t + 1; |
| 910 | mem.set(Limb, q.limbs[0..q.len()], 0); |
| 859 | 911 | |
| 860 | 912 | // 2. |
| 861 | 913 | try tmp.shiftLeft(y.*, Limb.bit_count * (n - t)); |
| ... | ... | @@ -880,7 +932,7 @@ pub const Int = struct { |
| 880 | 932 | tmp.limbs[0] = if (i >= 2) x.limbs[i - 2] else 0; |
| 881 | 933 | tmp.limbs[1] = if (i >= 1) x.limbs[i - 1] else 0; |
| 882 | 934 | tmp.limbs[2] = x.limbs[i]; |
| 883 | | tmp.normN(3); |
| 935 | tmp.normalize(3); |
| 884 | 936 | |
| 885 | 937 | while (true) { |
| 886 | 938 | // 2x1 limb multiplication unrolled against single-limb q[i-t-1] |
| ... | ... | @@ -888,7 +940,7 @@ pub const Int = struct { |
| 888 | 940 | r.limbs[0] = addMulLimbWithCarry(0, if (t >= 1) y.limbs[t - 1] else 0, q.limbs[i - t - 1], &carry); |
| 889 | 941 | r.limbs[1] = addMulLimbWithCarry(0, y.limbs[t], q.limbs[i - t - 1], &carry); |
| 890 | 942 | r.limbs[2] = carry; |
| 891 | | r.normN(3); |
| 943 | r.normalize(3); |
| 892 | 944 | |
| 893 | 945 | if (r.cmpAbs(tmp) <= 0) { |
| 894 | 946 | break; |
| ... | ... | @@ -903,7 +955,7 @@ pub const Int = struct { |
| 903 | 955 | try tmp.shiftLeft(tmp, Limb.bit_count * (i - t - 1)); |
| 904 | 956 | try x.sub(x.*, tmp); |
| 905 | 957 | |
| 906 | | if (!x.positive) { |
| 958 | if (!x.isPositive()) { |
| 907 | 959 | try tmp.shiftLeft(y.*, Limb.bit_count * (i - t - 1)); |
| 908 | 960 | try x.add(x.*, tmp); |
| 909 | 961 | q.limbs[i - t - 1] -= 1; |
| ... | ... | @@ -911,18 +963,20 @@ pub const Int = struct { |
| 911 | 963 | } |
| 912 | 964 | |
| 913 | 965 | // Denormalize |
| 914 | | q.normN(q.len); |
| 966 | q.normalize(q.len()); |
| 915 | 967 | |
| 916 | 968 | try r.shiftRight(x.*, norm_shift); |
| 917 | | r.normN(r.len); |
| 969 | r.normalize(r.len()); |
| 918 | 970 | } |
| 919 | 971 | |
| 920 | 972 | // r = a << shift, in other words, r = a * 2^shift |
| 921 | 973 | pub fn shiftLeft(r: *Int, a: Int, shift: usize) !void { |
| 922 | | try r.ensureCapacity(a.len + (shift / Limb.bit_count) + 1); |
| 923 | | llshl(r.limbs[0..], a.limbs[0..a.len], shift); |
| 924 | | r.norm1(a.len + (shift / Limb.bit_count) + 1); |
| 925 | | r.positive = a.positive; |
| 974 | r.assertWritable(); |
| 975 | |
| 976 | try r.ensureCapacity(a.len() + (shift / Limb.bit_count) + 1); |
| 977 | llshl(r.limbs[0..], a.limbs[0..a.len()], shift); |
| 978 | r.normalize(a.len() + (shift / Limb.bit_count) + 1); |
| 979 | r.setSign(a.isPositive()); |
| 926 | 980 | } |
| 927 | 981 | |
| 928 | 982 | fn llshl(r: []Limb, a: []const Limb, shift: usize) void { |
| ... | ... | @@ -950,17 +1004,18 @@ pub const Int = struct { |
| 950 | 1004 | |
| 951 | 1005 | // r = a >> shift |
| 952 | 1006 | pub fn shiftRight(r: *Int, a: Int, shift: usize) !void { |
| 953 | | if (a.len <= shift / Limb.bit_count) { |
| 954 | | r.len = 1; |
| 1007 | r.assertWritable(); |
| 1008 | |
| 1009 | if (a.len() <= shift / Limb.bit_count) { |
| 1010 | r.metadata = 1; |
| 955 | 1011 | r.limbs[0] = 0; |
| 956 | | r.positive = true; |
| 957 | 1012 | return; |
| 958 | 1013 | } |
| 959 | 1014 | |
| 960 | | try r.ensureCapacity(a.len - (shift / Limb.bit_count)); |
| 961 | | const r_len = llshr(r.limbs[0..], a.limbs[0..a.len], shift); |
| 962 | | r.len = a.len - (shift / Limb.bit_count); |
| 963 | | r.positive = a.positive; |
| 1015 | try r.ensureCapacity(a.len() - (shift / Limb.bit_count)); |
| 1016 | const r_len = llshr(r.limbs[0..], a.limbs[0..a.len()], shift); |
| 1017 | r.metadata = a.len() - (shift / Limb.bit_count); |
| 1018 | r.setSign(a.isPositive()); |
| 964 | 1019 | } |
| 965 | 1020 | |
| 966 | 1021 | fn llshr(r: []Limb, a: []const Limb, shift: usize) void { |
| ... | ... | @@ -985,14 +1040,16 @@ pub const Int = struct { |
| 985 | 1040 | |
| 986 | 1041 | // r = a | b |
| 987 | 1042 | pub fn bitOr(r: *Int, a: Int, b: Int) !void { |
| 988 | | if (a.len > b.len) { |
| 989 | | try r.ensureCapacity(a.len); |
| 990 | | llor(r.limbs[0..], a.limbs[0..a.len], b.limbs[0..b.len]); |
| 991 | | r.len = a.len; |
| 1043 | r.assertWritable(); |
| 1044 | |
| 1045 | if (a.len() > b.len()) { |
| 1046 | try r.ensureCapacity(a.len()); |
| 1047 | llor(r.limbs[0..], a.limbs[0..a.len()], b.limbs[0..b.len()]); |
| 1048 | r.setLen(a.len()); |
| 992 | 1049 | } else { |
| 993 | | try r.ensureCapacity(b.len); |
| 994 | | llor(r.limbs[0..], b.limbs[0..b.len], a.limbs[0..a.len]); |
| 995 | | r.len = b.len; |
| 1050 | try r.ensureCapacity(b.len()); |
| 1051 | llor(r.limbs[0..], b.limbs[0..b.len()], a.limbs[0..a.len()]); |
| 1052 | r.setLen(b.len()); |
| 996 | 1053 | } |
| 997 | 1054 | } |
| 998 | 1055 | |
| ... | ... | @@ -1012,14 +1069,16 @@ pub const Int = struct { |
| 1012 | 1069 | |
| 1013 | 1070 | // r = a & b |
| 1014 | 1071 | pub fn bitAnd(r: *Int, a: Int, b: Int) !void { |
| 1015 | | if (a.len > b.len) { |
| 1016 | | try r.ensureCapacity(b.len); |
| 1017 | | lland(r.limbs[0..], a.limbs[0..a.len], b.limbs[0..b.len]); |
| 1018 | | r.normN(b.len); |
| 1072 | r.assertWritable(); |
| 1073 | |
| 1074 | if (a.len() > b.len()) { |
| 1075 | try r.ensureCapacity(b.len()); |
| 1076 | lland(r.limbs[0..], a.limbs[0..a.len()], b.limbs[0..b.len()]); |
| 1077 | r.normalize(b.len()); |
| 1019 | 1078 | } else { |
| 1020 | | try r.ensureCapacity(a.len); |
| 1021 | | lland(r.limbs[0..], b.limbs[0..b.len], a.limbs[0..a.len]); |
| 1022 | | r.normN(a.len); |
| 1079 | try r.ensureCapacity(a.len()); |
| 1080 | lland(r.limbs[0..], b.limbs[0..b.len()], a.limbs[0..a.len()]); |
| 1081 | r.normalize(a.len()); |
| 1023 | 1082 | } |
| 1024 | 1083 | } |
| 1025 | 1084 | |
| ... | ... | @@ -1036,14 +1095,16 @@ pub const Int = struct { |
| 1036 | 1095 | |
| 1037 | 1096 | // r = a ^ b |
| 1038 | 1097 | pub fn bitXor(r: *Int, a: Int, b: Int) !void { |
| 1039 | | if (a.len > b.len) { |
| 1040 | | try r.ensureCapacity(a.len); |
| 1041 | | llxor(r.limbs[0..], a.limbs[0..a.len], b.limbs[0..b.len]); |
| 1042 | | r.normN(a.len); |
| 1098 | r.assertWritable(); |
| 1099 | |
| 1100 | if (a.len() > b.len()) { |
| 1101 | try r.ensureCapacity(a.len()); |
| 1102 | llxor(r.limbs[0..], a.limbs[0..a.len()], b.limbs[0..b.len()]); |
| 1103 | r.normalize(a.len()); |
| 1043 | 1104 | } else { |
| 1044 | | try r.ensureCapacity(b.len); |
| 1045 | | llxor(r.limbs[0..], b.limbs[0..b.len], a.limbs[0..a.len]); |
| 1046 | | r.normN(b.len); |
| 1105 | try r.ensureCapacity(b.len()); |
| 1106 | llxor(r.limbs[0..], b.limbs[0..b.len()], a.limbs[0..a.len()]); |
| 1107 | r.normalize(b.len()); |
| 1047 | 1108 | } |
| 1048 | 1109 | } |
| 1049 | 1110 | |
| ... | ... | @@ -1067,7 +1128,9 @@ pub const Int = struct { |
| 1067 | 1128 | // They will still run on larger than this and should pass, but the multi-limb code-paths |
| 1068 | 1129 | // may be untested in some cases. |
| 1069 | 1130 | |
| 1070 | | const al = debug.global_allocator; |
| 1131 | var buffer: [64 * 8192]u8 = undefined; |
| 1132 | var fixed = std.heap.FixedBufferAllocator.init(buffer[0..]); |
| 1133 | const al = &fixed.allocator; |
| 1071 | 1134 | |
| 1072 | 1135 | test "big.int comptime_int set" { |
| 1073 | 1136 | comptime var s = 0xefffffff00000001eeeeeeefaaaaaaab; |
| ... | ... | @@ -1088,14 +1151,14 @@ test "big.int comptime_int set negative" { |
| 1088 | 1151 | var a = try Int.initSet(al, -10); |
| 1089 | 1152 | |
| 1090 | 1153 | testing.expect(a.limbs[0] == 10); |
| 1091 | | testing.expect(a.positive == false); |
| 1154 | testing.expect(a.isPositive() == false); |
| 1092 | 1155 | } |
| 1093 | 1156 | |
| 1094 | 1157 | test "big.int int set unaligned small" { |
| 1095 | 1158 | var a = try Int.initSet(al, u7(45)); |
| 1096 | 1159 | |
| 1097 | 1160 | testing.expect(a.limbs[0] == 45); |
| 1098 | | testing.expect(a.positive == true); |
| 1161 | testing.expect(a.isPositive() == true); |
| 1099 | 1162 | } |
| 1100 | 1163 | |
| 1101 | 1164 | test "big.int comptime_int to" { |
| ... | ... | @@ -1116,7 +1179,7 @@ test "big.int to target too small error" { |
| 1116 | 1179 | testing.expectError(error.TargetTooSmall, a.to(u8)); |
| 1117 | 1180 | } |
| 1118 | 1181 | |
| 1119 | | test "big.int norm1" { |
| 1182 | test "big.int normalize" { |
| 1120 | 1183 | var a = try Int.init(al); |
| 1121 | 1184 | try a.ensureCapacity(8); |
| 1122 | 1185 | |
| ... | ... | @@ -1124,26 +1187,26 @@ test "big.int norm1" { |
| 1124 | 1187 | a.limbs[1] = 2; |
| 1125 | 1188 | a.limbs[2] = 3; |
| 1126 | 1189 | a.limbs[3] = 0; |
| 1127 | | a.norm1(4); |
| 1128 | | testing.expect(a.len == 3); |
| 1190 | a.normalize(4); |
| 1191 | testing.expect(a.len() == 3); |
| 1129 | 1192 | |
| 1130 | 1193 | a.limbs[0] = 1; |
| 1131 | 1194 | a.limbs[1] = 2; |
| 1132 | 1195 | a.limbs[2] = 3; |
| 1133 | | a.norm1(3); |
| 1134 | | testing.expect(a.len == 3); |
| 1196 | a.normalize(3); |
| 1197 | testing.expect(a.len() == 3); |
| 1135 | 1198 | |
| 1136 | 1199 | a.limbs[0] = 0; |
| 1137 | 1200 | a.limbs[1] = 0; |
| 1138 | | a.norm1(2); |
| 1139 | | testing.expect(a.len == 1); |
| 1201 | a.normalize(2); |
| 1202 | testing.expect(a.len() == 1); |
| 1140 | 1203 | |
| 1141 | 1204 | a.limbs[0] = 0; |
| 1142 | | a.norm1(1); |
| 1143 | | testing.expect(a.len == 1); |
| 1205 | a.normalize(1); |
| 1206 | testing.expect(a.len() == 1); |
| 1144 | 1207 | } |
| 1145 | 1208 | |
| 1146 | | test "big.int normN" { |
| 1209 | test "big.int normalize multi" { |
| 1147 | 1210 | var a = try Int.init(al); |
| 1148 | 1211 | try a.ensureCapacity(8); |
| 1149 | 1212 | |
| ... | ... | @@ -1151,25 +1214,25 @@ test "big.int normN" { |
| 1151 | 1214 | a.limbs[1] = 2; |
| 1152 | 1215 | a.limbs[2] = 0; |
| 1153 | 1216 | a.limbs[3] = 0; |
| 1154 | | a.normN(4); |
| 1155 | | testing.expect(a.len == 2); |
| 1217 | a.normalize(4); |
| 1218 | testing.expect(a.len() == 2); |
| 1156 | 1219 | |
| 1157 | 1220 | a.limbs[0] = 1; |
| 1158 | 1221 | a.limbs[1] = 2; |
| 1159 | 1222 | a.limbs[2] = 3; |
| 1160 | | a.normN(3); |
| 1161 | | testing.expect(a.len == 3); |
| 1223 | a.normalize(3); |
| 1224 | testing.expect(a.len() == 3); |
| 1162 | 1225 | |
| 1163 | 1226 | a.limbs[0] = 0; |
| 1164 | 1227 | a.limbs[1] = 0; |
| 1165 | 1228 | a.limbs[2] = 0; |
| 1166 | 1229 | a.limbs[3] = 0; |
| 1167 | | a.normN(4); |
| 1168 | | testing.expect(a.len == 1); |
| 1230 | a.normalize(4); |
| 1231 | testing.expect(a.len() == 1); |
| 1169 | 1232 | |
| 1170 | 1233 | a.limbs[0] = 0; |
| 1171 | | a.normN(1); |
| 1172 | | testing.expect(a.len == 1); |
| 1234 | a.normalize(1); |
| 1235 | testing.expect(a.len() == 1); |
| 1173 | 1236 | } |
| 1174 | 1237 | |
| 1175 | 1238 | test "big.int parity" { |
| ... | ... | @@ -1204,7 +1267,7 @@ test "big.int bitcount + sizeInBase" { |
| 1204 | 1267 | try a.shiftLeft(a, 5000); |
| 1205 | 1268 | testing.expect(a.bitCountAbs() == 5032); |
| 1206 | 1269 | testing.expect(a.sizeInBase(2) >= 5032); |
| 1207 | | a.positive = false; |
| 1270 | a.setSign(false); |
| 1208 | 1271 | |
| 1209 | 1272 | testing.expect(a.bitCountAbs() == 5032); |
| 1210 | 1273 | testing.expect(a.sizeInBase(2) >= 5033); |
| ... | ... | @@ -1980,6 +2043,98 @@ test "big.int div multi-multi (3.1/3.3 branch)" { |
| 1980 | 2043 | testing.expect((try r.to(u256)) == 0x1111111111111111111110b12222222222222222282); |
| 1981 | 2044 | } |
| 1982 | 2045 | |
| 2046 | test "big.int div multi-single zero-limb trailing" { |
| 2047 | var a = try Int.initSet(al, 0x60000000000000000000000000000000000000000000000000000000000000000); |
| 2048 | var b = try Int.initSet(al, 0x10000000000000000); |
| 2049 | |
| 2050 | var q = try Int.init(al); |
| 2051 | var r = try Int.init(al); |
| 2052 | try Int.divTrunc(&q, &r, a, b); |
| 2053 | |
| 2054 | var expected = try Int.initSet(al, 0x6000000000000000000000000000000000000000000000000); |
| 2055 | testing.expect(q.eq(expected)); |
| 2056 | testing.expect(r.eqZero()); |
| 2057 | } |
| 2058 | |
| 2059 | test "big.int div multi-multi zero-limb trailing (with rem)" { |
| 2060 | var a = try Int.initSet(al, 0x86666666555555558888888777777776111111111111111100000000000000000000000000000000); |
| 2061 | var b = try Int.initSet(al, 0x8666666655555555444444443333333300000000000000000000000000000000); |
| 2062 | |
| 2063 | var q = try Int.init(al); |
| 2064 | var r = try Int.init(al); |
| 2065 | try Int.divTrunc(&q, &r, a, b); |
| 2066 | |
| 2067 | testing.expect((try q.to(u128)) == 0x10000000000000000); |
| 2068 | |
| 2069 | const rs = try r.toString(al, 16); |
| 2070 | testing.expect(std.mem.eql(u8, rs, "4444444344444443111111111111111100000000000000000000000000000000")); |
| 2071 | } |
| 2072 | |
| 2073 | test "big.int div multi-multi zero-limb trailing (with rem) and dividend zero-limb count > divisor zero-limb count" { |
| 2074 | var a = try Int.initSet(al, 0x8666666655555555888888877777777611111111111111110000000000000000); |
| 2075 | var b = try Int.initSet(al, 0x8666666655555555444444443333333300000000000000000000000000000000); |
| 2076 | |
| 2077 | var q = try Int.init(al); |
| 2078 | var r = try Int.init(al); |
| 2079 | try Int.divTrunc(&q, &r, a, b); |
| 2080 | |
| 2081 | testing.expect((try q.to(u128)) == 0x1); |
| 2082 | |
| 2083 | const rs = try r.toString(al, 16); |
| 2084 | testing.expect(std.mem.eql(u8, rs, "444444434444444311111111111111110000000000000000")); |
| 2085 | } |
| 2086 | |
| 2087 | test "big.int div multi-multi zero-limb trailing (with rem) and dividend zero-limb count < divisor zero-limb count" { |
| 2088 | var a = try Int.initSet(al, 0x86666666555555558888888777777776111111111111111100000000000000000000000000000000); |
| 2089 | var b = try Int.initSet(al, 0x866666665555555544444444333333330000000000000000); |
| 2090 | |
| 2091 | var q = try Int.init(al); |
| 2092 | var r = try Int.init(al); |
| 2093 | try Int.divTrunc(&q, &r, a, b); |
| 2094 | |
| 2095 | const qs = try q.toString(al, 16); |
| 2096 | testing.expect(std.mem.eql(u8, qs, "10000000000000000820820803105186f")); |
| 2097 | |
| 2098 | const rs = try r.toString(al, 16); |
| 2099 | testing.expect(std.mem.eql(u8, rs, "4e11f2baa5896a321d463b543d0104e30000000000000000")); |
| 2100 | } |
| 2101 | |
| 2102 | test "big.int div multi-multi fuzz case #1" { |
| 2103 | var a = try Int.init(al); |
| 2104 | var b = try Int.init(al); |
| 2105 | |
| 2106 | try a.setString(16, "ffffffffffffffffffffffffffffc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); |
| 2107 | try b.setString(16, "3ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0000000000000000000000000000000000001ffffffffffffffffffffffffffffffffffffffffffffffffffc000000000000000000000000000000007fffffffffff"); |
| 2108 | |
| 2109 | var q = try Int.init(al); |
| 2110 | var r = try Int.init(al); |
| 2111 | try Int.divTrunc(&q, &r, a, b); |
| 2112 | |
| 2113 | const qs = try q.toString(al, 16); |
| 2114 | testing.expect(std.mem.eql(u8, qs, "3ffffffffffffffffffffffffffff0000000000000000000000000000000000001ffffffffffffffffffffffffffff7fffffffe000000000000000000000000000180000000000000000000003fffffbfffffffdfffffffffffffeffff800000100101000000100000000020003fffffdfbfffffe3ffffffffffffeffff7fffc00800a100000017ffe000002000400007efbfff7fe9f00000037ffff3fff7fffa004006100000009ffe00000190038200bf7d2ff7fefe80400060000f7d7f8fbf9401fe38e0403ffc0bdffffa51102c300d7be5ef9df4e5060007b0127ad3fa69f97d0f820b6605ff617ddf7f32ad7a05c0d03f2e7bc78a6000e087a8bbcdc59e07a5a079128a7861f553ddebed7e8e56701756f9ead39b48cd1b0831889ea6ec1fddf643d0565b075ff07e6caea4e2854ec9227fd635ed60a2f5eef2893052ffd54718fa08604acbf6a15e78a467c4a3c53c0278af06c4416573f925491b195e8fd79302cb1aaf7caf4ecfc9aec1254cc969786363ac729f914c6ddcc26738d6b0facd54eba026580aba2eb6482a088b0d224a8852420b91ec1")); |
| 2115 | |
| 2116 | const rs = try r.toString(al, 16); |
| 2117 | testing.expect(std.mem.eql(u8, rs, "310d1d4c414426b4836c2635bad1df3a424e50cbdd167ffccb4dfff57d36b4aae0d6ca0910698220171a0f3373c1060a046c2812f0027e321f72979daa5e7973214170d49e885de0c0ecc167837d44502430674a82522e5df6a0759548052420b91ec1")); |
| 2118 | } |
| 2119 | |
| 2120 | test "big.int div multi-multi fuzz case #2" { |
| 2121 | var a = try Int.init(al); |
| 2122 | var b = try Int.init(al); |
| 2123 | |
| 2124 | try a.setString(16, "3ffffffffe00000000000000000000000000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffe000000000000000000000000000000000000000000000000000000000000001fffffffffffffffff800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffc000000000000000000000000000000000000000000000000000000000000000"); |
| 2125 | try b.setString(16, "ffc0000000000000000000000000000000000000000000000000"); |
| 2126 | |
| 2127 | var q = try Int.init(al); |
| 2128 | var r = try Int.init(al); |
| 2129 | try Int.divTrunc(&q, &r, a, b); |
| 2130 | |
| 2131 | const qs = try q.toString(al, 16); |
| 2132 | testing.expect(std.mem.eql(u8, qs, "40100400fe3f8fe3f8fe3f8fe3f8fe3f8fe4f93e4f93e4f93e4f93e4f93e4f93e4f93e4f93e4f93e4f93e4f93e4f91e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4992649926499264991e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4792e4b92e4b92e4b92e4b92a4a92a4a92a4")); |
| 2133 | |
| 2134 | const rs = try r.toString(al, 16); |
| 2135 | testing.expect(std.mem.eql(u8, rs, "a900000000000000000000000000000000000000000000000000")); |
| 2136 | } |
| 2137 | |
| 1983 | 2138 | test "big.int shift-right single" { |
| 1984 | 2139 | var a = try Int.initSet(al, 0xffff0000); |
| 1985 | 2140 | try a.shiftRight(a, 16); |