moved the Float32toFloat16 codes to .cpp
This commit is contained in:
@@ -52,56 +52,7 @@ namespace hgl
|
||||
/**
|
||||
* 标准版float32转float16处理
|
||||
*/
|
||||
void Float32toFloat16(half_float *output,const float *input,const uint count)
|
||||
{
|
||||
const uint32 *src=(const uint32 *)input;
|
||||
uint16 *dst=output;
|
||||
|
||||
for(uint i=0;i<count;i++)
|
||||
{
|
||||
uint32 value=src[i];
|
||||
|
||||
uint32 sign_bit=value&0x80000000;
|
||||
uint32 exponent=(value&0x7F800000)>>23;
|
||||
uint32 mantissa=value&0x007FFFFF;
|
||||
|
||||
if(exponent==0xFF)
|
||||
{
|
||||
if(mantissa==0)
|
||||
dst[i]=sign_bit>>16;
|
||||
else
|
||||
dst[i]=0x7FFF;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(exponent==0)
|
||||
{
|
||||
if(mantissa==0)
|
||||
dst[i]=sign_bit>>16;
|
||||
else
|
||||
{
|
||||
uint32 mantissa_bit=0x00800000;
|
||||
|
||||
while((mantissa&mantissa_bit)==0)
|
||||
{
|
||||
mantissa_bit>>=1;
|
||||
--exponent;
|
||||
}
|
||||
|
||||
exponent+=1;
|
||||
mantissa&=~mantissa_bit;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
exponent+=15-127;
|
||||
mantissa>>=13;
|
||||
}
|
||||
|
||||
dst[i]=sign_bit>>16|exponent<<10|mantissa;
|
||||
}
|
||||
}
|
||||
}
|
||||
void Float32toFloat16(half_float *output,const float *input,const uint count);
|
||||
|
||||
/**
|
||||
* 快速版float32转float16处理
|
||||
|
@@ -35,11 +35,13 @@ SET(MATH_HEADER_FILES ${MATH_INCLUDE_PATH}/bvec.h
|
||||
${MATH_INCLUDE_PATH}/Vector.h
|
||||
${MATH_INCLUDE_PATH}/PrimaryMathematics.h
|
||||
${MATH_INCLUDE_PATH}/PhysicsConst.h
|
||||
${MATH_INCLUDE_PATH}/HalfFloat.h
|
||||
)
|
||||
|
||||
SET(MATH_SOURCE_FILES Math/LAtan.cpp
|
||||
Math/LSinCos.cpp
|
||||
Math/Matrix4f.cpp)
|
||||
Math/Matrix4f.cpp
|
||||
Math/HalfFloat.cpp)
|
||||
|
||||
SOURCE_GROUP("Math\\Header Files" FILES ${MATH_HEADER_FILES})
|
||||
SOURCE_GROUP("Math\\Source Files" FILES ${MATH_SOURCE_FILES})
|
||||
|
55
src/Math/HalfFloat.cpp
Normal file
55
src/Math/HalfFloat.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include<hgl/math/HalfFloat.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
void Float32toFloat16(half_float *output,const float *input,const uint count)
|
||||
{
|
||||
const uint32 *src=(const uint32 *)input;
|
||||
uint16 *dst=output;
|
||||
|
||||
for(uint i=0;i<count;i++)
|
||||
{
|
||||
uint32 value=src[i];
|
||||
|
||||
uint32 sign_bit=value&0x80000000;
|
||||
uint32 exponent=(value&0x7F800000)>>23;
|
||||
uint32 mantissa=value&0x007FFFFF;
|
||||
|
||||
if(exponent==0xFF)
|
||||
{
|
||||
if(mantissa==0)
|
||||
dst[i]=sign_bit>>16;
|
||||
else
|
||||
dst[i]=0x7FFF;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(exponent==0)
|
||||
{
|
||||
if(mantissa==0)
|
||||
dst[i]=sign_bit>>16;
|
||||
else
|
||||
{
|
||||
uint32 mantissa_bit=0x00800000;
|
||||
|
||||
while((mantissa&mantissa_bit)==0)
|
||||
{
|
||||
mantissa_bit>>=1;
|
||||
--exponent;
|
||||
}
|
||||
|
||||
exponent+=1;
|
||||
mantissa&=~mantissa_bit;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
exponent+=15-127;
|
||||
mantissa>>=13;
|
||||
}
|
||||
|
||||
dst[i]=sign_bit>>16|exponent<<10|mantissa;
|
||||
}
|
||||
}
|
||||
}
|
||||
}//namespace hgl
|
Reference in New Issue
Block a user