2017-11-20 19:17:49 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-12-12 22:29:37 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
# include <intrin.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
# include <x86intrin.h>
|
|
|
|
#endif
|
|
|
|
|
2017-12-02 19:01:07 +00:00
|
|
|
namespace EightBit {
|
|
|
|
int countBits(uint8_t value);
|
|
|
|
bool oddParity(uint8_t value);
|
2017-12-12 22:29:37 +00:00
|
|
|
int findFirstSet(int value);
|
2018-03-10 01:53:57 +00:00
|
|
|
void assume(int expression);
|
2017-12-02 19:01:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline int EightBit::countBits(uint8_t value) {
|
2018-05-02 01:47:47 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
return __popcnt(value);
|
|
|
|
#else
|
|
|
|
/*
|
|
|
|
Published in 1988, the C Programming Language 2nd Ed.
|
|
|
|
(by Brian W.Kernighan and Dennis M.Ritchie) mentions
|
|
|
|
this in exercise 2 - 9. On April 19, 2006 Don Knuth pointed
|
|
|
|
out to me that this method "was first published by Peter
|
|
|
|
Wegner in CACM 3 (1960), 322.
|
|
|
|
(Also discovered independently by Derrick Lehmer and published
|
|
|
|
in 1964 in a book edited by Beckenbach.)"
|
|
|
|
*/
|
2017-12-02 19:01:07 +00:00
|
|
|
int count; // c accumulates the total bits set in value
|
|
|
|
for (count = 0; value; ++count)
|
|
|
|
value &= value - 1; // clear the least significant bit set
|
|
|
|
return count;
|
2018-05-02 01:47:47 +00:00
|
|
|
#endif
|
2017-12-02 19:01:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool EightBit::oddParity(uint8_t value) {
|
|
|
|
return countBits(value) % 2;
|
|
|
|
}
|
|
|
|
|
2017-12-12 22:29:37 +00:00
|
|
|
inline int EightBit::findFirstSet(int value) {
|
2017-11-20 19:17:49 +00:00
|
|
|
#ifdef _MSC_VER
|
2017-12-12 22:29:37 +00:00
|
|
|
unsigned long index;
|
|
|
|
if (_BitScanForward(&index, value))
|
|
|
|
return index + 1;
|
|
|
|
return 0;
|
|
|
|
#elif defined(__GNUG__)
|
|
|
|
return __builtin_ffs(value);
|
|
|
|
#else
|
|
|
|
# error Find first set not implemented
|
|
|
|
#endif
|
|
|
|
}
|
2017-11-20 19:17:49 +00:00
|
|
|
|
2018-03-10 01:53:57 +00:00
|
|
|
inline void EightBit::assume(int expression) {
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
__assume(expression);
|
|
|
|
#elif defined(__GNUG__)
|
|
|
|
if (!expression)
|
|
|
|
__builtin_unreachable();
|
|
|
|
#else
|
|
|
|
assert(expression);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ASSUME(x) EightBit::assume(x)
|
|
|
|
|
2017-12-12 22:29:37 +00:00
|
|
|
#ifdef _MSC_VER
|
2017-11-20 19:17:49 +00:00
|
|
|
|
|
|
|
# define LIKELY(x) (x)
|
|
|
|
# define UNLIKELY(x) (x)
|
|
|
|
|
2018-05-02 02:31:05 +00:00
|
|
|
# define PARITY(x) EightBit::oddParity(x)
|
2017-11-20 19:17:49 +00:00
|
|
|
|
2018-03-10 01:53:57 +00:00
|
|
|
# define UNREACHABLE ASSUME(0)
|
2017-11-20 19:17:49 +00:00
|
|
|
|
2017-11-20 20:54:17 +00:00
|
|
|
#elif defined(__GNUG__)
|
2017-11-20 19:17:49 +00:00
|
|
|
|
|
|
|
# define LIKELY(x) __builtin_expect(!!(x), 1)
|
|
|
|
# define UNLIKELY(x) __builtin_expect(!!(x), 0)
|
|
|
|
|
2017-12-12 23:12:45 +00:00
|
|
|
# define PARITY(x) __builtin_parity(x)
|
2017-11-20 19:17:49 +00:00
|
|
|
|
2018-03-10 01:53:57 +00:00
|
|
|
# define UNREACHABLE __builtin_unreachable();
|
2017-11-20 19:17:49 +00:00
|
|
|
|
|
|
|
#else
|
2017-12-02 19:01:07 +00:00
|
|
|
|
|
|
|
# define LIKELY(x) (x)
|
|
|
|
# define UNLIKELY(x) (x)
|
|
|
|
|
2017-12-12 22:29:37 +00:00
|
|
|
# define PARITY(x) EightBit::oddParity(x)
|
2017-12-02 19:01:07 +00:00
|
|
|
|
2018-03-10 01:53:57 +00:00
|
|
|
# define UNREACHABLE ASSUME(0)
|
2017-12-02 19:01:07 +00:00
|
|
|
|
2017-11-20 19:17:49 +00:00
|
|
|
#endif
|