Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

size test

10 views
Skip to first unread message

Joe keane

unread,
Apr 25, 2012, 7:34:13 PM4/25/12
to
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)

with this:

#define CLASS_SIZE(X, Y) \
((0x4 | (Y) & 0x3) << (X))

How do i get an inverse of CLASS_SIZE.

That is. we do not necessraily get a posssible result of CLASS_SIZE, but
we always round *up*, not down.

Eric Sosman

unread,
Apr 25, 2012, 9:03:51 PM4/25/12
to
On 4/25/2012 7:34 PM, Joe keane wrote:
>[...]
> #define CLASS_SIZE(X, Y) \
> ((0x4 | (Y)& 0x3)<< (X))
>
> How do i get an inverse of CLASS_SIZE.

It's not invertible, because it's many-to-one. For example,
CLASS_SIZE(1,3) and CLASS_SIZE(1,127) both expand to expressions
that evaluate to 14. Given the 14, you can't tell whether it came
from 1,3 or 1,127 (or 1,63, or ...).

--
Eric Sosman
eso...@ieee-dot-org.invalid

Kaz Kylheku

unread,
Apr 25, 2012, 10:54:27 PM4/25/12
to
On 2012-04-25, Joe keane <j...@panix.com> wrote:
> 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.

--
If you ever need any coding done, I'm your goto man!
0 new messages