Add two implementations of round_up_to_next_power_of_two
David Blume

David Blume commited on 2019-01-24 20:54:16
Showing 5 changed files, with 82 additions and 4 deletions.

... ...
@@ -1 +1 @@
1
-*.exe
1
+testcode
... ...
@@ -6,7 +6,8 @@ LDFLAGS=-pthread -L $(LIBDIR) -lmy_lib
6 6
 OBJDIR=obj
7 7
 
8 8
 SOURCES=main.cpp main_helper.cpp ui/widget.cpp scoped_set_adder.cpp \
9
-	thread_with_lambda.cpp thread_manager.cpp deadlock/deadlock.cpp
9
+	thread_with_lambda.cpp thread_manager.cpp deadlock/deadlock.cpp \
10
+	next_power_of_two
10 11
 # //OBJECTS=$(OBJDIR)$(SOURCES:.cpp=.o)
11 12
 OBJECTS=$(foreach bname, $(basename $(SOURCES)), $(OBJDIR)/$(bname).o)
12 13
 EXECUTABLE=testcode
... ...
@@ -5,6 +5,7 @@
5 5
 #include <algorithm>
6 6
 #include <memory>
7 7
 #include <complex>
8
+#include <cassert>
8 9
 #include "main_helper.hpp"
9 10
 #include "scoped_set_adder.hpp"
10 11
 #include "ui/widget.hpp"
... ...
@@ -12,6 +13,7 @@
12 13
 #include "thread_with_lambda.hpp"
13 14
 #include "thread_manager.hpp"
14 15
 #include "deadlock/deadlock.hpp"
16
+#include "next_power_of_two.hpp"
15 17
 
16 18
 using namespace std;
17 19
 using namespace MY_LIB;
... ...
@@ -71,6 +73,16 @@ void test_cpp14() {
71 73
 #endif
72 74
 }
73 75
 
76
+struct Base {
77
+    virtual ~Base() {}
78
+    virtual void f() {}
79
+    int x_;
80
+};
81
+
82
+struct Derived: Base {
83
+    virtual void name() {}
84
+};
85
+
74 86
 int main() {
75 87
     length_of_array = 6;
76 88
     int a[6] = { 10, 20, 20, 30, 40, 40 };
... ...
@@ -78,6 +90,18 @@ int main() {
78 90
 
79 91
     // thread_with_lambda();
80 92
 
93
+    Base * pb = new Derived;
94
+    std::shared_ptr<Base> sp(pb);
95
+    cout << "pb is " << pb << endl;
96
+    cout << "sp is " << sp << endl;
97
+    Derived * pd = dynamic_cast<Derived *>(sp.get());
98
+    if (pd) {
99
+        cout << "pd exists " << endl;
100
+    } else {
101
+        cout << "pd is NULL " << endl;
102
+    }
103
+    cout << "pd is " << pd << endl;
104
+
81 105
     cout << "Removing " << item_to_remove << endl;
82 106
     length_of_array = remove_element(a, length_of_array, item_to_remove);
83 107
     cout << "a became " << a << endl;
... ...
@@ -128,8 +152,10 @@ int main() {
128 152
 
129 153
     // cout << "deadlock() returned " << deadlock() << endl;
130 154
 
131
-    // This_function_is_not_defined();
132
- 
155
+    for(unsigned i : {0, 1, 2, 3, 4, 5, 6, 7, 31, 32, 33, 4003, 65535, 65536, 65537}) {
156
+        cout << "next_power_of_two(" << i << ") = " << round_up_to_next_power_of_two(i) << endl;
157
+        assert(round_up_to_next_power_of_two(i) == another_round_up_to_next_power_of_two(i));
158
+    }
133 159
     cout << "Done." << endl;
134 160
     return 0;
135 161
 }
... ...
@@ -0,0 +1,24 @@
1
+#include <climits>
2
+#include "next_power_of_two.hpp"
3
+
4
+using namespace std;
5
+
6
+#ifndef USE_TEMPLATE
7
+
8
+unsigned round_up_to_next_power_of_two(unsigned x)
9
+{
10
+    // https://stackoverflow.com/questions/466204/rounding-up-to-next-power-of-2
11
+    --x;
12
+    for(size_t i = 1; i < sizeof(x) * CHAR_BIT; i *= 2)
13
+        x |= x >> i;
14
+    return ++x;
15
+}
16
+
17
+#endif
18
+
19
+unsigned another_round_up_to_next_power_of_two(unsigned x)
20
+{
21
+    // https://stackoverflow.com/questions/671815/what-is-the-fastest-most-efficient-way-to-find-the-highest-set-bit-msb-in-an-i
22
+    return (x > 1) ? 1 << (sizeof(x) * CHAR_BIT - __builtin_clz(x-1)) : x;
23
+}
24
+
... ...
@@ -0,0 +1,27 @@
1
+#pragma once
2
+
3
+#define USE_TEMPLATE
4
+#ifdef USE_TEMPLATE
5
+
6
+#include <cstddef>
7
+#include <type_traits>
8
+#include <climits>
9
+
10
+template <typename T>
11
+T round_up_to_next_power_of_two(T x)
12
+{
13
+    // https://stackoverflow.com/questions/466204/rounding-up-to-next-power-of-2
14
+     static_assert(std::is_unsigned<T>::value, "x must be an unsigned type");
15
+    --x;
16
+    for(size_t i = 1; i < sizeof(x) * CHAR_BIT; i *= 2)
17
+        x |= x >> i;
18
+    return ++x;
19
+}
20
+
21
+#else
22
+
23
+unsigned round_up_to_next_power_of_two(unsigned x);
24
+
25
+#endif
26
+
27
+unsigned another_round_up_to_next_power_of_two(unsigned x);
0 28