Skip to content

Commit b31a91b

Browse files
committed
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.
1 parent ed9aa8f commit b31a91b

24 files changed

+167
-133
lines changed

lib/compiler_rt/divmodei4.zig

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,20 @@ fn divmod(q: ?[]u32, r: ?[]u32, u: []u32, v: []u32) !void {
3333
if (r) |x| if (u_sign < 0) neg(x);
3434
}
3535

36-
pub fn __divei4(r_q: [*]u32, u_p: [*]u32, v_p: [*]u32, bits: usize) callconv(.c) void {
36+
pub fn __divei4(q_p: [*]u8, u_p: [*]u8, v_p: [*]u8, bits: usize) callconv(.c) void {
3737
@setRuntimeSafety(builtin.is_test);
38-
const u = u_p[0 .. std.math.divCeil(usize, bits, 32) catch unreachable];
39-
const v = v_p[0 .. std.math.divCeil(usize, bits, 32) catch unreachable];
40-
const q = r_q[0 .. std.math.divCeil(usize, bits, 32) catch unreachable];
38+
const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits));
39+
const q: []u32 = @ptrCast(@alignCast(q_p[0..byte_size]));
40+
const u: []u32 = @ptrCast(@alignCast(u_p[0..byte_size]));
41+
const v: []u32 = @ptrCast(@alignCast(v_p[0..byte_size]));
4142
@call(.always_inline, divmod, .{ q, null, u, v }) catch unreachable;
4243
}
4344

44-
pub fn __modei4(r_p: [*]u32, u_p: [*]u32, v_p: [*]u32, bits: usize) callconv(.c) void {
45+
pub fn __modei4(r_p: [*]u8, u_p: [*]u8, v_p: [*]u8, bits: usize) callconv(.c) void {
4546
@setRuntimeSafety(builtin.is_test);
46-
const u = u_p[0 .. std.math.divCeil(usize, bits, 32) catch unreachable];
47-
const v = v_p[0 .. std.math.divCeil(usize, bits, 32) catch unreachable];
48-
const r = r_p[0 .. std.math.divCeil(usize, bits, 32) catch unreachable];
47+
const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits));
48+
const r: []u32 = @ptrCast(@alignCast(r_p[0..byte_size]));
49+
const u: []u32 = @ptrCast(@alignCast(u_p[0..byte_size]));
50+
const v: []u32 = @ptrCast(@alignCast(v_p[0..byte_size]));
4951
@call(.always_inline, divmod, .{ null, r, u, v }) catch unreachable;
5052
}

lib/compiler_rt/fixdfei.zig

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
const divCeil = @import("std").math.divCeil;
2-
const common = @import("./common.zig");
3-
const bigIntFromFloat = @import("./int_from_float.zig").bigIntFromFloat;
1+
const std = @import("std");
2+
const builtin = @import("builtin");
3+
const common = @import("common.zig");
4+
const bigIntFromFloat = @import("int_from_float.zig").bigIntFromFloat;
45

56
pub const panic = common.panic;
67

78
comptime {
89
@export(&__fixdfei, .{ .name = "__fixdfei", .linkage = common.linkage, .visibility = common.visibility });
910
}
1011

11-
pub fn __fixdfei(r: [*]u32, bits: usize, a: f64) callconv(.c) void {
12-
return bigIntFromFloat(.signed, r[0 .. divCeil(usize, bits, 32) catch unreachable], a);
12+
pub fn __fixdfei(r: [*]u8, bits: usize, a: f64) callconv(.c) void {
13+
const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits));
14+
return bigIntFromFloat(.signed, @ptrCast(@alignCast(r[0..byte_size])), a);
1315
}

lib/compiler_rt/fixhfei.zig

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
const divCeil = @import("std").math.divCeil;
2-
const common = @import("./common.zig");
3-
const bigIntFromFloat = @import("./int_from_float.zig").bigIntFromFloat;
1+
const std = @import("std");
2+
const builtin = @import("builtin");
3+
const common = @import("common.zig");
4+
const bigIntFromFloat = @import("int_from_float.zig").bigIntFromFloat;
45

56
pub const panic = common.panic;
67

78
comptime {
89
@export(&__fixhfei, .{ .name = "__fixhfei", .linkage = common.linkage, .visibility = common.visibility });
910
}
1011

