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