aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/byzantine-lab/mcl/include/cybozu/bit_operation.hpp
blob: 865c1e47d45c7eac5656e710bc3e43796038a3c0 (plain) (blame)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#pragma once
/**
    @file
    @brief bit operation
*/
#include <assert.h>
#include <cybozu/inttype.hpp>

#if (CYBOZU_HOST == CYBOZU_HOST_INTEL)
    #if defined(_WIN32)
        #include <intrin.h>
    #elif defined(__linux__) || defined(__CYGWIN__) || defined(__clang__)
        #include <x86intrin.h>
    #elif defined(__GNUC__)
        #include <emmintrin.h>
    #endif
#endif

namespace cybozu {

namespace bit_op_local {

template<bool equalTo8>
struct Tag {};

// sizeof(T) < 8
template<>
struct Tag<false> {
    template<class T>
    static inline int bsf(T x)
    {
#if defined(_MSC_VER)
        unsigned long out;
        _BitScanForward(&out, x);
#pragma warning(suppress: 6102)
        return out;
#else
        return __builtin_ctz(x);
#endif
    }
    template<class T>
    static inline int bsr(T x)
    {
#if defined(_MSC_VER)
        unsigned long out;
        _BitScanReverse(&out, x);
#pragma warning(suppress: 6102)
        return out;
#else
        return __builtin_clz(x) ^ 0x1f;
#endif
    }
};

// sizeof(T) == 8
template<>
struct Tag<true> {
    template<class T>
    static inline int bsf(T x)
    {
#if defined(_MSC_VER) && defined(_WIN64)
        unsigned long out;
        _BitScanForward64(&out, x);
#pragma warning(suppress: 6102)
        return out;
#elif defined(__x86_64__)
        return __builtin_ctzll(x);
#else
        const uint32_t L = uint32_t(x);
        if (L) return Tag<false>::bsf(L);
        const uint32_t H = uint32_t(x >> 32);
        return Tag<false>::bsf(H) + 32;
#endif
    }
    template<class T>
    static inline int bsr(T x)
    {
#if defined(_MSC_VER) && defined(_WIN64)
        unsigned long out;
        _BitScanReverse64(&out, x);
#pragma warning(suppress: 6102)
        return out;
#elif defined(__x86_64__)
        return __builtin_clzll(x) ^ 0x3f;
#else
        const uint32_t H = uint32_t(x >> 32);
        if (H) return Tag<false>::bsr(H) + 32;
        const uint32_t L = uint32_t(x);
        return Tag<false>::bsr(L);
#endif
    }
};

} // bit_op_local

template<class T>
int bsf(T x)
{
    return bit_op_local::Tag<sizeof(T) == 8>::bsf(x);
}
template<class T>
int bsr(T x)
{
    return bit_op_local::Tag<sizeof(T) == 8>::bsr(x);
}

template<class T>
uint64_t makeBitMask64(T x)
{
    assert(x < 64);
    return (uint64_t(1) << x) - 1;
}

template<class T>
uint32_t popcnt(T x);

template<>
inline uint32_t popcnt<uint32_t>(uint32_t x)
{
#if defined(_MSC_VER)
    return static_cast<uint32_t>(_mm_popcnt_u32(x));
#else
    return static_cast<uint32_t>(__builtin_popcount(x));
#endif
}

template<>
inline uint32_t popcnt<uint64_t>(uint64_t x)
{
#if defined(__x86_64__)
    return static_cast<uint32_t>(__builtin_popcountll(x));
#elif defined(_WIN64)
    return static_cast<uint32_t>(_mm_popcnt_u64(x));
#else
    return popcnt<uint32_t>(static_cast<uint32_t>(x)) + popcnt<uint32_t>(static_cast<uint32_t>(x >> 32));
#endif
}

} // cybozu