zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit b31a91bbef2bafb19fd2b76cd8a1c8a4ed15eb61 (tree)
parent ed9aa8f259d452cb344064ee412fc5af18877d55
Author: Jacob Young <jacobly0@users.noreply.github.com>
Date:   Fri, 11 Apr 2025 04:23:36 -0400

compiler-rt: compute correct integer sizes from bits at runtime

Also, accepting `align(1)` pointers ensures that the alignment is safety
checked rather than assumed.

Diffstat:
Mlib/compiler_rt/divmodei4.zig | 18++++++++++--------
Mlib/compiler_rt/fixdfei.zig | 12+++++++-----
Mlib/compiler_rt/fixhfei.zig | 12+++++++-----
Mlib/compiler_rt/fixsfei.zig | 12+++++++-----
Mlib/compiler_rt/fixtfei.zig | 12+++++++-----
Mlib/compiler_rt/fixunsdfei.zig | 12+++++++-----
Mlib/compiler_rt/fixunshfei.zig | 12+++++++-----
Mlib/compiler_rt/fixunssfei.zig | 12+++++++-----
Mlib/compiler_rt/fixunstfei.zig | 12+++++++-----
Mlib/compiler_rt/fixunsxfei.zig | 12+++++++-----
Mlib/compiler_rt/fixxfei.zig | 12+++++++-----
Mlib/compiler_rt/float_from_int_test.zig | 6+-----
Mlib/compiler_rt/floateidf.zig | 12+++++++-----
Mlib/compiler_rt/floateihf.zig | 12+++++++-----
Mlib/compiler_rt/floateisf.zig | 12+++++++-----
Mlib/compiler_rt/floateitf.zig | 12+++++++-----
Mlib/compiler_rt/floateixf.zig | 12+++++++-----
Mlib/compiler_rt/floatuneidf.zig | 12+++++++-----
Mlib/compiler_rt/floatuneihf.zig | 12+++++++-----
Mlib/compiler_rt/floatuneisf.zig | 12+++++++-----
Mlib/compiler_rt/floatuneitf.zig | 12+++++++-----
Mlib/compiler_rt/floatuneixf.zig | 12+++++++-----
Mlib/compiler_rt/int_from_float_test.zig | 18++++++------------
Mlib/compiler_rt/udivmodei4.zig | 18++++++++++--------
24 files changed, 167 insertions(+), 133 deletions(-)

