From e7c95b8ce9f9ec695383b091463d194f698d38c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joel=20D=2E=20Sch=C3=BCller?= Date: Sat, 7 Oct 2023 16:21:39 +0200 Subject: [PATCH] math: Replace floatFallbackPrecision function with switch expression --- src/math/ray.zig | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/src/math/ray.zig b/src/math/ray.zig index 3812ea9a..a724c199 100644 --- a/src/math/ray.zig +++ b/src/math/ray.zig @@ -5,18 +5,6 @@ const testing = mach.testing; const math = mach.math; const vec = @import("vec.zig"); -// Determine the fallback precision for valid floating point precisions -// our Ray computations can have. Will return twice the input precision -fn floatFallbackPrecision(comptime float_precision: type) type { - switch (float_precision) { - f16 => return f32, - f32 => return f64, - f64 => return f128, - else => @compileError("Expected f16, f32, f64, found '" ++ - @typeName(float_precision) ++ "'"), - } -} - // Determine the 3D vector dimension with the largest scalar value fn maxDim(v: math.Vec3) u8 { if (v.v[0] > v.v[1]) { @@ -34,12 +22,18 @@ fn maxDim(v: math.Vec3) u8 { // A Ray in three-dimensional space pub fn Ray(comptime Vec3P: type) type { - // Floating precision, will be either f16, f32, or f64 + // Floating point precision, will be either f16, f32, or f64 const P: type = Vec3P.T; - // Fallback floating point precision to scale fallback according to - // input precision - const PP: type = floatFallbackPrecision(P); + // Adaptive scaling of the fallback precision for the ray-triangle + // intersection implementation. + const PP: type = switch (P) { + f16 => f32, + f32 => f64, + f64 => f128, + else => @compileError("Expected f16, f32, f64, found '" ++ + @typeName(P) ++ "'"), + }; return extern struct { origin: Vec3P,