authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-10-13 20:06:22+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-10-15 22:59:20+02:00
log26265e29f4599660a4bfcbc196fc98110bdac6d0
tree9f92a464f934fa14f24b7b091abaac2aa0b66996
parent6cec052b205b27e89950097e0b5ef1f470dec11a
signature Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

std.c, libzigc: fix ffs() family signatures and implementation bugs


3 files changed, 8 insertions(+), 12 deletions(-)

lib/c/strings.zig+4-4
......@@ -44,19 +44,19 @@ fn rindex(str: [*:0]const c_char, value: c_int) callconv(.c) ?[*:0]c_char {
4444 return @constCast(str[std.mem.findScalarLast(u8, std.mem.span(@as([*:0]const u8, @ptrCast(str))), @truncate(@as(c_uint, @bitCast(value)))) orelse return null ..]);
4545}
4646
47fn firstBitSet(comptime T: type, value: T) T {
48 return @bitSizeOf(T) - @clz(value);
47fn firstBitSet(comptime T: type, value: T) c_int {
48 return if (value == 0) 0 else @as(c_int, @ctz(value)) + 1;
4949}
5050
5151fn ffs(i: c_int) callconv(.c) c_int {
5252 return firstBitSet(c_int, i);
5353}
5454
55fn ffsl(i: c_long) callconv(.c) c_long {
55fn ffsl(i: c_long) callconv(.c) c_int {
5656 return firstBitSet(c_long, i);
5757}
5858
59fn ffsll(i: c_longlong) callconv(.c) c_longlong {
59fn ffsll(i: c_longlong) callconv(.c) c_int {
6060 return firstBitSet(c_longlong, i);
6161}
6262
lib/std/c.zig+2-2
......@@ -11579,8 +11579,8 @@ pub const wcsdup = switch (builtin.abi) {
1157911579};
1158011580
1158111581pub extern "c" fn ffs(i: c_int) c_int;
11582pub extern "c" fn ffsl(i: c_long) c_long;
11583pub extern "c" fn ffsll(i: c_longlong) c_longlong;
11582pub extern "c" fn ffsl(i: c_long) c_int;
11583pub extern "c" fn ffsll(i: c_longlong) c_int;
1158411584
1158511585pub extern "c" fn erand48(xsubi: *[3]c_ushort) f64;
1158611586pub extern "c" fn jrand48(xsubi: *[3]c_ushort) c_long;
test/c/strings.zig+2-6
......@@ -26,9 +26,8 @@ fn testFfs(comptime T: type) !void {
2626 try testing.expectEqual(0, ffs(0));
2727
2828 for (0..@bitSizeOf(T)) |i| {
29 const bit = @as(T, 1) << @intCast(i);
30
31 try testing.expectEqual(@as(T, @intCast(i + 1)), ffs(bit));
29 try testing.expectEqual(@as(c_int, @intCast(i + 1)), ffs(@as(T, 1) << @intCast(i)));
30 try testing.expectEqual(@as(c_int, @intCast(i + 1)), ffs(@as(T, -1) << @intCast(i)));
3231 }
3332}
3433
......@@ -41,9 +40,6 @@ test "ffs" {
4140 if (builtin.target.os.tag == .netbsd) return; // no ffsl/ffsll until 11
4241
4342 try testFfs(c_long);
44
45 if (@sizeOf(usize) == 4) return error.SkipZigTest; // TODO
46
4743 try testFfs(c_longlong);
4844}
4945