public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Bill Pringlemeir <bpringle@sympatico.ca>
To: David Woodhouse <dwmw2@infradead.org>
Cc: Keith Owens <kaos@ocs.com.au>, linux-kernel@vger.kernel.org
Subject: Re: macro conflict
Date: 24 Aug 2001 10:20:00 -0400	[thread overview]
Message-ID: <m2k7ztwne7.fsf@sympatico.ca> (raw)
In-Reply-To: <6208.998658929@ocs3.ocs-net> <16800.998659058@redhat.com>
In-Reply-To: David Woodhouse's message of "Fri, 24 Aug 2001 14:17:38 +0100"


>>>> kaos@ocs.com.au said:

 >> Did you try that?  Firstly typeof() is only defined in declaration
 >> context, it gets an error when used in an expression.  Secondly
 >> typeof() is not expanded by cpp so the stringify tricks do not
 >> work.  typeof(x) is handled by cc, not cpp.

>>>>> "David" == David Woodhouse <dwmw2@infradead.org> writes:

 David> No. It's far too silly for me to have actually tried it :)

David, you wild and crazy guy!  How crazy is this,

#define real_min(x,y) ({ typeof((x)) _x = (x); typeof((y)) _y = (y); (_x>_y)?_y:_x; })
#define min(x,y) ({extern void BUG(void); typeof(x) _x = 0; typeof(y) _y = 0;  \
        if (sizeof(typeof(x)) != sizeof(typeof(y)) ||   \
            (_x-1 > 0 && _y-1 < 0) || (_x-1<0 && _y-1>0)) \
                            BUG();                      \
        real_min(x,y); }) 

int main(int argc, char *argv[])
{
    int ix,iy;
    unsigned int ux,uy;
    long lx,ly;
    unsigned long Ux,Uy;

    ux = min(ix,uy);
    return 0;
}

sh> gcc -Wall test.c -O2 test

This is a little slimmy as the sizeof will evaluate the to same thing
on some architectures for long/int or short/int.  Even worse, it won't
handle pointers.  The macro is `physically correct' if a short/int are
the same.  Actually, you would probably want to cast up to which ever
is the bigger of the types to fully handle the precision.  Maybe the
sizeof() test isn't even needed due to promotion.  Just the signs are
important (afaik) and a test for pointer and integral mixing which I
cann't think of.  Maybe some clever use of arrays or "+ *x" or
something.

fwiw,
Bill Pringlemeir.





  reply	other threads:[~2001-08-24 14:24 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-08-23 19:03 macro conflict J. Imlay
2001-08-23 19:21 ` Alan Cox
2001-08-23 19:34 ` Tim Walberg
2001-08-23 20:01   ` Alan Cox
2001-08-23 20:02   ` raybry
2001-08-23 20:16     ` Magnus Naeslund(f)
2001-08-23 20:27       ` Alan Cox
2001-08-23 20:29         ` Magnus Naeslund(f)
2001-08-23 23:18       ` Andrew Cannon
2001-08-23 23:37         ` Magnus Naeslund(f)
2001-08-23 23:35       ` Roman Zippel
2001-08-24  1:42     ` Camiel Vanderhoeven
2001-08-24 13:03 ` David Woodhouse
2001-08-24 13:15   ` Keith Owens
2001-08-24 13:17     ` David Woodhouse
2001-08-24 14:20       ` Bill Pringlemeir [this message]
2001-08-24 21:17         ` Roman Zippel
2001-08-24 13:34   ` Richard B. Johnson
2001-08-24 18:20     ` David Wagner
2001-08-24 17:25   ` Alex Bligh - linux-kernel
2001-08-24 17:34   ` David Woodhouse
2001-08-24 18:12     ` Bill Pringlemeir

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2k7ztwne7.fsf@sympatico.ca \
    --to=bpringle@sympatico.ca \
    --cc=dwmw2@infradead.org \
    --cc=kaos@ocs.com.au \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox