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
|
/* Sjaak, a program for playing chess variants
* Copyright (C) 2011, 2014 Evert Glebbeek
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BITS32_H
#define BITS32_H
#include <stdint.h>
#include "bool.h"
#if defined _MSC_VER
# include <intrin.h>
#endif
static inline bool onebit32(uint32_t x)
{
return (x & (x-1)) == 0;
}
static inline int lsb32(uint32_t x) {
#ifdef __GNUC__
return __builtin_ctz (x);
#elif defined _MSC_VER
unsigned long res;
_BitScanForward(&res, x);
return (int)res;
#else
int n = 0;
assert(x);
while ((x&((uint32_t)1<<n)) == 0) n++;
return n;
#endif
}
static inline int msb32(uint32_t x) {
#ifdef __GNUC__
return 31 - __builtin_clz(x);
#elif defined _MSC_VER
unsigned long res;
_BitScanReverse(&res, x);
return (int)res;
#else
int n = 31;
assert(x);
while ((x&((uint32_t)1<<n)) == 0) n--;
return n;
#endif
}
static inline uint32_t sshift32(uint32_t x, int s)
{
signed char left = (signed char) s;
signed char right = -((signed char)(s >> 8) & left);
return (x >> right) << (right + left);
}
static inline int bitscan32(uint32_t x) {
#ifdef __GNUC__
return __builtin_ctz (x);
#elif defined _MSC_VER
unsigned long res;
_BitScanForward(&res, x);
return (int)res;
#else
int i = 0;
assert(x);
while (!(x & 1)) {
i++;
x >>= 1;
}
return i;
#endif
}
/* Return the number of bits set on a bitboard
* From http://chessprogramming.wikispaces.com/Population+Count
*/
static inline int popcount32(uint32_t x)
{
#ifdef __GNUC__
return __builtin_popcount(x);
#else
const uint32_t k1 = 0x55555555;
const uint32_t k2 = 0x33333333;
const uint32_t k4 = 0x0f0f0f0f;
const uint32_t kf = 0x01010101;
x = x - ((x >> 1) & k1);
x = (x & k2) + ((x >> 2) & k2);
x = (x + (x >> 4)) & k4;
x = (x * kf) >> 24;
return (int) x;
#endif
}
#endif
|