Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Kaz Kylheku <kaz@kylheku.com> |
|---|---|
| Newsgroups | comp.lang.c, comp.arch |
| Subject | Re: size test |
| Followup-To | comp.lang.c |
| Date | 2012-04-26 02:54 +0000 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <20120425193419.310@kylheku.com> (permalink) |
| References | <jna1ll$f2a$1@reader1.panix.com> |
Cross-posted to 2 groups.
Followups directed to: comp.lang.c
On 2012-04-25, Joe keane <jgk@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!
Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread
size test jgk@panix.com (Joe keane) - 2012-04-25 23:34 +0000 Re: size test Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-04-25 21:03 -0400 Re: size test Kaz Kylheku <kaz@kylheku.com> - 2012-04-26 02:54 +0000
csiph-web