Re: [PATCH] document optimizing macro for translating PROT_ to VM_ bits

From: Valdis . Kletnieks
Date: Mon Sep 29 2003 - 10:54:43 EST


On Mon, 29 Sep 2003 16:34:37 BST, Jamie Lokier said:
> Muli Ben-Yehuda wrote:
> > -/* Optimisation macro. */
> > +/* Optimisation macro, used to be defined as: */
> > +/* ((bit1 == bit2) ? (x & bit1) : (x & bit1) ? bit2 : 0) */
> > +/* but this version is faster */
> > +/* "check if bit1 is on in 'x'. If it is, return bit2" */
> > #define _calc_vm_trans(x,bit1,bit2) \
> > ((bit1) <= (bit2) ? ((x) & (bit1)) * ((bit2) / (bit1)) \
> > : ((x) & (bit1)) / ((bit1) / (bit2)))
>
> I agree with the intent of that comment, but the code in it is
> unnecessarily complex. See if you like this, and if you do feel free
> to submit it as a patch:
>
> /* Optimisation macro. It is equivalent to:
> (x & bit1) ? bit2 : 0
> but this version is faster. ("bit1" and "bit2" must be single bits). */

Is this supposed to return the bitmask bit2, or (x & bit2)? If the former,
then your code is right. If the latter, (x & bit1) ? (x & bit2) : 0

I'm totally failing to see why the original did the bit1 == bit2 compare,
so maybe mhyself and Jamie are both missing some subtlety?

Attachment: pgp00001.pgp
Description: PGP signature