Integer lerp and unlerp
This commit is contained in:
@@ -78,8 +78,34 @@ pub const cossin_x8 = trig.cossin_x8;
|
|||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
pub inline fn lerp(comptime T: type, a: T, b: T, t: T) T {
|
pub inline fn lerp(a: f32, b: f32, t: f32) f32 {
|
||||||
return @mulAdd(T, t, b, @mulAdd(T, -t, a, a));
|
return @mulAdd(f32, t, b, @mulAdd(f32, -t, a, a));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub inline fn lerpInt(a: i32, b: i32, t: f32) i32 {
|
||||||
|
const ab = b - a;
|
||||||
|
const ab_float: f32 = @floatFromInt(ab);
|
||||||
|
const d: i32 = @intFromFloat(@round(t * ab_float));
|
||||||
|
return a + d;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub inline fn lerpInt64(a: i64, b: i64, t: f32) i64 {
|
||||||
|
const ab = b - a;
|
||||||
|
const ab_float: f32 = @floatFromInt(ab);
|
||||||
|
const d: i64 = @intFromFloat(@round(t * ab_float));
|
||||||
|
return a + d;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub inline fn unlerp(a: f32, b: f32, x: f32) f32 {
|
||||||
|
return (x - a) / (b - a);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub inline fn unlerpInt(a: i32, b: i32, x: i32) f32 {
|
||||||
|
return @as(f32, @floatFromInt(x - a)) / @as(f32, @floatFromInt(b - a));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub inline fn unlerpInt64(a: i64, b: i64, x: i64) f32 {
|
||||||
|
return @as(f32, @floatFromInt(x - a)) / @as(f32, @floatFromInt(b - a));
|
||||||
}
|
}
|
||||||
|
|
||||||
test "refAllDecls" {
|
test "refAllDecls" {
|
||||||
|
|||||||
Reference in New Issue
Block a user