Diligent Engine  v.2.4.g
Align.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019-2021 Diligent Graphics LLC
3  * Copyright 2015-2019 Egor Yusov
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * In no event and under no legal theory, whether in tort (including negligence),
18  * contract, or otherwise, unless required by applicable law (such as deliberate
19  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
20  * liable for any damages, including any direct, indirect, special, incidental,
21  * or consequential damages of any character arising as a result of this License or
22  * out of the use or inability to use the software (including but not limited to damages
23  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
24  * all other commercial damages or losses), even if such Contributor has been advised
25  * of the possibility of such damages.
26  */
27 
28 #pragma once
29 
32 
33 #include <cstdint>
34 
35 #include "../../Platforms/Basic/interface/DebugUtilities.hpp"
36 
37 namespace Diligent
38 {
39 
40 template <typename T>
41 bool IsPowerOfTwo(T val)
42 {
43  return val > 0 && (val & (val - 1)) == 0;
44 }
45 
46 template <typename T1, typename T2>
47 inline typename std::conditional<sizeof(T1) >= sizeof(T2), T1, T2>::type AlignUp(T1 val, T2 alignment)
48 {
49  static_assert(std::is_unsigned<T1>::value == std::is_unsigned<T2>::value, "both types must be signed or unsigned");
50  static_assert(!std::is_pointer<T1>::value && !std::is_pointer<T2>::value, "types must not be pointers");
51  VERIFY(IsPowerOfTwo(alignment), "Alignment (", alignment, ") must be power of 2");
52 
53  using T = typename std::conditional<sizeof(T1) >= sizeof(T2), T1, T2>::type;
54  return (static_cast<T>(val) + static_cast<T>(alignment - 1)) & ~static_cast<T>(alignment - 1);
55 }
56 
57 template <typename PtrType, typename AlignType>
58 inline PtrType* AlignUp(PtrType* val, AlignType alignment)
59 {
60  return reinterpret_cast<PtrType*>(AlignUp(reinterpret_cast<uintptr_t>(val), static_cast<uintptr_t>(alignment)));
61 }
62 
63 template <typename T1, typename T2>
64 inline typename std::conditional<sizeof(T1) >= sizeof(T2), T1, T2>::type AlignDown(T1 val, T2 alignment)
65 {
66  static_assert(std::is_unsigned<T1>::value == std::is_unsigned<T2>::value, "both types must be signed or unsigned");
67  static_assert(!std::is_pointer<T1>::value && !std::is_pointer<T2>::value, "types must not be pointers");
68  VERIFY(IsPowerOfTwo(alignment), "Alignment (", alignment, ") must be power of 2");
69 
70  using T = typename std::conditional<sizeof(T1) >= sizeof(T2), T1, T2>::type;
71  return static_cast<T>(val) & ~static_cast<T>(alignment - 1);
72 }
73 
74 template <typename PtrType, typename AlignType>
75 inline PtrType* AlignDown(PtrType* val, AlignType alignment)
76 {
77  return reinterpret_cast<PtrType*>(AlignDown(reinterpret_cast<uintptr_t>(val), static_cast<uintptr_t>(alignment)));
78 }
79 
80 } // namespace Diligent
Diligent::T1
T1
Definition: Align.hpp:47
Diligent::IsPowerOfTwo
bool IsPowerOfTwo(T val)
Definition: Align.hpp:41
VERIFY
#define VERIFY(...)
Definition: DebugUtilities.hpp:76
Diligent::AlignUp
T2 ::type AlignUp(T1 val, T2 alignment)
Definition: Align.hpp:47
Diligent
The library uses Direct3D-style math:
Definition: AdvancedMath.hpp:37
Diligent::AlignDown
T2 ::type AlignDown(T1 val, T2 alignment)
Definition: Align.hpp:64