From d5d0619aacccd5e3b933b3196fba5ab6f8f9da47 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 19 Jan 2021 13:45:36 -0700 Subject: stage2: ELF: avoid multiplication for ideal capacity ideal capacity is now determined by e.g. x += x / f rather than x = x * b / a This turns a multiplication into an addition, making it less likely to overflow the integer. This commit also introduces padToIdeal() which does saturating arithmetic so that no overflow is possible when calculating ideal capacity. closes #7830 --- lib/std/math.zig | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/std/math.zig') diff --git a/lib/std/math.zig b/lib/std/math.zig index 77eed37304..f95bcce617 100644 --- a/lib/std/math.zig +++ b/lib/std/math.zig @@ -415,6 +415,7 @@ pub fn mul(comptime T: type, a: T, b: T) (error{Overflow}!T) { } pub fn add(comptime T: type, a: T, b: T) (error{Overflow}!T) { + if (T == comptime_int) return a + b; var answer: T = undefined; return if (@addWithOverflow(T, a, b, &answer)) error.Overflow else answer; } -- cgit v1.2.3