2022-06-18 02:10:00 +01:00
|
|
|
//! negv - negate oVerflow
|
|
|
|
//! * @panic, if result can not be represented
|
|
|
|
//! - negvXi4_generic for unoptimized version
|
2022-06-10 09:25:59 +01:00
|
|
|
const std = @import("std");
|
|
|
|
const builtin = @import("builtin");
|
2022-06-18 02:10:00 +01:00
|
|
|
const common = @import("common.zig");
|
|
|
|
|
|
|
|
pub const panic = common.panic;
|
2022-06-10 09:25:59 +01:00
|
|
|
|
|
|
|
comptime {
|
2022-12-28 13:57:17 +00:00
|
|
|
@export(__negvsi2, .{ .name = "__negvsi2", .linkage = common.linkage, .visibility = common.visibility });
|
|
|
|
@export(__negvdi2, .{ .name = "__negvdi2", .linkage = common.linkage, .visibility = common.visibility });
|
|
|
|
@export(__negvti2, .{ .name = "__negvti2", .linkage = common.linkage, .visibility = common.visibility });
|
2022-06-18 02:10:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn __negvsi2(a: i32) callconv(.C) i32 {
|
|
|
|
return negvXi(i32, a);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn __negvdi2(a: i64) callconv(.C) i64 {
|
|
|
|
return negvXi(i64, a);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn __negvti2(a: i128) callconv(.C) i128 {
|
|
|
|
return negvXi(i128, a);
|
2022-06-10 09:25:59 +01:00
|
|
|
}
|
compiler_rt: add __negvsi2, __negvdi2, __negvti2
- neg can only overflow, if a == MIN
- case `-0` is properly handled by hardware, so overflow check by comparing
`a == MIN` is sufficient
- tests: MIN, MIN+1, MIN+4, -42, -7, -1, 0, 1, 7..
See #1290
2021-12-12 21:25:29 +00:00
|
|
|
|
2022-01-02 02:49:34 +00:00
|
|
|
inline fn negvXi(comptime ST: type, a: ST) ST {
|
|
|
|
const UT = switch (ST) {
|
|
|
|
i32 => u32,
|
|
|
|
i64 => u64,
|
|
|
|
i128 => u128,
|
|
|
|
else => unreachable,
|
|
|
|
};
|
|
|
|
const N: UT = @bitSizeOf(ST);
|
2023-06-22 18:46:56 +01:00
|
|
|
const min: ST = @as(ST, @bitCast((@as(UT, 1) << (N - 1))));
|
2022-01-02 02:49:34 +00:00
|
|
|
if (a == min)
|
|
|
|
@panic("compiler_rt negv: overflow");
|
|
|
|
return -a;
|
|
|
|
}
|
|
|
|
|
compiler_rt: add __negvsi2, __negvdi2, __negvti2
- neg can only overflow, if a == MIN
- case `-0` is properly handled by hardware, so overflow check by comparing
`a == MIN` is sufficient
- tests: MIN, MIN+1, MIN+4, -42, -7, -1, 0, 1, 7..
See #1290
2021-12-12 21:25:29 +00:00
|
|
|
test {
|
|
|
|
_ = @import("negvsi2_test.zig");
|
|
|
|
_ = @import("negvdi2_test.zig");
|
|
|
|
_ = @import("negvti2_test.zig");
|
|
|
|
}
|