HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
dither.hpp
1// Copyright Take Vos 2021.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "xorshift128p.hpp"
8#include "../utility/module.hpp"
9
10namespace hi::inline v1 {
11
23class dither {
24public:
25 dither(dither const &) = default;
26 dither(dither &&) = default;
27 dither &operator=(dither const &) = default;
28 dither &operator=(dither &&) = default;
29
35 dither(int num_bits) noexcept
36 {
37 hi_axiom(num_bits > 0);
38 auto maximum_value = static_cast<float>((1_uz << num_bits) - 1);
39
40 // The maximum value from the rectangular probability density function.
41 maximum_value *= 127.0f;
42
43 // Triangular probability density function is has twice the range.
44 maximum_value *= 2.0f;
45
46 _multiplier = f32x4::broadcast(1.0f / maximum_value);
47 }
48
52 f32x4 next() noexcept
53 {
55 return f32x4{};
56 //if (to_bool(++_counter & 1)) {
57 // hilet rand = _state.next<u64x2>();
58 // hilet spdf = i16x16{bit_cast<i8x16>(rand)};
59 // hilet [spdf1, spdf2] = spdf.split<int16_t>();
60 //
61 // _tpdf = spdf1 + spdf2;
62 // return f32x4{i32x4{_tpdf}} * _multiplier;
63 //} else {
64 // hilet second_tpdf = bit_cast<i16x8>(bit_cast<u64x2>(_tpdf).yx());
65 // return f32x4{i32x4{second_tpdf}} * _multiplier;
66 //}
67 }
68
74 [[nodiscard]] f32x4 next(f32x4 samples) noexcept
75 {
76 return samples + next();
77 }
78
84 [[nodiscard]] float next(float sample) noexcept
85 {
86 return get<0>(f32x4::broadcast(sample) + next());
87 }
88
89private:
90 f32x4 _multiplier = {};
91 i16x8 _tpdf = {};
92 xorshift128p _state = {};
93 unsigned char _counter = 0;
94};
95
96} // namespace hi::inline v1
#define hi_not_implemented(...)
This part of the code has not been implemented yet.
Definition assert.hpp:335
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
DOXYGEN BUG.
Definition algorithm.hpp:13
An object that create dither values to add to samples before rounding.
Definition dither.hpp:23
dither(int num_bits) noexcept
Create a dither object.
Definition dither.hpp:35
f32x4 next() noexcept
Get 8 floating point number to add to a samples.
Definition dither.hpp:52
f32x4 next(f32x4 samples) noexcept
Add dither to the given samples.
Definition dither.hpp:74
float next(float sample) noexcept
Add dither to the given samples.
Definition dither.hpp:84