blob: 5456a6316850f56cf1d5818d8f64f1487638980c (
plain)
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
|
#ifndef TENSORFLOW_LIB_CORE_BITS_H_
#define TENSORFLOW_LIB_CORE_BITS_H_
#include "tensorflow/core/platform/port.h"
namespace tensorflow {
// Return floor(log2(n)) for positive integer n. Returns -1 iff n == 0.
int Log2Floor(uint32 n);
int Log2Floor64(uint64 n);
// Return ceiling(log2(n)) for positive integer n. Returns -1 iff n == 0.
int Log2Ceiling(uint32 n);
int Log2Ceiling64(uint64 n);
// ------------------------------------------------------------------------
// Implementation details follow
// ------------------------------------------------------------------------
#if defined(__GNUC__)
// Return floor(log2(n)) for positive integer n. Returns -1 iff n == 0.
inline int Log2Floor(uint32 n) {
return n == 0 ? -1 : 31 ^ __builtin_clz(n);
}
// Return floor(log2(n)) for positive integer n. Returns -1 iff n == 0.
inline int Log2Floor64(uint64 n) {
return n == 0 ? -1 : 63 ^ __builtin_clzll(n);
}
#else
// Return floor(log2(n)) for positive integer n. Returns -1 iff n == 0.
inline int Log2Floor(uint32 n) {
if (n == 0)
return -1;
int log = 0;
uint32 value = n;
for (int i = 4; i >= 0; --i) {
int shift = (1 << i);
uint32 x = value >> shift;
if (x != 0) {
value = x;
log += shift;
}
}
assert(value == 1);
return log;
}
// Return floor(log2(n)) for positive integer n. Returns -1 iff n == 0.
// Log2Floor64() is defined in terms of Log2Floor32()
inline int Log2Floor64(uint64 n) {
const uint32 topbits = static_cast<uint32>(n >> 32);
if (topbits == 0) {
// Top bits are zero, so scan in bottom bits
return Log2Floor(static_cast<uint32>(n));
} else {
return 32 + Log2Floor(topbits);
}
}
#endif
inline int Log2Ceiling(uint32 n) {
int floor = Log2Floor(n);
if (n == (n & ~(n - 1))) // zero or a power of two
return floor;
else
return floor + 1;
}
inline int Log2Ceiling64(uint64 n) {
int floor = Log2Floor64(n);
if (n == (n & ~(n - 1))) // zero or a power of two
return floor;
else
return floor + 1;
}
} // namespace tensorflow
#endif // TENSORFLOW_LIB_CORE_BITS_H_
|