smartOp.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(const N left, const M exp)
  2. auto mulPow2(const N left, const M exp)
    template smartOp(IntFlagPolicy policy)
    mulPow2
    @safe
    (
    N
    M
    )
    (
    const N left
    ,
    const M exp
    )
    if (
    isFixedPoint!N &&
    isFixedPoint!M
    )

Examples

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

Meta