safeOp.mulPow2

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 mulPow2(left, -exp) is equivalent to divPow2(left, exp).

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

Examples

import checkedint.sticky : safeOp; // use IntFlagPolicy.sticky

assert(safeOp.mulPow2(-23, 5) == -736);
safeOp.mulPow2(10_000_000, 10);
assert(IntFlags.local.clear() == IntFlag.posOver);

assert(safeOp.mulPow2(65536, -8) == 256);
assert(safeOp.mulPow2(-100, -100) == 0);

Meta