-
Notifications
You must be signed in to change notification settings - Fork 9
/
Utilities.cpp
68 lines (57 loc) · 1.27 KB
/
Utilities.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
#include "Utilities.h"
#include <cassert>
#include <climits>
namespace utils {
// Greatest common divisor
int gcd(int a, int b) {
assert(b != 0 && "Attempted modulo by zero.");
int c;
while(1) {
c = a % b;
if (c == 0) return b;
a = b;
b = c;
}
}
int gcdu(unsigned a, unsigned b) {
assert(b != 0 && "Attempted modulo by zero.");
unsigned c;
while(1) {
c = a % b;
if (c == 0) return b;
a = b;
b = c;
}
}
unsigned gcdSafe(unsigned x, unsigned y) {
if (y == 0) {
return x == 0 ? 0 : 1;
} else {
return gcdu(x, y);
}
}
int lcm(int a, int b) {
if (a == 0 || b == 0) return 0;
return a*b/gcd(a,b);
}
// Number of trailing zeros, e.g. if x is 1101000 (base 2), returns 3
int tlz(unsigned x) {
if (x == 0) return CHAR_BIT * sizeof(x);
int r = 0;
x = (x ^ (x - 1)) >> 1; // Set x's trailing 0s to 1s and zero rest
for (; x; r++) x >>= 1;
return r;
}
int max(int x, int y) {
return x ^ ((x ^ y) & -(x < y));
}
unsigned umin(unsigned x, unsigned y) {
return x < y ? x : y;
}
unsigned umax(unsigned x, unsigned y) {
return x > y ? x : y;
}
int min(int x, int y) {
return y ^ ((x ^ y) & -(x < y));
}
} // End of the utils namespace