2022-06-16 07:09:56 +01:00
|
|
|
/// absv - absolute oVerflow
|
|
|
|
/// * @panic if value can not be represented
|
|
|
|
pub inline fn absv(comptime ST: type, a: ST) ST {
|
2022-01-02 02:49:34 +00:00
|
|
|
const UT = switch (ST) {
|
|
|
|
i32 => u32,
|
|
|
|
i64 => u64,
|
|
|
|
i128 => u128,
|
|
|
|
else => unreachable,
|
|
|
|
};
|
|
|
|
// taken from Bit Twiddling Hacks
|
|
|
|
// compute the integer absolute value (abs) without branching
|
|
|
|
var x: ST = a;
|
|
|
|
const N: UT = @bitSizeOf(ST);
|
|
|
|
const sign: ST = a >> N - 1;
|
|
|
|
x +%= sign;
|
|
|
|
x ^= sign;
|
|
|
|
if (x < 0)
|
|
|
|
@panic("compiler_rt absv: overflow");
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
compiler_rt: add __absvsi2, __absvdi2, __absvti2
- abs can only overflow, if a == MIN
- comparing the sign change from wrapping addition is branchless
- tests: MIN, MIN+1,..MIN+4, -42, -7, -1, 0, 1, 7..
See #1290
2021-12-12 21:25:29 +00:00
|
|
|
test {
|
|
|
|
_ = @import("absvsi2_test.zig");
|
|
|
|
_ = @import("absvdi2_test.zig");
|
|
|
|
_ = @import("absvti2_test.zig");
|
|
|
|
}
|