Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
rt_work_counter.hpp
Go to the documentation of this file.
1#pragma once
2
41
42#include <atomic>
43#include <cmath>
44#include <complex>
45#include <cstdint>
46
47#ifndef PULP_RT_WORK_COUNTERS
48#define PULP_RT_WORK_COUNTERS 0
49#endif
50
52
56inline constexpr bool kWorkCountersEnabled = PULP_RT_WORK_COUNTERS != 0;
57
60 std::uint64_t fft = 0;
61 std::uint64_t trig = 0;
62 std::uint64_t bins = 0;
63
65 RtWorkCounts b) noexcept {
66 return {a.fft - b.fft, a.trig - b.trig, a.bins - b.bins};
67 }
69 RtWorkCounts b) noexcept {
70 return {a.fft + b.fft, a.trig + b.trig, a.bins + b.bins};
71 }
72 friend constexpr bool operator==(RtWorkCounts, RtWorkCounts) = default;
73};
74
75namespace detail {
76#if PULP_RT_WORK_COUNTERS
77inline std::atomic<std::uint64_t> g_fft_count{0};
78inline std::atomic<std::uint64_t> g_trig_count{0};
79inline std::atomic<std::uint64_t> g_bins_count{0};
80#endif
81} // namespace detail
82
84inline void count_fft() noexcept {
85#if PULP_RT_WORK_COUNTERS
86 detail::g_fft_count.fetch_add(1, std::memory_order_relaxed);
87#endif
88}
89
91inline void count_trig(std::uint64_t n = 1) noexcept {
92#if PULP_RT_WORK_COUNTERS
93 detail::g_trig_count.fetch_add(n, std::memory_order_relaxed);
94#else
95 (void)n;
96#endif
97}
98
100inline void count_bins(std::uint64_t n) noexcept {
101#if PULP_RT_WORK_COUNTERS
102 detail::g_bins_count.fetch_add(n, std::memory_order_relaxed);
103#else
104 (void)n;
105#endif
106}
107
110inline RtWorkCounts work_counts() noexcept {
111#if PULP_RT_WORK_COUNTERS
112 return {detail::g_fft_count.load(std::memory_order_relaxed),
113 detail::g_trig_count.load(std::memory_order_relaxed),
114 detail::g_bins_count.load(std::memory_order_relaxed)};
115#else
116 return {};
117#endif
118}
119
122public:
123 RtWorkCounter() noexcept : start_(work_counts()) {}
124 RtWorkCounts delta() const noexcept { return work_counts() - start_; }
125 void restart() noexcept { start_ = work_counts(); }
126
127private:
128 RtWorkCounts start_;
129};
130
132template <typename T>
133inline std::complex<T> polar(T magnitude, T phase) noexcept {
134 count_trig();
135 return std::polar(magnitude, phase);
136}
137
139template <typename T>
140inline T sin(T x) noexcept {
141 count_trig();
142 return std::sin(x);
143}
144
146template <typename T>
147inline T cos(T x) noexcept {
148 count_trig();
149 return std::cos(x);
150}
151
153template <typename T>
154inline T exp(T x) noexcept {
155 count_trig();
156 return std::exp(x);
157}
158
160template <typename T>
161inline T arg(const std::complex<T>& z) noexcept {
162 count_trig();
163 return std::arg(z);
164}
165
166} // namespace pulp::signal::rt
RtWorkCounter() noexcept
Definition rt_work_counter.hpp:123
void restart() noexcept
Definition rt_work_counter.hpp:125
RtWorkCounts delta() const noexcept
Definition rt_work_counter.hpp:124
Definition rt_work_counter.hpp:75
Definition rt_work_counter.hpp:51
RtWorkCounts work_counts() noexcept
Current process-wide totals.
Definition rt_work_counter.hpp:110
T cos(T x) noexcept
Counted std::cos. Bit-identical result.
Definition rt_work_counter.hpp:147
void count_trig(std::uint64_t n=1) noexcept
Record n transcendental evaluations.
Definition rt_work_counter.hpp:91
T exp(T x) noexcept
Counted std::exp. Bit-identical result.
Definition rt_work_counter.hpp:154
constexpr bool kWorkCountersEnabled
True when this build counts operations.
Definition rt_work_counter.hpp:56
std::complex< T > polar(T magnitude, T phase) noexcept
Counted std::polar. Bit-identical result.
Definition rt_work_counter.hpp:133
T sin(T x) noexcept
Counted std::sin. Bit-identical result.
Definition rt_work_counter.hpp:140
void count_fft() noexcept
Record one FFT execution.
Definition rt_work_counter.hpp:84
void count_bins(std::uint64_t n) noexcept
Record n units of per-bin accumulation work.
Definition rt_work_counter.hpp:100
T arg(const std::complex< T > &z) noexcept
Counted std::arg (an atan2). Bit-identical result.
Definition rt_work_counter.hpp:161
@ a
Definition formant_filter_bank.hpp:20
#define PULP_RT_WORK_COUNTERS
Definition rt_work_counter.hpp:48
A snapshot (or a difference of two snapshots) of the counted operations.
Definition rt_work_counter.hpp:59
std::uint64_t fft
Definition rt_work_counter.hpp:60
friend constexpr RtWorkCounts operator+(RtWorkCounts a, RtWorkCounts b) noexcept
Definition rt_work_counter.hpp:68
friend constexpr bool operator==(RtWorkCounts, RtWorkCounts)=default
std::uint64_t trig
Definition rt_work_counter.hpp:61
std::uint64_t bins
Definition rt_work_counter.hpp:62
friend constexpr RtWorkCounts operator-(RtWorkCounts a, RtWorkCounts b) noexcept
Definition rt_work_counter.hpp:64