File: intrinsics.hpp

package info (click to toggle)
iqtree 2.0.7%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,700 kB
  • sloc: cpp: 142,571; ansic: 57,789; sh: 275; python: 242; makefile: 95
file content (65 lines) | stat: -rw-r--r-- 1,260 bytes parent folder | download | duplicates (2)
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
#include <terraces/definitions.hpp>

#ifdef _WIN64
#pragma intrinsic(_BitScanForward64, _BitScanReverse64, __popcnt64)
#else
#pragma intrinsic(_BitScanForward, _BitScanReverse, __popcnt)
#endif

namespace terraces {
namespace bits {

#ifdef _WIN64
inline index popcount(index word) { return (index)__popcnt64(word); }

inline index bitscan(index word) {
	unsigned long idx;
	_BitScanForward64(&idx, word);
	return (index)idx;
}

inline index rbitscan(index word) {
	unsigned long idx;
	_BitScanReverse64(&idx, word);
	return (index)idx;
}
#else
inline index popcount(index word) { return index(__popcnt(word)); }

inline index bitscan(index word) {
	unsigned long idx;
	_BitScanForward(&idx, word);
	return index(idx);
}

inline index rbitscan(index word) {
	unsigned long idx;
	_BitScanReverse(&idx, word);
	return index(idx);
}
#endif

namespace {
constexpr index max_index = std::numeric_limits<index>::max();
}

inline bool add_overflow(index a, index b, index& result) {
	result = a + b;
	if (max_index - b < a) {
		return true;
	} else {
		return false;
	}
}

inline bool mul_overflow(index a, index b, index& result) {
	result = a * b;
	if (max_index / b < a) {
		return true;
	} else {
		return false;
	}
}

} // namespace bits
} // namespace terraces