netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net-next] etherdevice: Optimize compare_ether_addr/ether_addr_equal
@ 2013-08-31  8:54 Joe Perches
  2013-08-31 16:43 ` Stephen Hemminger
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Joe Perches @ 2013-08-31  8:54 UTC (permalink / raw)
  To: David Miller; +Cc: Eric Dumazet, netdev

When CONFIG_HAS_EFFICIENT_UNALIGNED_ACCESS is set,
optimize compare_ether_addr a little by removing an
xor and or by using a u32 and u16 comparison
instead of 3 separate u16 comparisons.

Make the ether_addr_equal_64bits code a bit simpler
by adding a test for CONFIG_64BIT and calling
ether_addr_equal otherwise.

This also slightly improves ether_addr_equal_64bits
by removing the zap_last_2bytes shifts in the !64bit
case.

Signed-off-by: Joe Perches <joe@perches.com>
---
 include/linux/etherdevice.h | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/include/linux/etherdevice.h b/include/linux/etherdevice.h
index c623861..2514d17 100644
--- a/include/linux/etherdevice.h
+++ b/include/linux/etherdevice.h
@@ -208,11 +208,19 @@ static inline void eth_hw_addr_random(struct net_device *dev)
  */
 static inline unsigned compare_ether_addr(const u8 *addr1, const u8 *addr2)
 {
+#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
+	u32 fold = ((*(const u32 *)addr1) ^ (*(const u32 *)addr2));
+	fold |= ((*(const u16 *)(addr1 + 4)) ^ (*(const u16 *)(addr2 + 4)));
+
+	BUILD_BUG_ON(ETH_ALEN != 6);
+	return fold != 0;
+#else
 	const u16 *a = (const u16 *) addr1;
 	const u16 *b = (const u16 *) addr2;
 
 	BUILD_BUG_ON(ETH_ALEN != 6);
 	return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | (a[2] ^ b[2])) != 0;
+#endif
 }
 
 /**
@@ -253,16 +261,11 @@ static inline unsigned long zap_last_2bytes(unsigned long value)
 static inline bool ether_addr_equal_64bits(const u8 addr1[6+2],
 					   const u8 addr2[6+2])
 {
-#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
+#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && defined(CONFIG_64BIT)
 	unsigned long fold = ((*(unsigned long *)addr1) ^
 			      (*(unsigned long *)addr2));
 
-	if (sizeof(fold) == 8)
-		return zap_last_2bytes(fold) == 0;
-
-	fold |= zap_last_2bytes((*(unsigned long *)(addr1 + 4)) ^
-				(*(unsigned long *)(addr2 + 4)));
-	return fold == 0;
+	return zap_last_2bytes(fold) == 0;
 #else
 	return ether_addr_equal(addr1, addr2);
 #endif

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH net-next] etherdevice: Optimize compare_ether_addr/ether_addr_equal
  2013-08-31  8:54 [PATCH net-next] etherdevice: Optimize compare_ether_addr/ether_addr_equal Joe Perches
@ 2013-08-31 16:43 ` Stephen Hemminger
  2013-08-31 20:58   ` Joe Perches
  2013-08-31 19:41 ` Sergei Shtylyov
  2013-08-31 22:27 ` David Miller
  2 siblings, 1 reply; 6+ messages in thread
From: Stephen Hemminger @ 2013-08-31 16:43 UTC (permalink / raw)
  To: Joe Perches; +Cc: David Miller, Eric Dumazet, netdev

On Sat, 31 Aug 2013 01:54:16 -0700
Joe Perches <joe@perches.com> wrote:

> When CONFIG_HAS_EFFICIENT_UNALIGNED_ACCESS is set,
> optimize compare_ether_addr a little by removing an
> xor and or by using a u32 and u16 comparison
> instead of 3 separate u16 comparisons.
> 
> Make the ether_addr_equal_64bits code a bit simpler
> by adding a test for CONFIG_64BIT and calling
> ether_addr_equal otherwise.
> 
> This also slightly improves ether_addr_equal_64bits
> by removing the zap_last_2bytes shifts in the !64bit
> case.
> 
> Signed-off-by: Joe Perches <joe@perches.com>
> ---
>  include/linux/etherdevice.h | 17 ++++++++++-------
>  1 file changed, 10 insertions(+), 7 deletions(-)
> 
> diff --git a/include/linux/etherdevice.h b/include/linux/etherdevice.h
> index c623861..2514d17 100644
> --- a/include/linux/etherdevice.h
> +++ b/include/linux/etherdevice.h
> @@ -208,11 +208,19 @@ static inline void eth_hw_addr_random(struct net_device *dev)
>   */
>  static inline unsigned compare_ether_addr(const u8 *addr1, const u8 *addr2)
>  {
> +#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
> +	u32 fold = ((*(const u32 *)addr1) ^ (*(const u32 *)addr2));
> +	fold |= ((*(const u16 *)(addr1 + 4)) ^ (*(const u16 *)(addr2 + 4)));
> +
> +	BUILD_BUG_ON(ETH_ALEN != 6);
> +	return fold != 0;
> +#else
>  	const u16 *a = (const u16 *) addr1;
>  	const u16 *b = (const u16 *) addr2;
>  
>  	BUILD_BUG_ON(ETH_ALEN != 6);
>  	return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | (a[2] ^ b[2])) != 0;
> +#endif
>  }
>  

If you really want to be efficient do it as one 64 bit mask and compare.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH net-next] etherdevice: Optimize compare_ether_addr/ether_addr_equal
  2013-08-31  8:54 [PATCH net-next] etherdevice: Optimize compare_ether_addr/ether_addr_equal Joe Perches
  2013-08-31 16:43 ` Stephen Hemminger
