Skip to content

Commit 2380d0a

Browse files
committed
[orc-rt] Add preliminary math.h header and basic operations.
The initial operations, isPowerOf2 and nextPowerOf2 will be used in an upcoming patch to add support for bitmask-enums.
1 parent 8a10fbb commit 2380d0a

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

orc-rt/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
set(ORC_RT_HEADERS
22
orc-rt-c/orc-rt.h
3+
orc-rt/math.h
34
orc-rt/span.h
45
)
56

orc-rt/include/orc-rt/math.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===--------- math.h - Math helpers for the ORC runtime --------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Math helper functions for the ORC runtime.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef ORC_RT_MATH_H
14+
#define ORC_RT_MATH_H
15+
16+
#include <cstdint>
17+
#include <limits>
18+
19+
namespace orc_rt {
20+
21+
/// Test whether the given value is a power of 2.
22+
template <typename T> constexpr bool isPowerOf2(T Val) noexcept {
23+
return Val != 0 && (Val & (Val - 1)) == 0;
24+
}
25+
26+
/// Calculates the next power of 2.
27+
template <typename T> constexpr T nextPowerOf2(T Val) noexcept {
28+
for (size_t I = 1; I < std::numeric_limits<T>::digits; I <<= 1)
29+
Val |= (Val >> I);
30+
return Val + 1;
31+
}
32+
33+
} // namespace orc_rt
34+
35+
#endif // ORC_RT_MATH_H

orc-rt/unittests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ function(add_orc_rt_unittest test_dirname)
1212
endfunction()
1313

1414
add_orc_rt_unittest(CoreTests
15+
math-test.cpp
1516
span-test.cpp
1617
DISABLE_LLVM_LINK_LLVM_DYLIB
1718
)

orc-rt/unittests/math-test.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//===- math-test.cpp ------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Tests for orc-rt's math.h APIs.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "orc-rt/math.h"
14+
#include "gtest/gtest.h"
15+
16+
using namespace orc_rt;
17+
18+
TEST(STLExtrasTest, isPowerOf2) {
19+
// Test [0..16]
20+
EXPECT_FALSE(isPowerOf2(0x00));
21+
EXPECT_TRUE(isPowerOf2(0x01));
22+
EXPECT_TRUE(isPowerOf2(0x02));
23+
EXPECT_FALSE(isPowerOf2(0x03));
24+
EXPECT_TRUE(isPowerOf2(0x04));
25+
EXPECT_FALSE(isPowerOf2(0x05));
26+
EXPECT_FALSE(isPowerOf2(0x06));
27+
EXPECT_FALSE(isPowerOf2(0x07));
28+
EXPECT_TRUE(isPowerOf2(0x08));
29+
EXPECT_FALSE(isPowerOf2(0x09));
30+
EXPECT_FALSE(isPowerOf2(0x0A));
31+
EXPECT_FALSE(isPowerOf2(0x0B));
32+
EXPECT_FALSE(isPowerOf2(0x0C));
33+
EXPECT_FALSE(isPowerOf2(0x0D));
34+
EXPECT_FALSE(isPowerOf2(0x0E));
35+
EXPECT_FALSE(isPowerOf2(0x0F));
36+
EXPECT_TRUE(isPowerOf2(0x10));
37+
38+
// Test some higher powers of two and their adjacent values.
39+
EXPECT_FALSE(isPowerOf2(0x1F));
40+
EXPECT_TRUE(isPowerOf2(0x20));
41+
EXPECT_FALSE(isPowerOf2(0x21));
42+
43+
EXPECT_FALSE(isPowerOf2(0x3F));
44+
EXPECT_TRUE(isPowerOf2(0x40));
45+
EXPECT_FALSE(isPowerOf2(0x41));
46+
47+
EXPECT_FALSE(isPowerOf2(0x7F));
48+
EXPECT_TRUE(isPowerOf2(0x80));
49+
EXPECT_FALSE(isPowerOf2(0x81));
50+
51+
// Test larger values.
52+
EXPECT_FALSE(isPowerOf2(0x3fffffff));
53+
EXPECT_TRUE(isPowerOf2(0x40000000));
54+
EXPECT_FALSE(isPowerOf2(0x40000001));
55+
56+
// Test negatives.
57+
EXPECT_FALSE(isPowerOf2(-1));
58+
}
59+
60+
TEST(STLExtrasTest, nextPowerOf2) {
61+
EXPECT_EQ(nextPowerOf2(0x00), (1 << 0));
62+
EXPECT_EQ(nextPowerOf2(0x01), (1 << 1));
63+
EXPECT_EQ(nextPowerOf2(0x02), (1 << 2));
64+
EXPECT_EQ(nextPowerOf2(0x03), (1 << 2));
65+
EXPECT_EQ(nextPowerOf2(0x04), (1 << 3));
66+
EXPECT_EQ(nextPowerOf2(0x05), (1 << 3));
67+
EXPECT_EQ(nextPowerOf2(0x06), (1 << 3));
68+
EXPECT_EQ(nextPowerOf2(0x07), (1 << 3));
69+
EXPECT_EQ(nextPowerOf2(0x08), (1 << 4));
70+
EXPECT_EQ(nextPowerOf2(0x09), (1 << 4));
71+
EXPECT_EQ(nextPowerOf2(0x0a), (1 << 4));
72+
EXPECT_EQ(nextPowerOf2(0x0b), (1 << 4));
73+
EXPECT_EQ(nextPowerOf2(0x0c), (1 << 4));
74+
EXPECT_EQ(nextPowerOf2(0x0d), (1 << 4));
75+
EXPECT_EQ(nextPowerOf2(0x0e), (1 << 4));
76+
EXPECT_EQ(nextPowerOf2(0x0f), (1 << 4));
77+
EXPECT_EQ(nextPowerOf2(0x10), (1 << 5));
78+
}

0 commit comments

Comments
 (0)