Staging: batman-adv: Calculate hamming weight using optimized kernel functions
[linux-2.6/libata-dev.git] / drivers / staging / batman-adv / bitarray.c
blob9dbaf1e8e6a5152b8f73fe258b86f13901945caa
1 /*
2 * Copyright (C) 2006-2010 B.A.T.M.A.N. contributors:
4 * Simon Wunderlich, Marek Lindner
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA
22 #include "main.h"
23 #include "bitarray.h"
25 #include <linux/bitops.h>
27 /* returns true if the corresponding bit in the given seq_bits indicates true
28 * and curr_seqno is within range of last_seqno */
29 uint8_t get_bit_status(TYPE_OF_WORD *seq_bits, uint32_t last_seqno,
30 uint32_t curr_seqno)
32 int32_t diff, word_offset, word_num;
34 diff = last_seqno - curr_seqno;
35 if (diff < 0 || diff >= TQ_LOCAL_WINDOW_SIZE) {
36 return 0;
37 } else {
38 /* which word */
39 word_num = (last_seqno - curr_seqno) / WORD_BIT_SIZE;
40 /* which position in the selected word */
41 word_offset = (last_seqno - curr_seqno) % WORD_BIT_SIZE;
43 if (seq_bits[word_num] & 1 << word_offset)
44 return 1;
45 else
46 return 0;
50 /* turn corresponding bit on, so we can remember that we got the packet */
51 void bit_mark(TYPE_OF_WORD *seq_bits, int32_t n)
53 int32_t word_offset, word_num;
55 /* if too old, just drop it */
56 if (n < 0 || n >= TQ_LOCAL_WINDOW_SIZE)
57 return;
59 /* which word */
60 word_num = n / WORD_BIT_SIZE;
61 /* which position in the selected word */
62 word_offset = n % WORD_BIT_SIZE;
64 seq_bits[word_num] |= 1 << word_offset; /* turn the position on */
67 /* shift the packet array by n places. */
68 static void bit_shift(TYPE_OF_WORD *seq_bits, int32_t n)
70 int32_t word_offset, word_num;
71 int32_t i;
73 if (n <= 0 || n >= TQ_LOCAL_WINDOW_SIZE)
74 return;
76 word_offset = n % WORD_BIT_SIZE;/* shift how much inside each word */
77 word_num = n / WORD_BIT_SIZE; /* shift over how much (full) words */
79 for (i = NUM_WORDS - 1; i > word_num; i--) {
80 /* going from old to new, so we don't overwrite the data we copy
81 * from.
83 * left is high, right is low: FEDC BA98 7654 3210
84 * ^^ ^^
85 * vvvv
86 * ^^^^ = from, vvvvv =to, we'd have word_num==1 and
87 * word_offset==WORD_BIT_SIZE/2 ????? in this example.
88 * (=24 bits)
90 * our desired output would be: 9876 5432 1000 0000
91 * */
93 seq_bits[i] =
94 (seq_bits[i - word_num] << word_offset) +
95 /* take the lower port from the left half, shift it left
96 * to its final position */
97 (seq_bits[i - word_num - 1] >>
98 (WORD_BIT_SIZE-word_offset));
99 /* and the upper part of the right half and shift it left to
100 * it's position */
101 /* for our example that would be: word[0] = 9800 + 0076 =
102 * 9876 */
104 /* now for our last word, i==word_num, we only have the it's "left"
105 * half. that's the 1000 word in our example.*/
107 seq_bits[i] = (seq_bits[i - word_num] << word_offset);
109 /* pad the rest with 0, if there is anything */
110 i--;
112 for (; i >= 0; i--)
113 seq_bits[i] = 0;
116 static void bit_reset_window(TYPE_OF_WORD *seq_bits)
118 int i;
119 for (i = 0; i < NUM_WORDS; i++)
120 seq_bits[i] = 0;
124 /* receive and process one packet within the sequence number window.
126 * returns:
127 * 1 if the window was moved (either new or very old)
128 * 0 if the window was not moved/shifted.
130 char bit_get_packet(TYPE_OF_WORD *seq_bits, int32_t seq_num_diff,
131 int8_t set_mark)
133 /* FIXME: each orig_node->batman_if will be attached to a softif */
134 struct bat_priv *bat_priv = netdev_priv(soft_device);
136 /* sequence number is slightly older. We already got a sequence number
137 * higher than this one, so we just mark it. */
139 if ((seq_num_diff <= 0) && (seq_num_diff > -TQ_LOCAL_WINDOW_SIZE)) {
140 if (set_mark)
141 bit_mark(seq_bits, -seq_num_diff);
142 return 0;
145 /* sequence number is slightly newer, so we shift the window and
146 * set the mark if required */
148 if ((seq_num_diff > 0) && (seq_num_diff < TQ_LOCAL_WINDOW_SIZE)) {
149 bit_shift(seq_bits, seq_num_diff);
151 if (set_mark)
152 bit_mark(seq_bits, 0);
153 return 1;
156 /* sequence number is much newer, probably missed a lot of packets */
158 if ((seq_num_diff >= TQ_LOCAL_WINDOW_SIZE)
159 || (seq_num_diff < EXPECTED_SEQNO_RANGE)) {
160 bat_dbg(DBG_BATMAN, bat_priv,
161 "We missed a lot of packets (%i) !\n",
162 seq_num_diff - 1);
163 bit_reset_window(seq_bits);
164 if (set_mark)
165 bit_mark(seq_bits, 0);
166 return 1;
169 /* received a much older packet. The other host either restarted
170 * or the old packet got delayed somewhere in the network. The
171 * packet should be dropped without calling this function if the
172 * seqno window is protected. */
174 if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
175 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
177 bat_dbg(DBG_BATMAN, bat_priv,
178 "Other host probably restarted!\n");
180 bit_reset_window(seq_bits);
181 if (set_mark)
182 bit_mark(seq_bits, 0);
184 return 1;
187 /* never reached */
188 return 0;
191 /* count the hamming weight, how many good packets did we receive? just count
192 * the 1's.
194 int bit_packet_count(TYPE_OF_WORD *seq_bits)
196 int i, hamming = 0;
198 for (i = 0; i < NUM_WORDS; i++)
199 hamming += hweight_long(seq_bits[i]);
201 return hamming;