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#include <hikocpu/hikocpu.hpp>
11#include <iterator>
12#include <exception>
13
14hi_export_module(hikogui.random.dither);
15
16hi_export namespace hi::inline v1 {
17
29class dither {
30public:
31 dither(dither const &) = default;
32 dither(dither &&) = default;
33 dither &operator=(dither const &) = default;
34 dither &operator=(dither &&) = default;
35
41 dither(int num_bits) noexcept
42 {
43 hi_axiom(num_bits > 0);
44 auto maximum_value = static_cast<float>((1_uz << num_bits) - 1);
45
46 // The maximum value from the rectangular probability density function.
47 maximum_value *= 127.0f;
48
49 // Triangular probability density function is has twice the range.
50 maximum_value *= 2.0f;
51
52 _multiplier = f32x4::broadcast(1.0f / maximum_value);
53 }
54
58 f32x4 next() noexcept
59 {
60 hi_not_implemented();
61 return f32x4{};
62 //if (to_bool(++_counter & 1)) {
63 // auto const rand = _state.next<u64x2>();
64 // auto const spdf = i16x16{bit_cast<i8x16>(rand)};
65 // auto const [spdf1, spdf2] = spdf.split<int16_t>();
66 //
67 // _tpdf = spdf1 + spdf2;
68 // return f32x4{i32x4{_tpdf}} * _multiplier;
69 //} else {
70 // auto const second_tpdf = bit_cast<i16x8>(bit_cast<u64x2>(_tpdf).yx());
71 // return f32x4{i32x4{second_tpdf}} * _multiplier;
72 //}
73 }
74
80 [[nodiscard]] f32x4 next(f32x4 samples) noexcept
81 {
82 return samples + next();
83 }
84
90 [[nodiscard]] float next(float sample) noexcept
91 {
92 return get<0>(f32x4::broadcast(sample) + next());
93 }
94
95private:
96 f32x4 _multiplier = {};
97 i16x8 _tpdf = {};
98 xorshift128p _state = {};
99 unsigned char _counter = 0;
100};
101
102} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
An object that create dither values to add to samples before rounding.
Definition dither.hpp:29
dither(int num_bits) noexcept
Create a dither object.
Definition dither.hpp:41
f32x4 next() noexcept
Get 8 floating point number to add to a samples.
Definition dither.hpp:58
f32x4 next(f32x4 samples) noexcept
Add dither to the given samples.
Definition dither.hpp:80
float next(float sample) noexcept
Add dither to the given samples.
Definition dither.hpp:90