int_conversions.h (2285B)
1 #pragma once 2 3 #include "intrinsics.h" 4 #include "platform_inline.h" 5 6 /* 7 * clamping casts 8 */ 9 10 template< typename T > 11 u64 clamp_u64( T x ) { 12 if( x < 0 ) return 0; 13 if( x > U64_MAX ) return U64_MAX; 14 return ( u64 ) x; 15 } 16 17 template< typename T > 18 u32 clamp_u32( T x ) { 19 if( x < 0 ) return 0; 20 if( x > U32_MAX ) return U32_MAX; 21 return ( u32 ) x; 22 } 23 24 template< typename T > 25 u16 clamp_u16( T x ) { 26 if( x < 0 ) return 0; 27 if( x > U16_MAX ) return U16_MAX; 28 return ( u16 ) x; 29 } 30 31 template< typename T > 32 u8 clamp_u8( T x ) { 33 if( x < 0 ) return 0; 34 if( x > U8_MAX ) return U8_MAX; 35 return ( u8 ) x; 36 } 37 38 /* 39 * size preserving signed/unsigned conversions 40 */ 41 42 forceinline s8 to_signed( u8 x ) { return ( s8 ) x; } 43 forceinline s16 to_signed( u16 x ) { return ( s16 ) x; } 44 forceinline s32 to_signed( u32 x ) { return ( s32 ) x; } 45 forceinline s64 to_signed( u64 x ) { return ( s64 ) x; } 46 47 forceinline u8 to_unsigned( s8 x ) { return ( u8 ) x; } 48 forceinline u16 to_unsigned( s16 x ) { return ( u16 ) x; } 49 forceinline u32 to_unsigned( s32 x ) { return ( u32 ) x; } 50 forceinline u64 to_unsigned( s64 x ) { return ( u64 ) x; } 51 52 /* 53 * quantization 54 */ 55 56 forceinline u32 quantize01( float x, u32 num_bits ) { 57 ASSERT( x >= 0.0f && x <= 1.0f ); 58 return u32( x * checked_cast< float >( ( 1 << num_bits ) - 1 ) + 0.5f ); 59 } 60 61 forceinline float dequantize01( u32 q, u32 num_bits ) { 62 float result = float( q ) / float( ( 1 << num_bits ) - 1 ); 63 ASSERT( result >= 0.0f && result <= 1.0f ); 64 return result; 65 } 66 67 forceinline s32 quantize11s( float x, u32 num_bits ) { 68 ASSERT( x >= -1.0f && x <= 1.0f ); 69 num_bits--; 70 if( x >= 0 ) { 71 return s32( x * checked_cast< float >( ( 1 << num_bits ) - 1 ) + 0.5f ); 72 } 73 74 return s32( x * checked_cast< float >( 1 << num_bits ) - 0.5f ); 75 } 76 77 forceinline float dequantize11s( s32 q, u32 num_bits ) { 78 num_bits--; 79 if( q >= 0 ) { 80 float result = dequantize01( checked_cast< u32 >( q ), num_bits ); 81 ASSERT( result >= 0.0f && result <= 1.0f ); 82 return result; 83 } 84 85 float result = checked_cast< float >( q ) / checked_cast< float >( 1 << num_bits ); 86 ASSERT( result < 0.0f && result >= -1.0f ); 87 return result; 88 }