1//! negv - negate oVerflow
2//! * @panic, if result can not be represented
3//! - negvXi4_generic for unoptimized version
4const std = @import("std");
5const builtin = @import("builtin");
6const compiler_rt = @import("../compiler_rt.zig");
7const symbol = compiler_rt.symbol;
8
9comptime {
10 symbol(&__negvsi2, "__negvsi2");
11 symbol(&__negvdi2, "__negvdi2");
12 symbol(&__negvti2, "__negvti2");
13}
14
15pub fn __negvsi2(a: i32) callconv(.c) i32 {
16 return negvXi(i32, a);
17}
18
19pub fn __negvdi2(a: i64) callconv(.c) i64 {
20 return negvXi(i64, a);
21}
22
23pub fn __negvti2(a: i128) callconv(.c) i128 {
24 return negvXi(i128, a);
25}
26
27inline fn negvXi(comptime ST: type, a: ST) ST {
28 const UT = switch (ST) {
29 i32 => u32,
30 i64 => u64,
31 i128 => u128,
32 else => unreachable,
33 };
34 const N: UT = @bitSizeOf(ST);
35 const min: ST = @as(ST, @bitCast((@as(UT, 1) << (N - 1))));
36 if (a == min) @panic("integer overflow");
37 return -a;
38}
39
40test {
41 _ = @import("negvsi2_test.zig");
42 _ = @import("negvdi2_test.zig");
43 _ = @import("negvti2_test.zig");
44}