Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
Suyu
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
many-archive
Suyu
Commits
8dbfa4e1
There was an error fetching the commit references. Please try again later.
Commit
8dbfa4e1
authored
4 years ago
by
bunnei
Browse files
Options
Downloads
Patches
Plain Diff
common: Port BitSet from Mesosphere.
parent
e18ee8d6
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
src/common/CMakeLists.txt
+1
-0
1 addition, 0 deletions
src/common/CMakeLists.txt
src/common/bit_set.h
+100
-0
100 additions, 0 deletions
src/common/bit_set.h
with
101 additions
and
0 deletions
src/common/CMakeLists.txt
+
1
−
0
View file @
8dbfa4e1
...
...
@@ -104,6 +104,7 @@ add_library(common STATIC
detached_tasks.h
bit_cast.h
bit_field.h
bit_set.h
bit_util.h
cityhash.cpp
cityhash.h
...
...
This diff is collapsed.
Click to expand it.
src/common/bit_set.h
0 → 100644
+
100
−
0
View file @
8dbfa4e1
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include
"common/alignment.h"
#include
"common/bit_util.h"
#include
"common/common_types.h"
namespace
Common
{
namespace
impl
{
#define BITSIZEOF(x) (sizeof(x) * CHAR_BIT)
template
<
typename
Storage
,
size_t
N
>
class
BitSet
{
private:
static_assert
(
std
::
is_integral
<
Storage
>::
value
);
static_assert
(
std
::
is_unsigned
<
Storage
>::
value
);
static_assert
(
sizeof
(
Storage
)
<=
sizeof
(
u64
));
static
constexpr
size_t
FlagsPerWord
=
BITSIZEOF
(
Storage
);
static
constexpr
size_t
NumWords
=
AlignUp
(
N
,
FlagsPerWord
)
/
FlagsPerWord
;
static
constexpr
auto
CountLeadingZeroImpl
(
Storage
word
)
{
return
CountLeadingZeroes64
(
static_cast
<
unsigned
long
long
>
(
word
))
-
(
BITSIZEOF
(
unsigned
long
long
)
-
FlagsPerWord
);
}
static
constexpr
Storage
GetBitMask
(
size_t
bit
)
{
return
Storage
(
1
)
<<
(
FlagsPerWord
-
1
-
bit
);
}
private
:
Storage
words
[
NumWords
];
public
:
constexpr
BitSet
()
:
words
()
{
/* ... */
}
constexpr
void
SetBit
(
size_t
i
)
{
this
->
words
[
i
/
FlagsPerWord
]
|=
GetBitMask
(
i
%
FlagsPerWord
);
}
constexpr
void
ClearBit
(
size_t
i
)
{
this
->
words
[
i
/
FlagsPerWord
]
&=
~
GetBitMask
(
i
%
FlagsPerWord
);
}
constexpr
size_t
CountLeadingZero
()
const
{
for
(
size_t
i
=
0
;
i
<
NumWords
;
i
++
)
{
if
(
this
->
words
[
i
])
{
return
FlagsPerWord
*
i
+
CountLeadingZeroImpl
(
this
->
words
[
i
]);
}
}
return
FlagsPerWord
*
NumWords
;
}
constexpr
size_t
GetNextSet
(
size_t
n
)
const
{
for
(
size_t
i
=
(
n
+
1
)
/
FlagsPerWord
;
i
<
NumWords
;
i
++
)
{
Storage
word
=
this
->
words
[
i
];
if
(
!
IsAligned
(
n
+
1
,
FlagsPerWord
))
{
word
&=
GetBitMask
(
n
%
FlagsPerWord
)
-
1
;
}
if
(
word
)
{
return
FlagsPerWord
*
i
+
CountLeadingZeroImpl
(
word
);
}
}
return
FlagsPerWord
*
NumWords
;
}
};
}
// namespace impl
template
<
size_t
N
>
using
BitSet8
=
impl
::
BitSet
<
u8
,
N
>
;
template
<
size_t
N
>
using
BitSet16
=
impl
::
BitSet
<
u16
,
N
>
;
template
<
size_t
N
>
using
BitSet32
=
impl
::
BitSet
<
u32
,
N
>
;
template
<
size_t
N
>
using
BitSet64
=
impl
::
BitSet
<
u64
,
N
>
;
}
// namespace Common
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