From 87e102ded928dcc4d9c4c79e22c68b709ea91c40 Mon Sep 17 00:00:00 2001 From: Avril Date: Mon, 19 Jul 2021 22:20:01 +0100 Subject: [PATCH] Added `TRANSMUTE(value, to_type)` macro, which performs a bit-cast on `value` to `to_type`. The sizes of the types must be the same or a compilation error is raised. There is currently no alignment restriction on the types (maybe there should be?). MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added test `transmute_copy`: This works on LittleEndian machines. (TODO: Re-work the test to use `memcmp()` on `output` + `expected` instead of testing for equality? Or perform `BSWAP()` on `output` on BigEndian machines.) Fortune for naka's current commit: Half curse − 半凶 --- include/macros.h | 8 ++++++++ src/main.c | 2 +- src/tests/transmute.c | 23 +++++++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/tests/transmute.c diff --git a/include/macros.h b/include/macros.h index 77a0b72..ead8d6b 100644 --- a/include/macros.h +++ b/include/macros.h @@ -206,6 +206,14 @@ static_assert_eq(bswap(128lu), 9223372036854775808lu, "bswap128 (lu) failed (1)" static_assert_eq(128lu, bswap(9223372036854775808lu), "bswap128 (lu) failed (2)"); static_assert_eq(bswap(bswap(128lu)), 128, "bswap128 (lu) failed (3)"); +// Transmute + +#define TRANSMUTE(val, type) ({ union _trans { var(val) input; type output; }; \ + _Static_assert(sizeof(union _trans) == sizeof(type), "Cannot transmute values of different sizes"); \ + /* XXX: Do we need do check this for equality? Can we re-word it or do we even need it at all? _Static_assert(_Alignof(union _trans) == _Alignof(type), "Cannot transmute values of different alignments");*/ \ + union _trans _trans__value = { .input = (val), }; \ + _trans__value.output; }) + // Trace message output #include "trace.h" diff --git a/src/main.c b/src/main.c index 4018324..4bf7778 100644 --- a/src/main.c +++ b/src/main.c @@ -125,7 +125,7 @@ static int map_haystacks(const char* const * h, map_t maps[pOUT]) } int main(int argc, char** argv) -{ +{ if(!trace_init()) { ERROR("Unknown trace value for %s: `%s'", _TRACE_CONTROL_ENV_NAME, getenv(_TRACE_CONTROL_ENV_NAME) ?: ""); } diff --git a/src/tests/transmute.c b/src/tests/transmute.c new file mode 100644 index 0000000..ffce727 --- /dev/null +++ b/src/tests/transmute.c @@ -0,0 +1,23 @@ + +#include + +#include +#include +#include + +DEFTEST(transmute_copy) +{ + static const u64 EXPECTED = 18446743474098365025lu; + struct { char i[4]; i32 u; } input = { .i = {'a','b','c','d'}, .u = -140 }; + let output = TRANSMUTE(input, u64); + TRACE("Transmute test, expected 0x%lx, got 0x%lx", EXPECTED, output); + //XXX: TODO: This test will fail on Big Endian machines. + TEST_ASSERT(output == EXPECTED); + TRACE("Alignment of output: %lu, alignment of input: %lu", _Alignof(output), _Alignof(input)); + let input2 = TRANSMUTE(output, var(input)); + TRACE("Transmute output passed, trying to transmute back"); + TEST_ASSERT( memcmp(&input2, &input, sizeof(input)) == 0 ); + + return TEST_OK; +} +RUNTEST_DEBUG(transmute_copy);