-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrv_bits.h
39 lines (32 loc) · 983 Bytes
/
rv_bits.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#pragma once
#include <cstdint>
template<std::size_t l, std::size_t s, typename T = std::uint32_t>
struct rv_bitfield
{
using value_type = T;
static constexpr value_type mask = l << s;
static constexpr value_type shamt = s;
constexpr operator value_type() const { return mask; }
};
// & operator
template<std::size_t l, std::size_t s, typename T = std::uint32_t>
constexpr T operator &(T val, const rv_bitfield<l,s,T>& bitf)
{
return val & bitf.mask;
}
// | operator
template<std::size_t l, std::size_t s, typename T = std::uint32_t>
constexpr T operator |(T val, const rv_bitfield<l,s,T>& bitf)
{
return val | bitf.mask;
}
template<std::size_t l, std::size_t s, typename T = std::uint32_t>
constexpr T operator <<(T val, const rv_bitfield<l,s,T>& bitf)
{
return val << bitf.shamt;
}
template<std::size_t l, std::size_t s, typename T = std::uint32_t>
constexpr T operator >>(T val, const rv_bitfield<l,s,T>& bitf)
{
return val >> bitf.shamt;
}