2021-12-29 21:09:00 +00:00
|
|
|
const std = @import("std");
|
2022-10-21 08:12:28 +00:00
|
|
|
const Log2Int = std.math.Log2Int;
|
2021-12-29 21:09:00 +00:00
|
|
|
|
2022-10-21 08:12:28 +00:00
|
|
|
pub inline fn sext(comptime bits: comptime_int, value: u32) u32 {
|
2022-10-21 08:12:24 +00:00
|
|
|
comptime std.debug.assert(bits <= 32);
|
|
|
|
const amount = 32 - bits;
|
2022-10-21 08:11:44 +00:00
|
|
|
|
2022-10-21 08:12:24 +00:00
|
|
|
return @bitCast(u32, @bitCast(i32, value << amount) >> amount);
|
2021-12-29 21:09:00 +00:00
|
|
|
}
|
2022-10-21 08:12:27 +00:00
|
|
|
|
2022-10-21 08:12:28 +00:00
|
|
|
/// See https://godbolt.org/z/W3en9Eche
|
|
|
|
pub inline fn rotr(comptime T: type, value: T, r: anytype) T {
|
|
|
|
comptime std.debug.assert(@typeInfo(T).Int.signedness == .unsigned);
|
|
|
|
const ar = @truncate(Log2Int(T), r);
|
|
|
|
|
|
|
|
return value >> ar | value << @truncate(Log2Int(T), @typeInfo(T).Int.bits - @as(T, ar));
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:12:27 +00:00
|
|
|
pub const FpsAverage = struct {
|
|
|
|
const Self = @This();
|
|
|
|
|
|
|
|
total: u64,
|
|
|
|
sample_count: u64,
|
|
|
|
|
|
|
|
pub fn init() Self {
|
|
|
|
return .{ .total = 0, .sample_count = 0 };
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add(self: *Self, sample: u64) void {
|
2022-10-21 08:12:27 +00:00
|
|
|
if (self.sample_count == 600) return self.reset(sample);
|
2022-10-21 08:12:27 +00:00
|
|
|
|
|
|
|
self.total += sample;
|
|
|
|
self.sample_count += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn calc(self: *const Self) u64 {
|
|
|
|
return self.total / self.sample_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn reset(self: *Self, sample: u64) void {
|
|
|
|
self.total = sample;
|
|
|
|
self.sample_count = 1;
|
|
|
|
}
|
|
|
|
};
|