authorgravatar for r00ster91@proton.meWooster <r00ster91@proton.me> 2023-07-02 11:40:01-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-07-03 11:00:13-07:00
log2b8687ba2d575de2dfbf857133121dfb92d00cb6
treef20040fca24928dbb60a3362e6859f60846952fe
parent2583a39fb6f85fa65caba04f202c1654a5e8f3f6

std.math.big.int: better name for equal function

All of the std except these few functions call it "eql" instead of "eq". This has previously tripped me up when I expected the equality check function to be called "eql" (just like all the rest of the std) instead of "eq". The motivation is consistency. If search "eq" on Autodoc, these functions stick out and it looks inconsistent. I just noticed there are also a few functions spelling it out as "equal" (such as std.mem.allEqual). Maybe those functions should also spell it "eql" but that can be done in a future PR.

8 files changed, 71 insertions(+), 58 deletions(-)

lib/std/math/big/int.zig+47-34
......@@ -147,9 +147,12 @@ pub const Mutable = struct {
147147 };
148148 }
149149
150 // TODO: remove after release of 0.11
151 pub const eqZero = @compileError("use eqlZero");
152
150153 /// Returns true if `a == 0`.
151 pub fn eqZero(self: Mutable) bool {
152 return self.toConst().eqZero();
154 pub fn eqlZero(self: Mutable) bool {
155 return self.toConst().eqlZero();
153156 }
154157
155158 /// Asserts that the allocator owns the limbs memory. If this is not the case,
......@@ -420,10 +423,10 @@ pub const Mutable = struct {
420423 ///
421424 /// Asserts r has enough elements to hold the result. The upper bound is `@max(a.limbs.len, b.limbs.len)`.
422425 fn addCarry(r: *Mutable, a: Const, b: Const) bool {
423 if (a.eqZero()) {
426 if (a.eqlZero()) {
424427 r.copy(b);
425428 return false;
426 } else if (b.eqZero()) {
429 } else if (b.eqlZero()) {
427430 r.copy(a);
428431 return false;
429432 } else if (a.positive != b.positive) {
......@@ -556,11 +559,11 @@ pub const Mutable = struct {
556559 ///
557560 /// Asserts r has enough elements to hold the result. The upper bound is `@max(a.limbs.len, b.limbs.len)`.
558561 fn subCarry(r: *Mutable, a: Const, b: Const) bool {
559 if (a.eqZero()) {
562 if (a.eqlZero()) {
560563 r.copy(b);
561564 r.positive = !b.positive;
562565 return false;
563 } else if (b.eqZero()) {
566 } else if (b.eqlZero()) {
564567 r.copy(a);
565568 return false;
566569 } else if (a.positive != b.positive) {
......@@ -1002,7 +1005,7 @@ pub const Mutable = struct {
10021005 // Else:
10031006 // @rem(a - 1, b) = @rem(a + b - 1, b) = @rem(b - 1, b) = b - 1
10041007 // => @mod(a, -b) = b - 1 - b + 1 = 0
1005 if (!r.eqZero()) {
1008 if (!r.eqlZero()) {
10061009 q.addScalar(q.toConst(), -1);
10071010 r.positive = true;
10081011 r.sub(r.toConst(), y.toConst().abs());
......@@ -1033,7 +1036,7 @@ pub const Mutable = struct {
10331036 // Else :
10341037 // @rem(a - 1, b) = b - 1
10351038 // => @mod(-a, b) = -(b - 1) + b - 1 = 0
1036 if (!r.eqZero()) {
1039 if (!r.eqlZero()) {
10371040 q.addScalar(q.toConst(), -1);
10381041 r.positive = false;
10391042 r.add(r.toConst(), y.toConst().abs());
......@@ -1119,7 +1122,7 @@ pub const Mutable = struct {
11191122 // 0-bit integers.
11201123 if (bit_count <= shift) {
11211124 // In this case, there is only no overflow if `a` is zero.
1122 if (a.eqZero()) {
1125 if (a.eqlZero()) {
11231126 r.set(0);
11241127 } else {
11251128 r.setTwosCompIntLimit(if (a.positive) .max else .min, signedness, bit_count);
......@@ -1214,10 +1217,10 @@ pub const Mutable = struct {
12141217 /// Asserts that r has enough limbs to store the result. Upper bound is `@max(a.limbs.len, b.limbs.len)`.
12151218 pub fn bitOr(r: *Mutable, a: Const, b: Const) void {
12161219 // Trivial cases, llsignedor does not support zero.
1217 if (a.eqZero()) {
1220 if (a.eqlZero()) {
12181221 r.copy(b);
12191222 return;
1220 } else if (b.eqZero()) {
1223 } else if (b.eqlZero()) {
12211224 r.copy(a);
12221225 return;
12231226 }
......@@ -1239,10 +1242,10 @@ pub const Mutable = struct {
12391242 /// If a and b are negative, the upper bound is `@max(a.limbs.len, b.limbs.len) + 1`.
12401243 pub fn bitAnd(r: *Mutable, a: Const, b: Const) void {
12411244 // Trivial cases, llsignedand does not support zero.
1242 if (a.eqZero()) {
1245 if (a.eqlZero()) {
12431246 r.copy(a);
12441247 return;
1245 } else if (b.eqZero()) {
1248 } else if (b.eqlZero()) {
12461249 r.copy(b);
12471250 return;
12481251 }
......@@ -1264,10 +1267,10 @@ pub const Mutable = struct {
12641267 /// but not both, the upper bound is `@max(a.limbs.len, b.limbs.len) + 1`.
12651268 pub fn bitXor(r: *Mutable, a: Const, b: Const) void {
12661269 // Trivial cases, because llsignedxor does not support negative zero.
1267 if (a.eqZero()) {
1270 if (a.eqlZero()) {
12681271 r.copy(b);
12691272 return;
1270 } else if (b.eqZero()) {
1273 } else if (b.eqlZero()) {
12711274 r.copy(a);
12721275 return;
12731276 }
......@@ -1330,7 +1333,7 @@ pub const Mutable = struct {
13301333 else => {},
13311334 }
13321335
1333 if (a.eqZero()) {
1336 if (a.eqlZero()) {
13341337 // 0^b = 0
13351338 return r.set(0);
13361339 } else if (a.limbs.len == 1 and a.limbs[0] == 1) {
......@@ -1442,7 +1445,7 @@ pub const Mutable = struct {
14421445 var tmp_x = try Managed.init(limbs_buffer.allocator);
14431446 defer tmp_x.deinit();
14441447
1445 while (y.len() > 1 and !y.eqZero()) {
1448 while (y.len() > 1 and !y.eqlZero()) {
14461449 assert(x.isPositive() and y.isPositive());
14471450 assert(x.len() >= y.len());
14481451
......@@ -1506,7 +1509,7 @@ pub const Mutable = struct {
15061509 // euclidean algorithm
15071510 assert(x.toConst().order(y.toConst()) != .lt);
15081511
1509 while (!y.toConst().eqZero()) {
1512 while (!y.toConst().eqlZero()) {
15101513 try t_big.divTrunc(&r, &x, &y);
15111514 x.swap(&y);
15121515 y.swap(&r);
......@@ -1517,7 +1520,7 @@ pub const Mutable = struct {
15171520
15181521 // Truncates by default.
15191522 fn div(q: *Mutable, r: *Mutable, x: *Mutable, y: *Mutable) void {
1520 assert(!y.eqZero()); // division by zero
1523 assert(!y.eqlZero()); // division by zero
15211524 assert(q != r); // illegal aliasing
15221525
15231526 const q_positive = (x.positive == y.positive);
......@@ -1745,7 +1748,7 @@ pub const Mutable = struct {
17451748 }
17461749
17471750 const req_limbs = calcTwosCompLimbCount(bit_count);
1748 if (req_limbs == 0 or a.eqZero()) {
1751 if (req_limbs == 0 or a.eqlZero()) {
17491752 r.set(0);
17501753 return;
17511754 }
......@@ -1776,7 +1779,7 @@ pub const Mutable = struct {
17761779 const req_limbs = calcTwosCompLimbCount(bit_count);
17771780
17781781 // Handle 0-bit integers.
1779 if (req_limbs == 0 or a.eqZero()) {
1782 if (req_limbs == 0 or a.eqlZero()) {
17801783 r.set(0);
17811784 return;
17821785 }
......@@ -2121,7 +2124,7 @@ pub const Const = struct {
21212124 }
21222125
21232126 pub fn fitsInTwosComp(self: Const, signedness: Signedness, bit_count: usize) bool {
2124 if (self.eqZero()) {
2127 if (self.eqlZero()) {
21252128 return true;
21262129 }
21272130 if (signedness == .unsigned and !self.positive) {
......@@ -2159,7 +2162,7 @@ pub const Const = struct {
21592162 switch (@typeInfo(T)) {
21602163 .Int => |info| {
21612164 // Make sure -0 is handled correctly.
2162 if (self.eqZero()) return 0;
2165 if (self.eqlZero()) return 0;
21632166
21642167 const UT = std.meta.Int(.unsigned, info.bits);
21652168
......@@ -2253,7 +2256,7 @@ pub const Const = struct {
22532256 assert(base >= 2);
22542257 assert(base <= 16);
22552258
2256 if (self.eqZero()) {
2259 if (self.eqlZero()) {
22572260 return allocator.dupe(u8, "0");
22582261 }
22592262 const string = try allocator.alloc(u8, self.sizeInBaseUpperBound(base));
......@@ -2278,7 +2281,7 @@ pub const Const = struct {
22782281 assert(base >= 2);
22792282 assert(base <= 16);
22802283
2281 if (self.eqZero()) {
2284 if (self.eqlZero()) {
22822285 string[0] = '0';
22832286 return 1;
22842287 }
......@@ -2478,20 +2481,25 @@ pub const Const = struct {
24782481 return order(lhs, rhs.toConst());
24792482 }
24802483
2484 // TODO: remove after release of 0.11
2485 pub const eqZero = @compileError("use eqlZero");
2486 pub const eqAbs = @compileError("use eqlAbs");
2487 pub const eq = @compileError("use eql");
2488
24812489 /// Returns true if `a == 0`.
2482 pub fn eqZero(a: Const) bool {
2490 pub fn eqlZero(a: Const) bool {
24832491 var d: Limb = 0;
24842492 for (a.limbs) |limb| d |= limb;
24852493 return d == 0;
24862494 }
24872495
24882496 /// Returns true if `|a| == |b|`.
2489 pub fn eqAbs(a: Const, b: Const) bool {
2497 pub fn eqlAbs(a: Const, b: Const) bool {
24902498 return orderAbs(a, b) == .eq;
24912499 }
24922500
24932501 /// Returns true if `a == b`.
2494 pub fn eq(a: Const, b: Const) bool {
2502 pub fn eql(a: Const, b: Const) bool {
24952503 return order(a, b) == .eq;
24962504 }
24972505
......@@ -2822,19 +2830,24 @@ pub const Managed = struct {
28222830 return a.toConst().order(b.toConst());
28232831 }
28242832
2833 // TODO: remove after release of 0.11
2834 pub const eqZero = @compileError("use eqlZero");
2835 pub const eqAbs = @compileError("use eqlAbs");
2836 pub const eq = @compileError("use eql");
2837
28252838 /// Returns true if a == 0.
2826 pub fn eqZero(a: Managed) bool {
2827 return a.toConst().eqZero();
2839 pub fn eqlZero(a: Managed) bool {
2840 return a.toConst().eqlZero();
28282841 }
28292842
28302843 /// Returns true if |a| == |b|.
2831 pub fn eqAbs(a: Managed, b: Managed) bool {
2832 return a.toConst().eqAbs(b.toConst());
2844 pub fn eqlAbs(a: Managed, b: Managed) bool {
2845 return a.toConst().eqlAbs(b.toConst());
28332846 }
28342847
28352848 /// Returns true if a == b.
2836 pub fn eq(a: Managed, b: Managed) bool {
2837 return a.toConst().eq(b.toConst());
2849 pub fn eql(a: Managed, b: Managed) bool {
2850 return a.toConst().eql(b.toConst());
28382851 }
28392852
28402853 /// Normalize a possible sequence of leading zeros.
lib/std/math/big/int_test.zig+16-16
......@@ -461,8 +461,8 @@ test "big.int equality" {
461461 var b = try Managed.initSet(testing.allocator, -0xffffffff1);
462462 defer b.deinit();
463463
464 try testing.expect(a.eqAbs(b));
465 try testing.expect(!a.eq(b));
464 try testing.expect(a.eqlAbs(b));
465 try testing.expect(!a.eql(b));
466466}
467467
468468test "big.int abs" {
......@@ -1006,7 +1006,7 @@ test "big.int mul large" {
10061006 try b.mul(&a, &a);
10071007 try c.sqr(&a);
10081008
1009 try testing.expect(b.eq(c));
1009 try testing.expect(b.eql(c));
10101010}
10111011
10121012test "big.int mulWrap single-single unsigned" {
......@@ -1088,7 +1088,7 @@ test "big.int mulWrap large" {
10881088 try c.sqr(&a);
10891089 try c.truncate(&c, .signed, testbits);
10901090
1091 try testing.expect(b.eq(c));
1091 try testing.expect(b.eql(c));
10921092}
10931093
10941094test "big.int div single-half no rem" {
......@@ -1716,8 +1716,8 @@ test "big.int div multi-single zero-limb trailing" {
17161716
17171717 var expected = try Managed.initSet(testing.allocator, 0x6000000000000000000000000000000000000000000000000);
17181718 defer expected.deinit();
1719 try testing.expect(q.eq(expected));
1720 try testing.expect(r.eqZero());
1719 try testing.expect(q.eql(expected));
1720 try testing.expect(r.eqlZero());
17211721}
17221722
17231723test "big.int div multi-multi zero-limb trailing (with rem)" {
......@@ -1962,7 +1962,7 @@ test "big.int saturate multi unsigned zero" {
19621962
19631963 try a.saturate(&a, .unsigned, @bitSizeOf(DoubleLimb));
19641964
1965 try testing.expect(a.eqZero());
1965 try testing.expect(a.eqlZero());
19661966}
19671967
19681968test "big.int saturate multi unsigned" {
......@@ -1993,7 +1993,7 @@ test "big.int shift-right multi" {
19931993 try a.shiftRight(&a, 63);
19941994 try a.shiftRight(&a, 63);
19951995 try a.shiftRight(&a, 2);
1996 try testing.expect(a.eqZero());
1996 try testing.expect(a.eqlZero());
19971997}
19981998
19991999test "big.int shift-left single" {
......@@ -2224,7 +2224,7 @@ test "big.int bitwise and negative-positive multi-limb" {
22242224
22252225 try a.bitAnd(&a, &b);
22262226
2227 try testing.expect(a.eqZero());
2227 try testing.expect(a.eqlZero());
22282228}
22292229
22302230test "big.int bitwise and positive-negative simple" {
......@@ -2246,7 +2246,7 @@ test "big.int bitwise and positive-negative multi-limb" {
22462246
22472247 try a.bitAnd(&a, &b);
22482248
2249 try testing.expect(a.eqZero());
2249 try testing.expect(a.eqlZero());
22502250}
22512251
22522252test "big.int bitwise and negative-negative simple" {
......@@ -2325,7 +2325,7 @@ test "big.int bitwise xor single negative zero" {
23252325
23262326 try a.bitXor(&a, &b);
23272327
2328 try testing.expect(a.eqZero());
2328 try testing.expect(a.eqlZero());
23292329}
23302330
23312331test "big.int bitwise xor single negative multi-limb" {
......@@ -2554,7 +2554,7 @@ test "big.int mutable to managed" {
25542554 var a = Mutable.init(limbs_buf, 0xdeadbeef);
25552555 var a_managed = a.toManaged(allocator);
25562556
2557 try testing.expect(a.toConst().eq(a_managed.toConst()));
2557 try testing.expect(a.toConst().eql(a_managed.toConst()));
25582558}
25592559
25602560test "big.int const to managed" {
......@@ -2564,7 +2564,7 @@ test "big.int const to managed" {
25642564 var b = try a.toConst().toManaged(testing.allocator);
25652565 defer b.deinit();
25662566
2567 try testing.expect(a.toConst().eq(b.toConst()));
2567 try testing.expect(a.toConst().eql(b.toConst()));
25682568}
25692569
25702570test "big.int pow" {
......@@ -2590,7 +2590,7 @@ test "big.int pow" {
25902590 // y and a are aliased
25912591 try a.pow(&a, 123);
25922592
2593 try testing.expect(a.eq(y));
2593 try testing.expect(a.eql(y));
25942594
25952595 const ys = try y.toString(testing.allocator, 16, .lower);
25962596 defer testing.allocator.free(ys);
......@@ -3096,7 +3096,7 @@ test "big.int mul multi-multi alias r with a and b" {
30963096 var want = try Managed.initSet(testing.allocator, 4 * maxInt(Limb) * maxInt(Limb));
30973097 defer want.deinit();
30983098
3099 try testing.expect(a.eq(want));
3099 try testing.expect(a.eql(want));
31003100
31013101 if (@typeInfo(Limb).Int.bits == 64) {
31023102 try testing.expectEqual(@as(usize, 5), a.limbs.len);
......@@ -3112,7 +3112,7 @@ test "big.int sqr multi alias r with a" {
31123112 var want = try Managed.initSet(testing.allocator, 4 * maxInt(Limb) * maxInt(Limb));
31133113 defer want.deinit();
31143114
3115 try testing.expect(a.eq(want));
3115 try testing.expect(a.eql(want));
31163116
31173117 if (@typeInfo(Limb).Int.bits == 64) {
31183118 try testing.expectEqual(@as(usize, 5), a.limbs.len);
lib/std/math/big/rational.zig+3-3
......@@ -205,7 +205,7 @@ pub const Rational = struct {
205205 const ebias = (1 << (esize - 1)) - 1;
206206 const emin = 1 - ebias;
207207
208 if (self.p.eqZero()) {
208 if (self.p.eqlZero()) {
209209 return 0;
210210 }
211211
......@@ -294,7 +294,7 @@ pub const Rational = struct {
294294
295295 try self.reduce();
296296
297 if (self.q.eqZero()) {
297 if (self.q.eqlZero()) {
298298 @panic("cannot set rational with denominator = 0");
299299 }
300300 }
......@@ -434,7 +434,7 @@ pub const Rational = struct {
434434 ///
435435 /// Returns an error if memory could not be allocated.
436436 pub fn div(r: *Rational, a: Rational, b: Rational) !void {
437 if (b.p.eqZero()) {
437 if (b.p.eqlZero()) {
438438 @panic("division by zero");
439439 }
440440
src/InternPool.zig+1-1
......@@ -1037,7 +1037,7 @@ pub const Key = union(enum) {
10371037 .big_int => |aa| switch (b_info.storage) {
10381038 .u64 => |bb| aa.orderAgainstScalar(bb) == .eq,
10391039 .i64 => |bb| aa.orderAgainstScalar(bb) == .eq,
1040 .big_int => |bb| aa.eq(bb),
1040 .big_int => |bb| aa.eql(bb),
10411041 .lazy_align, .lazy_size => false,
10421042 },
10431043 .lazy_align => |aa| switch (b_info.storage) {
src/Module.zig+1-1
......@@ -7049,7 +7049,7 @@ pub fn intBitsForValue(mod: *Module, val: Value, sign: bool) u16 {
70497049 if (big.positive) return @as(u16, @intCast(big.bitCountAbs() + @intFromBool(sign)));
70507050
70517051 // Zero is still a possibility, in which case unsigned is fine
7052 if (big.eqZero()) return 0;
7052 if (big.eqlZero()) return 0;
70537053
70547054 return @as(u16, @intCast(big.bitCountTwosComp()));
70557055 },
src/RangeSet.zig+1-1
......@@ -95,7 +95,7 @@ pub fn spans(self: *RangeSet, first: InternPool.Index, last: InternPool.Index) !
9595 try counter.addScalar(&counter, 1);
9696
9797 const cur_start_int = cur.first.toValue().toBigInt(&space, mod);
98 if (!cur_start_int.eq(counter.toConst())) {
98 if (!cur_start_int.eql(counter.toConst())) {
9999 return false;
100100 }
101101 }
src/Sema.zig+1-1
......@@ -36375,7 +36375,7 @@ fn float128IntPartToBigInt(
3637536375
3637636376 // The float is reduced in rational.setFloat, so we assert that denominator is equal to one
3637736377 const big_one = std.math.big.int.Const{ .limbs = &.{1}, .positive = true };
36378 assert(rational.q.toConst().eqAbs(big_one));
36378 assert(rational.q.toConst().eqlAbs(big_one));
3637936379
3638036380 if (is_negative) {
3638136381 rational.negate();
src/value.zig+1-1
......@@ -1771,7 +1771,7 @@ pub const Value = struct {
17711771 .ptr => |ptr| switch (ptr.addr) {
17721772 .int => {
17731773 var buf: BigIntSpace = undefined;
1774 return val.toBigInt(&buf, mod).eqZero();
1774 return val.toBigInt(&buf, mod).eqlZero();
17751775 },
17761776 else => false,
17771777 },