Skip to content

Commit 18e97f6

Browse files
committed
Use GCC/Clang builtins for bit arithmetics
1 parent 05ececa commit 18e97f6

3 files changed

Lines changed: 29 additions & 36 deletions

File tree

core/bitarithm.c

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* \{
1212
* \file
1313
* \author Kaspar Schleiser <kaspar.schleiser@fu-berlin.de>
14+
* \author Martin Lenders <mlenders@inf.fu-berlin.de>
1415
* \}
1516
*/
1617

@@ -39,29 +40,3 @@ number_of_highest_bit(unsigned v)
3940

4041
return r;
4142
}
42-
/*---------------------------------------------------------------------------*/
43-
unsigned
44-
number_of_lowest_bit(register unsigned v)
45-
{
46-
register unsigned r = 0;
47-
48-
while ((v & 0x01) == 0) {
49-
v >>= 1;
50-
r++;
51-
};
52-
53-
return r;
54-
}
55-
/*---------------------------------------------------------------------------*/
56-
unsigned
57-
number_of_bits_set(unsigned v)
58-
{
59-
unsigned c; // c accumulates the total bits set in v
60-
61-
for (c = 0; v; c++) {
62-
v &= v - 1; // clear the least significant bit set
63-
}
64-
65-
return c;
66-
}
67-

core/include/bitarithm.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* @file
1414
* @author Freie Universität Berlin, Computer Systems & Telematics
1515
* @author Kaspar Schleiser <kaspar.schleiser@fu-berlin.de>
16+
* @author Martin Lenders <mlenders@inf.fu-berlin.de>
1617
*/
1718

1819
#ifndef BITARITHM_H_
@@ -78,22 +79,17 @@ unsigned number_of_highest_bit(unsigned v);
7879

7980
/**
8081
* @brief Returns the number of the lowest '1' bit in a value
81-
* @param[in] v Input value - must be unequal to '0', otherwise the
82-
* function will produce an infinite loop
83-
* @return Bit Number
84-
*
85-
* Source: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious
82+
* @param[in] v Input value
83+
* @return Bit Number, 0 for least significant bit, -1 if input is 0
8684
*/
87-
unsigned number_of_lowest_bit(register unsigned v);
85+
#define number_of_lowest_bit(v) (__builtin_ffs(v)-1)
8886

8987
/**
9088
* @brief Returns the number of bits set in a value
9189
* @param[in] v Input value
9290
* @return Number of set bits
93-
*
94-
* Source: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious
9591
*/
96-
unsigned number_of_bits_set(unsigned v);
92+
#define number_of_bits_set(v) (__builtin_popcount(v))
9793

9894
/**
9995
* @}

sys/include/posix_io.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
/**
2+
* Copyright (C) 2013 INRIA.
3+
*
4+
* This file subject to the terms and conditions of the GNU Lesser General
5+
* Public License. See the file LICENSE in the top level directory for more
6+
* details.
7+
*/
8+
9+
/**
10+
* @addtogroup posix
11+
* @{
12+
* @file
13+
* @brief POSIX-like IO
14+
*
15+
* @author Freie Universität Berlin
16+
* @author Kaspar Schleiser <kaspar@schleiser.de>
17+
* @author Stephan Zeisberg <zeisberg@mi.fu-berlin.de>
18+
* @author Oliver Hahm <oleg@hobbykeller.org>
19+
* @author Martin Lenders <mlenders@inf.fu-berlin.de>
20+
*/
121
#ifndef __READ_H
222
#define __READ_H
323

@@ -15,5 +35,7 @@ int posix_open(int pid, int flags);
1535
int posix_close(int pid);
1636
int posix_read(int pid, char *buffer, int bufsize);
1737
int posix_write(int pid, char *buffer, int bufsize);
18-
38+
/**
39+
* @}
40+
*/
1941
#endif /* __READ_H */

0 commit comments

Comments
 (0)