Commit 8bb22a38 authored by Sven Eckelmann's avatar Sven Eckelmann Committed by Greg Kroah-Hartman

Staging: batman-adv: Calculate hamming weight using optimized kernel functions

The Kernighan algorithm is not able to calculate the number of set bits
in parallel and the compiler cannot replace it with optimized
instructions.

The kernel provides specialised functions for each cpu which can either
use a software implementation or hardware instruction depending on the
target cpu.
Reported-by: default avatarDavid S. Miller <davem@davemloft.net>
Signed-off-by: default avatarSven Eckelmann <sven.eckelmann@gmx.de>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent 556c83ec
* Use hweight* for hamming weight calculation
* Save/cache packets direktly as skb instead of using a normal memory region * Save/cache packets direktly as skb instead of using a normal memory region
and copying it in a skb using send_raw_packet and similar functions and copying it in a skb using send_raw_packet and similar functions
* Request a new review * Request a new review
......
...@@ -22,6 +22,8 @@ ...@@ -22,6 +22,8 @@
#include "main.h" #include "main.h"
#include "bitarray.h" #include "bitarray.h"
#include <linux/bitops.h>
/* returns true if the corresponding bit in the given seq_bits indicates true /* returns true if the corresponding bit in the given seq_bits indicates true
* and curr_seqno is within range of last_seqno */ * and curr_seqno is within range of last_seqno */
uint8_t get_bit_status(TYPE_OF_WORD *seq_bits, uint32_t last_seqno, uint8_t get_bit_status(TYPE_OF_WORD *seq_bits, uint32_t last_seqno,
...@@ -187,21 +189,14 @@ char bit_get_packet(TYPE_OF_WORD *seq_bits, int32_t seq_num_diff, ...@@ -187,21 +189,14 @@ char bit_get_packet(TYPE_OF_WORD *seq_bits, int32_t seq_num_diff,
} }
/* count the hamming weight, how many good packets did we receive? just count /* count the hamming weight, how many good packets did we receive? just count
* the 1's. The inner loop uses the Kernighan algorithm, see * the 1's.
* http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
*/ */
int bit_packet_count(TYPE_OF_WORD *seq_bits) int bit_packet_count(TYPE_OF_WORD *seq_bits)
{ {
int i, hamming = 0; int i, hamming = 0;
TYPE_OF_WORD word;
for (i = 0; i < NUM_WORDS; i++) { for (i = 0; i < NUM_WORDS; i++)
word = seq_bits[i]; hamming += hweight_long(seq_bits[i]);
while (word) {
word &= word-1;
hamming++;
}
}
return hamming; return hamming;
} }
...@@ -22,7 +22,8 @@ ...@@ -22,7 +22,8 @@
#ifndef _NET_BATMAN_ADV_BITARRAY_H_ #ifndef _NET_BATMAN_ADV_BITARRAY_H_
#define _NET_BATMAN_ADV_BITARRAY_H_ #define _NET_BATMAN_ADV_BITARRAY_H_
/* you should choose something big, if you don't want to waste cpu */ /* you should choose something big, if you don't want to waste cpu
* and keep the type in sync with bit_packet_count */
#define TYPE_OF_WORD unsigned long #define TYPE_OF_WORD unsigned long
#define WORD_BIT_SIZE (sizeof(TYPE_OF_WORD) * 8) #define WORD_BIT_SIZE (sizeof(TYPE_OF_WORD) * 8)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment