Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- //
- #define uint32 unsigned int
- #define sint32 signed int
- #define uint16 unsigned short
- #define sint16 signed short
- #define uint8 unsigned char
- // 16 bit sin tables
- extern uint16 sin_16_512[];
- extern uint16 sin_16_256[];
- extern uint16 sin_16_128[];
- // 8 bit sin tables
- extern uint8 sin_8_512 [];
- extern uint8 sin_8_256 [];
- extern uint8 sin_8_128 [];
- //
- #define fpsin( X ) sin_aprox_16bit( X )
- #define fpcos( X ) cos_aprox_16bit( X )
- // integer sin aproximation
- // input range [-0x80, +0x80] wrapped if exeeds
- // output tange [-0x80, +0x80]
- inline sint32 sin_aprox_8bit( sint32 x )
- {
- // wrap into range
- x = ((x+0x80) & 0xFF) - 0x80;
- // ax = absolute value
- sint32 ax = (x > 0) ? x : -x;
- return ((x << 2) - (((x << 2) * ax ) >> 7));
- }
- // integer sin aproximation
- // input range [-0x80, +0x80] wrapped if exeeds
- // output tange [-0x80, +0x80]
- inline sint32 cos_aprox_8bit( sint32 x )
- {
- // wrap into range
- x += 0x40;
- x = ((x+0x80) & 0xFF) - 0x80;
- // ax = absolute value
- sint32 ax = (x > 0) ? x : -x;
- return ((x << 2) - (((x << 2) * ax ) >> 7));
- }
- // integer sin aproximation
- // input range [-0x8000, +0x8000] wrapped if exeeds
- // output tange [-0x8000, +0x8000]
- inline sint32 sin_aprox_16bit( sint32 x )
- {
- // wrap into range
- x = ((x+0x8000) & 0xFFFF) - 0x8000;
- // ax = absolute value
- sint32 ax = (x > 0) ? x : -x;
- sint32 y = (x - ((x * (ax >> 2) ) >> 13));
- return y << 2;
- }
- // integer cos aproximation
- // input range [-0x8000, +0x8000] wrapped if exeeds
- // output tange [-0x8000, +0x8000]
- inline sint32 cos_aprox_16bit( sint32 x )
- {
- // wrap into range
- x += 0x4000;
- x = ((x+0x8000) & 0xFFFF) - 0x8000;
- // ax = absolute value
- sint32 ax = (x > 0) ? x : -x;
- sint32 y = (x - ((x * (ax >> 2) ) >> 13));
- return y << 2;
- }
- // fixed point, unsigned, quantised, 16 bit 512 sample sin table lookup
- // input is a 16 bit value unsigned
- // output is a 16 bit value unsigned
- inline uint16 qi_sin_u_16_512( uint32 index )
- {
- // bound and shift index
- index = (index & 0xFFFF) >> 7;
- // index the table
- return sin_16_512[ index ];
- }
- // fixed point, signed, quantised, 16 bit, 512 sample sin table lookup
- inline sint16 qi_sin_s_16_512( uint32 index )
- {
- // bound and shift index
- index = (index & 0xFFFF) >> 7;
- // index the table
- return (sint32)(sin_16_512[ index ]) - 0x8000;
- }
- // fixed point, unsigned quantised, 8 bit 128 sample sin table lookup
- // input is a 16 bit value unsigned
- // output is a 16 bit value unsigned
- inline uint16 qi_sin_u_08_128( uint32 index )
- {
- // bound and shift index
- index = (index & 0xFFFF) >> 9;
- // index table and shift
- return sin_8_128[ index ] << 8;
- }
- // fixed point, unsigned, linear interpolation, 8 bit 128 sample sin table lookup
- // input is a 16 bit value unsigned
- // output is a 16 bit value unsigned
- inline uint16 li_sin_u_08_128( uint32 index )
- {
- //
- uint8 i1 = (index>>9) & 0x7F;
- uint8 i2 = ((index>>9)+1) & 0x7F;
- uint8 f = (index>>1) & 0xFF;
- //
- i1 = sin_8_128[ i1 ];
- i2 = sin_8_128[ i2 ];
- //
- return ( ((uint32)i1) * (0x100-f) + ((uint32)i2 * f) );
- }
- // fixed point, unsigned, linear interpolation, 8 bit 128 sample cos table lookup
- // input is a 16 bit value unsigned
- // output is a 16 bit value unsigned
- inline uint16 li_cos_u_08_128( uint32 index )
- {
- // adjust into next quadrant
- index += 0x4000;
- //
- uint8 i1 = (index>>9) & 0x7F;
- uint8 i2 = ((index>>9)+1) & 0x7F;
- uint8 f = (index>>1) & 0xFF;
- //
- i1 = sin_8_128[ i1 ];
- i2 = sin_8_128[ i2 ];
- //
- return ( ((uint32)i1) * (0x100-f) + ((uint32)i2 * f) );
- }
- // fixed point, unsigned, linear interpolation, 8 bit 128 sample sin table lookup
- // input is a 16 bit value unsigned
- // output is a 16 bit value unsigned
- inline sint16 li_sin_s_08_128( uint32 index )
- {
- // find indices and fraction
- uint8 i1 = (index>>9) & 0x7F;
- uint8 i2 = ((index>>9)+1) & 0x7F;
- uint8 f = (index>>1) & 0xFF;
- // index the array
- i1 = sin_8_128[ i1 ];
- i2 = sin_8_128[ i2 ];
- // linear interpolate value
- sint32 v = ( ((uint32)i1) * (0x100-f) + ((uint32)i2 * f) );
- // adjust into signed region
- return v - 0x8000;
- }
- // fixed point, unsigned, linear interpolation, 8 bit 128 sample cos table lookup
- // input is a 16 bit value unsigned
- // output is a 16 bit value unsigned
- inline sint16 li_cos_s_08_128( uint32 index )
- {
- // adjust into next quadrant
- index += 0x4000;
- // find indices and fraction
- uint8 i1 = (index>>9) & 0x7F;
- uint8 i2 = ((index>>9)+1) & 0x7F;
- uint8 f = (index>>1) & 0xFF;
- // index the array
- i1 = sin_8_128[ i1 ];
- i2 = sin_8_128[ i2 ];
- // linear interpolate value
- sint32 v = ( ((uint32)i1) * (0x100-f) + ((uint32)i2 * f) );
- // adjust into signed region
- return v - 0x8000;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement