smartOp.divPow2

Equivalent to left / pow(2, exp), but faster and works with a wider range of inputs. This is a safer alternative to left >> exp that is still very fast.

Note that (conceptually) rounding occurs after the /, meaning that divPow2(left, -exp) is equivalent to mulPow2(left, exp).

  1. auto divPow2(const N left, const M exp)
    template smartOp(IntFlagPolicy policy)
    divPow2
    pure @safe nothrow @nogc
    (
    N
    M
    )
    (
    const N left
    ,
    const M exp
    )
    if (
    (
    isFloatingPoint!N &&
    isScalarType!M
    )
    ||
    (
    isScalarType!N &&
    isFloatingPoint!M
    )
    )
  2. auto divPow2(const N left, const M exp)

Examples

1 import checkedint.sticky : smartOp; // use IntFlagPolicy.sticky
2 
3 assert(smartOp.divPow2(65536, 8) == 256);
4 assert(smartOp.divPow2(-100, 100) == 0);
5 assert(smartOp.divPow2(-23, -5) == -736);
6 
7 smartOp.divPow2(10_000_000, -10);
8 assert(IntFlags.local.clear() == IntFlag.posOver);

Meta