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?).
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 − 半凶strings
parent
a826612de5
commit
87e102ded9
@ -0,0 +1,23 @@
|
|||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <ints.h>
|
||||||
|
#include <macros.h>
|
||||||
|
#include <tests.h>
|
||||||
|
|
||||||
|
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);
|
Loading…
Reference in new issue