Assume I have a sample source file, test.c, which I am compiling like so:
$ gcc -03 -Wall
test.c looks something like this ..
/// CMP128(x, y)
//
// arguments
// x - any pointer to an 128-bit int
// y - any pointer to an 128-bit int
//
// returns -1, 0, or 1 if x is less than, equal to, or greater than y
//
#define CMP128(x, y) // magic goes here
// example usages
uint8_t A[16];
uint16_t B[8];
uint32_t C[4];
uint64_t D[2];
struct in6_addr E;
uint8_t* F;
// use CMP128 on any combination of pointers to 128-bit ints, i.e.
CMP128(A, B);
CMP128(&C[0], &D[0]);
CMP128(&E, F);
// and so on
let's also say I accept the restriction that if you pass in two overlapping pointers, you get undefined results.
I've tried something like this (imagine these macros are properly formatted with backslash-escaped newlines at the end of each line)
#define CMP128(x, y) ({
uint64_t* a = (void*)x;
uint64_t* b = (void*)y;
// compare a[0] with b[0], a[1] with b[1]
})
but when I dereference a in the macro (a[0] < b[0]) I get "dereferencing breaks strict-aliasing rules" errors from gcc
I had thought that you were supposed to use unions to properly refer to a single place in memory in two different ways, so next I tried something like
#define CMP128(x, y) ({
union {
typeof(x) a;
typeof(y) b;
uint64_t* c;
} d = { .a = (x) }
, e = { .b = (y) };
// compare d.c[0] with e.c[0], etc
})
Except that I get the exact same errors from the compiler about strict-aliasing rules.
So: is there some way to do this without breaking strict-aliasing, short of actually COPYING the memory?
(may_alias doesnt count, it just allows you to bypass the strict-aliasing rules)
EDIT: use memcmp to do this. I got caught up on the aliasing rules and didn't think of it.