picasso_frontend: Get rid of undefined behavior
Type-punning via a union is well-defined in C (specifically C99 and onwards), but not C++
This commit is contained in:
parent
299c35d6fc
commit
d1a1c0a011
@ -1,19 +1,13 @@
|
|||||||
#include "picasso.h"
|
#include "picasso.h"
|
||||||
|
|
||||||
static inline uint32_t floatrawbits(float f)
|
|
||||||
{
|
|
||||||
union { float f; uint32_t i; } s;
|
|
||||||
s.f = f;
|
|
||||||
return s.i;
|
|
||||||
}
|
|
||||||
|
|
||||||
// f24 has:
|
// f24 has:
|
||||||
// - 1 sign bit
|
// - 1 sign bit
|
||||||
// - 7 exponent bits
|
// - 7 exponent bits
|
||||||
// - 16 mantissa bits
|
// - 16 mantissa bits
|
||||||
uint32_t f32tof24(float f)
|
uint32_t f32tof24(float f)
|
||||||
{
|
{
|
||||||
uint32_t i = floatrawbits(f);
|
uint32_t i;
|
||||||
|
std::memcpy(&i, &f, sizeof(f));
|
||||||
|
|
||||||
uint32_t mantissa = (i << 9) >> 9;
|
uint32_t mantissa = (i << 9) >> 9;
|
||||||
int32_t exponent = (i << 1) >> 24;
|
int32_t exponent = (i << 1) >> 24;
|
||||||
|
Loading…
Reference in New Issue
Block a user