forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitUtility.cs
More file actions
204 lines (178 loc) · 8.25 KB
/
Copy pathBitUtility.cs
File metadata and controls
204 lines (178 loc) · 8.25 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You 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.
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Apache.Arrow
{
public static class BitUtility
{
private static ReadOnlySpan<byte> PopcountTable => new byte[] {
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,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
};
private static ReadOnlySpan<byte> BitMask => new byte[] {
1, 2, 4, 8, 16, 32, 64, 128
};
public static bool GetBit(byte data, int index) =>
((data >> index) & 1) != 0;
public static bool GetBit(ReadOnlySpan<byte> data, int index) =>
(data[index / 8] & BitMask[index % 8]) != 0;
public static void ClearBit(Span<byte> data, int index)
{
data[index / 8] &= (byte) ~BitMask[index % 8];
}
public static void SetBit(Span<byte> data, int index)
{
data[index / 8] |= BitMask[index % 8];
}
public static void SetBit(Span<byte> data, int index, bool value)
{
int idx = index / 8;
int mod = index % 8;
data[idx] = value
? (byte)(data[idx] | BitMask[mod])
: (byte)(data[idx] & ~BitMask[mod]);
}
public static void ToggleBit(Span<byte> data, int index)
{
data[index / 8] ^= BitMask[index % 8];
}
/// <summary>
/// Counts the number of set bits in a span of bytes starting
/// at a specific bit offset.
/// </summary>
/// <param name="data">Span to count bits</param>
/// <param name="offset">Bit offset to start counting from</param>
/// <returns>Count of set (one) bits</returns>
public static int CountBits(ReadOnlySpan<byte> data, int offset) =>
CountBits(data, offset, data.Length * 8 - offset);
/// <summary>
/// Counts the number of set bits in a span of bytes starting
/// at a specific bit offset, and limiting to a certain number of bits
/// in the span.
/// </summary>
/// <param name="data">Span to count bits.</param>
/// <param name="offset">Bit offset to start counting from.</param>
/// <param name="length">Maximum of bits in the span to consider.</param>
/// <returns>Count of set (one) bits</returns>
public static int CountBits(ReadOnlySpan<byte> data, int offset, int length)
{
int startByteIndex = offset / 8;
int startBitOffset = offset % 8;
int endByteIndex = (offset + length - 1) / 8;
int endBitOffset = (offset + length - 1) % 8;
if (startBitOffset < 0)
return 0;
int count = 0;
if (startByteIndex == endByteIndex)
{
// Range starts and ends within the same byte.
var slice = data.Slice(startByteIndex, 1);
for (int i = startBitOffset; i <= endBitOffset; i++)
count += GetBit(slice, i) ? 1 : 0;
return count;
}
// If the starting index and ending index are not byte-aligned,
// we'll need to count bits the slow way. If they are
// byte-aligned, and for all other bytes in the 'middle', we
// can use a faster byte-aligned count.
int fullByteStartIndex = startBitOffset == 0 ? startByteIndex : startByteIndex + 1;
int fullByteEndIndex = endBitOffset == 7 ? endByteIndex : endByteIndex - 1;
if (startBitOffset != 0)
{
var slice = data.Slice(startByteIndex, 1);
for (int i = startBitOffset; i <= 7; i++)
count += GetBit(slice, i) ? 1 : 0;
}
if (fullByteEndIndex >= fullByteStartIndex)
{
var slice = data.Slice(fullByteStartIndex, fullByteEndIndex - fullByteStartIndex + 1);
count += CountBits(slice);
}
if (endBitOffset != 7)
{
var slice = data.Slice(endByteIndex, 1);
for (int i = 0; i <= endBitOffset; i++)
count += GetBit(slice, i) ? 1 : 0;
}
return count;
}
/// <summary>
/// Counts the number of set bits in a span of bytes.
/// </summary>
/// <param name="data">Span to count bits</param>
/// <returns>Count of set (one) bits.</returns>
public static int CountBits(ReadOnlySpan<byte> data)
{
int count = 0;
foreach (byte t in data)
count += PopcountTable[t];
return count;
}
/// <summary>
/// Rounds an integer to the nearest multiple of 64.
/// </summary>
/// <param name="n">Integer to round.</param>
/// <returns>Integer rounded to the nearest multiple of 64.</returns>
public static long RoundUpToMultipleOf64(long n) =>
RoundUpToMultiplePowerOfTwo(n, 64);
/// <summary>
/// Rounds an integer to the nearest multiple of 8.
/// </summary>
/// <param name="n">Integer to round.</param>
/// <returns>Integer rounded to the nearest multiple of 8.</returns>
public static long RoundUpToMultipleOf8(long n) =>
RoundUpToMultiplePowerOfTwo(n, 8);
/// <summary>
/// Rounds an integer up to the nearest multiple of factor, where
/// factor must be a power of two.
///
/// This function does not throw when the factor is not a power of two.
/// </summary>
/// <param name="n">Integer to round up.</param>
/// <param name="factor">Power of two factor to round up to.</param>
/// <returns>Integer rounded up to the nearest power of two.</returns>
public static long RoundUpToMultiplePowerOfTwo(long n, int factor)
{
// Assert that factor is a power of two.
Debug.Assert(factor > 0 && (factor & (factor - 1)) == 0);
return (n + (factor - 1)) & ~(factor - 1);
}
internal static bool IsMultipleOf8(long n) => n % 8 == 0;
/// <summary>
/// Calculates the number of bytes required to store n bits.
/// </summary>
/// <param name="n">number of bits</param>
/// <returns>number of bytes</returns>
public static int ByteCount(int n)
{
Debug.Assert(n >= 0);
return n / 8 + (n % 8 != 0 ? 1 : 0); // ceil(n / 8)
}
internal static int ReadInt32(ReadOnlyMemory<byte> value)
{
Debug.Assert(value.Length >= sizeof(int));
return Unsafe.ReadUnaligned<int>(ref MemoryMarshal.GetReference(value.Span));
}
}
}