1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-16 10:03:50 +01:00

net/ipv6: make use of bitarithm_clzb() in ipv6_addr_match_prefix()

This commit is contained in:
Benjamin Valentin 2023-05-23 14:37:25 +02:00
parent abe5949cd2
commit d8438c47cf

View File

@ -20,6 +20,7 @@
#include <stdio.h> #include <stdio.h>
#include "fmt.h" #include "fmt.h"
#include "bitarithm.h"
#include "kernel_defines.h" #include "kernel_defines.h"
#include "net/ipv6/addr.h" #include "net/ipv6/addr.h"
@ -52,26 +53,16 @@ uint8_t ipv6_addr_match_prefix(const ipv6_addr_t *a, const ipv6_addr_t *b)
} }
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
uint8_t xor = a->u8[i] ^ b->u8[i];
if (xor) {
/* if bytes aren't equal count matching leading bits */
prefix_len += bitarithm_clzb(xor);
break;
}
else {
/* if bytes are equal add 8 */ /* if bytes are equal add 8 */
if (a->u8[i] == b->u8[i]) {
prefix_len += 8; prefix_len += 8;
} }
else {
uint8_t xor = (a->u8[i] ^ b->u8[i]);
/* while bits from byte equal add 1 */
for (int j = 0; j < 8; j++) {
if ((xor & 0x80) == 0) {
prefix_len++;
xor = xor << 1;
}
else {
break;
}
}
break;
}
} }
return prefix_len; return prefix_len;