static void test_bigend_f64(TestBatch *batch) { double source[] = { -1.3, 0.0, 100.2 }; size_t count = 3; size_t amount = (count + 1) * sizeof(double); 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_f64(source[i], &target); target += sizeof(double); } target = encoded; for (size_t i = 0; i < count; i++) { double got = NumUtil_decode_bigend_f64(target); TEST_TRUE(batch, got == source[i], "bigend f64"); target += sizeof(double); } target = encoded; NumUtil_encode_bigend_f64(-2.0, &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.0, byte 0"); for (size_t i = 1; i < sizeof(double); i++) { TEST_INT_EQ(batch, encoded[i], 0, "IEEE 754 representation of -2.0, byte %d", (int)i); } FREEMEM(allocated); }
void OutStream_Write_F64_IMP(OutStream *self, double value) { OutStreamIVARS *const ivars = OutStream_IVARS(self); char buf[sizeof(double)]; char *buf_copy = buf; NumUtil_encode_bigend_f64(value, &buf_copy); SI_write_bytes(self, ivars, buf_copy, sizeof(double)); }