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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
|
/++
This module contains a collection of bit-level operations.
Authors: Ilia Ki, Phobos & LDC Authors (original Phobos unittests, docs, conventions).
+/
module mir.bitop;
version(LDC)
import ldc.intrinsics;
version(GNU)
import gcc.builtins;
import mir.math.common: fastmath;
/// Right shift vallue for bit index to get element's index (5 for `uint`).
enum uint bitElemShift(T : ubyte) = 3;
/// ditto
enum uint bitElemShift(T : byte) = 3;
/// ditto
enum uint bitElemShift(T : ushort) = 4;
/// ditto
enum uint bitElemShift(T : short) = 4;
/// ditto
enum uint bitElemShift(T : uint) = 5;
/// ditto
enum uint bitElemShift(T : int) = 5;
/// ditto
enum uint bitElemShift(T : ulong) = 6;
/// ditto
enum uint bitElemShift(T : long) = 6;
static if (is(ucent))
/// ditto
enum uint bitElemShift(T : ucent) = 7;
/// ditto
static if (is(cent))
enum uint bitElemShift(T : cent) = 7;
/// Bit mask for bit index to get element's bit shift (31 for uint).
enum uint bitShiftMask(T : ubyte) = 7;
/// ditto
enum uint bitShiftMask(T : byte) = 7;
/// ditto
enum uint bitShiftMask(T : ushort) = 15;
/// ditto
enum uint bitShiftMask(T : short) = 15;
/// ditto
enum uint bitShiftMask(T : uint) = 31;
/// ditto
enum uint bitShiftMask(T : int) = 31;
/// ditto
enum uint bitShiftMask(T : ulong) = 63;
/// ditto
enum uint bitShiftMask(T : long) = 63;
static if (is(ucent))
/// ditto
enum uint bitShiftMask(T : ucent) = 127;
static if (is(cent))
/// ditto
enum uint bitShiftMask(T : cent) = 127;
// no effect on this function, but better for optimization of other @fastmath code that uses this
@fastmath:
/++
+/
T nTrailingBitsToCount(T)(in T value, in T popcnt)
if (__traits(isUnsigned, T))
{
import std.traits;
import mir.internal.utility: Iota;
alias S = Signed!(CommonType!(int, T));
S mask = S(-1) << T.sizeof * 4;
foreach_reverse (s; Iota!(bitElemShift!T - 1))
{{
enum shift = 1 << s;
if (S(popcnt) > S(ctpop(cast(T)(value & ~mask))))
mask <<= shift;
else
mask >>= shift;
}}
return cttz(cast(T)mask) + (S(popcnt) != ctpop(cast(T)(value & ~mask)));
}
///
version(mir_core_test) unittest
{
assert(nTrailingBitsToCount(0xF0u, 3u) == 7);
assert(nTrailingBitsToCount(0xE00u, 3u) == 12);
foreach(uint i; 1 .. 32)
assert(nTrailingBitsToCount(uint.max, i) == i);
}
/++
+/
T nLeadingBitsToCount(T)(in T value, in T popcnt)
if (__traits(isUnsigned, T))
{
import std.traits;
import mir.internal.utility: Iota;
alias S = Signed!(CommonType!(int, T));
S mask = S(-1) << T.sizeof * 4;
foreach_reverse (s; Iota!(bitElemShift!T - 1))
{{
enum shift = 1 << s;
if (S(popcnt) > S(ctpop(cast(T)(value & mask))))
mask >>= shift;
else
mask <<= shift;
}}
return ctlz(cast(T)~mask) + (S(popcnt) != ctpop(cast(T)(value & mask)));
}
///
version(mir_core_test) unittest
{
assert(nLeadingBitsToCount(0xF0u, 3u) == 32 - 5);
assert(nLeadingBitsToCount(0x700u, 3u) == 32 - 8);
foreach(uint i; 1 .. 32)
assert(nLeadingBitsToCount(uint.max, i) == i);
}
/++
Tests the bit.
Returns:
A non-zero value if the bit was set, and a zero
if it was clear.
+/
auto bt(Field, T = typeof(Field.init[size_t.init]))(auto ref Field p, size_t bitnum)
if (__traits(isUnsigned, T))
{
auto index = bitnum >> bitElemShift!T;
auto mask = T(1) << (bitnum & bitShiftMask!T);
return p[index] & mask;
}
///
@system pure version(mir_core_test) unittest
{
size_t[2] array;
array[0] = 2;
array[1] = 0x100;
assert(bt(array.ptr, 1));
assert(array[0] == 2);
assert(array[1] == 0x100);
}
/++
Tests and assign the bit.
Returns:
A non-zero value if the bit was set, and a zero if it was clear.
+/
auto bta(Field, T = typeof(Field.init[size_t.init]))(auto ref Field p, size_t bitnum, bool value)
if (__traits(isUnsigned, T))
{
auto index = bitnum >> bitElemShift!T;
auto shift = bitnum & bitShiftMask!T;
auto mask = T(1) << shift;
static if (__traits(compiles, &p[size_t.init]))
{
auto qp = &p[index];
auto q = *qp;
auto ret = q & mask;
*qp = cast(T)((q & ~mask) ^ (T(value) << shift));
}
else
{
auto q = p[index];
auto ret = q & mask;
p[index] = cast(T)((q & ~mask) ^ (T(value) << shift));
}
return ret;
}
/++
Tests and complements the bit.
Returns:
A non-zero value if the bit was set, and a zero if it was clear.
+/
auto btc(Field, T = typeof(Field.init[size_t.init]))(auto ref Field p, size_t bitnum)
if (__traits(isUnsigned, T))
{
auto index = bitnum >> bitElemShift!T;
auto mask = T(1) << (bitnum & bitShiftMask!T);
static if (__traits(compiles, &p[size_t.init]))
{
auto qp = &p[index];
auto q = *qp;
auto ret = q & mask;
*qp = cast(T)(q ^ mask);
}
else
{
auto q = p[index];
auto ret = q & mask;
p[index] = cast(T)(q ^ mask);
}
return ret;
}
/++
Tests and resets (sets to 0) the bit.
Returns:
A non-zero value if the bit was set, and a zero if it was clear.
+/
auto btr(Field, T = typeof(Field.init[size_t.init]))(auto ref Field p, size_t bitnum)
if (__traits(isUnsigned, T))
{
auto index = bitnum >> bitElemShift!T;
auto mask = T(1) << (bitnum & bitShiftMask!T);
static if (__traits(compiles, &p[size_t.init]))
{
auto qp = &p[index];
auto q = *qp;
auto ret = q & mask;
*qp = cast(T)(q & ~mask);
}
else
{
auto q = p[index];
auto ret = q & mask;
p[index] = cast(T)(q & ~mask);
}
return ret;
}
/++
Tests and sets the bit.
Params:
p = a non-NULL field / pointer to an array of unsigned integers.
bitnum = a bit number, starting with bit 0 of p[0],
and progressing. It addresses bits like the expression:
---
p[index / (T.sizeof*8)] & (1 << (index & ((T.sizeof*8) - 1)))
---
Returns:
A non-zero value if the bit was set, and a zero if it was clear.
+/
auto bts(Field, T = typeof(Field.init[size_t.init]))(auto ref Field p, size_t bitnum)
if (__traits(isUnsigned, T))
{
auto index = bitnum >> bitElemShift!T;
auto mask = T(1) << (bitnum & bitShiftMask!T);
static if (__traits(compiles, &p[size_t.init]))
{
auto qp = &p[index];
auto q = *qp;
auto ret = q & mask;
*qp = cast(T)(q | mask);
}
else
{
auto q = p[index];
auto ret = q & mask;
p[index] = cast(T)(q | mask);
}
return ret;
}
///
@system pure version(mir_core_test) unittest
{
size_t[2] array;
array[0] = 2;
array[1] = 0x100;
assert(btc(array.ptr, 35) == 0);
if (size_t.sizeof == 8)
{
assert(array[0] == 0x8_0000_0002);
assert(array[1] == 0x100);
}
else
{
assert(array[0] == 2);
assert(array[1] == 0x108);
}
assert(btc(array.ptr, 35));
assert(array[0] == 2);
assert(array[1] == 0x100);
assert(bts(array.ptr, 35) == 0);
if (size_t.sizeof == 8)
{
assert(array[0] == 0x8_0000_0002);
assert(array[1] == 0x100);
}
else
{
assert(array[0] == 2);
assert(array[1] == 0x108);
}
assert(btr(array.ptr, 35));
assert(array[0] == 2);
assert(array[1] == 0x100);
}
/// The 'ctpop' family of intrinsics counts the number of bits set in a value.
T ctpop(T)(in T src)
if (__traits(isUnsigned, T))
{
version(LDC) if (!__ctfe)
return llvm_ctpop(src);
version(GNU) if (!__ctfe)
{
static if (T.sizeof < __builtin_clong.sizeof)
return cast(T) __builtin_popcount(src);
else static if (T.sizeof <= __builtin_clong.sizeof)
return cast(T) __builtin_popcountl(src);
else
return cast(T) __builtin_popcountll(src);
}
import core.bitop: popcnt;
return cast(T) popcnt(src);
}
/++
The 'ctlz' family of intrinsic functions counts the number of leading zeros in a variable.
Result is undefined if the argument is zero.
+/
T ctlz(T)(in T src)
if (__traits(isUnsigned, T))
{
version(LDC) if (!__ctfe)
return llvm_ctlz(src, true);
version(GNU) if (!__ctfe)
{
// Do not zero-extend when counting leading zeroes.
static if (T.sizeof < __builtin_clong.sizeof && T.sizeof >= uint.sizeof)
return cast(T) __builtin_clz(src);
else static if (T.sizeof == __builtin_clong.sizeof)
return cast(T) __builtin_clzl(src);
else static if (T.sizeof > __builtin_clong.sizeof)
return cast(T) __builtin_clzll(src);
}
import core.bitop: bsr;
return cast(T)(T.sizeof * 8 - 1 - bsr(src));
}
///
version (mir_core_test) @nogc nothrow pure @safe version(mir_core_test) unittest
{
assert(ctlz(cast(ubyte) 0b0011_1111) == 2);
assert(ctlz(cast(ushort) 0b0000_0001_1111_1111) == 7);
}
/++
The 'ctlzp' family of intrinsic functions counts the number of leading zeros in a variable.
Result is properly defined if the argument is zero.
+/
T ctlzp(T)(in T src)
if (__traits(isUnsigned, T))
{
version(LDC) if (!__ctfe)
return llvm_ctlz(src, false);
return src ? ctlz(src) : T.sizeof * 8;
}
///
version (mir_core_test) @nogc nothrow pure @safe version(mir_core_test) unittest
{
assert(ctlzp(cast(ubyte) 0b0000_0000) == 8);
assert(ctlzp(cast(ubyte) 0b0011_1111) == 2);
assert(ctlzp(cast(ushort) 0b0000_0001_1111_1111) == 7);
assert(ctlzp(cast(ushort) 0) == 16);
assert(ctlzp(cast(ulong) 0) == 64);
}
/++
The 'cttz' family of intrinsic functions counts the number of trailing zeros.
Result is undefined if the argument is zero.
+/
T cttz(T)(in T src)
if (__traits(isUnsigned, T))
{
version(LDC) if (!__ctfe)
return llvm_cttz(src, true);
version(GNU) if (!__ctfe)
{
static if (T.sizeof <__builtin_clong.sizeof)
return cast(T) __builtin_ctz(src);
else static if (T.sizeof <=__builtin_clong.sizeof)
return cast(T) __builtin_ctzl(src);
else
return cast(T) __builtin_ctzll(src);
}
import core.bitop: bsf;
return cast(T) bsf(src);
}
///
version (mir_core_test) @nogc nothrow pure @safe version(mir_core_test) unittest
{
assert(cttzp(cast(ubyte) 0b11111100) == 2);
assert(cttzp(cast(ushort) 0b1111111110000000) == 7);
}
/++
The 'cttz' family of intrinsic functions counts the number of trailing zeros.
Result is properly defined if the argument is zero.
+/
T cttzp(T)(in T src)
if (__traits(isUnsigned, T))
{
version(LDC) if (!__ctfe)
return llvm_cttz(src, false);
return src ? cttz(src) : T.sizeof * 8;
}
///
version (mir_core_test) @nogc nothrow pure @safe version(mir_core_test) unittest
{
assert(cttzp(cast(ubyte) 0b0000_0000) == 8);
assert(cttzp(cast(ubyte) 0b11111100) == 2);
assert(cttzp(cast(ushort) 0b1111111110000000) == 7);
assert(cttzp(cast(ushort) 0) == 16);
assert(cttzp(cast(ulong) 0) == 64);
}
|