Skip to content

Commit ff8eb83

Browse files
committed
std: use math overflow helpers instead of builtins
This patch replaces all usages of the `@___WithOverflow` builtins where the overflow result is discarded with the nicer `math` helpers that return `error.Overflow`. This cleaned up a bit of code and hopefully impresses the useful `math` functions on more Zig code later.
1 parent a96b78c commit ff8eb83

File tree

5 files changed

+19
-67
lines changed

5 files changed

+19
-67
lines changed

lib/std/leb128.zig

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const std = @import("std");
2+
const math = std.math;
23
const testing = std.testing;
34

45
/// Read a single unsigned LEB128 value from the given reader as type T,
@@ -15,10 +16,8 @@ pub fn readULEB128(comptime T: type, reader: anytype) !T {
1516
while (group < max_group) : (group += 1) {
1617
const byte = try reader.readByte();
1718

18-
const ov = @shlWithOverflow(@as(U, byte & 0x7f), group * 7);
19-
if (ov[1] != 0) return error.Overflow;
20-
21-
value |= ov[0];
19+
const mask = try math.shlExact(U, byte & 0x7f, group * 7);
20+
value |= mask;
2221
if (byte & 0x80 == 0) break;
2322
} else {
2423
return error.Overflow;

lib/std/math/powi.zig

+3-9
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,16 @@ pub fn powi(comptime T: type, x: T, y: T) (error{
7070

7171
while (exp > 1) {
7272
if (exp & 1 == 1) {
73-
const ov = @mulWithOverflow(acc, base);
74-
if (ov[1] != 0) return error.Overflow;
75-
acc = ov[0];
73+
acc = try math.mul(T, acc, base);
7674
}
7775

7876
exp >>= 1;
7977

80-
const ov = @mulWithOverflow(base, base);
81-
if (ov[1] != 0) return error.Overflow;
82-
base = ov[0];
78+
base = try math.mul(T, base, base);
8379
}
8480

8581
if (exp == 1) {
86-
const ov = @mulWithOverflow(acc, base);
87-
if (ov[1] != 0) return error.Overflow;
88-
acc = ov[0];
82+
acc = try math.mul(T, acc, base);
8983
}
9084

9185
return acc;

lib/std/mem.zig

+3-4
Original file line numberDiff line numberDiff line change
@@ -3786,13 +3786,12 @@ pub fn alignPointerOffset(ptr: anytype, align_to: usize) ?usize {
37863786

37873787
// Calculate the aligned base address with an eye out for overflow.
37883788
const addr = @intFromPtr(ptr);
3789-
var ov = @addWithOverflow(addr, align_to - 1);
3790-
if (ov[1] != 0) return null;
3791-
ov[0] &= ~@as(usize, align_to - 1);
3789+
var aligned_addr = math.add(usize, addr, align_to - 1) catch return null;
3790+
aligned_addr &= ~@as(usize, align_to - 1);
37923791

37933792
// The delta is expressed in terms of bytes, turn it into a number of child
37943793
// type elements.
3795-
const delta = ov[0] - addr;
3794+
const delta = aligned_addr - addr;
37963795
const pointee_size = @sizeOf(info.Pointer.child);
37973796
if (delta % pointee_size != 0) return null;
37983797
return delta / pointee_size;

lib/std/net.zig

+6-30
Original file line numberDiff line numberDiff line change
@@ -402,16 +402,8 @@ pub const Ip6Address = extern struct {
402402
if (scope_id) {
403403
if (c >= '0' and c <= '9') {
404404
const digit = c - '0';
405-
{
406-
const ov = @mulWithOverflow(result.sa.scope_id, 10);
407-
if (ov[1] != 0) return error.Overflow;
408-
result.sa.scope_id = ov[0];
409-
}
410-
{
411-
const ov = @addWithOverflow(result.sa.scope_id, digit);
412-
if (ov[1] != 0) return error.Overflow;
413-
result.sa.scope_id = ov[0];
414-
}
405+
result.sa.scope_id = try std.math.mul(u32, result.sa.scope_id, 10);
406+
result.sa.scope_id = try std.math.add(u32, result.sa.scope_id, digit);
415407
} else {
416408
return error.InvalidCharacter;
417409
}
@@ -462,16 +454,8 @@ pub const Ip6Address = extern struct {
462454
return result;
463455
} else {
464456
const digit = try std.fmt.charToDigit(c, 16);
465-
{
466-
const ov = @mulWithOverflow(x, 16);
467-
if (ov[1] != 0) return error.Overflow;
468-
x = ov[0];
469-
}
470-
{
471-
const ov = @addWithOverflow(x, digit);
472-
if (ov[1] != 0) return error.Overflow;
473-
x = ov[0];
474-
}
457+
x = try std.math.mul(u16, x, 16);
458+
x = try std.math.add(u16, x, digit);
475459
saw_any_digits = true;
476460
}
477461
}
@@ -584,16 +568,8 @@ pub const Ip6Address = extern struct {
584568
return result;
585569
} else {
586570
const digit = try std.fmt.charToDigit(c, 16);
587-
{
588-
const ov = @mulWithOverflow(x, 16);
589-
if (ov[1] != 0) return error.Overflow;
590-
x = ov[0];
591-
}
592-
{
593-
const ov = @addWithOverflow(x, digit);
594-
if (ov[1] != 0) return error.Overflow;
595-
x = ov[0];
596-
}
571+
x = try std.math.mul(u16, x, 16);
572+
x = try std.math.add(u16, x, digit);
597573
saw_any_digits = true;
598574
}
599575
}

lib/std/process.zig

+4-20
Original file line numberDiff line numberDiff line change
@@ -1531,16 +1531,8 @@ pub fn posixGetUserInfo(name: []const u8) !UserInfo {
15311531
'0'...'9' => byte - '0',
15321532
else => return error.CorruptPasswordFile,
15331533
};
1534-
{
1535-
const ov = @mulWithOverflow(uid, 10);
1536-
if (ov[1] != 0) return error.CorruptPasswordFile;
1537-
uid = ov[0];
1538-
}
1539-
{
1540-
const ov = @addWithOverflow(uid, digit);
1541-
if (ov[1] != 0) return error.CorruptPasswordFile;
1542-
uid = ov[0];
1543-
}
1534+
uid = std.math.mul(posix.uid_t, uid, 10) catch return error.CorruptPasswordFile;
1535+
uid = std.math.add(posix.uid_t, uid, digit) catch return error.CorruptPasswordFile;
15441536
},
15451537
},
15461538
.ReadGroupId => switch (byte) {
@@ -1555,16 +1547,8 @@ pub fn posixGetUserInfo(name: []const u8) !UserInfo {
15551547
'0'...'9' => byte - '0',
15561548
else => return error.CorruptPasswordFile,
15571549
};
1558-
{
1559-
const ov = @mulWithOverflow(gid, 10);
1560-
if (ov[1] != 0) return error.CorruptPasswordFile;
1561-
gid = ov[0];
1562-
}
1563-
{
1564-
const ov = @addWithOverflow(gid, digit);
1565-
if (ov[1] != 0) return error.CorruptPasswordFile;
1566-
gid = ov[0];
1567-
}
1550+
gid = std.math.mul(posix.gid_t, gid, 10) catch return error.CorruptPasswordFile;
1551+
gid = std.math.add(posix.gid_t, gid, digit) catch return error.CorruptPasswordFile;
15681552
},
15691553
},
15701554
}

0 commit comments

Comments
 (0)