Bit Operation Intrinsics (The GNU D Compiler)

#### 2.5.1 Bit Operation Intrinsics ¶

The following functions are a collection of intrinsics that do bit-level operations, available by importing the `core.bitop` module.

Although most are named after x86 hardware instructions, it is not guaranteed that they will result in generating equivalent assembly on x86. If the compiler determines there is a better way to get the same result in hardware, then that will be used instead.

Function: `int` core.bitop.bsf `(uint v)` ¶
Function: `int` core.bitop.bsf `(ulong v)` ¶

  Scans the bits in v starting with bit `0`, looking for the first set bit. Returns the bit number of the first bit set. The return value is undefined if v is zero.

  This intrinsic is the same as the GCC built-in function `__builtin_ctz`.

Function: `int` core.bitop.bsr `(uint v)` ¶
Function: `int` core.bitop.bsr `(ulong v)` ¶

  Scans the bits in v from the most significant bit to the least significant bit, looking for the first set bit. Returns the bit number of the first bit set. The return value is undefined if v is zero.

  This intrinsic is equivalent to writing the following:

      result = __builtin_clz(v) ^ (v.sizeof * 8 - 1)

Function: `int` core.bitop.bt `(scope const(uint*) p, uint bitnum)` ¶
Function: `int` core.bitop.bt `(scope const(uint*) p, uint bitnum)` ¶

  Tests the bit bitnum in the input parameter p. Returns a non-zero value if the bit was set, and a zero if it was clear.

  This intrinsic is equivalent to writing the following:

      immutable bits_per_unit = (*p).sizeof * 8;
      immutable bit_mask = size_t(1) << (bitnum % bits_per_unit);

      result = (p[bitnum / bits_per_unit] & bit_mask) != 0;

Function: `int` core.bitop.btc `(uint* p, uint bitnum)` ¶
Function: `int` core.bitop.btc `(ulong* p, ulong bitnum)` ¶

  Tests and complements the bit bitnum in the input parameter p. Returns a non-zero value if the bit was set, and a zero if it was clear.

  This intrinsic is equivalent to writing the following:

      immutable bits_per_unit = (*p).sizeof * 8;
      immutable bit_mask = size_t(1) << (bitnum % bits_per_unit);

      result = (p[bitnum / bits_per_unit] & bit_mask) != 0;

      p[bitnum / bits_per_unit] ^= bit_mask;

Function: `int` core.bitop.btr `(uint* p, uint bitnum)` ¶
Function: `int` core.bitop.btr `(ulong* p, ulong bitnum)` ¶

  Tests and resets (sets to 0) the bit bitnum in the input parameter p. Returns a non-zero value if the bit was set, and a zero if it was clear.

  This intrinsic is equivalent to writing the following:

      immutable bits_per_unit = (*p).sizeof * 8;
      immutable bit_mask = size_t(1) << (bitnum % bits_per_unit);

      result = (p[bitnum / bits_per_unit] & bit_mask) != 0;

      p[bitnum / bits_per_unit] &= ~bit_mask;

Function: `int` core.bitop.bts `(uint* p, uint bitnum)` ¶
Function: `int` core.bitop.bts `(ulong* p, ulong bitnum)` ¶

  Tests and sets the bit bitnum in the input parameter p. Returns a non-zero value if the bit was set, and a zero if it was clear.

  This intrinsic is equivalent to writing the following:

      immutable bits_per_unit = (*p).sizeof * 8;
      immutable bit_mask = size_t(1) << (bitnum % bits_per_unit);

      result = (p[bitnum / bits_per_unit] & bit_mask) != 0;

      p[bitnum / bits_per_unit] |= bit_mask;

Function: `ushort` core.bitop.byteswap `(ushort x)` ¶
Function: `uint` core.bitop.bswap `(uint x)` ¶
Function: `ulong` core.bitop.bswap `(ulong x)` ¶

  Swaps the bytes in x end-to-end; for example, in a 4-byte `uint`, byte `0` becomes byte `3`, byte `1` becomes byte `2`, etc.

  This intrinsic is the same as the GCC built-in function `__builtin_bswap`.

Function: `int` core.bitop.popcnt `(uint x)` ¶
Function: `int` core.bitop.popcnt `(ulong x)` ¶

  Calculates the number of set bits in x.

  This intrinsic is the same as the GCC built-in function `__builtin_popcount`.

Template: `T` core.bitop.rol `(T)(const T value, const uint count)` ¶
Template: `T` core.bitop.rol `(uint count, T)(const T value)` ¶

  Bitwise rotate value left by count bit positions.

  This intrinsic is equivalent to writing the following:

      result = cast(T) ((value << count) | (value >> (T.sizeof * 8 - count)));

Template: `T` core.bitop.ror `(T)(const T value, const uint count)` ¶
Template: `T` core.bitop.ror `(uint count, T)(const T value)` ¶

  Bitwise rotate value right by count bit positions.

  This intrinsic is equivalent to writing the following:

      result = cast(T) ((value >> count) | (value << (T.sizeof * 8 - count)));