authorgravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2020-03-05 15:59:19+11:00
committergravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2020-03-05 16:00:19+11:00
log488ba1560fb80c1115b3b4794031859c0063ba3e
tree9a83d6820cc0788fb64511691c0f2789aebdfd55
parent4f58bfe1a81a385ac9120510b816ff081ab73437
signaturelock-open Commit is signed but in an unrecognized format.

std: fix math.absCast on i1


2 files changed, 25 insertions(+), 13 deletions(-)

lib/std/fmt.zig+1-1
...@@ -947,7 +947,7 @@ fn formatIntSigned(...@@ -947,7 +947,7 @@ fn formatIntSigned(
947 const Uint = std.meta.IntType(false, bit_count);947 const Uint = std.meta.IntType(false, bit_count);
948 if (value < 0) {948 if (value < 0) {
949 try output(context, "-");949 try output(context, "-");
950 const new_value = ~@bitCast(Uint, value +% -1);950 const new_value = math.absCast(value);
951 return formatIntUnsigned(new_value, base, uppercase, new_options, context, Errors, output);951 return formatIntUnsigned(new_value, base, uppercase, new_options, context, Errors, output);
952 } else if (options.width == null or options.width.? == 0) {952 } else if (options.width == null or options.width.? == 0) {
953 return formatIntUnsigned(@intCast(Uint, value), base, uppercase, options, context, Errors, output);953 return formatIntUnsigned(@intCast(Uint, value), base, uppercase, options, context, Errors, output);
lib/std/math.zig+24-12
...@@ -670,23 +670,35 @@ fn testRem() void {...@@ -670,23 +670,35 @@ fn testRem() void {
670670
671/// Returns the absolute value of the integer parameter.671/// Returns the absolute value of the integer parameter.
672/// Result is an unsigned integer.672/// Result is an unsigned integer.
673pub fn absCast(x: var) t: {673pub fn absCast(x: var) switch(@typeInfo(@TypeOf(x))) {
674 if (@TypeOf(x) == comptime_int) {674 .ComptimeInt => comptime_int,
675 break :t comptime_int;675 .Int => |intInfo| std.meta.IntType(false, intInfo.bits),
676 } else {676 else => @compileError("absCast only accepts integers"),
677 break :t std.meta.IntType(false, @TypeOf(x).bit_count);
678 }677 }
679} {678{
680 if (@TypeOf(x) == comptime_int) {679 switch(@typeInfo(@TypeOf(x))) {
681 return if (x < 0) -x else x;680 .ComptimeInt => {
681 if (x < 0) {
682 return -x;
683 } else {
684 return x;
685 }
686 },
687 .Int => |intInfo| {
688 const Uint = std.meta.IntType(false, intInfo.bits);
689 if (x < 0) {
690 return ~@bitCast(Uint, x +% -1);
691 } else {
692 return @intCast(Uint, x);
693 }
694 },
695 else => unreachable,
682 }696 }
683 const uint = std.meta.IntType(false, @TypeOf(x).bit_count);
684 if (x >= 0) return @intCast(uint, x);
685
686 return @intCast(uint, -(x + 1)) + 1;
687}697}
688698
689test "math.absCast" {699test "math.absCast" {
700 testing.expect(absCast(@as(i1, -1)) == 1);
701
690 testing.expect(absCast(@as(i32, -999)) == 999);702 testing.expect(absCast(@as(i32, -999)) == 999);
691 testing.expect(@TypeOf(absCast(@as(i32, -999))) == u32);703 testing.expect(@TypeOf(absCast(@as(i32, -999))) == u32);
692704