Next: , Up: Arithmetic on poly_ints   [Contents][Index]


10.4.1 Using poly_int with C++ arithmetic operators

The following C++ expressions are supported, where p1 and p2 are poly_ints and where c1 and c2 are scalars:

-p1
~p1

p1 + p2
p1 + c2
c1 + p2

p1 - p2
p1 - c2
c1 - p2

c1 * p2
p1 * c2

p1 << c2

p1 += p2
p1 += c2

p1 -= p2
p1 -= c2

p1 *= c2
p1 <<= c2

These arithmetic operations handle integer ranks in a similar way to C++. The main difference is that every coefficient narrower than HOST_WIDE_INT promotes to HOST_WIDE_INT, whereas in C++ everything narrower than int promotes to int. For example:

poly_uint16     + int          -> poly_int64
unsigned int    + poly_uint16  -> poly_int64
poly_int64      + int          -> poly_int64
poly_int32      + poly_uint64  -> poly_uint64
uint64          + poly_int64   -> poly_uint64
poly_offset_int + int32        -> poly_offset_int
offset_int      + poly_uint16  -> poly_offset_int

In the first two examples, both coefficients are narrower than HOST_WIDE_INT, so the result has coefficients of type HOST_WIDE_INT. In the other examples, the coefficient with the highest rank “wins”.

If one of the operands is wide_int or poly_wide_int, the rules are the same as for wide_int arithmetic.