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


Groups > comp.lang.c > #99351

Re: Sum using variadic macros

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: Sum using variadic macros
Date 2016-12-14 16:26 +0000
Organization A noiseless patient Spider
Message-ID <87oa0e79yp.fsf@bsb.me.uk> (permalink)
References <03e6722a-248a-46e2-b06d-d1e84e471463@googlegroups.com> <871sxa919h.fsf@bsb.me.uk> <139c05b9-299f-4b55-ba6a-8fd4308c7ce4@googlegroups.com>

Show all headers | View raw


Thiago Adams <thiago.adams@gmail.com> writes:

> On Wednesday, December 14, 2016 at 9:52:02 AM UTC-2, Ben Bacarisse wrote:
>> Thiago Adams  writes:
>> 
>> > Is it possible to do a sum with variadic macros?
>> >
>> > SUM('a', 'b', 'c') 
>> >
>> > I want to expand to 'a' + 'b' + 'c'
>> 
>> If you can accept a maximum number of arguments and the operation has an
>> identity element you can so this:
>> 
>> #define sum(...) (sum2(__VA_ARGS__, 0, 0, 0, 0, 0, 0, 0, 0))
>> #define sum2(a, b, c, d, e, f, g, h, ...) a + b + c + d + e + f + g + h
>> 
>> <snip>
>> > What I was planning to do is to generate a hash.
>> > HASH('t', 'h', 'i', 's');
>> 
>> Maybe.  That depends on the hash computation.
>> 
>
> This one:
> https://en.wikipedia.org/wiki/Java_hashCode()
>
> I think is the same of
>
> HASH3(a, b, c)  ((a) << 5 << 5) + ((b) << 5)  + (c)

If the order does not matter, this is reasonably simple:

#define sum(...) sum2(__VA_ARGS__, 0, 0, 0, 0, 0, 0))
#define sum2(a, b, c, d, e, f, ...) \
     ((((((((((f << 5) + e) << 5) + d) << 5) + c) << 5) + b) << 5) + a)

Add brackets (sorry, parentheses!) round a to f if you need to.

-- 
Ben.

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


Thread

Sum using variadic macros Thiago Adams <thiago.adams@gmail.com> - 2016-12-14 02:54 -0800
  Re: Sum using variadic macros Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-14 11:51 +0000
    Re: Sum using variadic macros David Brown <david.brown@hesbynett.no> - 2016-12-14 13:58 +0100
      Re: Sum using variadic macros Richard Heathfield <rjh@cpax.org.uk> - 2016-12-14 14:01 +0000
      Re: Sum using variadic macros Thiago Adams <thiago.adams@gmail.com> - 2016-12-14 08:05 -0800
        Re: Sum using variadic macros BartC <bc@freeuk.com> - 2016-12-14 19:20 +0000
          Re: Sum using variadic macros Thiago Adams <thiago.adams@gmail.com> - 2016-12-14 11:26 -0800
            Re: Sum using variadic macros BartC <bc@freeuk.com> - 2016-12-14 20:06 +0000
    Re: Sum using variadic macros Thiago Adams <thiago.adams@gmail.com> - 2016-12-14 05:25 -0800
      Re: Sum using variadic macros mark.bluemel@gmail.com - 2016-12-14 08:10 -0800
        Re: Sum using variadic macros Thiago Adams <thiago.adams@gmail.com> - 2016-12-14 08:23 -0800
          Re: Sum using variadic macros Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-12-14 11:32 -0800
      Re: Sum using variadic macros Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-14 16:26 +0000
        Re: Sum using variadic macros David Brown <david.brown@hesbynett.no> - 2016-12-14 19:09 +0100
          Re: Sum using variadic macros supercat@casperkitty.com - 2016-12-14 11:33 -0800
          Re: Sum using variadic macros Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-14 19:40 +0000

csiph-web