-
Notifications
You must be signed in to change notification settings - Fork 0
/
BranchFreeTimeoutNew.cpp
169 lines (123 loc) · 3.79 KB
/
BranchFreeTimeoutNew.cpp
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
164
165
166
167
168
169
/**
* Simple comparison of algorithms with conditions and branch-free algorithms.
*
* THE PROGRAM IS RELEASED UNDER THE WTF PUBLIC LICENSE
* http://www.wtfpl.net/txt/copying/
*
* Functions to compete: sign, abs, min and max (signed and unsigned).
* Suffix '0' - Functions with condition.
* Suffix '1' - Branch free functions.
*
* Compilation is correct in
* - VC++ 2015
* - GCC 4.8.1 (From MinGW).
*
* Artem M. Karavaev, 13.04.2016 (http://zealcomputing.ru)
* ````````````````` `````````` ```````````````````````
*/
#include <iostream>
#include <iomanip>
#include <chrono>
#include <cstdint>
// Testing function 'F' with time duration variable 'D'
#define TEST(F, D) \
begin = get_now(); \
a = 0; \
do { \
a = 19993*a+1; \
s+=(u32)F; \
} while (a != 0); \
end = get_now(); \
auto D = end - begin;
// Output working time of -0's and -1's functions
#define OUT(S, N) output_vs (S, N##0_time - empty, N##1_time - empty);
typedef int32_t i32;
typedef uint32_t u32;
typedef int8_t sign_t;
const u32 SHIFT = 31;
using namespace std;
// Some helpers
static chrono::high_resolution_clock::time_point get_now () {
return chrono::high_resolution_clock::now();
}
static void output_vs (const char * str, const chrono::high_resolution_clock::duration &duration0, const chrono::high_resolution_clock::duration &duration1) {
cout << str << ": " << fixed << setprecision(2) << chrono::duration_cast <chrono::duration<double>> (duration0).count() << " vs "
<< fixed << setprecision(2) << chrono::duration_cast <chrono::duration<double>> (duration1).count() << endl;
}
// Sign functions
static sign_t sign0 (i32 a) {
if (a>0) return +1;
if (a<0) return -1;
return 0;
}
static sign_t sign1 (i32 a) {
return (a >> SHIFT) | ((u32)(-a) >> SHIFT);
}
// Abs functions
static u32 abs0 (i32 a) {
if (a<0) return -a;
return a;
}
static u32 abs1 (i32 a) {
const i32 b = a >> SHIFT;
return (a+b) ^ b;
}
// Max/min functions
static i32 mini0 (i32 a, i32 b) {
return a>b ? b:a;
}
static i32 maxi0 (i32 a, i32 b) {
return a<b ? b:a;
}
static i32 mini1 (i32 a, i32 b) {
i32 d = a-b;
return a - (d&(~(d^((a^b)&(d^a))) >> SHIFT));
}
static i32 maxi1 (i32 a, i32 b) {
i32 d = a-b;
return b + (d&(~(d^((a^b)&(d^a))) >> SHIFT));
}
static u32 minu0 (u32 a, u32 b) {
return a>b ? b:a;
}
static u32 maxu0 (u32 a, u32 b) {
return a<b ? b:a;
}
static u32 minu1 (u32 a, u32 b) {
u32 d = a-b;
return a - (d&~(int((~a&b)|(~(a^b)&d)) >> SHIFT));
}
static u32 maxu1 (u32 a, u32 b) {
u32 d = a-b;
return b + (d&~(int((~a&b)|(~(a^b)&d)) >> SHIFT));
}
int main() {
u32 a, s = 0;
chrono::high_resolution_clock::time_point begin, end;
// Empty loop
TEST (a, empty)
// Testing the functions
TEST (sign0(i32(a)), sign0_time)
TEST (sign1(i32(a)), sign1_time)
TEST (abs0(i32(a)), abs0_time)
TEST (abs1(i32(a)), abs1_time)
TEST (mini0(i32(a), i32(~a)), mini0_time)
TEST (maxi0(i32(a), i32(~a)), maxi0_time)
TEST (mini1(i32(a), i32(~a)), mini1_time)
TEST (maxi1(i32(a), i32(~a)), maxi1_time)
TEST (minu0(u32(a), u32(~a)), minu0_time)
TEST (maxu0(u32(a), u32(~a)), maxu0_time)
TEST (minu1(u32(a), u32(~a)), minu1_time)
TEST (maxu1(u32(a), u32(~a)), maxu1_time)
// Output the result
OUT ("sign", sign)
OUT (" abs", abs)
OUT ("mini", mini)
OUT ("maxi", maxi)
OUT ("minu", minu)
OUT ("maxu", maxu)
// Output 's' to avoid aggressive optimization (removing useless loops).
// ('volatile' is not a good way to achieve the same effect).
cerr << s << endl;
return 0;
}