Adding a 1 bit flag in a 64 bit pointer

If you define a union to traverse a data structure by an addend value ( start

and end

offset) or through a pointer to a tree structure depending on the number of data items in a 64-bit system where these unions are cache-aligned, is it possible to add one bit flag to one of the of those 64 bits in order to know which traversal should be used and still allow the correct pointer to be restored?

union {
    uint32_t offsets[2];
    Tree<NodeData> * tree;
};

      

+3


source to share


1 answer


It depends on the system, but I don't think any 64 bit system actually uses its entire pointer length.
Also, if you know your data is 2 n maybe those n bits are just sitting around (on some older systems they just didn't exist. But I don't think any of these were 64 bit systems, and in any they are no longer of interest).

As an example, x86_64 uses 48 bits, the top 16 should be the same as bit 47. (By sign)
Another example: ARM64 uses 49 bits (2 collations 48 bits at a time), so only 15 bits are left.



Just remember to cut fluffy bits. (You might want to use uintptr_t

instead of a pointer and convert after fixing.)

Using a misaligned or impossible pointer raises behaviors ranging from silent automatic correction, ranging from silent misbehavior to loud crashes.

+5


source







All Articles