static void test_bigend_f32(TestBatch *batch) { float source[] = { -1.3f, 0.0f, 100.2f }; size_t count = 3; size_t amount = (count + 1) * sizeof(float); uint8_t *allocated = (uint8_t*)CALLOCATE(amount, sizeof(uint8_t)); uint8_t *encoded = allocated + 1; // Intentionally misaligned. uint8_t *target = encoded; for (size_t i = 0; i < count; i++) { NumUtil_encode_bigend_f32(source[i], &target); target += sizeof(float); } target = encoded; for (size_t i = 0; i < count; i++) { float got = NumUtil_decode_bigend_f32(target); TEST_TRUE(batch, got == source[i], "bigend f32"); target += sizeof(float); } target = encoded; NumUtil_encode_bigend_f32(-2.0f, &target); TEST_INT_EQ(batch, (encoded[0] & 0x80), 0x80, "Truly big-endian (IEEE 754 sign bit set for negative number)"); TEST_INT_EQ(batch, encoded[0], 0xC0, "IEEE 754 representation of -2.0f, byte 0"); for (size_t i = 1; i < sizeof(float); i++) { TEST_INT_EQ(batch, encoded[i], 0, "IEEE 754 representation of -2.0f, byte %d", (int)i); } FREEMEM(allocated); }
void OutStream_Write_F32_IMP(OutStream *self, float value) { OutStreamIVARS *const ivars = OutStream_IVARS(self); char buf[sizeof(float)]; char *buf_copy = buf; NumUtil_encode_bigend_f32(value, &buf_copy); SI_write_bytes(self, ivars, buf_copy, sizeof(float)); }