11-
pub fn __fixhfei(r: [*]u32, bits: usize, a: f16) callconv(.c) void {
12-
return bigIntFromFloat(.signed, r[0 .. divCeil(usize, bits, 32) catch unreachable], a);
12+
pub fn __fixhfei(r: [*]u8, bits: usize, a: f16) callconv(.c) void {
13+
const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits));
14+
return bigIntFromFloat(.signed, @ptrCast(@alignCast(r[0..byte_size])), a);
1315
}

lib/compiler_rt/fixsfei.zig

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
const divCeil = @import("std").math.divCeil;
2-
const common = @import("./common.zig");
3-
const bigIntFromFloat = @import("./int_from_float.zig").bigIntFromFloat;
1+
const std = @import("std");
2+
const builtin = @import("builtin");
3+
const common = @import("common.zig");
4+
const bigIntFromFloat = @import("int_from_float.zig").bigIntFromFloat;
45

56
pub const panic = common.panic;
67

78
comptime {
89
@export(&__fixsfei, .{ .name = "__fixsfei", .linkage = common.linkage, .visibility = common.visibility });
910
}
1011

11-
pub fn __fixsfei(r: [*]u32, bits: usize, a: f32) callconv(.c) void {
12-
return bigIntFromFloat(.signed, r[0 .. divCeil(usize, bits, 32) catch unreachable], a);
12+
pub fn __fixsfei(r: [*]u8, bits: usize, a: f32) callconv(.c) void {
13+
const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits));
14+
return bigIntFromFloat(.signed, @ptrCast(@alignCast(r[0..byte_size])), a);
1315
}

lib/compiler_rt/fixtfei.zig

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
const divCeil = @import("std").math.divCeil;
2-
const common = @import("./common.zig");
3-
const bigIntFromFloat = @import("./int_from_float.zig").bigIntFromFloat;
1+
const std = @import("std");
2+
const builtin = @import("builtin");
3+
const common = @import("common.zig");
4+
const bigIntFromFloat = @import("int_from_float.zig").bigIntFromFloat;
45

56
pub const panic = common.panic;
67

78
comptime {
89
@export(&__fixtfei, .{ .name = "__fixtfei", .linkage = common.linkage, .visibility = common.visibility });
910
}
1011

11-
pub fn __fixtfei(r: [*]u32, bits: usize, a: f128) callconv(.c) void {
12-
return bigIntFromFloat(.signed, r[0 .. divCeil(usize, bits, 32) catch unreachable], a);
12+
pub fn __fixtfei(r: [*]u8, bits: usize, a: f128) callconv(.c) void {
13+
const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits));
14+
return bigIntFromFloat(.signed, @ptrCast(@alignCast(r[0..byte_size])), a);
1315
}

lib/compiler_rt/fixunsdfei.zig

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
const divCeil = @import("std").math.divCeil;
2-
const common = @import("./common.zig");
3-
const bigIntFromFloat = @import("./int_from_float.zig").bigIntFromFloat;
1+
const std = @import("std");
2+
const builtin = @import("builtin");
3+
const common = @import("common.zig");
4+
const bigIntFromFloat = @import("int_from_float.zig").bigIntFromFloat;
45

56
pub const panic = common.panic;
67

78
comptime {
89
@export(&__fixunsdfei, .{ .name = "__fixunsdfei", .linkage = common.linkage, .visibility = common.visibility });
910
}
1011

11-
pub fn __fixunsdfei(r: [*]u32, bits: usize, a: f64) callconv(.c) void {
12-
return bigIntFromFloat(.unsigned, r[0 .. divCeil(usize, bits, 32) catch unreachable], a);
12+
pub fn __fixunsdfei(r: [*]u8, bits: usize, a: f64) callconv(.c) void {
13+
const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits));
14+
return bigIntFromFloat(.unsigned, @ptrCast(@alignCast(r[0..byte_size])), a);
1315
}

lib/compiler_rt/fixunshfei.zig

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
const divCeil = @import("std").math.divCeil;
2-
const common = @import("./common.zig");
3-
const bigIntFromFloat = @import("./int_from_float.zig").bigIntFromFloat;
1+
const std = @import("std");
2+
const builtin = @import("builtin");
3+
const common = @import("common.zig");
4+
const bigIntFromFloat = @import("int_from_float.zig").bigIntFromFloat;
45

56
pub const panic = common.panic;
67

78
comptime {
89
@export(&__fixunshfei, .{ .name = "__fixunshfei", .linkage = common.linkage, .visibility = common.visibility });
910
}
1011

11-
pub fn __fixunshfei(r: [*]u32, bits: usize, a: f16) callconv(.c) void {
12-
return bigIntFromFloat(.unsigned, r[0 .. divCeil(usize, bits, 32) catch unreachable], a);
12+
pub fn __fixunshfei(r: [*]u8, bits: usize, a: f16) callconv(.c) void {
13+
const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits));
14+
return bigIntFromFloat(.unsigned, @ptrCast(@alignCast(r[0..byte_size])), a);
1315
}

lib/compiler_rt/fixunssfei.zig

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
const divCeil = @import("std").math.divCeil;
2-
const common = @import("./common.zig");
3-
const bigIntFromFloat = @import("./int_from_float.zig").bigIntFromFloat;
1+
const std = @import("std");
2+
const builtin = @import("builtin");
3+
const common = @import("common.zig");
4+
const bigIntFromFloat = @import("int_from_float.zig").bigIntFromFloat;
45

56
pub const panic = common.panic;
67

78
comptime {
89
@export(&__fixunssfei, .{ .name = "__fixunssfei", .linkage = common.linkage, .visibility = common.visibility });
910
}
1011

11-
pub fn __fixunssfei(r: [*]u32, bits: usize, a: f32) callconv(.c) void {
12-
return bigIntFromFloat(.unsigned, r[0 .. divCeil(usize, bits, 32) catch unreachable], a);
12+
pub fn __fixunssfei(r: [*]u8, bits: usize, a: f32) callconv(.c) void {
13+
const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits));
14+
return bigIntFromFloat(.unsigned, @ptrCast(@alignCast(r[0..byte_size])), a);
1315
}

lib/compiler_rt/fixunstfei.zig

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
const divCeil = @import("std").math.divCeil;
2-
const common = @import("./common.zig");
3-
const bigIntFromFloat = @import("./int_from_float.zig").bigIntFromFloat;
1+
const std = @import("std");
2+
const builtin = @import("builtin");
3+
const common = @import("common.zig");
4+
const bigIntFromFloat = @import("int_from_float.zig").bigIntFromFloat;
45

56
pub const panic = common.panic;
67

78
comptime {
89
@export(&__fixunstfei, .{ .name = "__fixunstfei", .linkage = common.linkage, .visibility = common.visibility });
910
}
1011

11-
pub fn __fixunstfei(r: [*]u32, bits: usize, a: f128) callconv(.c) void {
12-
return bigIntFromFloat(.unsigned, r[0 .. divCeil(usize, bits, 32) catch unreachable], a);
12+
pub fn __fixunstfei(r: [*]u8, bits: usize, a: f128) callconv(.c) void {
13+
const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits));
14+
return bigIntFromFloat(.unsigned, @ptrCast(@alignCast(r[0..byte_size])), a);
1315
}

lib/compiler_rt/fixunsxfei.zig

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
const divCeil = @import("std").math.divCeil;
2-
const common = @import("./common.zig");
3-
const bigIntFromFloat = @import("./int_from_float.zig").bigIntFromFloat;
1+
const std = @import("std");
2+
const builtin = @import("builtin");
3+
const common = @import("common.zig");
4+
const bigIntFromFloat = @import("int_from_float.zig").bigIntFromFloat;
45

56
pub const panic = common.panic;
67

78
comptime {
89
@export(&__fixunsxfei, .{ .name = "__fixunsxfei", .linkage = common.linkage, .visibility = common.visibility });
910
}
1011

11-
pub fn __fixunsxfei(r: [*]u32, bits: usize, a: f80) callconv(.c) void {
12-
return bigIntFromFloat(.unsigned, r[0 .. divCeil(usize, bits, 32) catch unreachable], a);
12+
pub fn __fixunsxfei(r: [*]u8, bits: usize, a: f80) callconv(.c) void {
13+
const byte_size = std.zig.target.intByteSize(builtin.target, @intCast(bits));
14+
return bigIntFromFloat(.unsigned, @ptrCast(@alignCast(r[0..byte_size])), a);
1315
}

0 commit comments

Comments
 (0)