Initial import of libdpf.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
commit
e4e666f459
4563 changed files with 1690372 additions and 0 deletions
75
include/dpf/packed_lane.hpp
Normal file
75
include/dpf/packed_lane.hpp
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
/// @file dpf/packed_lane.hpp
|
||||
/// @brief Extract and deposit one `dpf::bit`, `dpf::twobit`, or `dpf::nyble`
|
||||
/// lane in a leaf node. Lane 0 is the low bits of the first byte.
|
||||
|
||||
#ifndef LIBDPF_INCLUDE_DPF_PACKED_LANE_HPP__
|
||||
#define LIBDPF_INCLUDE_DPF_PACKED_LANE_HPP__
|
||||
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
#include "hedley/hedley.h"
|
||||
|
||||
#include "dpf/bit.hpp"
|
||||
#include "dpf/nyble.hpp"
|
||||
#include "dpf/twobit.hpp"
|
||||
|
||||
namespace dpf
|
||||
{
|
||||
namespace packed
|
||||
{
|
||||
|
||||
template <typename LaneT, typename LeafT>
|
||||
HEDLEY_ALWAYS_INLINE
|
||||
HEDLEY_NO_THROW
|
||||
HEDLEY_PURE
|
||||
LaneT extract_lane(const LeafT & leaf, std::size_t lane) noexcept
|
||||
{
|
||||
constexpr unsigned bits = utils::packed_lane_bits_v<LaneT>;
|
||||
static_assert(bits == 1 || bits == 2 || bits == 4,
|
||||
"packed lane width must be 1, 2, or 4");
|
||||
constexpr unsigned mask = (1u << bits) - 1u;
|
||||
const auto * bytes = reinterpret_cast<const unsigned char *>(
|
||||
std::addressof(leaf));
|
||||
const std::size_t bit = lane * bits;
|
||||
assert(bit / 8u < sizeof(LeafT));
|
||||
const unsigned shift = static_cast<unsigned>(bit % 8u);
|
||||
const unsigned val = (bytes[bit / 8u] >> shift) & mask;
|
||||
return static_cast<LaneT>(val);
|
||||
}
|
||||
|
||||
/// @brief zero `out` is the caller's job; this writes one lane and leaves
|
||||
/// every other lane untouched.
|
||||
template <typename LaneT, typename LeafT>
|
||||
HEDLEY_ALWAYS_INLINE
|
||||
HEDLEY_NO_THROW
|
||||
void deposit_lane(LeafT & leaf, std::size_t lane, LaneT value) noexcept
|
||||
{
|
||||
constexpr unsigned bits = utils::packed_lane_bits_v<LaneT>;
|
||||
static_assert(bits == 1 || bits == 2 || bits == 4,
|
||||
"packed lane width must be 1, 2, or 4");
|
||||
constexpr unsigned mask = (1u << bits) - 1u;
|
||||
auto * bytes = reinterpret_cast<unsigned char *>(std::addressof(leaf));
|
||||
const std::size_t bit = lane * bits;
|
||||
assert(bit / 8u < sizeof(LeafT));
|
||||
const unsigned shift = static_cast<unsigned>(bit % 8u);
|
||||
const unsigned val = static_cast<unsigned>(value) & mask;
|
||||
unsigned char & cell = bytes[bit / 8u];
|
||||
cell = static_cast<unsigned char>((cell & ~(mask << shift)) | (val << shift));
|
||||
}
|
||||
|
||||
template <typename NodeT, typename LaneT>
|
||||
HEDLEY_ALWAYS_INLINE
|
||||
HEDLEY_NO_THROW
|
||||
NodeT make_lane_node(std::size_t lane, LaneT value) noexcept
|
||||
{
|
||||
NodeT node{};
|
||||
deposit_lane<LaneT>(node, lane, value);
|
||||
return node;
|
||||
}
|
||||
|
||||
} // namespace packed
|
||||
} // namespace dpf
|
||||
|
||||
#endif // LIBDPF_INCLUDE_DPF_PACKED_LANE_HPP__
|
||||
Loading…
Add table
Add a link
Reference in a new issue