smartOp

Implements various integer math operations with error checking.

smartOp strives to give the mathematically correct result, with integer-style rounding, for all inputs. Only if the correct result is undefined or not representable by the return type is an error signalled, using checkedint.flags.

The error-signalling policy may be selected using the policy template parameter.

Members

Functions

abs
Unsigned!N abs(const N num)
IntFromChar!N abs(const N num)

Get the absolute value of num. Because the return type is always unsigned, overflow is not possible.

binary
auto binary(const N left, const M right)
N binary(return ref N left, const M right)

Perform the binary (two-argument) integer operation specified by op.

bsf
ubyte bsf(const N num)

core.bitop.bsf without the undefined behaviour. smartOp.bsf(0) will raise IntFlag.undef.

bsr
ubyte bsr(const N num)

core.bitop.bsr without the undefined behaviour. smartOp.bsr(0) will raise IntFlag.undef.

cmp
bool cmp(const N left, const M right)

Compare left and right using op.

  • Unlike the standard integer comparison operator, this function correctly handles negative values in signed/unsigned comparisons.
  • Like the standard operator, comparisons involving any floating-point nan value always return false.


Direct comparisons between boolean values and numeric ones are forbidden. Make the intent explicit:

  • numeric == cast(N)boolean
  • (numeric != 0) == boolean
cmp
int cmp(const N left, const M right)

Defines a total order on all basic scalar values, using the same rules as std.math.cmp().

divPow2
auto divPow2(const N left, const M exp)
auto divPow2(const N left, const M exp)

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.

ilogb
ubyte ilogb(const N num)

Get the base 2 logarithm of abs(num), rounded down to the nearest integer.

modPow2
auto modPow2(const N left, const M exp)
auto modPow2(const N left, const M exp)

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

mulPow2
auto mulPow2(const N left, const M exp)
auto mulPow2(const N left, const M exp)

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.

pow
auto pow(const N base, const M exp)
auto pow(const N base, const M exp)

Raise base to the exp power.

unary
N unary(const N num)
IntFromChar!N unary(const N num)
Signed!(Promoted!N) unary(const N num)
N unary(return ref N num)

Perform the unary (single-argument) integer operation specified by op.

Meta