Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
RIOT
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cm-projects
RIOT
Commits
0ecaaf02
Commit
0ecaaf02
authored
6 years ago
by
Peter Kietzmann
Browse files
Options
Downloads
Patches
Plain Diff
core/bitarithm: add explicit 32-bit function
parent
6bfd3338
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
core/bitarithm.c
+11
-0
11 additions, 0 deletions
core/bitarithm.c
core/include/bitarithm.h
+16
-1
16 additions, 1 deletion
core/include/bitarithm.h
tests/unittests/tests-core/tests-core-bitarithm.c
+10
-2
10 additions, 2 deletions
tests/unittests/tests-core/tests-core-bitarithm.c
with
37 additions
and
3 deletions
core/bitarithm.c
+
11
−
0
View file @
0ecaaf02
...
...
@@ -57,6 +57,17 @@ unsigned bitarithm_bits_set(unsigned v)
return
c
;
}
#if !ARCH_32_BIT
uint8_t
bitarithm_bits_set_u32
(
uint32_t
v
)
{
uint8_t
c
;
for
(
c
=
0
;
v
;
c
++
)
{
v
&=
v
-
1
;
/* clear the least significant bit set */
}
return
c
;
}
#endif
const
uint8_t
MultiplyDeBruijnBitPosition
[
32
]
=
{
0
,
1
,
28
,
2
,
29
,
14
,
24
,
3
,
30
,
22
,
20
,
15
,
25
,
17
,
4
,
8
,
...
...
This diff is collapsed.
Click to expand it.
core/include/bitarithm.h
+
16
−
1
View file @
0ecaaf02
...
...
@@ -115,12 +115,27 @@ static inline unsigned bitarithm_lsb(unsigned v);
/**
* @brief Returns the number of bits set in a value
* @param[in] v Input value
* @param[in] v Input value
with platform-dependent word size
* @return Number of set bits
*
*/
unsigned
bitarithm_bits_set
(
unsigned
v
);
/**
* @brief Returns the (uint32_t version) number of bits set in a value
* @param[in] v Input value with 32 bit size
* @return Number of set bits
*
*/
#if ARCH_32_BIT
static
inline
uint8_t
bitarithm_bits_set_u32
(
uint32_t
v
)
{
return
bitarithm_bits_set
(
v
);
}
#else
uint8_t
bitarithm_bits_set_u32
(
uint32_t
v
);
#endif
/* implementations */
static
inline
unsigned
bitarithm_lsb
(
unsigned
v
)
...
...
This diff is collapsed.
Click to expand it.
tests/unittests/tests-core/tests-core-bitarithm.c
+
10
−
2
View file @
0ecaaf02
...
...
@@ -155,7 +155,8 @@ static void test_bitarithm_msb_limit(void)
static
void
test_bitarithm_msb_random
(
void
)
{
TEST_ASSERT_EQUAL_INT
(
4
,
bitarithm_msb
(
19
));
/* randomized by fair
dice roll ;-) */
* dice roll ;-)
*/
}
static
void
test_bitarithm_msb_16bit
(
void
)
...
...
@@ -208,7 +209,13 @@ static void test_bitarithm_bits_set_limit(void)
static
void
test_bitarithm_bits_set_random
(
void
)
{
TEST_ASSERT_EQUAL_INT
(
3
,
bitarithm_bits_set
(
7
));
/* randomized by fair
dice roll ;-) */
* dice roll ;-)
*/
}
static
void
test_bitarithm_bits_set_u32_random
(
void
)
{
TEST_ASSERT_EQUAL_INT
(
21
,
bitarithm_bits_set_u32
(
4072524027
));
/* Source: https://www.random.org/bytes */
}
Test
*
tests_core_bitarithm_tests
(
void
)
...
...
@@ -244,6 +251,7 @@ Test *tests_core_bitarithm_tests(void)
new_TestFixture
(
test_bitarithm_bits_set_one
),
new_TestFixture
(
test_bitarithm_bits_set_limit
),
new_TestFixture
(
test_bitarithm_bits_set_random
),
new_TestFixture
(
test_bitarithm_bits_set_u32_random
),
};
EMB_UNIT_TESTCALLER
(
core_bitarithm_tests
,
NULL
,
NULL
,
fixtures
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment