Pixie
Loading...
Searching...
No Matches
cache_line.h
1#pragma once
2
3#include <array>
4#include <cstddef>
5#include <cstdint>
6#include <span>
7#include <vector>
8
12struct alignas(64) CacheLine {
13 std::array<std::byte, 64> data;
14};
15
23class AlignedStorage {
24 private:
25 std::vector<CacheLine> data_;
26
27 public:
28 AlignedStorage() = default;
33 AlignedStorage(size_t bits) : data_((bits + 511) / 512) {}
34
38 void resize(size_t bits) { data_.resize((bits + 511) / 512); }
40 std::span<CacheLine> AsLines() { return data_; }
42 std::span<const CacheLine> AsConstLines() const { return data_; }
43
47 std::span<uint64_t> As64BitInts() {
48 return std::span<uint64_t>(reinterpret_cast<uint64_t*>(data_.data()),
49 data_.size() * 8);
50 }
51
53 std::span<const uint64_t> AsConst64BitInts() const {
54 return std::span<const uint64_t>(
55 reinterpret_cast<const uint64_t*>(data_.data()), data_.size() * 8);
56 }
57
62 std::span<std::byte> AsBytes() {
63 return std::span<std::byte>(reinterpret_cast<std::byte*>(data_.data()),
64 data_.size() * 64);
65 }
66
68 std::span<const std::byte> AsConstBytes() const {
69 return std::span<const std::byte>(
70 reinterpret_cast<const std::byte*>(data_.data()), data_.size() * 64);
71 }
72
77 std::span<std::uint16_t> As16BitInts() {
78 return std::span<std::uint16_t>(
79 reinterpret_cast<std::uint16_t*>(data_.data()), data_.size() * 32);
80 }
81
83 std::span<const std::uint16_t> AsConst16BitInts() const {
84 return std::span<const std::uint16_t>(
85 reinterpret_cast<const std::uint16_t*>(data_.data()),
86 data_.size() * 32);
87 }
88};
std::span< const std::uint16_t > AsConst16BitInts() const
Const view as bytes.
Definition cache_line.h:83
void resize(size_t bits)
Resize storage to hold at least bits bits, rounded up to 512.
Definition cache_line.h:38
std::span< const std::byte > AsConstBytes() const
Const view as bytes.
Definition cache_line.h:68
std::span< const uint64_t > AsConst64BitInts() const
Const view as 64-bit words.
Definition cache_line.h:53
std::span< std::uint16_t > As16BitInts()
Mutable view as bytes.
Definition cache_line.h:77
std::span< CacheLine > AsLines()
Mutable view as cache lines.
Definition cache_line.h:40
AlignedStorage(size_t bits)
Construct storage for at least bits bytes, rounded up to 512 bits.
Definition cache_line.h:33
std::span< std::byte > AsBytes()
Mutable view as bytes.
Definition cache_line.h:62
std::span< const CacheLine > AsConstLines() const
Const view as cache lines.
Definition cache_line.h:42
std::span< uint64_t > As64BitInts()
Mutable view as 64-bit words.
Definition cache_line.h:47
A simple struct to represent a aligned storage for a cache line.
Definition cache_line.h:12