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
499c1812
Commit
499c1812
authored
7 years ago
by
Kaspar Schleiser
Browse files
Options
Downloads
Patches
Plain Diff
core/bitarithm: provide multiple implementations for bitarithm_lsb()
parent
ef947831
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
core/bitarithm.c
+8
-12
8 additions, 12 deletions
core/bitarithm.c
core/include/bitarithm.h
+34
-4
34 additions, 4 deletions
core/include/bitarithm.h
with
42 additions
and
16 deletions
core/bitarithm.c
+
8
−
12
View file @
499c1812
...
@@ -21,6 +21,8 @@
...
@@ -21,6 +21,8 @@
#include
<stdio.h>
#include
<stdio.h>
#include
"bitarithm.h"
unsigned
bitarithm_msb
(
unsigned
v
)
unsigned
bitarithm_msb
(
unsigned
v
)
{
{
register
unsigned
r
;
// result of log2(v) will go here
register
unsigned
r
;
// result of log2(v) will go here
...
@@ -43,19 +45,7 @@ unsigned bitarithm_msb(unsigned v)
...
@@ -43,19 +45,7 @@ unsigned bitarithm_msb(unsigned v)
return
r
;
return
r
;
}
}
/*---------------------------------------------------------------------------*/
unsigned
bitarithm_lsb
(
register
unsigned
v
)
{
register
unsigned
r
=
0
;
while
((
v
&
0x01
)
==
0
)
{
v
>>=
1
;
r
++
;
};
return
r
;
}
/*---------------------------------------------------------------------------*/
unsigned
bitarithm_bits_set
(
unsigned
v
)
unsigned
bitarithm_bits_set
(
unsigned
v
)
{
{
unsigned
c
;
// c accumulates the total bits set in v
unsigned
c
;
// c accumulates the total bits set in v
...
@@ -66,3 +56,9 @@ unsigned bitarithm_bits_set(unsigned v)
...
@@ -66,3 +56,9 @@ unsigned bitarithm_bits_set(unsigned v)
return
c
;
return
c
;
}
}
const
uint8_t
MultiplyDeBruijnBitPosition
[
32
]
=
{
0
,
1
,
28
,
2
,
29
,
14
,
24
,
3
,
30
,
22
,
20
,
15
,
25
,
17
,
4
,
8
,
31
,
27
,
13
,
23
,
21
,
19
,
16
,
7
,
26
,
12
,
18
,
6
,
11
,
5
,
10
,
9
};
This diff is collapsed.
Click to expand it.
core/include/bitarithm.h
+
34
−
4
View file @
499c1812
/*
/*
* Copyright (C) 2014 Freie Universität Berlin
* Copyright (C) 2017 Kaspar Schleiser <kaspar@schleiser.de>
* 2014 Freie Universität Berlin
*
*
* This file is subject to the terms and conditions of the GNU Lesser
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* General Public License v2.1. See the file LICENSE in the top level
...
@@ -20,6 +21,10 @@
...
@@ -20,6 +21,10 @@
#ifndef BITARITHM_H
#ifndef BITARITHM_H
#define BITARITHM_H
#define BITARITHM_H
#include
<stdint.h>
#include
"cpu_conf.h"
#ifdef __cplusplus
#ifdef __cplusplus
extern
"C"
{
extern
"C"
{
#endif
#endif
...
@@ -105,19 +110,44 @@ unsigned bitarithm_msb(unsigned v);
...
@@ -105,19 +110,44 @@ unsigned bitarithm_msb(unsigned v);
* function will produce an infinite loop
* function will produce an infinite loop
* @return Bit Number
* @return Bit Number
*
*
* Source: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious
*/
*/
unsigned
bitarithm_lsb
(
register
unsigned
v
);
static
inline
unsigned
bitarithm_lsb
(
unsigned
v
);
/**
/**
* @brief Returns the number of bits set in a value
* @brief Returns the number of bits set in a value
* @param[in] v Input value
* @param[in] v Input value
* @return Number of set bits
* @return Number of set bits
*
*
* Source: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious
*/
*/
unsigned
bitarithm_bits_set
(
unsigned
v
);
unsigned
bitarithm_bits_set
(
unsigned
v
);
/* implementations */
static
inline
unsigned
bitarithm_lsb
(
unsigned
v
)
#if defined(BITARITHM_LSB_BUILTIN)
{
return
__builtin_ffs
(
v
)
-
1
;
}
#elif defined(BITARITHM_LSB_LOOKUP)
{
/* Source: http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightMultLookup */
extern
const
uint8_t
MultiplyDeBruijnBitPosition
[
32
];
return
MultiplyDeBruijnBitPosition
[((
uint32_t
)((
v
&
-
v
)
*
0x077CB531U
))
>>
27
];
}
#else
{
/* Source: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious */
unsigned
r
=
0
;
while
((
v
&
0x01
)
==
0
)
{
v
>>=
1
;
r
++
;
};
return
r
;
}
#endif
#ifdef __cplusplus
#ifdef __cplusplus
}
}
#endif
#endif
...
...
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