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/utility.hpp"
9#include "../macros.hpp"
10
11
12
13namespace hi::inline v1 {
14
26class dither {
27public:
28 dither(dither const &) = default;
29 dither(dither &&) = default;
30 dither &operator=(dither const &) = default;
31 dither &operator=(dither &&) = default;
32
38 dither(int num_bits) noexcept
39 {
40 hi_axiom(num_bits > 0);
41 auto maximum_value = static_cast<float>((1_uz << num_bits) - 1);
42
43 // The maximum value from the rectangular probability density function.
44 maximum_value *= 127.0f;
45
46 // Triangular probability density function is has twice the range.
47 maximum_value *= 2.0f;
48
49 _multiplier = f32x4::broadcast(1.0f / maximum_value);
50 }
51
55 f32x4 next() noexcept
56 {
57 hi_not_implemented();
58 return f32x4{};
59 //if (to_bool(++_counter & 1)) {
60 // hilet rand = _state.next<u64x2>();
61 // hilet spdf = i16x16{bit_cast<i8x16>(rand)};
62 // hilet [spdf1, spdf2] = spdf.split<int16_t>();
63 //
64 // _tpdf = spdf1 + spdf2;
65 // return f32x4{i32x4{_tpdf}} * _multiplier;
66 //} else {
67 // hilet second_tpdf = bit_cast<i16x8>(bit_cast<u64x2>(_tpdf).yx());
68 // return f32x4{i32x4{second_tpdf}} * _multiplier;
69 //}
70 }
71
77 [[nodiscard]] f32x4 next(f32x4 samples) noexcept
78 {
79 return samples + next();
80 }
81
87 [[nodiscard]] float next(float sample) noexcept
88 {
89 return get<0>(f32x4::broadcast(sample) + next());
90 }
91
92private:
93 f32x4 _multiplier = {};
94 i16x8 _tpdf = {};
95 xorshift128p _state = {};
96 unsigned char _counter = 0;
97};
98
99} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm.hpp:16
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
An object that create dither values to add to samples before rounding.
Definition dither.hpp:26
dither(int num_bits) noexcept
Create a dither object.
Definition dither.hpp:38
f32x4 next() noexcept
Get 8 floating point number to add to a samples.
Definition dither.hpp:55
f32x4 next(f32x4 samples) noexcept
Add dither to the given samples.
Definition dither.hpp:77
float next(float sample) noexcept
Add dither to the given samples.
Definition dither.hpp:87