On 2016-Aug-07 11:03:23 +0200, Hans Petter Selasky <[email protected]> wrote: >On 08/07/16 05:48, Adrian Chadd wrote: >> +#define ETHER_IS_BROADCAST(addr) \ >> + (((addr)[0] & (addr)[1] & (addr)[2] & \ >> + (addr)[3] & (addr)[4] & (addr)[5]) == 0xff) >> >The compiler might be able to produce more optimal code if you use "+" >instead of "&", because there are instructions on x86, that can add >multiple variables at the same time. With "&" you need to process every >one as a single instructions usually I think. > > > +#define ETHER_IS_BROADCAST(addr) \ > > + (((addr)[0] + (addr)[1] + (addr)[2] + \ > > + (addr)[3] + (addr)[4] + (addr)[5]) == (6*0xff))
IMHO, Adrian's code is clearer and micro-optimisations like this belong
in the complier, not the code. There are lots of alternative ways you
could write the macro and, unless you can demonstrate that the micro-
optimisation is worthwhile, you should pick the one that is clearer to
the software maintainer.
If you want to make a few more assumptions (64-bit unaligned little-endian
architecture with at least 2 accessible bytes beyond the address), the
following has the added advantage of only referencing (addr) once. (I'm
not suggesting it as a replacement though).
#define ETHER_IS_BROADCAST(addr) \
((*(const long *)(addr) & 0x00ffffffl) == 0x00ffffffl)
--
Peter Jeremy
signature.asc
Description: PGP signature
