Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Insano 24-bit integer to ieee float conversion by JoaCHIP. Winners do drugs. Here be dragons.
- Assumptions: Your FPU uses ieee float and your CPU uses reverse "intel" byte order.
- Expected output:
- -8388605 ---> -1.000000
- -6710884 ---> -0.800000
- -5033163 ---> -0.600000
- -3355442 ---> -0.400000
- -1677721 ---> -0.200000
- +0000000 ---> +0.000000
- +1677721 ---> +0.200000
- +3355442 ---> +0.400000
- +5033163 ---> +0.600000
- +6710884 ---> +0.800000
- +8388605 ---> +1.000000
- */
- #include <stdio.h>
- int main()
- {
- unsigned char input[4];
- for (int a=-5; a<=5; a++)
- {
- // just some weird example value that goes from appx. the lowest 24-bit integer value -8388608 to the highest +8388607
- int audiosample = a * (8388607 / 5);
- // and here comes the insane int to ieee float conversion (it's okay to be scared)
- unsigned char input[4];
- input[3] = ((audiosample>>23) & 1) << 7 | 64;
- input[2] = (audiosample>>17) & 0x7F;
- input[1] = (audiosample>>9) & 0xFF;
- input[0] = (audiosample>>1) & 0xFF;
- float result = audiosample>=0 ? (*((float*)input))-2.0f : -4.0f-(*((float*)input));
- // print out result
- printf("%+08d ---> %f\n", audiosample, result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement