Convert float32 to float24 (PXR24 format)
| 4027 | |
| 4028 | // Convert float32 to float24 (PXR24 format) |
| 4029 | static inline unsigned int float_to_float24(float f) { |
| 4030 | union { float f; unsigned int i; } u; |
| 4031 | u.f = f; |
| 4032 | |
| 4033 | unsigned int s = u.i & 0x80000000; |
| 4034 | unsigned int e = u.i & 0x7f800000; |
| 4035 | unsigned int m = u.i & 0x007fffff; |
| 4036 | |
| 4037 | if (e == 0x7f800000) { |
| 4038 | if (m) { |
| 4039 | // NaN - preserve sign and 15 leftmost mantissa bits |
| 4040 | m >>= 8; |
| 4041 | return (s >> 8) | (e >> 8) | m | (m == 0 ? 1 : 0); |
| 4042 | } else { |
| 4043 | // Infinity |
| 4044 | return (s >> 8) | (e >> 8); |
| 4045 | } |
| 4046 | } |
| 4047 | |
| 4048 | // Finite - round mantissa to 15 bits |
| 4049 | unsigned int i = ((e | m) + (m & 0x00000080)) >> 8; |
| 4050 | |
| 4051 | if (i >= 0x7f8000) { |
| 4052 | // Overflow - truncate instead of round |
| 4053 | i = (e | m) >> 8; |
| 4054 | } |
| 4055 | |
| 4056 | return (s >> 8) | i; |
| 4057 | } |
| 4058 | |
| 4059 | static bool CompressPxr24(std::vector<unsigned char>& outBuf, |
| 4060 | const unsigned char *inPtr, size_t inLen, |