authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-02-21 23:40:44+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-02-21 23:40:44+01:00
log6236f5b8105f4f5ae773190d18ebff2db90e34e5
tree2b5b9e5a3bb389abea35c365e5ff410968533105
parent2ad2e2f6f5c327187b37619c0d7dd207c29916a1

link+riscv: simplify bitSlice helper


1 files changed, 10 insertions(+), 6 deletions(-)

src/link/riscv.zig+10-6
...@@ -32,7 +32,7 @@ pub fn writeInstU(code: *[4]u8, value: u32) void {...@@ -32,7 +32,7 @@ pub fn writeInstU(code: *[4]u8, value: u32) void {
32 ), code),32 ), code),
33 };33 };
34 const compensated: u32 = @bitCast(@as(i32, @bitCast(value)) + 0x800);34 const compensated: u32 = @bitCast(@as(i32, @bitCast(value)) + 0x800);
35 inst.U.imm12_31 = @truncate(bitSlice(compensated, 31, 12));35 inst.U.imm12_31 = bitSlice(compensated, 31, 12);
36 mem.writeInt(u32, code, inst.toU32(), .little);36 mem.writeInt(u32, code, inst.toU32(), .little);
37}37}
3838
...@@ -43,7 +43,7 @@ pub fn writeInstI(code: *[4]u8, value: u32) void {...@@ -43,7 +43,7 @@ pub fn writeInstI(code: *[4]u8, value: u32) void {
43 Instruction.I,43 Instruction.I,
44 ), code),44 ), code),
45 };45 };
46 inst.I.imm0_11 = @truncate(bitSlice(value, 11, 0));46 inst.I.imm0_11 = bitSlice(value, 11, 0);
47 mem.writeInt(u32, code, inst.toU32(), .little);47 mem.writeInt(u32, code, inst.toU32(), .little);
48}48}
4949
...@@ -54,13 +54,17 @@ pub fn writeInstS(code: *[4]u8, value: u32) void {...@@ -54,13 +54,17 @@ pub fn writeInstS(code: *[4]u8, value: u32) void {
54 Instruction.S,54 Instruction.S,
55 ), code),55 ), code),
56 };56 };
57 inst.S.imm0_4 = @truncate(bitSlice(value, 4, 0));57 inst.S.imm0_4 = bitSlice(value, 4, 0);
58 inst.S.imm5_11 = @truncate(bitSlice(value, 11, 5));58 inst.S.imm5_11 = bitSlice(value, 11, 5);
59 mem.writeInt(u32, code, inst.toU32(), .little);59 mem.writeInt(u32, code, inst.toU32(), .little);
60}60}
6161
62fn bitSlice(value: anytype, high: std.math.Log2Int(@TypeOf(value)), low: std.math.Log2Int(@TypeOf(value))) @TypeOf(value) {62fn bitSlice(
63 return (value >> low) & ((@as(@TypeOf(value), 1) << (high - low + 1)) - 1);63 value: anytype,
64 comptime high: comptime_int,
65 comptime low: comptime_int,
66) std.math.IntFittingRange(0, 1 << high - low) {
67 return @truncate((value >> low) & (1 << (high - low + 1)) - 1);
64}68}
6569
66const bits = @import("../arch/riscv64/bits.zig");70const bits = @import("../arch/riscv64/bits.zig");