medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 98aa59a34632e101e55c0132ecd8611aaa1718e7
parent 430387c24815c59512043d0b8f8d852ca76f0b92
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Wed Oct 12 21:15:19 +0300

Add quantize/dequantize

Diffstat:
int_conversions.h | 13+++++++++++++
1 file changed, 13 insertions(+), 0 deletions(-)
diff --git a/int_conversions.h b/int_conversions.h @@ -140,4 +140,17 @@ INLINE u16 to_unsigned( s16 x ) { return ( u16 ) x; } INLINE u32 to_unsigned( s32 x ) { return ( u32 ) x; } INLINE u64 to_unsigned( s64 x ) { return ( u64 ) x; } +/* + * quantization + */ + +INLINE u32 quantize( float x, u32 num_bits ) { + assert( x >= 0 && x <= 1 ); + return u32( x * float( ( 1 << num_bits ) - 1 ) + 0.5f ); +} + +INLINE float dequantize( u32 q, u32 num_bits ) { + return float( q ) / float( 1 << num_bits ); +} + #endif // _INT_CONVERSIONS_H_