📄 quantize.c
字号:
#include "quantize.h"
#include "mathutil.h"
/****
the intent here is just to stuff the floats to ints, and
get as much extra stuff out of it as possible
we also get rid of signs here : the ints output by quantize
are all positive, with the sign bit in the last bit of the int
BTW one of the things a quantizer could do to perform better would be:
instead of restoring to the middle of the bucket, restore to the
average of the values that got put in that bucket.
*****/
void doquantize(void *plane,int width,int height,int stride,float Q)
{
int x,y;
float Qinv;
float *bptr;
assert(float_DoesRoundToZero());
Qinv = 1.0f/Q;
bptr = plane;
for(y=0;y<height;y++)
{
uint sign,qval;
float val;
for(x=0;x<width;x++)
{
val = *bptr;
if ( val < 0 )
{
sign = 1;
val = - val;
}
else
{
sign = 0;
}
if ( val < Q )
{
*((uint *)bptr) = 0;
}
else
{
val = (val - Q)*Qinv;
qval = ftoi(val) + 1;
*((uint *)bptr) = (qval<<1) + sign;
}
bptr++;
}
bptr += stride - width;
}
}
void dequantizerow(uint *fm,float *to,int width,float Q)
{
float val;
uint sign,qval;
assert(float_DoesRoundToZero());
// @@ should write 'dequantizerow' in ASM
while(width--)
{
qval = *fm++;
if ( qval == 0 )
{
*to++ = 0.0f;
}
else
{
sign = qval&1;
qval >>= 1;
assert( qval );
// <> goddamned compiler creates an FSTP FLD redundant pair here cuz of the itof inline
val = (itof(qval) + 0.5f)*Q;
if ( sign ) val = -val;
*to++ = val;
}
}
}
void dequantize(void * plane,int width,int height,int stride,float Q)
{
float * bptr;
int x,y;
assert(float_DoesRoundToZero());
bptr = plane;
for(y=0;y<height;y++)
{
uint sign,qval;
float val;
for(x=0;x<width;x++)
{
qval = *((uint *)bptr);
if ( qval == 0 )
{
*bptr = 0.0f;
}
else
{
sign = qval&1;
qval >>= 1;
assert( qval );
val = (itof(qval) + 0.5f)*Q;
if ( sign ) val = -val;
*bptr = val;
}
bptr++;
}
bptr += stride - width;
}
}
void doquantizenopack(void *plane,int width,int height,int stride,float Q)
{
int x,y;
float Qinv;
float *bptr;
assert(float_DoesRoundToZero());
Qinv = 1.0f/Q;
bptr = plane;
for(y=0;y<height;y++)
{
int sign,qval;
float val;
for(x=0;x<width;x++)
{
val = *bptr;
if ( val < 0 )
{
sign = -1;
val = - val;
}
else
{
sign = 1;
}
if ( val < Q )
{
*((int *)bptr) = 0;
}
else
{
val = (val - Q)*Qinv;
qval = ftoi(val) + 1;
*((int *)bptr) = qval * sign;
}
bptr++;
}
bptr += stride - width;
}
}
void dequantizenopack(void * plane,int width,int height,int stride,float Q)
{
float * bptr;
int x,y;
assert(float_DoesRoundToZero());
bptr = plane;
for(y=0;y<height;y++)
{
int qval;
for(x=0;x<width;x++)
{
qval = *((int *)bptr);
if ( qval == 0 )
{
*bptr = 0.0f;
}
else
{
if ( qval > 0 )
*bptr = (itof(qval) + 0.5f)*Q;
else
*bptr = - (itof(-qval) + 0.5f)*Q;
}
bptr++;
}
bptr += stride - width;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -