| ... | ... | @@ -22,13 +22,18 @@ comptime { |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | pub const Int = struct { |
| 25 | const sign_bit: usize = 1 << (usize.bit_count - 1); |
| 26 | |
| 25 | 27 | allocator: ?*Allocator, |
| 26 | | positive: bool, |
| 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,26 +50,45 @@ 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 | |
| 58 | 83 | // Initialize an Int directly from a fixed set of limb values. This is considered read-only |
| 59 | 84 | // and cannot be used as a receiver argument to any functions. If this tries to allocate |
| 60 | 85 | // at any point a panic will occur due to the null allocator. |
| 61 | 86 | pub fn initFixed(limbs: []const Limb) Int { |
| 62 | 87 | var self = Int{ |
| 63 | 88 | .allocator = null, |
| 64 | | .positive = true, |
| 89 | .metadata = limbs.len, |
| 65 | 90 | // Cast away the const, invalid use to pass as a pointer argument. |
| 66 | 91 | .limbs = @intToPtr([*]Limb, @ptrToInt(limbs.ptr))[0..limbs.len], |
| 67 | | .len = limbs.len, |
| 68 | 92 | }; |
| 69 | 93 | |
| 70 | 94 | self.normalize(limbs.len); |
| ... | ... | @@ -96,13 +120,12 @@ pub const Int = struct { |
| 96 | 120 | other.assertWritable(); |
| 97 | 121 | return Int{ |
| 98 | 122 | .allocator = other.allocator, |
| 99 | | .positive = other.positive, |
| 123 | .metadata = other.metadata, |
| 100 | 124 | .limbs = block: { |
| 101 | | var limbs = try other.allocator.?.alloc(Limb, other.len); |
| 102 | | 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()]); |
| 103 | 127 | break :block limbs; |
| 104 | 128 | }, |
| 105 | | .len = other.len, |
| 106 | 129 | }; |
| 107 | 130 | } |
| 108 | 131 | |
| ... | ... | @@ -112,10 +135,9 @@ pub const Int = struct { |
| 112 | 135 | return; |
| 113 | 136 | } |
| 114 | 137 | |
| 115 | | self.positive = other.positive; |
| 116 | | try self.ensureCapacity(other.len); |
| 117 | | mem.copy(Limb, self.limbs[0..], other.limbs[0..other.len]); |
| 118 | | 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; |
| 119 | 141 | } |
| 120 | 142 | |
| 121 | 143 | pub fn swap(self: *Int, other: *Int) void { |
| ... | ... | @@ -131,11 +153,11 @@ pub const Int = struct { |
| 131 | 153 | } |
| 132 | 154 | |
| 133 | 155 | pub fn negate(self: *Int) void { |
| 134 | | self.positive = !self.positive; |
| 156 | self.metadata ^= sign_bit; |
| 135 | 157 | } |
| 136 | 158 | |
| 137 | 159 | pub fn abs(self: *Int) void { |
| 138 | | self.positive = true; |
| 160 | self.metadata &= ~sign_bit; |
| 139 | 161 | } |
| 140 | 162 | |
| 141 | 163 | pub fn isOdd(self: Int) bool { |
| ... | ... | @@ -148,7 +170,7 @@ pub const Int = struct { |
| 148 | 170 | |
| 149 | 171 | // Returns the number of bits required to represent the absolute value of self. |
| 150 | 172 | fn bitCountAbs(self: Int) usize { |
| 151 | | 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])); |
| 152 | 174 | } |
| 153 | 175 | |
| 154 | 176 | // Returns the number of bits required to represent the integer in twos-complement form. |
| ... | ... | @@ -164,11 +186,11 @@ pub const Int = struct { |
| 164 | 186 | |
| 165 | 187 | // If the entire value has only one bit set (e.g. 0b100000000) then the negation in twos |
| 166 | 188 | // complement requires one less bit. |
| 167 | | if (!self.positive) block: { |
| 189 | if (!self.isPositive()) block: { |
| 168 | 190 | bits += 1; |
| 169 | 191 | |
| 170 | | if (@popCount(self.limbs[self.len - 1]) == 1) { |
| 171 | | 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| { |
| 172 | 194 | if (@popCount(limb) != 0) { |
| 173 | 195 | break :block; |
| 174 | 196 | } |
| ... | ... | @@ -185,11 +207,11 @@ pub const Int = struct { |
| 185 | 207 | if (self.eqZero()) { |
| 186 | 208 | return true; |
| 187 | 209 | } |
| 188 | | if (!is_signed and !self.positive) { |
| 210 | if (!is_signed and !self.isPositive()) { |
| 189 | 211 | return false; |
| 190 | 212 | } |
| 191 | 213 | |
| 192 | | const req_bits = self.bitCountTwosComp() + @boolToInt(self.positive and is_signed); |
| 214 | const req_bits = self.bitCountTwosComp() + @boolToInt(self.isPositive() and is_signed); |
| 193 | 215 | return bit_count >= req_bits; |
| 194 | 216 | } |
| 195 | 217 | |
| ... | ... | @@ -201,7 +223,7 @@ pub const Int = struct { |
| 201 | 223 | // the minus sign. This is used for determining the number of characters needed to print the |
| 202 | 224 | // value. It is inexact and will exceed the given value by 1-2 digits. |
| 203 | 225 | pub fn sizeInBase(self: Int, base: usize) usize { |
| 204 | | const bit_count = usize(@boolToInt(!self.positive)) + self.bitCountAbs(); |
| 226 | const bit_count = usize(@boolToInt(!self.isPositive())) + self.bitCountAbs(); |
| 205 | 227 | return (bit_count / math.log2(base)) + 1; |
| 206 | 228 | } |
| 207 | 229 | |
| ... | ... | @@ -214,19 +236,19 @@ pub const Int = struct { |
| 214 | 236 | const UT = if (T.is_signed) @IntType(false, T.bit_count - 1) else T; |
| 215 | 237 | |
| 216 | 238 | try self.ensureCapacity(@sizeOf(UT) / @sizeOf(Limb)); |
| 217 | | self.positive = value >= 0; |
| 218 | | self.len = 0; |
| 239 | self.metadata = 0; |
| 240 | self.setSign(value >= 0); |
| 219 | 241 | |
| 220 | 242 | var w_value: UT = if (value < 0) @intCast(UT, -value) else @intCast(UT, value); |
| 221 | 243 | |
| 222 | 244 | if (info.bits <= Limb.bit_count) { |
| 223 | 245 | self.limbs[0] = Limb(w_value); |
| 224 | | self.len = 1; |
| 246 | self.metadata += 1; |
| 225 | 247 | } else { |
| 226 | 248 | var i: usize = 0; |
| 227 | 249 | while (w_value != 0) : (i += 1) { |
| 228 | 250 | self.limbs[i] = @truncate(Limb, w_value); |
| 229 | | self.len += 1; |
| 251 | self.metadata += 1; |
| 230 | 252 | |
| 231 | 253 | // TODO: shift == 64 at compile-time fails. Fails on u128 limbs. |
| 232 | 254 | w_value >>= Limb.bit_count / 2; |
| ... | ... | @@ -240,8 +262,8 @@ pub const Int = struct { |
| 240 | 262 | const req_limbs = @divFloor(math.log2(w_value), Limb.bit_count) + 1; |
| 241 | 263 | try self.ensureCapacity(req_limbs); |
| 242 | 264 | |
| 243 | | self.positive = value >= 0; |
| 244 | | self.len = req_limbs; |
| 265 | self.metadata = req_limbs; |
| 266 | self.setSign(value >= 0); |
| 245 | 267 | |
| 246 | 268 | if (w_value <= maxInt(Limb)) { |
| 247 | 269 | self.limbs[0] = w_value; |
| ... | ... | @@ -282,17 +304,17 @@ pub const Int = struct { |
| 282 | 304 | if (@sizeOf(UT) <= @sizeOf(Limb)) { |
| 283 | 305 | r = @intCast(UT, self.limbs[0]); |
| 284 | 306 | } else { |
| 285 | | for (self.limbs[0..self.len]) |_, ri| { |
| 286 | | 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]; |
| 287 | 309 | r <<= Limb.bit_count; |
| 288 | 310 | r |= limb; |
| 289 | 311 | } |
| 290 | 312 | } |
| 291 | 313 | |
| 292 | 314 | if (!T.is_signed) { |
| 293 | | return if (self.positive) @intCast(T, r) else error.NegativeIntoUnsigned; |
| 315 | return if (self.isPositive()) @intCast(T, r) else error.NegativeIntoUnsigned; |
| 294 | 316 | } else { |
| 295 | | if (self.positive) { |
| 317 | if (self.isPositive()) { |
| 296 | 318 | return @intCast(T, r); |
| 297 | 319 | } else { |
| 298 | 320 | if (math.cast(T, r)) |ok| { |
| ... | ... | @@ -355,7 +377,7 @@ pub const Int = struct { |
| 355 | 377 | try self.mul(self.*, ap_base); |
| 356 | 378 | try self.add(self.*, ap_d); |
| 357 | 379 | } |
| 358 | | self.positive = positive; |
| 380 | self.setSign(positive); |
| 359 | 381 | } |
| 360 | 382 | |
| 361 | 383 | /// TODO make this call format instead of the other way around |
| ... | ... | @@ -377,7 +399,7 @@ pub const Int = struct { |
| 377 | 399 | if (base & (base - 1) == 0) { |
| 378 | 400 | const base_shift = math.log2_int(Limb, base); |
| 379 | 401 | |
| 380 | | for (self.limbs[0..self.len]) |limb| { |
| 402 | for (self.limbs[0..self.len()]) |limb| { |
| 381 | 403 | var shift: usize = 0; |
| 382 | 404 | while (shift < Limb.bit_count) : (shift += base_shift) { |
| 383 | 405 | const r = @intCast(u8, (limb >> @intCast(Log2Limb, shift)) & Limb(base - 1)); |
| ... | ... | @@ -404,11 +426,11 @@ pub const Int = struct { |
| 404 | 426 | } |
| 405 | 427 | |
| 406 | 428 | var q = try self.clone(); |
| 407 | | q.positive = true; |
| 429 | q.abs(); |
| 408 | 430 | var r = try Int.init(allocator); |
| 409 | 431 | var b = try Int.initSet(allocator, limb_base); |
| 410 | 432 | |
| 411 | | while (q.len >= 2) { |
| 433 | while (q.len() >= 2) { |
| 412 | 434 | try Int.divTrunc(&q, &r, q, b); |
| 413 | 435 | |
| 414 | 436 | var r_word = r.limbs[0]; |
| ... | ... | @@ -421,7 +443,7 @@ pub const Int = struct { |
| 421 | 443 | } |
| 422 | 444 | |
| 423 | 445 | { |
| 424 | | debug.assert(q.len == 1); |
| 446 | debug.assert(q.len() == 1); |
| 425 | 447 | |
| 426 | 448 | var r_word = q.limbs[0]; |
| 427 | 449 | while (r_word != 0) { |
| ... | ... | @@ -432,7 +454,7 @@ pub const Int = struct { |
| 432 | 454 | } |
| 433 | 455 | } |
| 434 | 456 | |
| 435 | | if (!self.positive) { |
| 457 | if (!self.isPositive()) { |
| 436 | 458 | try digits.append('-'); |
| 437 | 459 | } |
| 438 | 460 | |
| ... | ... | @@ -460,14 +482,14 @@ pub const Int = struct { |
| 460 | 482 | |
| 461 | 483 | // returns -1, 0, 1 if |a| < |b|, |a| == |b| or |a| > |b| respectively. |
| 462 | 484 | pub fn cmpAbs(a: Int, b: Int) i8 { |
| 463 | | if (a.len < b.len) { |
| 485 | if (a.len() < b.len()) { |
| 464 | 486 | return -1; |
| 465 | 487 | } |
| 466 | | if (a.len > b.len) { |
| 488 | if (a.len() > b.len()) { |
| 467 | 489 | return 1; |
| 468 | 490 | } |
| 469 | 491 | |
| 470 | | var i: usize = a.len - 1; |
| 492 | var i: usize = a.len() - 1; |
| 471 | 493 | while (i != 0) : (i -= 1) { |
| 472 | 494 | if (a.limbs[i] != b.limbs[i]) { |
| 473 | 495 | break; |
| ... | ... | @@ -485,17 +507,17 @@ pub const Int = struct { |
| 485 | 507 | |
| 486 | 508 | // returns -1, 0, 1 if a < b, a == b or a > b respectively. |
| 487 | 509 | pub fn cmp(a: Int, b: Int) i8 { |
| 488 | | if (a.positive != b.positive) { |
| 489 | | return if (a.positive) i8(1) else -1; |
| 510 | if (a.isPositive() != b.isPositive()) { |
| 511 | return if (a.isPositive()) i8(1) else -1; |
| 490 | 512 | } else { |
| 491 | 513 | const r = cmpAbs(a, b); |
| 492 | | return if (a.positive) r else -r; |
| 514 | return if (a.isPositive()) r else -r; |
| 493 | 515 | } |
| 494 | 516 | } |
| 495 | 517 | |
| 496 | 518 | // if a == 0 |
| 497 | 519 | pub fn eqZero(a: Int) bool { |
| 498 | | return a.len == 1 and a.limbs[0] == 0; |
| 520 | return a.len() == 1 and a.limbs[0] == 0; |
| 499 | 521 | } |
| 500 | 522 | |
| 501 | 523 | // if |a| == |b| |
| ... | ... | @@ -525,16 +547,15 @@ pub const Int = struct { |
| 525 | 547 | } |
| 526 | 548 | |
| 527 | 549 | // Handle zero |
| 528 | | r.len = if (j != 0) j else 1; |
| 550 | r.setLen(if (j != 0) j else 1); |
| 529 | 551 | } |
| 530 | 552 | |
| 531 | 553 | // Cannot be used as a result argument to any function. |
| 532 | 554 | fn readOnlyPositive(a: Int) Int { |
| 533 | 555 | return Int{ |
| 534 | 556 | .allocator = null, |
| 535 | | .positive = true, |
| 557 | .metadata = a.len(), |
| 536 | 558 | .limbs = a.limbs, |
| 537 | | .len = a.len, |
| 538 | 559 | }; |
| 539 | 560 | } |
| 540 | 561 | |
| ... | ... | @@ -549,8 +570,8 @@ pub const Int = struct { |
| 549 | 570 | return; |
| 550 | 571 | } |
| 551 | 572 | |
| 552 | | if (a.positive != b.positive) { |
| 553 | | if (a.positive) { |
| 573 | if (a.isPositive() != b.isPositive()) { |
| 574 | if (a.isPositive()) { |
| 554 | 575 | // (a) + (-b) => a - b |
| 555 | 576 | try r.sub(a, readOnlyPositive(b)); |
| 556 | 577 | } else { |
| ... | ... | @@ -558,17 +579,17 @@ pub const Int = struct { |
| 558 | 579 | try r.sub(b, readOnlyPositive(a)); |
| 559 | 580 | } |
| 560 | 581 | } else { |
| 561 | | if (a.len >= b.len) { |
| 562 | | try r.ensureCapacity(a.len + 1); |
| 563 | | lladd(r.limbs[0..], a.limbs[0..a.len], b.limbs[0..b.len]); |
| 564 | | r.normalize(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); |
| 565 | 586 | } else { |
| 566 | | try r.ensureCapacity(b.len + 1); |
| 567 | | lladd(r.limbs[0..], b.limbs[0..b.len], a.limbs[0..a.len]); |
| 568 | | r.normalize(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); |
| 569 | 590 | } |
| 570 | 591 | |
| 571 | | r.positive = a.positive; |
| 592 | r.setSign(a.isPositive()); |
| 572 | 593 | } |
| 573 | 594 | } |
| 574 | 595 | |
| ... | ... | @@ -599,41 +620,41 @@ pub const Int = struct { |
| 599 | 620 | // r = a - b |
| 600 | 621 | pub fn sub(r: *Int, a: Int, b: Int) !void { |
| 601 | 622 | r.assertWritable(); |
| 602 | | if (a.positive != b.positive) { |
| 603 | | if (a.positive) { |
| 623 | if (a.isPositive() != b.isPositive()) { |
| 624 | if (a.isPositive()) { |
| 604 | 625 | // (a) - (-b) => a + b |
| 605 | 626 | try r.add(a, readOnlyPositive(b)); |
| 606 | 627 | } else { |
| 607 | 628 | // (-a) - (b) => -(a + b) |
| 608 | 629 | try r.add(readOnlyPositive(a), b); |
| 609 | | r.positive = false; |
| 630 | r.setSign(false); |
| 610 | 631 | } |
| 611 | 632 | } else { |
| 612 | | if (a.positive) { |
| 633 | if (a.isPositive()) { |
| 613 | 634 | // (a) - (b) => a - b |
| 614 | 635 | if (a.cmp(b) >= 0) { |
| 615 | | try r.ensureCapacity(a.len + 1); |
| 616 | | llsub(r.limbs[0..], a.limbs[0..a.len], b.limbs[0..b.len]); |
| 617 | | r.normalize(a.len); |
| 618 | | 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); |
| 619 | 640 | } else { |
| 620 | | try r.ensureCapacity(b.len + 1); |
| 621 | | llsub(r.limbs[0..], b.limbs[0..b.len], a.limbs[0..a.len]); |
| 622 | | r.normalize(b.len); |
| 623 | | 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); |
| 624 | 645 | } |
| 625 | 646 | } else { |
| 626 | 647 | // (-a) - (-b) => -(a - b) |
| 627 | 648 | if (a.cmp(b) < 0) { |
| 628 | | try r.ensureCapacity(a.len + 1); |
| 629 | | llsub(r.limbs[0..], a.limbs[0..a.len], b.limbs[0..b.len]); |
| 630 | | r.normalize(a.len); |
| 631 | | 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); |
| 632 | 653 | } else { |
| 633 | | try r.ensureCapacity(b.len + 1); |
| 634 | | llsub(r.limbs[0..], b.limbs[0..b.len], a.limbs[0..a.len]); |
| 635 | | r.normalize(b.len); |
| 636 | | 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); |
| 637 | 658 | } |
| 638 | 659 | } |
| 639 | 660 | } |
| ... | ... | @@ -674,7 +695,7 @@ pub const Int = struct { |
| 674 | 695 | |
| 675 | 696 | var sr: Int = undefined; |
| 676 | 697 | if (aliased) { |
| 677 | | sr = try Int.initCapacity(rma.allocator.?, a.len + b.len); |
| 698 | sr = try Int.initCapacity(rma.allocator.?, a.len() + b.len()); |
| 678 | 699 | r = &sr; |
| 679 | 700 | aliased = true; |
| 680 | 701 | } |
| ... | ... | @@ -683,16 +704,16 @@ pub const Int = struct { |
| 683 | 704 | r.deinit(); |
| 684 | 705 | }; |
| 685 | 706 | |
| 686 | | try r.ensureCapacity(a.len + b.len); |
| 707 | try r.ensureCapacity(a.len() + b.len()); |
| 687 | 708 | |
| 688 | | if (a.len >= b.len) { |
| 689 | | 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()]); |
| 690 | 711 | } else { |
| 691 | | 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()]); |
| 692 | 713 | } |
| 693 | 714 | |
| 694 | | r.positive = a.positive == b.positive; |
| 695 | | r.normalize(a.len + b.len); |
| 715 | r.normalize(a.len() + b.len()); |
| 716 | r.setSign(a.isPositive() == b.isPositive()); |
| 696 | 717 | } |
| 697 | 718 | |
| 698 | 719 | // a + b * c + *carry, sets carry to the overflow bits |
| ... | ... | @@ -742,17 +763,17 @@ pub const Int = struct { |
| 742 | 763 | try div(q, r, a, b); |
| 743 | 764 | |
| 744 | 765 | // Trunc -> Floor. |
| 745 | | if (!q.positive) { |
| 766 | if (!q.isPositive()) { |
| 746 | 767 | const one = Int.initFixed(([]Limb{1})[0..]); |
| 747 | 768 | try q.sub(q.*, one); |
| 748 | 769 | try r.add(q.*, one); |
| 749 | 770 | } |
| 750 | | r.positive = b.positive; |
| 771 | r.setSign(b.isPositive()); |
| 751 | 772 | } |
| 752 | 773 | |
| 753 | 774 | pub fn divTrunc(q: *Int, r: *Int, a: Int, b: Int) !void { |
| 754 | 775 | try div(q, r, a, b); |
| 755 | | r.positive = a.positive; |
| 776 | r.setSign(a.isPositive()); |
| 756 | 777 | } |
| 757 | 778 | |
| 758 | 779 | // Truncates by default. |
| ... | ... | @@ -770,10 +791,9 @@ pub const Int = struct { |
| 770 | 791 | if (a.cmpAbs(b) < 0) { |
| 771 | 792 | // quo may alias a so handle rem first |
| 772 | 793 | try rem.copy(a); |
| 773 | | rem.positive = a.positive == b.positive; |
| 794 | rem.setSign(a.isPositive() == b.isPositive()); |
| 774 | 795 | |
| 775 | | quo.positive = true; |
| 776 | | quo.len = 1; |
| 796 | quo.metadata = 1; |
| 777 | 797 | quo.limbs[0] = 0; |
| 778 | 798 | return; |
| 779 | 799 | } |
| ... | ... | @@ -782,14 +802,14 @@ pub const Int = struct { |
| 782 | 802 | // algorithms. |
| 783 | 803 | const a_zero_limb_count = blk: { |
| 784 | 804 | var i: usize = 0; |
| 785 | | while (i < a.len) : (i += 1) { |
| 805 | while (i < a.len()) : (i += 1) { |
| 786 | 806 | if (a.limbs[i] != 0) break; |
| 787 | 807 | } |
| 788 | 808 | break :blk i; |
| 789 | 809 | }; |
| 790 | 810 | const b_zero_limb_count = blk: { |
| 791 | 811 | var i: usize = 0; |
| 792 | | while (i < b.len) : (i += 1) { |
| 812 | while (i < b.len()) : (i += 1) { |
| 793 | 813 | if (b.limbs[i] != 0) break; |
| 794 | 814 | } |
| 795 | 815 | break :blk i; |
| ... | ... | @@ -797,39 +817,37 @@ pub const Int = struct { |
| 797 | 817 | |
| 798 | 818 | const ab_zero_limb_count = std.math.min(a_zero_limb_count, b_zero_limb_count); |
| 799 | 819 | |
| 800 | | if (b.len - ab_zero_limb_count == 1) { |
| 801 | | try quo.ensureCapacity(a.len); |
| 820 | if (b.len() - ab_zero_limb_count == 1) { |
| 821 | try quo.ensureCapacity(a.len()); |
| 802 | 822 | |
| 803 | | lldiv1(quo.limbs[0..], &rem.limbs[0], a.limbs[ab_zero_limb_count..a.len], b.limbs[b.len - 1]); |
| 804 | | quo.normalize(a.len - ab_zero_limb_count); |
| 805 | | 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()); |
| 806 | 826 | |
| 807 | | rem.len = 1; |
| 808 | | rem.positive = true; |
| 827 | rem.metadata = 1; |
| 809 | 828 | } else { |
| 810 | 829 | // x and y are modified during division |
| 811 | | var x = try Int.initCapacity(quo.allocator.?, a.len); |
| 830 | var x = try Int.initCapacity(quo.allocator.?, a.len()); |
| 812 | 831 | defer x.deinit(); |
| 813 | 832 | try x.copy(a); |
| 814 | 833 | |
| 815 | | var y = try Int.initCapacity(quo.allocator.?, b.len); |
| 834 | var y = try Int.initCapacity(quo.allocator.?, b.len()); |
| 816 | 835 | defer y.deinit(); |
| 817 | 836 | try y.copy(b); |
| 818 | 837 | |
| 819 | 838 | // x may grow one limb during normalization |
| 820 | | try quo.ensureCapacity(a.len + y.len); |
| 839 | try quo.ensureCapacity(a.len() + y.len()); |
| 821 | 840 | |
| 822 | 841 | // Shrink x, y such that the trailing zero limbs shared between are removed. |
| 823 | 842 | if (ab_zero_limb_count != 0) { |
| 824 | 843 | std.mem.copy(Limb, x.limbs[0..], x.limbs[ab_zero_limb_count..]); |
| 825 | 844 | std.mem.copy(Limb, y.limbs[0..], y.limbs[ab_zero_limb_count..]); |
| 826 | | x.len -= ab_zero_limb_count; |
| 827 | | y.len -= ab_zero_limb_count; |
| 845 | x.metadata -= ab_zero_limb_count; |
| 846 | y.metadata -= ab_zero_limb_count; |
| 828 | 847 | } |
| 829 | 848 | |
| 830 | 849 | try divN(quo.allocator.?, quo, rem, &x, &y); |
| 831 | | |
| 832 | | quo.positive = a.positive == b.positive; |
| 850 | quo.setSign(a.isPositive() == b.isPositive()); |
| 833 | 851 | } |
| 834 | 852 | |
| 835 | 853 | if (ab_zero_limb_count != 0) { |
| ... | ... | @@ -868,28 +886,28 @@ pub const Int = struct { |
| 868 | 886 | // |
| 869 | 887 | // x = qy + r where 0 <= r < y |
| 870 | 888 | fn divN(allocator: *Allocator, q: *Int, r: *Int, x: *Int, y: *Int) !void { |
| 871 | | debug.assert(y.len >= 2); |
| 872 | | debug.assert(x.len >= y.len); |
| 873 | | 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); |
| 874 | 892 | debug.assert(default_capacity >= 3); // see 3.2 |
| 875 | 893 | |
| 876 | 894 | var tmp = try Int.init(allocator); |
| 877 | 895 | defer tmp.deinit(); |
| 878 | 896 | |
| 879 | 897 | // Normalize so y > Limb.bit_count / 2 (i.e. leading bit is set) and even |
| 880 | | var norm_shift = @clz(y.limbs[y.len - 1]); |
| 898 | var norm_shift = @clz(y.limbs[y.len() - 1]); |
| 881 | 899 | if (norm_shift == 0 and y.isOdd()) { |
| 882 | 900 | norm_shift = Limb.bit_count; |
| 883 | 901 | } |
| 884 | 902 | try x.shiftLeft(x.*, norm_shift); |
| 885 | 903 | try y.shiftLeft(y.*, norm_shift); |
| 886 | 904 | |
| 887 | | const n = x.len - 1; |
| 888 | | const t = y.len - 1; |
| 905 | const n = x.len() - 1; |
| 906 | const t = y.len() - 1; |
| 889 | 907 | |
| 890 | 908 | // 1. |
| 891 | | q.len = n - t + 1; |
| 892 | | 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); |
| 893 | 911 | |
| 894 | 912 | // 2. |
| 895 | 913 | try tmp.shiftLeft(y.*, Limb.bit_count * (n - t)); |
| ... | ... | @@ -937,7 +955,7 @@ pub const Int = struct { |
| 937 | 955 | try tmp.shiftLeft(tmp, Limb.bit_count * (i - t - 1)); |
| 938 | 956 | try x.sub(x.*, tmp); |
| 939 | 957 | |
| 940 | | if (!x.positive) { |
| 958 | if (!x.isPositive()) { |
| 941 | 959 | try tmp.shiftLeft(y.*, Limb.bit_count * (i - t - 1)); |
| 942 | 960 | try x.add(x.*, tmp); |
| 943 | 961 | q.limbs[i - t - 1] -= 1; |
| ... | ... | @@ -945,20 +963,20 @@ pub const Int = struct { |
| 945 | 963 | } |
| 946 | 964 | |
| 947 | 965 | // Denormalize |
| 948 | | q.normalize(q.len); |
| 966 | q.normalize(q.len()); |
| 949 | 967 | |
| 950 | 968 | try r.shiftRight(x.*, norm_shift); |
| 951 | | r.normalize(r.len); |
| 969 | r.normalize(r.len()); |
| 952 | 970 | } |
| 953 | 971 | |
| 954 | 972 | // r = a << shift, in other words, r = a * 2^shift |
| 955 | 973 | pub fn shiftLeft(r: *Int, a: Int, shift: usize) !void { |
| 956 | 974 | r.assertWritable(); |
| 957 | 975 | |
| 958 | | try r.ensureCapacity(a.len + (shift / Limb.bit_count) + 1); |
| 959 | | llshl(r.limbs[0..], a.limbs[0..a.len], shift); |
| 960 | | r.normalize(a.len + (shift / Limb.bit_count) + 1); |
| 961 | | r.positive = a.positive; |
| 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()); |
| 962 | 980 | } |
| 963 | 981 | |
| 964 | 982 | fn llshl(r: []Limb, a: []const Limb, shift: usize) void { |
| ... | ... | @@ -988,17 +1006,16 @@ pub const Int = struct { |
| 988 | 1006 | pub fn shiftRight(r: *Int, a: Int, shift: usize) !void { |
| 989 | 1007 | r.assertWritable(); |
| 990 | 1008 | |
| 991 | | if (a.len <= shift / Limb.bit_count) { |
| 992 | | r.len = 1; |
| 1009 | if (a.len() <= shift / Limb.bit_count) { |
| 1010 | r.metadata = 1; |
| 993 | 1011 | r.limbs[0] = 0; |
| 994 | | r.positive = true; |
| 995 | 1012 | return; |
| 996 | 1013 | } |
| 997 | 1014 | |
| 998 | | try r.ensureCapacity(a.len - (shift / Limb.bit_count)); |
| 999 | | const r_len = llshr(r.limbs[0..], a.limbs[0..a.len], shift); |
| 1000 | | r.len = a.len - (shift / Limb.bit_count); |
| 1001 | | 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()); |
| 1002 | 1019 | } |
| 1003 | 1020 | |
| 1004 | 1021 | fn llshr(r: []Limb, a: []const Limb, shift: usize) void { |
| ... | ... | @@ -1025,14 +1042,14 @@ pub const Int = struct { |
| 1025 | 1042 | pub fn bitOr(r: *Int, a: Int, b: Int) !void { |
| 1026 | 1043 | r.assertWritable(); |
| 1027 | 1044 | |
| 1028 | | if (a.len > b.len) { |
| 1029 | | try r.ensureCapacity(a.len); |
| 1030 | | llor(r.limbs[0..], a.limbs[0..a.len], b.limbs[0..b.len]); |
| 1031 | | r.len = a.len; |
| 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()); |
| 1032 | 1049 | } else { |
| 1033 | | try r.ensureCapacity(b.len); |
| 1034 | | llor(r.limbs[0..], b.limbs[0..b.len], a.limbs[0..a.len]); |
| 1035 | | 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()); |
| 1036 | 1053 | } |
| 1037 | 1054 | } |
| 1038 | 1055 | |
| ... | ... | @@ -1054,14 +1071,14 @@ pub const Int = struct { |
| 1054 | 1071 | pub fn bitAnd(r: *Int, a: Int, b: Int) !void { |
| 1055 | 1072 | r.assertWritable(); |
| 1056 | 1073 | |
| 1057 | | if (a.len > b.len) { |
| 1058 | | try r.ensureCapacity(b.len); |
| 1059 | | lland(r.limbs[0..], a.limbs[0..a.len], b.limbs[0..b.len]); |
| 1060 | | r.normalize(b.len); |
| 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()); |
| 1061 | 1078 | } else { |
| 1062 | | try r.ensureCapacity(a.len); |
| 1063 | | lland(r.limbs[0..], b.limbs[0..b.len], a.limbs[0..a.len]); |
| 1064 | | r.normalize(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()); |
| 1065 | 1082 | } |
| 1066 | 1083 | } |
| 1067 | 1084 | |
| ... | ... | @@ -1080,14 +1097,14 @@ pub const Int = struct { |
| 1080 | 1097 | pub fn bitXor(r: *Int, a: Int, b: Int) !void { |
| 1081 | 1098 | r.assertWritable(); |
| 1082 | 1099 | |
| 1083 | | if (a.len > b.len) { |
| 1084 | | try r.ensureCapacity(a.len); |
| 1085 | | llxor(r.limbs[0..], a.limbs[0..a.len], b.limbs[0..b.len]); |
| 1086 | | r.normalize(a.len); |
| 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()); |
| 1087 | 1104 | } else { |
| 1088 | | try r.ensureCapacity(b.len); |
| 1089 | | llxor(r.limbs[0..], b.limbs[0..b.len], a.limbs[0..a.len]); |
| 1090 | | r.normalize(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()); |
| 1091 | 1108 | } |
| 1092 | 1109 | } |
| 1093 | 1110 | |
| ... | ... | @@ -1134,14 +1151,14 @@ test "big.int comptime_int set negative" { |
| 1134 | 1151 | var a = try Int.initSet(al, -10); |
| 1135 | 1152 | |
| 1136 | 1153 | testing.expect(a.limbs[0] == 10); |
| 1137 | | testing.expect(a.positive == false); |
| 1154 | testing.expect(a.isPositive() == false); |
| 1138 | 1155 | } |
| 1139 | 1156 | |
| 1140 | 1157 | test "big.int int set unaligned small" { |
| 1141 | 1158 | var a = try Int.initSet(al, u7(45)); |
| 1142 | 1159 | |
| 1143 | 1160 | testing.expect(a.limbs[0] == 45); |
| 1144 | | testing.expect(a.positive == true); |
| 1161 | testing.expect(a.isPositive() == true); |
| 1145 | 1162 | } |
| 1146 | 1163 | |
| 1147 | 1164 | test "big.int comptime_int to" { |
| ... | ... | @@ -1171,22 +1188,22 @@ test "big.int normalize" { |
| 1171 | 1188 | a.limbs[2] = 3; |
| 1172 | 1189 | a.limbs[3] = 0; |
| 1173 | 1190 | a.normalize(4); |
| 1174 | | testing.expect(a.len == 3); |
| 1191 | testing.expect(a.len() == 3); |
| 1175 | 1192 | |
| 1176 | 1193 | a.limbs[0] = 1; |
| 1177 | 1194 | a.limbs[1] = 2; |
| 1178 | 1195 | a.limbs[2] = 3; |
| 1179 | 1196 | a.normalize(3); |
| 1180 | | testing.expect(a.len == 3); |
| 1197 | testing.expect(a.len() == 3); |
| 1181 | 1198 | |
| 1182 | 1199 | a.limbs[0] = 0; |
| 1183 | 1200 | a.limbs[1] = 0; |
| 1184 | 1201 | a.normalize(2); |
| 1185 | | testing.expect(a.len == 1); |
| 1202 | testing.expect(a.len() == 1); |
| 1186 | 1203 | |
| 1187 | 1204 | a.limbs[0] = 0; |
| 1188 | 1205 | a.normalize(1); |
| 1189 | | testing.expect(a.len == 1); |
| 1206 | testing.expect(a.len() == 1); |
| 1190 | 1207 | } |
| 1191 | 1208 | |
| 1192 | 1209 | test "big.int normalize multi" { |
| ... | ... | @@ -1198,24 +1215,24 @@ test "big.int normalize multi" { |
| 1198 | 1215 | a.limbs[2] = 0; |
| 1199 | 1216 | a.limbs[3] = 0; |
| 1200 | 1217 | a.normalize(4); |
| 1201 | | testing.expect(a.len == 2); |
| 1218 | testing.expect(a.len() == 2); |
| 1202 | 1219 | |
| 1203 | 1220 | a.limbs[0] = 1; |
| 1204 | 1221 | a.limbs[1] = 2; |
| 1205 | 1222 | a.limbs[2] = 3; |
| 1206 | 1223 | a.normalize(3); |
| 1207 | | testing.expect(a.len == 3); |
| 1224 | testing.expect(a.len() == 3); |
| 1208 | 1225 | |
| 1209 | 1226 | a.limbs[0] = 0; |
| 1210 | 1227 | a.limbs[1] = 0; |
| 1211 | 1228 | a.limbs[2] = 0; |
| 1212 | 1229 | a.limbs[3] = 0; |
| 1213 | 1230 | a.normalize(4); |
| 1214 | | testing.expect(a.len == 1); |
| 1231 | testing.expect(a.len() == 1); |
| 1215 | 1232 | |
| 1216 | 1233 | a.limbs[0] = 0; |
| 1217 | 1234 | a.normalize(1); |
| 1218 | | testing.expect(a.len == 1); |
| 1235 | testing.expect(a.len() == 1); |
| 1219 | 1236 | } |
| 1220 | 1237 | |
| 1221 | 1238 | test "big.int parity" { |
| ... | ... | @@ -1250,7 +1267,7 @@ test "big.int bitcount + sizeInBase" { |
| 1250 | 1267 | try a.shiftLeft(a, 5000); |
| 1251 | 1268 | testing.expect(a.bitCountAbs() == 5032); |
| 1252 | 1269 | testing.expect(a.sizeInBase(2) >= 5032); |
| 1253 | | a.positive = false; |
| 1270 | a.setSign(false); |
| 1254 | 1271 | |
| 1255 | 1272 | testing.expect(a.bitCountAbs() == 5032); |
| 1256 | 1273 | testing.expect(a.sizeInBase(2) >= 5033); |