Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.arch > #6659 > unrolled thread

size test

Started byjgk@panix.com (Joe keane)
First post2012-04-25 23:34 +0000
Last post2012-04-26 02:54 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.arch


Contents

  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

#6659 — size test

Fromjgk@panix.com (Joe keane)
Date2012-04-25 23:34 +0000
Subjectsize test
Message-ID<jna1ll$f2a$1@reader1.panix.com>
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.

[toc] | [next] | [standalone]


#6661

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2012-04-25 21:03 -0400
Message-ID<jna6to$r4p$1@dont-email.me>
In reply to#6659
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
esosman@ieee-dot-org.invalid

[toc] | [prev] | [next] | [standalone]


#6662

FromKaz Kylheku <kaz@kylheku.com>
Date2012-04-26 02:54 +0000
Message-ID<20120425193419.310@kylheku.com>
In reply to#6659
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!

[toc] | [prev] | [standalone]


Back to top | Article view | comp.arch


csiph-web