Skip to content

Commit

Permalink
quantization: Add assertions for quantizeFloat N argument
Browse files Browse the repository at this point in the history
Also convert C comments to C++ - this is no longer part of
meshoptimizer.h so it doesn't need to be C89 compatible.
  • Loading branch information
zeux committed Aug 24, 2023
1 parent 3d1a098 commit 275fb30
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/quantization.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// This file is part of meshoptimizer library; see meshoptimizer.h for version/license details
#include "meshoptimizer.h"

#include <assert.h>

unsigned short meshopt_quantizeHalf(float v)
{
union { float f; unsigned int ui; } u = {v};
Expand All @@ -9,23 +11,25 @@ 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);
}

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;

Expand All @@ -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;
Expand Down

0 comments on commit 275fb30

Please sign in to comment.