1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
#include <chrono>
#include <random>
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <intrin.h>
#pragma intrinsic(_BitScanReverse)
int prevent_optimization = 0;
template <typename T, T d, T M> __forceinline T floor_log_2_bisT(T n)
{
T tmp = (1 << d);
// int _d = d; int _res = M; // pour voir les params
if (M != 0) {
if (n < tmp) {
return floor_log_2_bisT<T, d - M, M / 2>(n);
}
else {
return floor_log_2_bisT<T, d + M, M / 2>(n);
}
}
else {
if (n < tmp) {
return d;
}
else {
return d + 1;
}
}
}
template <typename T> T _floor_log_2(T n) {
T res = floor_log_2_bisT<T, sizeof(T)* 4, sizeof(T)* 2>(n);
return res;
}
unsigned int floor_log_2_binary_unroll(unsigned int n) {
return _floor_log_2<unsigned int>(n);
}
int floor_log_2_binary_naive(unsigned int n) {
int nb_bit_for_type = sizeof(int)* 8;
int M = nb_bit_for_type / 4;
int d = nb_bit_for_type / 2;
int res = 0;
while (M) {
int tmp = 1 << d;
if (n < tmp) {
d -= M;
}
else {
d += M;
}
M = M / 2;
}
int tmp = 1 << d;
if (n >= tmp) {
d += 1;
}
return d;
}
int floor_log_2_naive(unsigned int t)
{
int r = 1;
while (t >>= 1)
{
r++;
}
return r;
}
int floor_log_2_intrinsic(unsigned int t)
{
if (t == 0)
return 1;
unsigned long r;
_BitScanReverse(&r, t);
return r + 1;
}
int floor_log_2_intrinsic_noz(unsigned int t)
{
// give random result if t == 0
unsigned long r = 0;
_BitScanReverse(&r, t);
return r + 1;
}
template <class Rep, class Period>
int to_ms(const std::chrono::duration<Rep, Period>& d)
{
return std::chrono::duration_cast<std::chrono::milliseconds>(d).count();
}
template <typename Func>
void Do(const std::string& desc, const std::vector<int>& v, Func f)
{
std::chrono::system_clock clock;
int res = 0;
auto start = clock.now();
for (int i : v)
{
res += f(i);
}
auto end = clock.now();
prevent_optimization += res;
std::cout << std::left << std::setw(25) << desc;
std::cout << " : " << to_ms(end - start) << " ms" << std::endl;
}
int pick_a_number(int min, int max)
{
static std::default_random_engine rng;
std::uniform_int_distribution<> d(min, max);
return d(rng);
}
void test(int nbint, int min, int max)
{
std::cout << min << " " << max << std::endl;
std::vector<int> v;
for (int i = 0; i < nbint; i++)
{
v.push_back(pick_a_number(min, max));
}
Do("floor_log_2_naive", v, &floor_log_2_naive);
Do("floor_log_2_binary_naive", v, &floor_log_2_binary_naive);
Do("floor_log_2_binary_unroll", v, &floor_log_2_binary_unroll);
Do("floor_log_2_intrinsic", v, &floor_log_2_intrinsic);
Do("floor_log_2_intrinsic_noz", v, &floor_log_2_intrinsic_noz);
std::cout << std::endl;
}
int main()
{
int nbitem = 100000000;
for (int i = 0; i < 31; i++)
test(nbitem, 0, 1 << i);
std::cout << prevent_optimization;
return 0;
} |
Partager