[ovs-dev] [VLAN/SNAP 1/3] datapath: Fix handling of 802.1Q and SNAP headers.

Ben Pfaff blp at nicira.com
Fri Aug 6 22:58:36 UTC 2010


On Fri, Aug 06, 2010 at 06:44:01PM -0400, Jesse Gross wrote:
> On Wed, Jul 28, 2010 at 5:01 PM, Ben Pfaff <blp at nicira.com> wrote:
> > +           llc->oui[0] != 0 || llc->oui[1] != 0 || llc->oui[2] != 0)
> > +               return htons(ODP_DL_TYPE_NOT_ETH_TYPE);
> >
> 
> I would be tempted to write this as ((llc->oui[0] | llc->oui[1]
> | llc->oui[2]) != 0).

Out of curiosity, I tried compiling the following:

    int func(char x[3])
    {
       return x[0] != 0 || x[1] != 0 || x[2] != 0;
    }
    int func2(char x[3])
    {
       return (x[0] | x[1] | x[2]) != 0;
    }

with GCC 4.4 at various optimization levels.  I really expected that
both functions would generate the same code.  I was wrong.  func2() is
the clear winner.

Here's the disassembly for -O3:

00000000 <func>:
   0:	55                   	push   %ebp
   1:	89 e5                	mov    %esp,%ebp
   3:	8b 45 08             	mov    0x8(%ebp),%eax
   6:	80 38 00             	cmpb   $0x0,(%eax)
   9:	75 15                	jne    20 <func+0x20>
   b:	80 78 01 00          	cmpb   $0x0,0x1(%eax)
   f:	75 0f                	jne    20 <func+0x20>
  11:	80 78 02 00          	cmpb   $0x0,0x2(%eax)
  15:	5d                   	pop    %ebp
  16:	0f 95 c0             	setne  %al
  19:	0f b6 c0             	movzbl %al,%eax
  1c:	c3                   	ret    
  1d:	8d 76 00             	lea    0x0(%esi),%esi
  20:	b8 01 00 00 00       	mov    $0x1,%eax
  25:	5d                   	pop    %ebp
  26:	c3                   	ret    
  27:	89 f6                	mov    %esi,%esi
  29:	8d bc 27 00 00 00 00 	lea    0x0(%edi,%eiz,1),%edi

00000030 <func2>:
  30:	55                   	push   %ebp
  31:	89 e5                	mov    %esp,%ebp
  33:	8b 45 08             	mov    0x8(%ebp),%eax
  36:	5d                   	pop    %ebp
  37:	0f b6 50 01          	movzbl 0x1(%eax),%edx
  3b:	0a 50 02             	or     0x2(%eax),%dl
  3e:	0a 10                	or     (%eax),%dl
  40:	0f 95 c0             	setne  %al
  43:	0f b6 c0             	movzbl %al,%eax
  46:	c3                   	ret    




More information about the dev mailing list