The same trick as in the previous diff can be used a second time:
widen the type, accumulate before folding.
I've also shuffled things into an order where the introduction of
a stoeplitz_hash_n32(scache, n32) suggests itself as a next step.
It would just call stoeplitz_hash_n16(scache, n32 ^ (n32 >> 16));
This trivializes the ip4 versions to one line:
return (stoeplitz_hash_n32(faddr ^ laddr));
and
return (stoeplitz_hash_n32(faddr ^ laddr ^ fport ^ lport));
respectively. At that point I will definitely be done since there
will be no more code to remove... The ip6 versions are only slightly
simplified by this.
While I think this is rather neat, I don't want to push things too far.
Should I do this?
Index: toeplitz.c
===================================================================
RCS file: /var/cvs/src/sys/net/toeplitz.c,v
retrieving revision 1.4
diff -u -p -r1.4 toeplitz.c
--- toeplitz.c 18 Jun 2020 05:33:17 -0000 1.4
+++ toeplitz.c 18 Jun 2020 09:35:12 -0000
@@ -116,30 +116,25 @@ uint16_t
stoeplitz_hash_ip4(const struct stoeplitz_cache *scache,
in_addr_t faddr, in_addr_t laddr)
{
- uint16_t lo;
+ uint32_t n32;
- lo = faddr >> 0;
- lo ^= faddr >> 16;
- lo ^= laddr >> 0;
- lo ^= laddr >> 16;
+ n32 = faddr ^ laddr;
+ n32 ^= n32 >> 16;
- return (stoeplitz_hash_n16(scache, lo));
+ return (stoeplitz_hash_n16(scache, n32));
}
uint16_t
stoeplitz_hash_ip4port(const struct stoeplitz_cache *scache,
in_addr_t faddr, in_addr_t laddr, in_port_t fport, in_port_t lport)
{
- uint16_t lo;
+ uint32_t n32;
- lo = faddr >> 0;
- lo ^= faddr >> 16;
- lo ^= laddr >> 0;
- lo ^= laddr >> 16;
- lo ^= fport >> 0;
- lo ^= lport >> 0;
+ n32 = faddr ^ laddr;
+ n32 ^= fport ^ lport;
+ n32 ^= n32 >> 16;
- return (stoeplitz_hash_n16(scache, lo));
+ return (stoeplitz_hash_n16(scache, n32));
}
#ifdef INET6
@@ -147,44 +142,32 @@ uint16_t
stoeplitz_hash_ip6(const struct stoeplitz_cache *scache,
const struct in6_addr *faddr6, const struct in6_addr *laddr6)
{
- uint16_t lo = 0;
+ uint32_t n32 = 0;
size_t i;
- for (i = 0; i < nitems(faddr6->s6_addr32); i++) {
- uint32_t faddr = faddr6->s6_addr32[i];
- uint32_t laddr = laddr6->s6_addr32[i];
-
- lo ^= faddr >> 0;
- lo ^= faddr >> 16;
- lo ^= laddr >> 0;
- lo ^= laddr >> 16;
- }
+ for (i = 0; i < nitems(faddr6->s6_addr32); i++)
+ n32 ^= faddr6->s6_addr32[i] ^ laddr6->s6_addr32[i];
+
+ n32 ^= n32 >> 16;
- return (stoeplitz_hash_n16(scache, lo));
+ return (stoeplitz_hash_n16(scache, n32));
}
uint16_t
stoeplitz_hash_ip6port(const struct stoeplitz_cache *scache,
- const struct in6_addr *faddr6, const struct in6_addr * laddr6,
+ const struct in6_addr *faddr6, const struct in6_addr *laddr6,
in_port_t fport, in_port_t lport)
{
- uint16_t lo = 0;
+ uint32_t n32 = 0;
size_t i;
- for (i = 0; i < nitems(faddr6->s6_addr32); i++) {
- uint32_t faddr = faddr6->s6_addr32[i];
- uint32_t laddr = laddr6->s6_addr32[i];
-
- lo ^= faddr >> 0;
- lo ^= faddr >> 16;
- lo ^= laddr >> 0;
- lo ^= laddr >> 16;
- }
+ for (i = 0; i < nitems(faddr6->s6_addr32); i++)
+ n32 ^= faddr6->s6_addr32[i] ^ laddr6->s6_addr32[i];
- lo ^= fport >> 0;
- lo ^= lport >> 0;
+ n32 ^= fport ^ lport;
+ n32 ^= n32 >> 16;
- return (stoeplitz_hash_n16(scache, lo));
+ return (stoeplitz_hash_n16(scache, n32));
}
#endif /* INET6 */