From 3f321740e1ea4e0a11e720522b4209be81c10a69 Mon Sep 17 00:00:00 2001 From: hgn Date: Tue, 3 Oct 2023 19:17:28 +0100 Subject: [PATCH] quantization functions --- vg_m.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/vg_m.h b/vg_m.h index c753be7..4fe4590 100644 --- a/vg_m.h +++ b/vg_m.h @@ -32,6 +32,7 @@ #define VG_PIf 3.14159265358979323846264338327950288f #define VG_TAUf 6.28318530717958647692528676655900576f + /* * ----------------------------------------------------------------------------- * Section 0. Misc Operations @@ -106,6 +107,22 @@ static inline f32 vg_rad( f32 deg ) return deg * VG_PIf / 180.0f; } +/* + * quantize float to bit count + */ +static u32 vg_quantf( f32 a, u32 bits, f32 min, f32 max ){ + u32 mask = (0x1 << bits) - 1; + return vg_clampf((a - min) * ((f32)mask/(max-min)), 0.0f, mask ); +} + +/* + * un-quantize discreet to float + */ +static f32 vg_dequantf( u32 q, u32 bits, f32 min, f32 max ){ + u32 mask = (0x1 << bits) - 1; + return min + (f32)q * ((max-min) / (f32)mask); +} + /* * ----------------------------------------------------------------------------- * Section 2.a 2D Vectors -- 2.25.1