From b9c3084138b3e6b0c3d88d3cf5cf94f0c3ab5ff5 Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Wed, 17 Jul 2024 05:04:18 +0200 Subject: Bit order --- bxgen.c | 58 ++++++++++++++++++++++++++-------------------------------- 1 file changed, 26 insertions(+), 32 deletions(-) (limited to 'bxgen.c') diff --git a/bxgen.c b/bxgen.c index 2395a0f..428d82c 100755 --- a/bxgen.c +++ b/bxgen.c @@ -845,6 +845,16 @@ i32 host_byte_order() { return BE; } +i32 host_f64_dword_order() { + if ((*(u64 *) &(f64) { -1.4575323640233e-306 } & 0xffffffffull) == 0x40301fcbull) + return host_byte_order(); + if ((*(u64 *) &(f64) { -1.4575323640233e-306 } & 0xffffffff00000000ull) == 0x40301fcb00000000ull) + return host_byte_order() == LE ? BE : LE; + + BX_ASSERT(0 && "Unknown native floating-point number format"); + return 0; +} + u8 read_u8(i32 bit_order, u8 *v) { if (bit_order == host_bit_order()) return *v; @@ -997,26 +1007,14 @@ i64 read_i64(i32 bit_order, i32 byte_order, void *v, void *v_end) { } f32 read_f32(i32 bit_order, i32 byte_order, void *v, void *v_end) { - if ((*(u64 *) &(f64) { -1.4575323640233e-306 } & 0xffffffffull) != 0x40301fcbull && - (*(u64 *) &(f64) { -1.4575323640233e-306 } & 0xffffffff00000000ull) != 0x40301fcb00000000ull) - BX_ASSERT(0 && "Unknown native floating-point number format"); - + host_f64_dword_order(); // FIXME return *(f32 *) &(u32) { read_u32(bit_order, byte_order, v, v_end) }; } -f32 read_f64(i32 bit_order, i32 byte_order, i32 f64_word_order, void *v, void *v_end) { - b8 native_word_order_same; - - if ( (*(u64 *) &(f64) { -1.4575323640233e-306 } & 0xffffffffull) == 0x40301fcbull) - native_word_order_same = 1; - else if ((*(u64 *) &(f64) { -1.4575323640233e-306 } & 0xffffffff00000000ull) == 0x40301fcb00000000ull) - native_word_order_same = 0; - else - BX_ASSERT(0 && "Unknown native floating-point number format"); - +f32 read_f64(i32 bit_order, i32 byte_order, i32 f64_dword_order, void *v, void *v_end) { u64 x = read_u64(bit_order, byte_order, v, v_end); - if (native_word_order_same != (byte_order == f64_word_order)) + if (f64_dword_order != host_f64_dword_order()) x = ((x & 0xffffffffull) << 32) | ((x >> 32) & 0xffffffffull); return *(f64 *) &x; @@ -1035,24 +1033,12 @@ void write_i64(i32 bit_order, i32 byte_order, i64 x, void *v, void *v_end) { } void write_f32(i32 bit_order, i32 byte_order, f32 x, void *v, void *v_end) { - if ((*(u64 *) &(f64) { -1.4575323640233e-306 } & 0xffffffffull) != 0x40301fcbull && - (*(u64 *) &(f64) { -1.4575323640233e-306 } & 0xffffffff00000000ull) != 0x40301fcb00000000ull) - BX_ASSERT(0 && "Unknown native floating-point number format"); - + host_f64_dword_order(); // FIXME write_u32(bit_order, byte_order, *(u32 *) &x, v, v_end); } -void write_f64(i32 bit_order, i32 byte_order, i32 f64_word_order, f64 x, void *v, void *v_end) { - b8 native_word_order_same; - - if ( (*(u64 *) &(f64) { -1.4575323640233e-306 } & 0xffffffffull) == 0x40301fcbull) - native_word_order_same = 1; - else if ((*(u64 *) &(f64) { -1.4575323640233e-306 } & 0xffffffff00000000ull) == 0x40301fcb00000000ull) - native_word_order_same = 0; - else - BX_ASSERT(0 && "Unknown native floating-point number format"); - - if (native_word_order_same == (byte_order == f64_word_order)) +void write_f64(i32 bit_order, i32 byte_order, i32 f64_dword_order, f64 x, void *v, void *v_end) { + if (f64_dword_order == host_f64_dword_order()) write_u64(bit_order, byte_order, *(u64 *) &x, v, v_end); else { write_u32(bit_order, byte_order, *(((u32 *) &x) + 1), (u8 *) v, v_end); @@ -1063,6 +1049,13 @@ void write_f64(i32 bit_order, i32 byte_order, i32 f64_word_order, f64 x, void *v #define HBIO host_bit_order() #define HBYO host_byte_order() +#define HO_u8 host_bit_order() +#define HO_u16 host_bit_order(), host_byte_order() +#define HO_u32 host_bit_order(), host_byte_order() +#define HO_u64 host_bit_order(), host_byte_order() +#define HO_f32 host_bit_order(), host_byte_order() +#define HO_f64 host_bit_order(), host_byte_order(), host_f64_dword_order() + // ================================================================ // // * Code generation and linking @@ -2551,8 +2544,9 @@ int main(int argc, char **argv) { (void) argc; (void) argv; - printf("host bit order: %s\n", host_bit_order() == LE ? "LE" : "BE"); - printf("host byte order: %s\n\n", host_byte_order() == LE ? "LE" : "BE"); + printf("host bit order: %s\n", host_bit_order() == LE ? "LE" : "BE"); + printf("host byte order: %s\n", host_byte_order() == LE ? "LE" : "BE"); + printf("host f64 dword order: %s\n\n", host_f64_dword_order() == LE ? "LE" : "BE"); printf("entity - %d bytes\n", (i32) sizeof(Entity)); printf("binary output buffer - %d MB\n\n", (i32) sizeof(Binary_Output) / (1024 * 1024)); -- cgit v1.2.3