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:58:53+02:00
log8b52a7c0c5a602439b13d7791902777bfe80d454
tree8e6fc208b9f09ea29a64b1a89eb2ea6a0c99729e
parent9c011c6a40e31995839bbea226715062bc5e07eb

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
......@@ -11377,8 +11377,8 @@ pub const wcsdup = switch (builtin.abi) {
1137711377};
1137811378
1137911379pub extern "c" fn ffs(i: c_int) c_int;
11380pub extern "c" fn ffsl(i: c_long) c_long;
11381pub extern "c" fn ffsll(i: c_longlong) c_longlong;
11380pub extern "c" fn ffsl(i: c_long) c_int;
11381pub extern "c" fn ffsll(i: c_longlong) c_int;
1138211382
1138311383pub extern "c" fn erand48(xsubi: *[3]c_ushort) f64;
1138411384pub 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