diff --git a/lib/compiler_rt/divmodei4.zig b/lib/compiler_rt/divmodei4.zig @@ -33,18 +33,20 @@ fn divmod(q: ?[]u32, r: ?[]u32, u: []u32, v: []u32) !void { if (r) |x| if (u_sign < 0) neg(x); } -pub fn __divei4(r_q: [*]u32, u_p: [*]u32, v_p: [*]u32, bits: usize) callconv(.c) void { +pub fn __divei4(q_p: [*]u8, u_p: [*]u8, v_p: [*]u8, bits: usize) callconv(.c) void { @setRuntimeSafety(builtin.is_test); - const u = u_p[0 .. std.math.divCeil(usize, bits, 32) catch unreachable]; - const v = v_p[0 .. std.math.divCeil(usize, bits, 32) catch unreachable]; - const q = r_q[0 .. std.math.divCeil(usize, bits, 32) catch unreachable]; + const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits)); + const q: []u32 = @ptrCast(@alignCast(q_p[0..byte_size])); + const u: []u32 = @ptrCast(@alignCast(u_p[0..byte_size])); + const v: []u32 = @ptrCast(@alignCast(v_p[0..byte_size])); @call(.always_inline, divmod, .{ q, null, u, v }) catch unreachable; } -pub fn __modei4(r_p: [*]u32, u_p: [*]u32, v_p: [*]u32, bits: usize) callconv(.c) void { +pub fn __modei4(r_p: [*]u8, u_p: [*]u8, v_p: [*]u8, bits: usize) callconv(.c) void { @setRuntimeSafety(builtin.is_test); - const u = u_p[0 .. std.math.divCeil(usize, bits, 32) catch unreachable]; - const v = v_p[0 .. std.math.divCeil(usize, bits, 32) catch unreachable]; - const r = r_p[0 .. std.math.divCeil(usize, bits, 32) catch unreachable]; + const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits)); + const r: []u32 = @ptrCast(@alignCast(r_p[0..byte_size])); + const u: []u32 = @ptrCast(@alignCast(u_p[0..byte_size])); + const v: []u32 = @ptrCast(@alignCast(v_p[0..byte_size])); @call(.always_inline, divmod, .{ null, r, u, v }) catch unreachable; } diff --git a/lib/compiler_rt/fixdfei.zig b/lib/compiler_rt/fixdfei.zig @@ -1,6 +1,7 @@ -const divCeil = @import("std").math.divCeil; -const common = @import("./common.zig"); -const bigIntFromFloat = @import("./int_from_float.zig").bigIntFromFloat; +const std = @import("std"); +const builtin = @import("builtin"); +const common = @import("common.zig"); +const bigIntFromFloat = @import("int_from_float.zig").bigIntFromFloat; pub const panic = common.panic; @@ -8,6 +9,7 @@ comptime { @export(&__fixdfei, .{ .name = "__fixdfei", .linkage = common.linkage, .visibility = common.visibility }); } -pub fn __fixdfei(r: [*]u32, bits: usize, a: f64) callconv(.c) void { - return bigIntFromFloat(.signed, r[0 .. divCeil(usize, bits, 32) catch unreachable], a); +pub fn __fixdfei(r: [*]u8, bits: usize, a: f64) callconv(.c) void { + const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits)); + return bigIntFromFloat(.signed, @ptrCast(@alignCast(r[0..byte_size])), a); } diff --git a/lib/compiler_rt/fixhfei.zig b/lib/compiler_rt/fixhfei.zig @@ -1,6 +1,7 @@ -const divCeil = @import("std").math.divCeil; -const common = @import("./common.zig"); -const bigIntFromFloat = @import("./int_from_float.zig").bigIntFromFloat; +const std = @import("std"); +const builtin = @import("builtin"); +const common = @import("common.zig"); +const bigIntFromFloat = @import("int_from_float.zig").bigIntFromFloat; pub const panic = common.panic; @@ -8,6 +9,7 @@ comptime { @export(&__fixhfei, .{ .name = "__fixhfei", .linkage = common.linkage, .visibility = common.visibility }); } -pub fn __fixhfei(r: [*]u32, bits: usize, a: f16) callconv(.c) void { - return bigIntFromFloat(.signed, r[0 .. divCeil(usize, bits, 32) catch unreachable], a); +pub fn __fixhfei(r: [*]u8, bits: usize, a: f16) callconv(.c) void { + const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits)); + return bigIntFromFloat(.signed, @ptrCast(@alignCast(r[0..byte_size])), a); } diff --git a/lib/compiler_rt/fixsfei.zig b/lib/compiler_rt/fixsfei.zig @@ -1,6 +1,7 @@ -const divCeil = @import("std").math.divCeil; -const common = @import("./common.zig"); -const bigIntFromFloat = @import("./int_from_float.zig").bigIntFromFloat; +const std = @import("std"); +const builtin = @import("builtin"); +const common = @import("common.zig"); +const bigIntFromFloat = @import("int_from_float.zig").bigIntFromFloat; pub const panic = common.panic; @@ -8,6 +9,7 @@ comptime { @export(&__fixsfei, .{ .name = "__fixsfei", .linkage = common.linkage, .visibility = common.visibility }); } -pub fn __fixsfei(r: [*]u32, bits: usize, a: f32) callconv(.c) void { - return bigIntFromFloat(.signed, r[0 .. divCeil(usize, bits, 32) catch unreachable], a); +pub fn __fixsfei(r: [*]u8, bits: usize, a: f32) callconv(.c) void { + const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits)); + return bigIntFromFloat(.signed, @ptrCast(@alignCast(r[0..byte_size])), a); } diff --git a/lib/compiler_rt/fixtfei.zig b/lib/compiler_rt/fixtfei.zig @@ -1,6 +1,7 @@ -const divCeil = @import("std").math.divCeil; -const common = @import("./common.zig"); -const bigIntFromFloat = @import("./int_from_float.zig").bigIntFromFloat; +const std = @import("std"); +const builtin = @import("builtin"); +const common = @import("common.zig"); +const bigIntFromFloat = @import("int_from_float.zig").bigIntFromFloat; pub const panic = common.panic; @@ -8,6 +9,7 @@ comptime { @export(&__fixtfei, .{ .name = "__fixtfei", .linkage = common.linkage, .visibility = common.visibility }); } -pub fn __fixtfei(r: [*]u32, bits: usize, a: f128) callconv(.c) void { - return bigIntFromFloat(.signed, r[0 .. divCeil(usize, bits, 32) catch unreachable], a); +pub fn __fixtfei(r: [*]u8, bits: usize, a: f128) callconv(.c) void { + const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits)); + return bigIntFromFloat(.signed, @ptrCast(@alignCast(r[0..byte_size])), a); } diff --git a/lib/compiler_rt/fixunsdfei.zig b/lib/compiler_rt/fixunsdfei.zig @@ -1,6 +1,7 @@ -const divCeil = @import("std").math.divCeil; -const common = @import("./common.zig"); -const bigIntFromFloat = @import("./int_from_float.zig").bigIntFromFloat; +const std = @import("std"); +const builtin = @import("builtin"); +const common = @import("common.zig"); +const bigIntFromFloat = @import("int_from_float.zig").bigIntFromFloat; pub const panic = common.panic; @@ -8,6 +9,7 @@ comptime { @export(&__fixunsdfei, .{ .name = "__fixunsdfei", .linkage = common.linkage, .visibility = common.visibility }); } -pub fn __fixunsdfei(r: [*]u32, bits: usize, a: f64) callconv(.c) void { - return bigIntFromFloat(.unsigned, r[0 .. divCeil(usize, bits, 32) catch unreachable], a); +pub fn __fixunsdfei(r: [*]u8, bits: usize, a: f64) callconv(.c) void { + const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits)); + return bigIntFromFloat(.unsigned, @ptrCast(@alignCast(r[0..byte_size])), a); } diff --git a/lib/compiler_rt/fixunshfei.zig b/lib/compiler_rt/fixunshfei.zig @@ -1,6 +1,7 @@ -const divCeil = @import("std").math.divCeil; -const common = @import("./common.zig"); -const bigIntFromFloat = @import("./int_from_float.zig").bigIntFromFloat; +const std = @import("std"); +const builtin = @import("builtin"); +const common = @import("common.zig"); +const bigIntFromFloat = @import("int_from_float.zig").bigIntFromFloat; pub const panic = common.panic; @@ -8,6 +9,7 @@ comptime { @export(&__fixunshfei, .{ .name = "__fixunshfei", .linkage = common.linkage, .visibility = common.visibility }); } -pub fn __fixunshfei(r: [*]u32, bits: usize, a: f16) callconv(.c) void { - return bigIntFromFloat(.unsigned, r[0 .. divCeil(usize, bits, 32) catch unreachable], a); +pub fn __fixunshfei(r: [*]u8, bits: usize, a: f16) callconv(.c) void { + const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits)); + return bigIntFromFloat(.unsigned, @ptrCast(@alignCast(r[0..byte_size])), a); } diff --git a/lib/compiler_rt/fixunssfei.zig b/lib/compiler_rt/fixunssfei.zig @@ -1,6 +1,7 @@ -const divCeil = @import("std").math.divCeil; -const common = @import("./common.zig"); -const bigIntFromFloat = @import("./int_from_float.zig").bigIntFromFloat; +const std = @import("std"); +const builtin = @import("builtin"); +const common = @import("common.zig"); +const bigIntFromFloat = @import("int_from_float.zig").bigIntFromFloat; pub const panic = common.panic; @@ -8,6 +9,7 @@ comptime { @export(&__fixunssfei, .{ .name = "__fixunssfei", .linkage = common.linkage, .visibility = common.visibility }); } -pub fn __fixunssfei(r: [*]u32, bits: usize, a: f32) callconv(.c) void { - return bigIntFromFloat(.unsigned, r[0 .. divCeil(usize, bits, 32) catch unreachable], a); +pub fn __fixunssfei(r: [*]u8, bits: usize, a: f32) callconv(.c) void { + const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits)); + return bigIntFromFloat(.unsigned, @ptrCast(@alignCast(r[0..byte_size])), a); } diff --git a/lib/compiler_rt/fixunstfei.zig b/lib/compiler_rt/fixunstfei.zig @@ -1,6 +1,7 @@ -const divCeil = @import("std").math.divCeil; -const common = @import("./common.zig"); -const bigIntFromFloat = @import("./int_from_float.zig").bigIntFromFloat; +const std = @import("std"); +const builtin = @import("builtin"); +const common = @import("common.zig"); +const bigIntFromFloat = @import("int_from_float.zig").bigIntFromFloat; pub const panic = common.panic; @@ -8,6 +9,7 @@ comptime { @export(&__fixunstfei, .{ .name = "__fixunstfei", .linkage = common.linkage, .visibility = common.visibility }); } -pub fn __fixunstfei(r: [*]u32, bits: usize, a: f128) callconv(.c) void { - return bigIntFromFloat(.unsigned, r[0 .. divCeil(usize, bits, 32) catch unreachable], a); +pub fn __fixunstfei(r: [*]u8, bits: usize, a: f128) callconv(.c) void { + const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits)); + return bigIntFromFloat(.unsigned, @ptrCast(@alignCast(r[0..byte_size])), a); } diff --git a/lib/compiler_rt/fixunsxfei.zig b/lib/compiler_rt/fixunsxfei.zig @@ -1,6 +1,7 @@ -const divCeil = @import("std").math.divCeil; -const common = @import("./common.zig"); -const bigIntFromFloat = @import("./int_from_float.zig").bigIntFromFloat; +const std = @import("std"); +const builtin = @import("builtin"); +const common = @import("common.zig"); +const bigIntFromFloat = @import("int_from_float.zig").bigIntFromFloat; pub const panic = common.panic; @@ -8,6 +9,7 @@ comptime { @export(&__fixunsxfei, .{ .name = "__fixunsxfei", .linkage = common.linkage, .visibility = common.visibility }); } -pub fn __fixunsxfei(r: [*]u32, bits: usize, a: f80) callconv(.c) void { - return bigIntFromFloat(.unsigned, r[0 .. divCeil(usize, bits, 32) catch unreachable], a); +pub fn __fixunsxfei(r: [*]u8, bits: usize, a: f80) callconv(.c) void { + const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits)); + return bigIntFromFloat(.unsigned, @ptrCast(@alignCast(r[0..byte_size])), a); } diff --git a/lib/compiler_rt/fixxfei.zig b/lib/compiler_rt/fixxfei.zig @@ -1,6 +1,7 @@ -const divCeil = @import("std").math.divCeil; -const common = @import("./common.zig"); -const bigIntFromFloat = @import("./int_from_float.zig").bigIntFromFloat; +const std = @import("std"); +const builtin = @import("builtin"); +const common = @import("common.zig"); +const bigIntFromFloat = @import("int_from_float.zig").bigIntFromFloat; pub const panic = common.panic; @@ -8,6 +9,7 @@ comptime { @export(&__fixxfei, .{ .name = "__fixxfei", .linkage = common.linkage, .visibility = common.visibility }); } -pub fn __fixxfei(r: [*]u32, bits: usize, a: f80) callconv(.c) void { - return bigIntFromFloat(.signed, r[0 .. divCeil(usize, bits, 32) catch unreachable], a); +pub fn __fixxfei(r: [*]u8, bits: usize, a: f80) callconv(.c) void { + const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits)); + return bigIntFromFloat(.signed, @ptrCast(@alignCast(r[0..byte_size])), a); } diff --git a/lib/compiler_rt/float_from_int_test.zig b/lib/compiler_rt/float_from_int_test.zig @@ -1,8 +1,6 @@ const std = @import("std"); -const builtin = @import("builtin"); const testing = std.testing; const math = std.math; -const endian = builtin.cpu.arch.endian(); const __floatunsihf = @import("floatunsihf.zig").__floatunsihf; @@ -237,12 +235,10 @@ test "floatuntisf" { fn test_floateisf(expected: u32, comptime T: type, a: T) !void { const int = @typeInfo(T).int; - var a_buf: [@divExact(int.bits, 32)]u32 = undefined; - std.mem.writeInt(T, std.mem.asBytes(&a_buf), a, endian); const r = switch (int.signedness) { .signed => __floateisf, .unsigned => __floatuneisf, - }(&a_buf, int.bits); + }(@ptrCast(&a), int.bits); try testing.expect(expected == @as(u32, @bitCast(r))); } diff --git a/lib/compiler_rt/floateidf.zig b/lib/compiler_rt/floateidf.zig @@ -1,6 +1,7 @@ -const divCeil = @import("std").math.divCeil; -const common = @import("./common.zig"); -const floatFromBigInt = @import("./float_from_int.zig").floatFromBigInt; +const std = @import("std"); +const builtin = @import("builtin"); +const common = @import("common.zig"); +const floatFromBigInt = @import("float_from_int.zig").floatFromBigInt; pub const panic = common.panic; @@ -8,6 +9,7 @@ comptime { @export(&__floateidf, .{ .name = "__floateidf", .linkage = common.linkage, .visibility = common.visibility }); } -pub fn __floateidf(a: [*]const u32, bits: usize) callconv(.c) f64 { - return floatFromBigInt(f64, .signed, a[0 .. divCeil(usize, bits, 32) catch unreachable]); +pub fn __floateidf(a: [*]const u8, bits: usize) callconv(.c) f64 { + const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits)); + return floatFromBigInt(f64, .signed, @ptrCast(@alignCast(a[0..byte_size]))); } diff --git a/lib/compiler_rt/floateihf.zig b/lib/compiler_rt/floateihf.zig @@ -1,6 +1,7 @@ -const divCeil = @import("std").math.divCeil; -const common = @import("./common.zig"); -const floatFromBigInt = @import("./float_from_int.zig").floatFromBigInt; +const std = @import("std"); +const builtin = @import("builtin"); +const common = @import("common.zig"); +const floatFromBigInt = @import("float_from_int.zig").floatFromBigInt; pub const panic = common.panic; @@ -8,6 +9,7 @@ comptime { @export(&__floateihf, .{ .name = "__floateihf", .linkage = common.linkage, .visibility = common.visibility }); } -pub fn __floateihf(a: [*]const u32, bits: usize) callconv(.c) f16 { - return floatFromBigInt(f16, .signed, a[0 .. divCeil(usize, bits, 32) catch unreachable]); +pub fn __floateihf(a: [*]const u8, bits: usize) callconv(.c) f16 { + const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits)); + return floatFromBigInt(f16, .signed, @ptrCast(@alignCast(a[0..byte_size]))); } diff --git a/lib/compiler_rt/floateisf.zig b/lib/compiler_rt/floateisf.zig @@ -1,6 +1,7 @@ -const divCeil = @import("std").math.divCeil; -const common = @import("./common.zig"); -const floatFromBigInt = @import("./float_from_int.zig").floatFromBigInt; +const std = @import("std"); +const builtin = @import("builtin"); +const common = @import("common.zig"); +const floatFromBigInt = @import("float_from_int.zig").floatFromBigInt; pub const panic = common.panic; @@ -8,6 +9,7 @@ comptime { @export(&__floateisf, .{ .name = "__floateisf", .linkage = common.linkage, .visibility = common.visibility }); } -pub fn __floateisf(a: [*]const u32, bits: usize) callconv(.c) f32 { - return floatFromBigInt(f32, .signed, a[0 .. divCeil(usize, bits, 32) catch unreachable]); +pub fn __floateisf(a: [*]const u8, bits: usize) callconv(.c) f32 { + const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits)); + return floatFromBigInt(f32, .signed, @ptrCast(@alignCast(a[0..byte_size]))); } diff --git a/lib/compiler_rt/floateitf.zig b/lib/compiler_rt/floateitf.zig @@ -1,6 +1,7 @@ -const divCeil = @import("std").math.divCeil; -const common = @import("./common.zig"); -const floatFromBigInt = @import("./float_from_int.zig").floatFromBigInt; +const std = @import("std"); +const builtin = @import("builtin"); +const common = @import("common.zig"); +const floatFromBigInt = @import("float_from_int.zig").floatFromBigInt; pub const panic = common.panic; @@ -8,6 +9,7 @@ comptime { @export(&__floateitf, .{ .name = "__floateitf", .linkage = common.linkage, .visibility = common.visibility }); } -pub fn __floateitf(a: [*]const u32, bits: usize) callconv(.c) f128 { - return floatFromBigInt(f128, .signed, a[0 .. divCeil(usize, bits, 32) catch unreachable]); +pub fn __floateitf(a: [*]const u8, bits: usize) callconv(.c) f128 { + const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits)); + return floatFromBigInt(f128, .signed, @ptrCast(@alignCast(a[0..byte_size]))); } diff --git a/lib/compiler_rt/floateixf.zig b/lib/compiler_rt/floateixf.zig @@ -1,6 +1,7 @@ -const divCeil = @import("std").math.divCeil; -const common = @import("./common.zig"); -const floatFromBigInt = @import("./float_from_int.zig").floatFromBigInt; +const std = @import("std"); +const builtin = @import("builtin"); +const common = @import("common.zig"); +const floatFromBigInt = @import("float_from_int.zig").floatFromBigInt; pub const panic = common.panic; @@ -8,6 +9,7 @@ comptime { @export(&__floateixf, .{ .name = "__floateixf", .linkage = common.linkage, .visibility = common.visibility }); } -pub fn __floateixf(a: [*]const u32, bits: usize) callconv(.c) f80 { - return floatFromBigInt(f80, .signed, a[0 .. divCeil(usize, bits, 32) catch unreachable]); +pub fn __floateixf(a: [*]const u8, bits: usize) callconv(.c) f80 { + const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits)); + return floatFromBigInt(f80, .signed, @ptrCast(@alignCast(a[0..byte_size]))); } diff --git a/lib/compiler_rt/floatuneidf.zig b/lib/compiler_rt/floatuneidf.zig @@ -1,6 +1,7 @@ -const divCeil = @import("std").math.divCeil; -const common = @import("./common.zig"); -const floatFromBigInt = @import("./float_from_int.zig").floatFromBigInt; +const std = @import("std"); +const builtin = @import("builtin"); +const common = @import("common.zig"); +const floatFromBigInt = @import("float_from_int.zig").floatFromBigInt; pub const panic = common.panic; @@ -8,6 +9,7 @@ comptime { @export(&__floatuneidf, .{ .name = "__floatuneidf", .linkage = common.linkage, .visibility = common.visibility }); } -pub fn __floatuneidf(a: [*]const u32, bits: usize) callconv(.c) f64 { - return floatFromBigInt(f64, .unsigned, a[0 .. divCeil(usize, bits, 32) catch unreachable]); +pub fn __floatuneidf(a: [*]const u8, bits: usize) callconv(.c) f64 { + const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits)); + return floatFromBigInt(f64, .unsigned, @ptrCast(@alignCast(a[0..byte_size]))); } diff --git a/lib/compiler_rt/floatuneihf.zig b/lib/compiler_rt/floatuneihf.zig @@ -1,6 +1,7 @@ -const divCeil = @import("std").math.divCeil; -const common = @import("./common.zig"); -const floatFromBigInt = @import("./float_from_int.zig").floatFromBigInt; +const std = @import("std"); +const builtin = @import("builtin"); +const common = @import("common.zig"); +const floatFromBigInt = @import("float_from_int.zig").floatFromBigInt; pub const panic = common.panic; @@ -8,6 +9,7 @@ comptime { @export(&__floatuneihf, .{ .name = "__floatuneihf", .linkage = common.linkage, .visibility = common.visibility }); } -pub fn __floatuneihf(a: [*]const u32, bits: usize) callconv(.c) f16 { - return floatFromBigInt(f16, .unsigned, a[0 .. divCeil(usize, bits, 32) catch unreachable]); +pub fn __floatuneihf(a: [*]const u8, bits: usize) callconv(.c) f16 { + const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits)); + return floatFromBigInt(f16, .unsigned, @ptrCast(@alignCast(a[0..byte_size]))); } diff --git a/lib/compiler_rt/floatuneisf.zig b/lib/compiler_rt/floatuneisf.zig @@ -1,6 +1,7 @@ -const divCeil = @import("std").math.divCeil; -const common = @import("./common.zig"); -const floatFromBigInt = @import("./float_from_int.zig").floatFromBigInt; +const std = @import("std"); +const builtin = @import("builtin"); +const common = @import("common.zig"); +const floatFromBigInt = @import("float_from_int.zig").floatFromBigInt; pub const panic = common.panic; @@ -8,6 +9,7 @@ comptime { @export(&__floatuneisf, .{ .name = "__floatuneisf", .linkage = common.linkage, .visibility = common.visibility }); } -pub fn __floatuneisf(a: [*]const u32, bits: usize) callconv(.c) f32 { - return floatFromBigInt(f32, .unsigned, a[0 .. divCeil(usize, bits, 32) catch unreachable]); +pub fn __floatuneisf(a: [*]const u8, bits: usize) callconv(.c) f32 { + const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits)); + return floatFromBigInt(f32, .unsigned, @ptrCast(@alignCast(a[0..byte_size]))); } diff --git a/lib/compiler_rt/floatuneitf.zig b/lib/compiler_rt/floatuneitf.zig @@ -1,6 +1,7 @@ -const divCeil = @import("std").math.divCeil; -const common = @import("./common.zig"); -const floatFromBigInt = @import("./float_from_int.zig").floatFromBigInt; +const std = @import("std"); +const builtin = @import("builtin"); +const common = @import("common.zig"); +const floatFromBigInt = @import("float_from_int.zig").floatFromBigInt; pub const panic = common.panic; @@ -8,6 +9,7 @@ comptime { @export(&__floatuneitf, .{ .name = "__floatuneitf", .linkage = common.linkage, .visibility = common.visibility }); } -pub fn __floatuneitf(a: [*]const u32, bits: usize) callconv(.c) f128 { - return floatFromBigInt(f128, .unsigned, a[0 .. divCeil(usize, bits, 32) catch unreachable]); +pub fn __floatuneitf(a: [*]const u8, bits: usize) callconv(.c) f128 { + const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits)); + return floatFromBigInt(f128, .unsigned, @ptrCast(@alignCast(a[0..byte_size]))); } diff --git a/lib/compiler_rt/floatuneixf.zig b/lib/compiler_rt/floatuneixf.zig @@ -1,6 +1,7 @@ -const divCeil = @import("std").math.divCeil; -const common = @import("./common.zig"); -const floatFromBigInt = @import("./float_from_int.zig").floatFromBigInt; +const std = @import("std"); +const builtin = @import("builtin"); +const common = @import("common.zig"); +const floatFromBigInt = @import("float_from_int.zig").floatFromBigInt; pub const panic = common.panic; @@ -8,6 +9,7 @@ comptime { @export(&__floatuneixf, .{ .name = "__floatuneixf", .linkage = common.linkage, .visibility = common.visibility }); } -pub fn __floatuneixf(a: [*]const u32, bits: usize) callconv(.c) f80 { - return floatFromBigInt(f80, .unsigned, a[0 .. divCeil(usize, bits, 32) catch unreachable]); +pub fn __floatuneixf(a: [*]const u8, bits: usize) callconv(.c) f80 { + const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits)); + return floatFromBigInt(f80, .unsigned, @ptrCast(@alignCast(a[0..byte_size]))); } diff --git a/lib/compiler_rt/int_from_float_test.zig b/lib/compiler_rt/int_from_float_test.zig @@ -1,8 +1,6 @@ const std = @import("std"); -const builtin = @import("builtin"); const testing = std.testing; const math = std.math; -const endian = builtin.cpu.arch.endian(); const __fixunshfti = @import("fixunshfti.zig").__fixunshfti; const __fixunsxfti = @import("fixunsxfti.zig").__fixunsxfti; @@ -350,14 +348,12 @@ test "fixunssfti" { fn test_fixsfei(comptime T: type, expected: T, a: f32) !void { const int = @typeInfo(T).int; - var expected_buf: [@divExact(int.bits, 32)]u32 = undefined; - std.mem.writeInt(T, std.mem.asBytes(&expected_buf), expected, endian); - var actual_buf: [@divExact(int.bits, 32)]u32 = undefined; + var actual: T = undefined; _ = switch (int.signedness) { .signed => __fixsfei, .unsigned => __fixunssfei, - }(&actual_buf, int.bits, a); - try testing.expect(std.mem.eql(u32, &expected_buf, &actual_buf)); + }(@ptrCast(&actual), int.bits, a); + try testing.expect(expected == actual); } test "fixsfei" { @@ -685,14 +681,12 @@ test "fixunsdfti" { fn test_fixdfei(comptime T: type, expected: T, a: f64) !void { const int = @typeInfo(T).int; - var expected_buf: [@divExact(int.bits, 32)]u32 = undefined; - std.mem.writeInt(T, std.mem.asBytes(&expected_buf), expected, endian); - var actual_buf: [@divExact(int.bits, 32)]u32 = undefined; + var actual: T = undefined; _ = switch (int.signedness) { .signed => __fixdfei, .unsigned => __fixunsdfei, - }(&actual_buf, int.bits, a); - try testing.expect(std.mem.eql(u32, &expected_buf, &actual_buf)); + }(@ptrCast(&actual), int.bits, a); + try testing.expect(expected == actual); } test "fixdfei" { diff --git a/lib/compiler_rt/udivmodei4.zig b/lib/compiler_rt/udivmodei4.zig @@ -112,19 +112,21 @@ pub fn divmod(q: ?[]u32, r: ?[]u32, u: []const u32, v: []const u32) !void { } } -pub fn __udivei4(r_q: [*]u32, u_p: [*]const u32, v_p: [*]const u32, bits: usize) callconv(.c) void { +pub fn __udivei4(q_p: [*]u8, u_p: [*]const u8, v_p: [*]const u8, bits: usize) callconv(.c) void { @setRuntimeSafety(builtin.is_test); - const u = u_p[0 .. std.math.divCeil(usize, bits, 32) catch unreachable]; - const v = v_p[0 .. std.math.divCeil(usize, bits, 32) catch unreachable]; - const q = r_q[0 .. std.math.divCeil(usize, bits, 32) catch unreachable]; + const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits)); + const q: []u32 = @ptrCast(@alignCast(q_p[0..byte_size])); + const u: []const u32 = @ptrCast(@alignCast(u_p[0..byte_size])); + const v: []const u32 = @ptrCast(@alignCast(v_p[0..byte_size])); @call(.always_inline, divmod, .{ q, null, u, v }) catch unreachable; } -pub fn __umodei4(r_p: [*]u32, u_p: [*]const u32, v_p: [*]const u32, bits: usize) callconv(.c) void { +pub fn __umodei4(r_p: [*]u8, u_p: [*]const u8, v_p: [*]const u8, bits: usize) callconv(.c) void { @setRuntimeSafety(builtin.is_test); - const u = u_p[0 .. std.math.divCeil(usize, bits, 32) catch unreachable]; - const v = v_p[0 .. std.math.divCeil(usize, bits, 32) catch unreachable]; - const r = r_p[0 .. std.math.divCeil(usize, bits, 32) catch unreachable]; + const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits)); + const r: []u32 = @ptrCast(@alignCast(r_p[0..byte_size])); + const u: []const u32 = @ptrCast(@alignCast(u_p[0..byte_size])); + const v: []const u32 = @ptrCast(@alignCast(v_p[0..byte_size])); @call(.always_inline, divmod, .{ null, r, u, v }) catch unreachable; }