Page MenuHomeFreeBSD
Paste P281

Clang 8.0.0 -O3, ether zero, broadcast functions; add variants inlined. GCC 8.3.0 generates similar code.
ActivePublic

Authored by cem on Aug 1 2019, 2:44 PM.
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
static inline bool
is_zero_add(const uint8_t * addr)
{
return ((addr[0] + addr[1] + addr[2] + addr[3] + addr[4] + addr[5]) == 0);
}
static inline bool
is_bcast_add(const uint8_t * addr)
{
return ((addr[0] + addr[1] + addr[2] + addr[3] + addr[4] + addr[5]) == (6*0xff));
}
extern int foo(const uint8_t addr[static 6]);
int
foo(const uint8_t addr[static 6])
{
int res;
// Prevent compiler from wholly obviating the computation
res = 0;
res += (int)is_zero_add(addr);
res += (int)is_bcast_add(addr);
return (res);
}
===========================================================================================
(gdb) disas /m foo
Dump of assembler code for function foo:
22 return ((addr[0] + addr[1] + addr[2] + addr[3] + addr[4] + addr[5]) == 0);
0x0000000000000004 <+4>: movzbl (%rdi),%eax
0x0000000000000007 <+7>: movzbl 0x1(%rdi),%ecx
0x000000000000000b <+11>: add %eax,%ecx
0x000000000000000d <+13>: movzbl 0x2(%rdi),%eax
0x0000000000000011 <+17>: add %ecx,%eax
0x0000000000000013 <+19>: movzbl 0x3(%rdi),%ecx
0x0000000000000017 <+23>: add %eax,%ecx
0x0000000000000019 <+25>: movzbl 0x4(%rdi),%eax
0x000000000000001d <+29>: add %ecx,%eax
0x000000000000001f <+31>: movzbl 0x5(%rdi),%ecx
0x0000000000000023 <+35>: add %eax,%ecx
// Clang just emits ordinary 2-argument adds.
23 }
25 static inline bool
26 is_bcast_add(const uint8_t * addr)
27 {
28 return ((addr[0] + addr[1] + addr[2] + addr[3] + addr[4] + addr[5]) == (6*0xff));
0x0000000000000025 <+37>: xor %eax,%eax
0x0000000000000027 <+39>: cmp $0x5fa,%ecx
0x000000000000002d <+45>: sete %al
// The broadcast check can reuse the addition result

Event Timeline

cem created this paste.Aug 1 2019, 2:44 PM
cem created this object in space S1 Global.