@ 2013-08-31 19:41 ` Sergei Shtylyov
  2013-08-31 22:27 ` David Miller
  2 siblings, 0 replies; 6+ messages in thread
From: Sergei Shtylyov @ 2013-08-31 19:41 UTC (permalink / raw)
  To: Joe Perches; +Cc: David Miller, Eric Dumazet, netdev

Hello.

On 08/31/2013 12:54 PM, Joe Perches wrote:

> When CONFIG_HAS_EFFICIENT_UNALIGNED_ACCESS is set,
> optimize compare_ether_addr a little by removing an
> xor and or by using a u32 and u16 comparison
> instead of 3 separate u16 comparisons.

> Make the ether_addr_equal_64bits code a bit simpler
> by adding a test for CONFIG_64BIT and calling
> ether_addr_equal otherwise.

> This also slightly improves ether_addr_equal_64bits
> by removing the zap_last_2bytes shifts in the !64bit
> case.

> Signed-off-by: Joe Perches <joe@perches.com>
> ---
>   include/linux/etherdevice.h | 17 ++++++++++-------
>   1 file changed, 10 insertions(+), 7 deletions(-)

> diff --git a/include/linux/etherdevice.h b/include/linux/etherdevice.h
> index c623861..2514d17 100644
> --- a/include/linux/etherdevice.h
> +++ b/include/linux/etherdevice.h
> @@ -208,11 +208,19 @@ static inline void eth_hw_addr_random(struct net_device *dev)
>    */
>   static inline unsigned compare_ether_addr(const u8 *addr1, const u8 *addr2)
>   {
> +#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
> +	u32 fold = ((*(const u32 *)addr1) ^ (*(const u32 *)addr2));

    Could you keep the same style as before and insert empty line after 
declaration?

> +	fold |= ((*(const u16 *)(addr1 + 4)) ^ (*(const u16 *)(addr2 + 4)));
> +
> +	BUILD_BUG_ON(ETH_ALEN != 6);
> +	return fold != 0;
> +#else
>   	const u16 *a = (const u16 *) addr1;
>   	const u16 *b = (const u16 *) addr2;
>
>   	BUILD_BUG_ON(ETH_ALEN != 6);
>   	return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | (a[2] ^ b[2])) != 0;
> +#endif
>   }
>
>   /**

WBR, Sergei

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH net-next] etherdevice: Optimize compare_ether_addr/ether_addr_equal
  2013-08-31 16:43 ` Stephen Hemminger
@ 2013-08-31 20:58   ` Joe Perches
  0 siblings, 0 replies; 6+ messages in thread
From: Joe Perches @ 2013-08-31 20:58 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: David Miller, Eric Dumazet, netdev

On Sat, 2013-08-31 at 09:43 -0700, Stephen Hemminger wrote:
> On Sat, 31 Aug 2013 01:54:16 -0700
> Joe Perches <joe@perches.com> wrote:
> 
> > When CONFIG_HAS_EFFICIENT_UNALIGNED_ACCESS is set,
> > optimize compare_ether_addr a little by removing an
> > xor and or by using a u32 and u16 comparison
> > instead of 3 separate u16 comparisons.
> > 
> > Make the ether_addr_equal_64bits code a bit simpler
> > by adding a test for CONFIG_64BIT and calling
> > ether_addr_equal otherwise.
> > 
> > This also slightly improves ether_addr_equal_64bits
> > by removing the zap_last_2bytes shifts in the !64bit
> > case.
> > 
> > Signed-off-by: Joe Perches <joe@perches.com>
> > ---
> >  include/linux/etherdevice.h | 17 ++++++++++-------
> >  1 file changed, 10 insertions(+), 7 deletions(-)
> > 
> > diff --git a/include/linux/etherdevice.h b/include/linux/etherdevice.h
> > index c623861..2514d17 100644
> > --- a/include/linux/etherdevice.h
> > +++ b/include/linux/etherdevice.h
> > @@ -208,11 +208,19 @@ static inline void eth_hw_addr_random(struct net_device *dev)
> >   */
> >  static inline unsigned compare_ether_addr(const u8 *addr1, const u8 *addr2)
> >  {
> > +#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
> > +	u32 fold = ((*(const u32 *)addr1) ^ (*(const u32 *)addr2));
> > +	fold |= ((*(const u16 *)(addr1 + 4)) ^ (*(const u16 *)(addr2 + 4)));
> > +
> > +	BUILD_BUG_ON(ETH_ALEN != 6);
> > +	return fold != 0;
> > +#else
> >  	const u16 *a = (const u16 *) addr1;
> >  	const u16 *b = (const u16 *) addr2;
> >  
> >  	BUILD_BUG_ON(ETH_ALEN != 6);
> >  	return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | (a[2] ^ b[2])) != 0;
> > +#endif
> >  }
> >  
> 
> If you really want to be efficient do it as one 64 bit mask and compare.

