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


Groups > comp.lang.c > #15919

Re: macro

From blp@cs.stanford.edu (Ben Pfaff)
Newsgroups comp.lang.c
Subject Re: macro
Date 2012-01-18 11:28 -0800
Message-ID <878vl4resl.fsf@blp.benpfaff.org> (permalink)
References (6 earlier) <MPG.297ed91372934fba989686@news.eternal-september.org> <lnaa5myr89.fsf@nuthaus.mib.org> <9nmh3nFaaoU1@mid.individual.net> <8a04a8ba-eee0-48fd-9cb6-98695875840e@f33g2000yqh.googlegroups.com> <9nnjboFaaoU3@mid.individual.net>

Show all headers | View raw


Ian Collins <ian-news@hotmail.com> writes:

> I guess the use of "do...while(0)" (and function-like macros in
> general) is a hang over from 80s and 90s compiler which didn't inline
> functions.

Mostly.

In the tree I'm looking at now, I see two uses that don't fall
into that category.  The first is where I had a notion that
macros would generate better code, specifically for these macros:

    #define HASH_ROT(x, k) (((x) << (k)) | ((x) >> (32 - (k))))

    #define HASH_MIX(a, b, c)                       \
        do {                                        \
          a -= c; a ^= HASH_ROT(c,  4); c += b;     \
          b -= a; b ^= HASH_ROT(a,  6); a += c;     \
          c -= b; c ^= HASH_ROT(b,  8); b += a;     \
          a -= c; a ^= HASH_ROT(c, 16); c += b;     \
          b -= a; b ^= HASH_ROT(a, 19); a += c;     \
          c -= b; c ^= HASH_ROT(b,  4); b += a;     \
        } while (0)

    #define HASH_FINAL(a, b, c)                     \
        do {                                        \
          c ^= b; c -= HASH_ROT(b, 14);             \
          a ^= c; a -= HASH_ROT(c, 11);             \
          b ^= a; b -= HASH_ROT(a, 25);             \
          c ^= b; c -= HASH_ROT(b, 16);             \
          a ^= c; a -= HASH_ROT(c,  4);             \
          b ^= a; b -= HASH_ROT(a, 14);             \
          c ^= b; c -= HASH_ROT(b, 24);             \
        } while (0)

In an attempt to demonstrate that they do generate better code, I
converted them to the following inline functions:

    static inline uint32_t
    hash_rot(uint32_t x, int k)
    {
        return (x << k) | (x >> (32 - k));
    }

    static inline void
    hash_mix(uint32_t *a, uint32_t *b, uint32_t *c)
    {
          *a -= *c; *a ^= hash_rot(*c,  4); *c += *b;
          *b -= *a; *b ^= hash_rot(*a,  6); *a += *c;
          *c -= *b; *c ^= hash_rot(*b,  8); *b += *a;
          *a -= *c; *a ^= hash_rot(*c, 16); *c += *b;
          *b -= *a; *b ^= hash_rot(*a, 19); *a += *c;
          *c -= *b; *c ^= hash_rot(*b,  4); *b += *a;
    }

    static inline void
    hash_final(uint32_t *a, uint32_t *b, uint32_t *c)
    {
          *c ^= *b; *c -= hash_rot(*b, 14);
          *a ^= *c; *a -= hash_rot(*c, 11);
          *b ^= *a; *b -= hash_rot(*a, 25);
          *c ^= *b; *c -= hash_rot(*b, 16);
          *a ^= *c; *a -= hash_rot(*c,  4);
          *b ^= *a; *b -= hash_rot(*a, 14);
          *c ^= *b; *c -= hash_rot(*b, 24);
    }

(Of course this required some changes to the users also.)
However, when I looked at the generated code, it was completely
identical with GCC 4.4.5.  So I've now submitted a patch to
change these to inline functions:
        http://openvswitch.org/pipermail/dev/2012-January/014369.html

I ran out of time so I won't be able to describe my other use
case :-)
-- 
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

macro dspfun <dspfun@hotmail.com> - 2012-01-15 05:29 -0800
  Re: macro Dr Nick <3-nospam@temporary-address.org.uk> - 2012-01-15 13:44 +0000
    Re: macro dspfun <dspfun@hotmail.com> - 2012-01-15 05:58 -0800
      Re: macro "Joachim Schmitz" <nospam.jojo@schmitz-digital.de> - 2012-01-15 15:34 +0100
      Re: macro "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2012-01-15 06:37 -0800
        Re: macro dspfun <dspfun@hotmail.com> - 2012-01-15 07:28 -0800
          Re: macro Philip Lantz <prl@canterey.us> - 2012-01-15 17:30 -0800
            Re: macro Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-16 00:18 -0800
              Re: macro Hans Vlems <hvlems@freenet.de> - 2012-01-16 00:52 -0800
                Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-16 22:08 +1300
                Re: macro Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-16 03:04 -0800
                Re: macro Markus Wichmann <nullplan@gmx.net> - 2012-01-16 12:42 +0100
        Re: macro jgk@panix.com (Joe keane) - 2012-01-16 14:48 +0000
          Re: macro Keith Thompson <kst-u@mib.org> - 2012-01-16 12:01 -0800
            Re: macro Philip Lantz <prl@canterey.us> - 2012-01-17 01:02 -0800
              Re: macro Keith Thompson <kst-u@mib.org> - 2012-01-17 13:05 -0800
                Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-18 13:12 +1300
                Re: macro dspfun <dspfun@hotmail.com> - 2012-01-18 01:05 -0800
                Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-18 22:57 +1300
                Re: macro "osmium" <r124c4u102@comcast.net> - 2012-01-18 06:22 -0600
                Re: macro dspfun <dspfun@hotmail.com> - 2012-01-18 07:50 -0800
                Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-20 14:09 +1300
                Re: macro blp@cs.stanford.edu (Ben Pfaff) - 2012-01-18 11:28 -0800
                Re: macro Kaz Kylheku <kaz@kylheku.com> - 2012-01-18 21:09 +0000
                Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-20 15:08 +1300
                [OT] Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-20 15:58 +1300
                Re: [OT] Re: macro Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-20 12:22 +0000
                Re: [OT] Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-21 07:58 +1300
                Re: macro James Kuyper <jameskuyper@verizon.net> - 2012-01-19 22:47 -0500
                Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-20 17:06 +1300
                Re: macro James Kuyper <jameskuyper@verizon.net> - 2012-01-20 00:12 -0500
                Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-20 18:28 +1300
                Re: macro Keith Thompson <kst-u@mib.org> - 2012-01-20 03:21 -0800
                Re: macro Shao Miller <sha0.miller@gmail.com> - 2012-01-20 10:50 -0500
                Re: macro Shao Miller <sha0.miller@gmail.com> - 2012-01-20 11:46 -0500
                Re: macro Keith Thompson <kst-u@mib.org> - 2012-01-18 14:24 -0800
                Re: macro jgk@panix.com (Joe keane) - 2012-01-18 22:36 +0000
                Re: macro Kaz Kylheku <kaz@kylheku.com> - 2012-01-18 22:50 +0000
            Re: macro jgk@panix.com (Joe keane) - 2012-01-17 22:08 +0000
  Re: macro Keith Thompson <kst-u@mib.org> - 2012-01-15 08:00 -0800
  Re: macro NEON China BLUE <chine.bleu@yahoo.com> - 2012-01-15 08:15 -0800
  Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-16 09:04 +1300
    Re: macro Philip Lantz <prl@canterey.us> - 2012-01-15 17:36 -0800
  Re: macro Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-01-16 12:01 +0000

csiph-web