mirror of
https://codeberg.org/ziglang/zig.git
synced 2026-03-08 05:04:52 +01:00
- use symbol export helper - move all declarations from common.zig into c.zig - correct documentation - delete dead code
36 lines
855 B
Zig
36 lines
855 B
Zig
const builtin = @import("builtin");
|
|
|
|
const std = @import("std");
|
|
const intmax_t = std.c.intmax_t;
|
|
const imaxdiv_t = std.c.imaxdiv_t;
|
|
|
|
const symbol = @import("../c.zig").symbol;
|
|
|
|
comptime {
|
|
if (builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) {
|
|
// Functions specific to musl and wasi-libc.
|
|
symbol(&imaxabs, "imaxabs");
|
|
symbol(&imaxdiv, "imaxdiv");
|
|
}
|
|
}
|
|
|
|
fn imaxabs(a: intmax_t) callconv(.c) intmax_t {
|
|
return @intCast(@abs(a));
|
|
}
|
|
|
|
fn imaxdiv(a: intmax_t, b: intmax_t) callconv(.c) imaxdiv_t {
|
|
return .{
|
|
.quot = @divTrunc(a, b),
|
|
.rem = @rem(a, b),
|
|
};
|
|
}
|
|
|
|
test imaxabs {
|
|
const val: intmax_t = -10;
|
|
try std.testing.expectEqual(10, imaxabs(val));
|
|
}
|
|
|
|
test imaxdiv {
|
|
const expected: imaxdiv_t = .{ .quot = 9, .rem = 0 };
|
|
try std.testing.expectEqual(expected, imaxdiv(9, 1));
|
|
}
|