summaryrefslogtreecommitdiff
path: root/src/support/bits.cpp
blob: f57a6d0e86e8ad5260c64f1428cc721a6b16ba7e (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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * Copyright 2015 WebAssembly Community Group participants
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define wasm_support_bits_definitions
#include "support/bits.h"
#include "../compiler-support.h"
#include "support/utilities.h"

#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
#endif

namespace wasm {

template<> int PopCount<uint8_t>(uint8_t v) {
  // Small table lookup.
  static const uint8_t tbl[32] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2,
                                  3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3,
                                  3, 4, 2, 3, 3, 4, 3, 4, 4, 5};
  return tbl[v & 0xf] + tbl[v >> 4];
}

template<> int PopCount<uint16_t>(uint16_t v) {
#if __has_builtin(__builtin_popcount) || defined(__GNUC__) || defined(_MSC_VER)
  return (int)__builtin_popcount(v);
#else
  return PopCount((uint8_t)(v & 0xFF)) + PopCount((uint8_t)(v >> 8));
#endif
}

template<> int PopCount<uint32_t>(uint32_t v) {
#if __has_builtin(__builtin_popcount) || defined(__GNUC__) || defined(_MSC_VER)
  return (int)__builtin_popcount(v);
#else
  // See Stanford bithacks, counting bits set in parallel, "best method":
  // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
  v = v - ((v >> 1) & 0x55555555);
  v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
  return (((v + (v >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
#endif
}

template<> int PopCount<uint64_t>(uint64_t v) {
#if __has_builtin(__builtin_popcount) || defined(__GNUC__) || defined(_MSC_VER)
  return (int)__builtin_popcountll(v);
#else
  return PopCount((uint32_t)v) + PopCount((uint32_t)(v >> 32));
#endif
}

template<> uint32_t BitReverse<uint32_t>(uint32_t v) {
  // See Hacker's Delight, first edition, figure 7-1.
  v = ((v & 0x55555555) << 1) | ((v >> 1) & 0x55555555);
  v = ((v & 0x33333333) << 2) | ((v >> 2) & 0x33333333);
  v = ((v & 0x0F0F0F0F) << 4) | ((v >> 4) & 0x0F0F0F0F);
  v = (v << 24) | ((v & 0xFF00) << 8) | ((v >> 8) & 0xFF00) | (v >> 24);
  return v;
}

template<> int CountTrailingZeroes<uint32_t>(uint32_t v) {
  if (v == 0) {
    return 32;
  }
#if __has_builtin(__builtin_ctz) || defined(__GNUC__)
  return __builtin_ctz(v);
#elif defined(_MSC_VER)
  unsigned long count;
  _BitScanForward(&count, v);
  return (int)count;
#else
  // See Stanford bithacks, count the consecutive zero bits (trailing) on the
  // right with multiply and lookup:
  // http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightMultLookup
  static const uint8_t tbl[32] = {0,  1,  28, 2,  29, 14, 24, 3,  30, 22, 20,
                                  15, 25, 17, 4,  8,  31, 27, 13, 23, 21, 19,
                                  16, 7,  26, 12, 18, 6,  11, 5,  10, 9};
  return (int)tbl[((uint32_t)((v & -v) * 0x077CB531U)) >> 27];
#endif
}

template<> int CountTrailingZeroes<uint64_t>(uint64_t v) {
  if (v == 0) {
    return 64;
  }
#if __has_builtin(__builtin_ctzll) || defined(__GNUC__)
  return __builtin_ctzll(v);
#elif defined(_MSC_VER)
  unsigned long count;
  _BitScanForward64(&count, v);
  return (int)count;
#else
  return (uint32_t)v ? CountTrailingZeroes((uint32_t)v)
                     : 32 + CountTrailingZeroes((uint32_t)(v >> 32));
#endif
}

template<> int CountLeadingZeroes<uint32_t>(uint32_t v) {
  if (v == 0) {
    return 32;
  }
#if __has_builtin(__builtin_clz) || defined(__GNUC__)
  return __builtin_clz(v);
#elif defined(_MSC_VER)
  unsigned long count;
  _BitScanReverse(&count, v);
  return (int)count;
#else
  // See Stanford bithacks, find the log base 2 of an N-bit integer in
  // O(lg(N)) operations with multiply and lookup:
  // http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogDeBruijn
  static const uint8_t tbl[32] = {31, 22, 30, 21, 18, 10, 29, 2,  20, 17, 15,
                                  13, 9,  6,  28, 1,  23, 19, 11, 3,  16, 14,
                                  7,  24, 12, 4,  8,  25, 5,  26, 27, 0};
  v = v | (v >> 1);
  v = v | (v >> 2);
  v = v | (v >> 4);
  v = v | (v >> 8);
  v = v | (v >> 16);
  return (int)tbl[((uint32_t)(v * 0x07C4ACDDU)) >> 27];
#endif
}

template<> int CountLeadingZeroes<uint64_t>(uint64_t v) {
  if (v == 0) {
    return 64;
  }
#if __has_builtin(__builtin_clzll) || defined(__GNUC__)
  return __builtin_clzll(v);
#elif defined(_MSC_VER)
  unsigned long count;
  _BitScanReverse64(&count, v);
  return (int)count;
#else
  return v >> 32 ? CountLeadingZeroes((uint32_t)(v >> 32))
                 : 32 + CountLeadingZeroes((uint32_t)v);
#endif
}

uint32_t Log2(uint32_t v) {
  switch (v) {
    default:
      WASM_UNREACHABLE("invalid value");
    case 1:
      return 0;
    case 2:
      return 1;
    case 4:
      return 2;
    case 8:
      return 3;
    case 16:
      return 4;
    case 32:
      return 5;
  }
}

uint32_t Pow2(uint32_t v) { return 1 << v; }

} // namespace wasm