authorgravatar for fncontroloption@noreply.codeberg.orgFnControlOption <fncontroloption@noreply.codeberg.org> 2025-04-16 11:59:47-07:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-11-29 21:09:08+01:00
logbfe3317059131ab552f7583b88d6bc82609d198c
tree69fc90fcec9d34981403ff7e8c5b8d2a549b58ce
parent44e99edd7a12ea7e04fbfe53746c3af4b531f687

Return a `usize` from `@abs` if given an `isize`

Also: - `c_ushort` for `c_short` - `c_uint` for `c_int` - `c_ulong` for `c_long` - `c_ulonglong` for `c_longlong`

2 files changed, 70 insertions(+), 7 deletions(-)

src/Type.zig+16-7
...@@ -3445,13 +3445,22 @@ pub fn optEuBaseType(ty: Type, zcu: *const Zcu) Type {...@@ -3445,13 +3445,22 @@ pub fn optEuBaseType(ty: Type, zcu: *const Zcu) Type {
34453445
3446pub fn toUnsigned(ty: Type, pt: Zcu.PerThread) !Type {3446pub fn toUnsigned(ty: Type, pt: Zcu.PerThread) !Type {
3447 const zcu = pt.zcu;3447 const zcu = pt.zcu;
3448 return switch (ty.zigTypeTag(zcu)) {3448 return switch (ty.toIntern()) {
3449 .int => pt.intType(.unsigned, ty.intInfo(zcu).bits),3449 // zig fmt: off
3450 .vector => try pt.vectorType(.{3450 .usize_type, .isize_type => .usize,
3451 .len = ty.vectorLen(zcu),3451 .c_ushort_type, .c_short_type => .c_ushort,
3452 .child = (try ty.childType(zcu).toUnsigned(pt)).toIntern(),3452 .c_uint_type, .c_int_type => .c_uint,
3453 }),3453 .c_ulong_type, .c_long_type => .c_ulong,
3454 else => unreachable,3454 .c_ulonglong_type, .c_longlong_type => .c_ulonglong,
3455 // zig fmt: on
3456 else => switch (ty.zigTypeTag(zcu)) {
3457 .int => pt.intType(.unsigned, ty.intInfo(zcu).bits),
3458 .vector => try pt.vectorType(.{
3459 .len = ty.vectorLen(zcu),
3460 .child = (try ty.childType(zcu).toUnsigned(pt)).toIntern(),
3461 }),
3462 else => unreachable,
3463 },
3455 };3464 };
3456}3465}
34573466
test/behavior/abs.zig+54
...@@ -1,5 +1,6 @@...@@ -1,5 +1,6 @@
1const builtin = @import("builtin");1const builtin = @import("builtin");
2const std = @import("std");2const std = @import("std");
3const assert = std.debug.assert;
3const expect = std.testing.expect;4const expect = std.testing.expect;
45
5test "@abs integers" {6test "@abs integers" {
...@@ -48,6 +49,33 @@ fn testAbsIntegers() !void {...@@ -48,6 +49,33 @@ fn testAbsIntegers() !void {
48 }49 }
49}50}
5051
52test "@abs signed C ABI integers" {
53 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
54 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
55 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
56 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; // TODO
57
58 const S = struct {
59 fn doTheTest() !void {
60 try testOne(isize, usize);
61 try testOne(c_short, c_ushort);
62 try testOne(c_int, c_uint);
63 try testOne(c_long, c_ulong);
64 if (!builtin.cpu.arch.isSpirV()) try testOne(c_longlong, c_ulonglong);
65 }
66 fn testOne(comptime Signed: type, comptime Unsigned: type) !void {
67 var negative_one: Signed = undefined;
68 negative_one = -1;
69 const one = @abs(negative_one);
70 comptime assert(@TypeOf(one) == Unsigned);
71 try expect(one == 1);
72 }
73 };
74
75 try S.doTheTest();
76 try comptime S.doTheTest();
77}
78
51test "@abs unsigned integers" {79test "@abs unsigned integers" {
52 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO80 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
53 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO81 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
...@@ -87,6 +115,32 @@ fn testAbsUnsignedIntegers() !void {...@@ -87,6 +115,32 @@ fn testAbsUnsignedIntegers() !void {
87 }115 }
88}116}
89117
118test "@abs unsigned C ABI integers" {
119 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
120 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
121 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
122
123 const S = struct {
124 fn doTheTest() !void {
125 try testOne(usize);
126 try testOne(c_ushort);
127 try testOne(c_uint);
128 try testOne(c_ulong);
129 if (!builtin.cpu.arch.isSpirV()) try testOne(c_ulonglong);
130 }
131 fn testOne(comptime Unsigned: type) !void {
132 var one: Unsigned = undefined;
133 one = 1;
134 const still_one = @abs(one);
135 comptime assert(@TypeOf(still_one) == Unsigned);
136 try expect(still_one == 1);
137 }
138 };
139
140 try S.doTheTest();
141 try comptime S.doTheTest();
142}
143
90test "@abs big int <= 128 bits" {144test "@abs big int <= 128 bits" {
91 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO145 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
92 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO146 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO