I have macros:
#define MAKE_SIZE_CLASS (X, Y, CLS)
((CLS) = ((X) & 0xF << 2) | (Y) & 0x3)
#define BLOWUP_SIZE_CLASS (CLS, X, Y)
((X) = (CLS) >> 4 & 0xF, (Y) = (CLS) & 0x3)
Here you are constructing a size class object with well-defined
bitfields for the X and Y, which makes it possible to destructure
the object back to X and Y.
You really need some parentheses here. Below you're relying on |
having a lower precedence than &, and in several places on the
relative precedence of << and &.
with this:
#define CLASS_SIZE(X, Y) \
((0x4 | (Y) & 0x3) << (X))
How do i get an inverse of CLASS_SIZE.
This is not as nicely behaved because the position of datum Y is shifted by X
bits.
Fortunately, you put in that 0x4 bit which gives us a correct frame
of reference for the position shift.
This means that if you shift the value to the right until it is in the range
8-15, you can then extract the Y bit fields by AND-ing it with 3.
Counting the number of shifts recovers X.
You can do this in one big macro with an if/else ladder expressed using the
ternary operator, but it's probably better to write a function.
In the macro, we unroll the shifting.
If the class is of the form ...0000xxx, it means that we have
....00001yy. If it is of the form ...0000xxxx, it means that it is
....0001yy0 and so on. Each of these can be a separately tested and processed
case:
Here is what it might look like, assuming CLS is an unsigned long, and we just
go up to 32 bits:
#define DESTRUCTURE_CLASS_SIZE(CLS, X, Y) \
((CLS) <= 0x7 ? \
(((Y) = (CLS) & 3), (X) = 0) \
: ((CLS) <= 0xf) ? \
(((Y) = ((CLS)>>1) & 3), (X) = 1) \
: ((CLS) <= 0x1f) ? \
(((Y) = ((CLS)>>2) & 3), (X) = 2) \
\
[ .. snip ... ] \
\
: ((CLS) <= 0x7FFFFFFF) ? \
(((Y) = ((CLS)>>28) & 3), (X) = 28) \
: \
(((Y) = ((CLS)>>29) & 3), (X) = 29))
I *think* that's how to code an if/else ladder the ?: operator.