From 275fb30063488eda319b3ee94d19784fb242ddd7 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Thu, 24 Aug 2023 12:00:29 -0700 Subject: [PATCH] quantization: Add assertions for quantizeFloat N argument Also convert C comments to C++ - this is no longer part of meshoptimizer.h so it doesn't need to be C89 compatible. --- src/quantization.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/quantization.cpp b/src/quantization.cpp index 59592dc80..7399781ed 100644 --- a/src/quantization.cpp +++ b/src/quantization.cpp @@ -1,6 +1,8 @@ // This file is part of meshoptimizer library; see meshoptimizer.h for version/license details #include "meshoptimizer.h" +#include + unsigned short meshopt_quantizeHalf(float v) { union { float f; unsigned int ui; } u = {v}; @@ -9,16 +11,16 @@ unsigned short meshopt_quantizeHalf(float v) int s = (ui >> 16) & 0x8000; int em = ui & 0x7fffffff; - /* bias exponent and round to nearest; 112 is relative exponent bias (127-15) */ + // bias exponent and round to nearest; 112 is relative exponent bias (127-15) int h = (em - (112 << 23) + (1 << 12)) >> 13; - /* underflow: flush to zero; 113 encodes exponent -14 */ + // underflow: flush to zero; 113 encodes exponent -14 h = (em < (113 << 23)) ? 0 : h; - /* overflow: infinity; 143 encodes exponent 16 */ + // overflow: infinity; 143 encodes exponent 16 h = (em >= (143 << 23)) ? 0x7c00 : h; - /* NaN; note that we convert all types of NaN to qNaN */ + // NaN; note that we convert all types of NaN to qNaN h = (em > (255 << 23)) ? 0x7e00 : h; return (unsigned short)(s | h); @@ -26,6 +28,8 @@ unsigned short meshopt_quantizeHalf(float v) float meshopt_quantizeFloat(float v, int N) { + assert(N >= 0 && N <= 23); + union { float f; unsigned int ui; } u = {v}; unsigned int ui = u.ui; @@ -35,10 +39,10 @@ float meshopt_quantizeFloat(float v, int N) int e = ui & 0x7f800000; unsigned int rui = (ui + round) & ~mask; - /* round all numbers except inf/nan; this is important to make sure nan doesn't overflow into -0 */ + // round all numbers except inf/nan; this is important to make sure nan doesn't overflow into -0 ui = e == 0x7f800000 ? ui : rui; - /* flush denormals to zero */ + // flush denormals to zero ui = e == 0 ? 0 : ui; u.ui = ui;