Debian Codegraph @

src/openmpt/soundbase/SampleClipFixedPoint.hpp

raw
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* SPDX-FileCopyrightText: OpenMPT Project Developers and Contributors */
3
4
5 #pragma once
6
7 #include "openmpt/all/BuildSettings.hpp"
8
9 #include "mpt/base/macros.hpp"
10
11
12 OPENMPT_NAMESPACE_BEGIN
13
14
15 namespace SC
16 { // SC = _S_ample_C_onversion
17
18
19 template <typename Tfixed, int fractionalBits, bool clipOutput>
20 struct ClipFixed
21 {
22 using input_t = Tfixed;
23 using output_t = Tfixed;
24 MPT_FORCEINLINE Tfixed operator()(Tfixed val)
25 {
26 static_assert(fractionalBits >= 0 && fractionalBits <= sizeof(output_t) * 8 - 1);
27 if constexpr(clipOutput)
28 {
29 constexpr Tfixed clip_max = (Tfixed(1) << fractionalBits) - Tfixed(1);
30 constexpr Tfixed clip_min = Tfixed(0) - (Tfixed(1) << fractionalBits);
31 if(val < clip_min) val = clip_min;
32 if(val > clip_max) val = clip_max;
33 return val;
34 } else
35 {
36 return val;
37 }
38 }
39 };
40
41
42 } // namespace SC
43
44
45 OPENMPT_NAMESPACE_END