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

Aliases

abs
alias abs = smartOp!(IntFlagPolicy.none).abs
Undocumented in source.
cmp
alias cmp = smartOp!(IntFlagPolicy.none).cmp
Undocumented in source.

Functions

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

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

binary
auto binary(N left, M right)
N binary(N left, M right)

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

bsf
ubyte bsf(N num)

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

bsr
ubyte bsr(N num)

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

cmp
bool cmp(N left, 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 your intention explicit:

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

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

divPow2
auto divPow2(N left, 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(N num)

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

modPow2
auto modPow2(N left, 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(N left, 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(N base, M exp)

Raise base to the exp power.

unary
N unary(N num)
IntFromChar!N unary(N num)
Signed!(Promoted!N) unary(N num)

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

Meta