diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-01-19 13:45:36 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-01-19 13:47:51 -0700 |
| commit | d5d0619aacccd5e3b933b3196fba5ab6f8f9da47 (patch) | |
| tree | c6705b09ed101a0fb362f1a646b1a44b9aac3d30 /lib/std/math.zig | |
| parent | d5b0a963d1bf3399e3d8b63b03ea61f7d771adbd (diff) | |
| download | zig-d5d0619aacccd5e3b933b3196fba5ab6f8f9da47.tar.gz zig-d5d0619aacccd5e3b933b3196fba5ab6f8f9da47.zip | |
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
Diffstat (limited to 'lib/std/math.zig')
| -rw-r--r-- | lib/std/math.zig | 1 |
1 files changed, 1 insertions, 0 deletions
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; } |