Nope.

That's what ether_addr_equal_64bits does
when it's known that a 64 bit test can be done.

Otherwise, there's no guarantee that 64 bits
are available to be read from 48 bits of data.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH net-next] etherdevice: Optimize compare_ether_addr/ether_addr_equal
  2013-08-31  8:54 [PATCH net-next] etherdevice: Optimize compare_ether_addr/ether_addr_equal Joe Perches
  2013-08-31 16:43 ` Stephen Hemminger
  2013-08-31 19:41 ` Sergei Shtylyov
@ 2013-08-31 22:27 ` David Miller
  2013-09-02 16:52   ` Joe Perches
  2 siblings, 1 reply; 6+ messages in thread
From: David Miller @ 2013-08-31 22:27 UTC (permalink / raw)
  To: joe; +Cc: eric.dumazet, netdev

From: Joe Perches <joe@perches.com>
Date: Sat, 31 Aug 2013 01:54:16 -0700

> When CONFIG_HAS_EFFICIENT_UNALIGNED_ACCESS is set,
> optimize compare_ether_addr a little by removing an
> xor and or by using a u32 and u16 comparison
> instead of 3 separate u16 comparisons.
> 
> Make the ether_addr_equal_64bits code a bit simpler
> by adding a test for CONFIG_64BIT and calling
> ether_addr_equal otherwise.
> 
> This also slightly improves ether_addr_equal_64bits
> by removing the zap_last_2bytes shifts in the !64bit
> case.
> 
> Signed-off-by: Joe Perches <joe@perches.com>

You'll need to update Documantion/unaligned-memory-access.txt as well
because it uses this funcation as a "real life" example.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH net-next] etherdevice: Optimize compare_ether_addr/ether_addr_equal
  2013-08-31 22:27 ` David Miller
@ 2013-09-02 16:52   ` Joe Perches
  0 siblings, 0 replies; 6+ messages in thread
From: Joe Perches @ 2013-09-02 16:52 UTC (permalink / raw)
  To: David Miller; +Cc: eric.dumazet, netdev

On Sat, 2013-08-31 at 18:27 -0400, David Miller wrote:
> From: Joe Perches <joe@perches.com>
> Date: Sat, 31 Aug 2013 01:54:16 -0700
> 
> > When CONFIG_HAS_EFFICIENT_UNALIGNED_ACCESS is set,
> > optimize compare_ether_addr a little by removing an
> > xor and or by using a u32 and u16 comparison
> > instead of 3 separate u16 comparisons.
> > 
> > Make the ether_addr_equal_64bits code a bit simpler
> > by adding a test for CONFIG_64BIT and calling
> > ether_addr_equal otherwise.
> > 
> > This also slightly improves ether_addr_equal_64bits
> > by removing the zap_last_2bytes shifts in the !64bit
> > case.
> > 
> > Signed-off-by: Joe Perches <joe@perches.com>
> 
> You'll need to update Documantion/unaligned-memory-access.txt as well
> because it uses this funcation as a "real life" example.

I submitted patches converting compare_ether_addr uses
to ether_addr_equal.

I'll send this patch again along with documenting the
requirement for ether_addr_equal if/after those patches
are applied so compare_ether_addr can be removed.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2013-09-02 16:52 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-08-31  8:54 [PATCH net-next] etherdevice: Optimize compare_ether_addr/ether_addr_equal Joe Perches
2013-08-31 16:43 ` Stephen Hemminger
2013-08-31 20:58   ` Joe Perches
2013-08-31 19:41 ` Sergei Shtylyov
2013-08-31 22:27 ` David Miller
2013-09-02 16:52   ` Joe Perches

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).