chore: reorganize util.zig
This commit is contained in:
parent
78080b4682
commit
bf4207ba8c
10
src/util.zig
10
src/util.zig
|
@ -1,6 +1,10 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
pub fn signExtend(comptime T: type, comptime bits: usize, value: anytype) T {
|
pub fn u32SignExtend(comptime bits: usize, value: u32) u32 {
|
||||||
|
return @bitCast(u32, signExtend(i32, bits, @bitCast(i32, value)));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signExtend(comptime T: type, comptime bits: usize, value: anytype) T {
|
||||||
const ValT = comptime @TypeOf(value);
|
const ValT = comptime @TypeOf(value);
|
||||||
comptime std.debug.assert(isInteger(ValT));
|
comptime std.debug.assert(isInteger(ValT));
|
||||||
comptime std.debug.assert(isSigned(ValT));
|
comptime std.debug.assert(isSigned(ValT));
|
||||||
|
@ -16,10 +20,6 @@ pub fn signExtend(comptime T: type, comptime bits: usize, value: anytype) T {
|
||||||
return ((value & ((1 << bits) - 1)) << bit_diff) >> bit_diff;
|
return ((value & ((1 << bits) - 1)) << bit_diff) >> bit_diff;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn u32SignExtend(comptime bits: usize, value: u32) u32 {
|
|
||||||
return @bitCast(u32, signExtend(i32, bits, @bitCast(i32, value)));
|
|
||||||
}
|
|
||||||
|
|
||||||
fn isInteger(comptime T: type) bool {
|
fn isInteger(comptime T: type) bool {
|
||||||
return @typeInfo(T) == .Int;
|
return @typeInfo(T) == .Int;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue