nngn
Loading...
Searching...
No Matches
stats.h
Go to the documentation of this file.
1#ifndef NNGN_TIMING_STATS_H
2#define NNGN_TIMING_STATS_H
3
4#include <array>
5#include <bitset>
6#include <cassert>
7#include <type_traits>
8
9#include "utils/def.h"
10#include "utils/pointer_flag.h"
11
12namespace nngn {
13
14template<typename CRTP, std::size_t N>
15struct StatsBase {
16 static constexpr auto N_EVENTS = N;
17 static constexpr std::size_t size(void);
18 auto *to_u64_array(void) const;
19 auto *to_u64_array(void);
20};
21
22class Stats {
23 static constexpr std::size_t MAX = 2;
24 static std::array<pointer_flag<void>, MAX> v;
25 static std::size_t check(std::size_t i);
26public:
27 static void reserve(std::size_t i, void *p);
28 static void release(std::size_t i) { v[check(i)] = {}; }
29 static bool active(std::size_t i) { assert(i < MAX); return v[i].flag(); }
30 static void set_active(std::size_t i, bool a) { v[check(i)].set_flag(a); }
31 static void *data(std::size_t i) { return v[check(i)].get(); }
32 template<typename T> static auto *u64_data(void);
33};
34
35template<typename CRTP, std::size_t N>
36constexpr std::size_t StatsBase<CRTP, N>::size(void) {
37 return CRTP::names.size() * N_EVENTS;
38}
39
40template<typename CRTP, std::size_t N>
42 constexpr auto n = CRTP::size();
43 static_assert(n == sizeof(CRTP) / sizeof(u64));
44 using T = const std::array<u64, n>*;
45 return reinterpret_cast<T>(static_cast<const CRTP&>(*this).to_u64());
46}
47
48template<typename CRTP, std::size_t N>
50 auto &ret = *static_cast<const StatsBase*>(this)->to_u64_array();
51 return &const_cast<std::decay_t<decltype(ret)>&>(ret);
52}
53
54inline std::size_t Stats::check(std::size_t i) {
55 assert(i < MAX);
56 assert(v[i].get());
57 return i;
58}
59
60inline void Stats::reserve(std::size_t i, void *p) {
61 assert(i < MAX);
62 assert(!v[i].get());
63 v[i] = pointer_flag(p);
64}
65
66template<typename T>
67inline auto *Stats::u64_data() {
68 using S = typename T::Stats;
69 constexpr auto I = T::STATS_IDX;
70 return static_cast<S*>(Stats::data(I))->to_u64_array();
71}
72
73}
74
75#endif
Definition stats.h:22
static constexpr std::size_t MAX
Definition stats.h:23
static bool active(std::size_t i)
Definition stats.h:29
static void * data(std::size_t i)
Definition stats.h:31
static std::size_t check(std::size_t i)
Definition stats.h:54
static void release(std::size_t i)
Definition stats.h:28
static std::array< pointer_flag< void >, MAX > v
Definition stats.h:24
static auto * u64_data(void)
Definition stats.h:67
static void reserve(std::size_t i, void *p)
Definition stats.h:60
static void set_active(std::size_t i, bool a)
Definition stats.h:30
Definition pointer_flag.h:9
assert
Definition debug.lua:3
for i
Definition font.lua:5
local n
Definition dump_lights.lua:5
#define T(f0, f1, f2)
local N
Definition gamma.lua:5
Definition audio.cpp:7
std::uint64_t u64
Definition def.h:15
get
Definition camera.lua:46
local a
Definition input.lua:35
Definition stats.h:15
static constexpr auto N_EVENTS
Definition stats.h:16
auto * to_u64_array(void)
Definition stats.h:49
auto * to_u64_array(void) const
Definition stats.h:41
static constexpr std::size_t size(void)
Definition stats.h:36
#define S(v0, v1)
Definition vec2.h:16