Skip to content

Commit 3d8fe01

Browse files
committed
OrcLib: Utils: add Round.h
1 parent 5373c24 commit 3d8fe01

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/OrcLib/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,7 @@ set(SRC_UTILITIES
833833
"Utils/MakeArray.h"
834834
"Utils/MetaPtr.h"
835835
"Utils/Result.h"
836+
"Utils/Round.h"
836837
"Utils/String.cpp"
837838
"Utils/String.h"
838839
"Utils/Time.cpp"

src/OrcLib/Utils/Round.h

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// SPDX-License-Identifier: LGPL-2.1-or-later
3+
//
4+
// Copyright © 2023 ANSSI. All Rights Reserved.
5+
//
6+
// Author(s): fabienfl (ANSSI)
7+
//
8+
#pragma once
9+
10+
#include <type_traits>
11+
12+
namespace Orc {
13+
14+
template <typename T>
15+
inline std::enable_if_t<std::is_unsigned_v<T>, T> RoundUp(T value, size_t multiple)
16+
{
17+
if (multiple == 0)
18+
{
19+
return value;
20+
}
21+
22+
const T remainder = value % multiple;
23+
if (remainder == 0)
24+
{
25+
return value;
26+
}
27+
28+
return value + multiple - remainder;
29+
}
30+
31+
// Faster version for power of 2 multiples
32+
template <typename T>
33+
constexpr T RoundUpPow2(T value, size_t multiple)
34+
{
35+
return ((value - 1u) & ~(static_cast<T>(multiple) - 1u)) + multiple;
36+
}
37+
38+
} // namespace Orc

0 commit comments

Comments
 (0)