safeOp.binary

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

  • Unsafe signed/unsigned operations will generate a compile-time error.
  • +, -, *, /, and % are checked for overflow at runtime.
  • / and % are also checked for divide-by-zero.
  • <<, >>, and >>> are checked to verify that right >= 0 and right < (8 * typeof(left).sizeof). Otherwise, IntFlag.undef is raised.


Note also:

  • The shift operators are not checked for overflow and should not be used for multiplication, division, or exponentiation. Instead, use mulPow2() and divPow2(), which internally use the bitshifts for speed, but check for overflow and correctly handle negative values.
  • Likewise, modPow2() should be used for remainders instead of &.
  • ^^ and ^^= will remain disabled in favour of pow until DMD issues 15288 and 15412 are fixed.


Like the standard equivalents, the assignment operators (+=, -=, *=, etc.) take left by ref and will overwrite it with the result of the operation.

  1. OpType!(N, op, M) binary(const N left, const M right)
    template safeOp(IntFlagPolicy policy)
    OpType!(N, op, M)
    binary
    @safe
    (
    string op
    N
    M
    )
    (
    const N left
    ,
    const M right
    )
    if (
    isFixedPoint!N &&
    isFixedPoint!M
    &&
    op.among!("+", "-", "*", "/", "%", "^^", "<<", ">>", ">>>", "&", "|", "^")
    )
  2. N binary(return ref N left, const M right)

Examples

1 import checkedint.sticky : safeOp; // use IntFlagPolicy.sticky
2 
3 assert(safeOp.binary!"+"(17, -5) == 12);
4 static assert(!__traits(compiles, safeOp.binary!"+"(-1, 1u)));
5 
6 ulong a = 18_446_744_073_709_551_615uL;
7 safeOp.binary!"+="(a, 1u);
8 assert(IntFlags.local.clear() == IntFlag.posOver);
9 
10 assert(safeOp.binary!"-"(17u, 5u) == 12u);
11 safeOp.binary!"-"(5u, 17u);
12 assert(IntFlags.local.clear() == IntFlag.negOver);
13 
14 ulong b = 123_456_789_987_654_321uL;
15 static assert(!__traits(compiles, safeOp.binary!"-="(b, 987_654_321)));
16 assert(safeOp.binary!"-="(b, 987_654_321u) == 123_456_789_000_000_000uL);
17 assert(b == 123_456_789_000_000_000uL);
18 
19 assert(safeOp.binary!"*"(-1 << 30, 2) == int.min);
20 safeOp.binary!"*"(1 << 30, 2);
21 assert(IntFlags.local.clear() == IntFlag.negOver);
22 
23 uint c = 1u << 18;
24 assert(safeOp.binary!"*="(c, 1u << 4) == 1u << 22);
25 assert(c == 1u << 22);
26 
27 assert(safeOp.binary!"/"(22, 11) == 2);
28 assert(!__traits(compiles, safeOp.binary!"/"(-22, 11u)));
29 safeOp.binary!"/"(0, 0);
30 assert(IntFlags.local.clear() == IntFlag.div0);
31 
32 long j = long.min;
33 safeOp.binary!"/="(j, -1);
34 assert(IntFlags.local.clear() == IntFlag.posOver);
35 
36 assert(safeOp.binary!"%"(20u, 7u) == 6u);
37 static assert(!__traits(compiles, safeOp.binary!"%"(20u, -7)));
38 safeOp.binary!"%"(20u, 0u);
39 assert(IntFlags.local.clear() == IntFlag.div0);
40 
41 short n = 75;
42 assert(safeOp.binary!"%="(n, -10) == 5);
43 assert(n == 5);
1 import checkedint.sticky : safeOp; // use IntFlagPolicy.sticky
2 
3 assert(safeOp.binary!"<<"(-0x80,  2) == -0x200);
4 safeOp.binary!"<<"(-0x80, -2);
5 assert(IntFlags.local.clear() == IntFlag.undef);
6 
7 ubyte a = 0x3u;
8 safeOp.binary!"<<="(a, 7);
9 assert(a == 0x80u);
10 
11 assert(safeOp.binary!">>"(-0xC, 5u) == -0x1);
12 safeOp.binary!">>"(-0xC, long.max);
13 assert(IntFlags.local.clear() == IntFlag.undef);
14 
15 short b = 0x700;
16 assert(safeOp.binary!">>="(b, 8) == 0x7);
17 assert(b == 0x7);
18 
19 assert(safeOp.binary!">>>"(-0x80, 2u) == 0x3FFF_FFE0);
20 safeOp.binary!">>>"(-0x80, 32);
21 assert(IntFlags.local.clear() == IntFlag.undef);
22 
23 int c = 0xFE_DCBA;
24 assert(safeOp.binary!">>>="(c, 12) == 0xFED);
25 assert(c == 0xFED);
26 
27 assert(safeOp.binary!"&"(0x6Fu, 0x4076)  == 0x66u);
28 
29 ubyte d = 0x6Fu;
30 assert(safeOp.binary!"&="(d, 0x4076) == 0x66u);
31 assert(d == 0x66u);
32 
33 assert(safeOp.binary!"|"(0x6F, 0x4076u) == 0x407Fu);
34 
35 byte e = 0x6F;
36 assert(safeOp.binary!"|="(e, 0x4076u) == 0x7F);
37 assert(e == 0x7F);
38 
39 assert(safeOp.binary!"^"(0x6F, 0x4076) == 0x4019);
40 
41 int f = 0x6F;
42 assert(safeOp.binary!"^="(f, 0x4076) == 0x4019);
43 assert(f == 0x4019);
44 
45 assert(!IntFlags.local);

Meta