mirror of
https://codeberg.org/ziglang/zig.git
synced 2026-03-08 02:44:43 +01:00
feat(libzigc): add div, ldiv, lldiv and imaxdiv
* also remove musl implementation
This commit is contained in:
parent
62d6bbc7dc
commit
335c0fcba1
9 changed files with 77 additions and 32 deletions
|
|
@ -2,11 +2,13 @@ const std = @import("std");
|
|||
const common = @import("common.zig");
|
||||
const builtin = @import("builtin");
|
||||
const intmax_t = std.c.intmax_t;
|
||||
const imaxdiv_t = std.c.imaxdiv_t;
|
||||
|
||||
comptime {
|
||||
if (builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) {
|
||||
// Functions specific to musl and wasi-libc.
|
||||
@export(&imaxabs, .{ .name = "imaxabs", .linkage = common.linkage, .visibility = common.visibility });
|
||||
@export(&imaxdiv, .{ .name = "imaxdiv", .linkage = common.linkage, .visibility = common.visibility });
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -14,7 +16,19 @@ 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));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,9 @@
|
|||
const std = @import("std");
|
||||
const common = @import("common.zig");
|
||||
const builtin = @import("builtin");
|
||||
const div_t = std.c.div_t;
|
||||
const ldiv_t = std.c.ldiv_t;
|
||||
const lldiv_t = std.c.lldiv_t;
|
||||
|
||||
comptime {
|
||||
if (builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) {
|
||||
|
|
@ -9,6 +12,10 @@ comptime {
|
|||
@export(&labs, .{ .name = "labs", .linkage = common.linkage, .visibility = common.visibility });
|
||||
@export(&llabs, .{ .name = "llabs", .linkage = common.linkage, .visibility = common.visibility });
|
||||
|
||||
@export(&div, .{ .name = "div", .linkage = common.linkage, .visibility = common.visibility });
|
||||
@export(&ldiv, .{ .name = "ldiv", .linkage = common.linkage, .visibility = common.visibility });
|
||||
@export(&lldiv, .{ .name = "lldiv", .linkage = common.linkage, .visibility = common.visibility });
|
||||
|
||||
@export(&qsort_r, .{ .name = "qsort_r", .linkage = common.linkage, .visibility = common.visibility });
|
||||
@export(&qsort, .{ .name = "qsort", .linkage = common.linkage, .visibility = common.visibility });
|
||||
}
|
||||
|
|
@ -26,6 +33,27 @@ fn llabs(a: c_longlong) callconv(.c) c_longlong {
|
|||
return @intCast(@abs(a));
|
||||
}
|
||||
|
||||
fn div(a: c_int, b: c_int) callconv(.c) div_t {
|
||||
return .{
|
||||
.quot = @divTrunc(a, b),
|
||||
.rem = @rem(a, b),
|
||||
};
|
||||
}
|
||||
|
||||
fn ldiv(a: c_long, b: c_long) callconv(.c) ldiv_t {
|
||||
return .{
|
||||
.quot = @divTrunc(a, b),
|
||||
.rem = @rem(a, b),
|
||||
};
|
||||
}
|
||||
|
||||
fn lldiv(a: c_longlong, b: c_longlong) callconv(.c) lldiv_t {
|
||||
return .{
|
||||
.quot = @divTrunc(a, b),
|
||||
.rem = @rem(a, b),
|
||||
};
|
||||
}
|
||||
|
||||
// NOTE: Despite its name, `qsort` doesn't have to use quicksort or make any complexity or stability guarantee.
|
||||
fn qsort_r(base: *anyopaque, n: usize, size: usize, compare: *const fn (a: *const anyopaque, b: *const anyopaque, arg: ?*anyopaque) callconv(.c) c_int, arg: ?*anyopaque) callconv(.c) void {
|
||||
const Context = struct {
|
||||
|
|
@ -81,3 +109,18 @@ test llabs {
|
|||
const val: c_longlong = -10;
|
||||
try std.testing.expectEqual(10, llabs(val));
|
||||
}
|
||||
|
||||
test div {
|
||||
const expected: div_t = .{ .quot = 5, .rem = 5 };
|
||||
try std.testing.expectEqual(expected, div(55, 10));
|
||||
}
|
||||
|
||||
test ldiv {
|
||||
const expected: ldiv_t = .{ .quot = -6, .rem = 2 };
|
||||
try std.testing.expectEqual(expected, ldiv(38, -6));
|
||||
}
|
||||
|
||||
test lldiv {
|
||||
const expected: lldiv_t = .{ .quot = 1, .rem = 2 };
|
||||
try std.testing.expectEqual(expected, lldiv(5, 3));
|
||||
}
|
||||
|
|
|
|||
6
lib/libc/musl/src/stdlib/div.c
vendored
6
lib/libc/musl/src/stdlib/div.c
vendored
|
|
@ -1,6 +0,0 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
div_t div(int num, int den)
|
||||
{
|
||||
return (div_t){ num/den, num%den };
|
||||
}
|
||||
6
lib/libc/musl/src/stdlib/imaxdiv.c
vendored
6
lib/libc/musl/src/stdlib/imaxdiv.c
vendored
|
|
@ -1,6 +0,0 @@
|
|||
#include <inttypes.h>
|
||||
|
||||
imaxdiv_t imaxdiv(intmax_t num, intmax_t den)
|
||||
{
|
||||
return (imaxdiv_t){ num/den, num%den };
|
||||
}
|
||||
6
lib/libc/musl/src/stdlib/ldiv.c
vendored
6
lib/libc/musl/src/stdlib/ldiv.c
vendored
|
|
@ -1,6 +0,0 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
ldiv_t ldiv(long num, long den)
|
||||
{
|
||||
return (ldiv_t){ num/den, num%den };
|
||||
}
|
||||
6
lib/libc/musl/src/stdlib/lldiv.c
vendored
6
lib/libc/musl/src/stdlib/lldiv.c
vendored
|
|
@ -1,6 +0,0 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
lldiv_t lldiv(long long num, long long den)
|
||||
{
|
||||
return (lldiv_t){ num/den, num%den };
|
||||
}
|
||||
|
|
@ -11051,6 +11051,26 @@ else
|
|||
b: c_longdouble,
|
||||
};
|
||||
|
||||
pub const div_t = extern struct {
|
||||
quot: c_int,
|
||||
rem: c_int,
|
||||
};
|
||||
|
||||
pub const ldiv_t = extern struct {
|
||||
quot: c_long,
|
||||
rem: c_long,
|
||||
};
|
||||
|
||||
pub const lldiv_t = extern struct {
|
||||
quot: c_longlong,
|
||||
rem: c_longlong,
|
||||
};
|
||||
|
||||
pub const imaxdiv_t = extern struct {
|
||||
quot: intmax_t,
|
||||
rem: intmax_t,
|
||||
};
|
||||
|
||||
pub const intmax_t = i64;
|
||||
pub const uintmax_t = u64;
|
||||
|
||||
|
|
|
|||
|
|
@ -1718,13 +1718,9 @@ const src_files = [_][]const u8{
|
|||
"musl/src/stdlib/atol.c",
|
||||
"musl/src/stdlib/atoll.c",
|
||||
"musl/src/stdlib/bsearch.c",
|
||||
"musl/src/stdlib/div.c",
|
||||
"musl/src/stdlib/ecvt.c",
|
||||
"musl/src/stdlib/fcvt.c",
|
||||
"musl/src/stdlib/gcvt.c",
|
||||
"musl/src/stdlib/imaxdiv.c",
|
||||
"musl/src/stdlib/ldiv.c",
|
||||
"musl/src/stdlib/lldiv.c",
|
||||
"musl/src/stdlib/strtod.c",
|
||||
"musl/src/stdlib/strtol.c",
|
||||
"musl/src/stdlib/wcstod.c",
|
||||
|
|
|
|||
|
|
@ -1008,13 +1008,9 @@ const libc_top_half_src_files = [_][]const u8{
|
|||
"musl/src/stdlib/atol.c",
|
||||
"musl/src/stdlib/atoll.c",
|
||||
"musl/src/stdlib/bsearch.c",
|
||||
"musl/src/stdlib/div.c",
|
||||
"musl/src/stdlib/ecvt.c",
|
||||
"musl/src/stdlib/fcvt.c",
|
||||
"musl/src/stdlib/gcvt.c",
|
||||
"musl/src/stdlib/imaxdiv.c",
|
||||
"musl/src/stdlib/ldiv.c",
|
||||
"musl/src/stdlib/lldiv.c",
|
||||
"musl/src/stdlib/strtol.c",
|
||||
"musl/src/string/bcopy.c",
|
||||
"musl/src/string/explicit_bzero.c",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue