template<typename Scalar_>
class Eigen::IntDivider< Scalar_ >
Divides integers by a divisor that is fixed at run time, with multiplications and shifts in place of division instructions.
- Template Parameters
-
| Scalar_ | the type of the numerators and quotients: a built-in integer type other than bool. |
The constructor precomputes a multiplier and a shift count from the divisor. Each quotient then costs a double-width multiplication, a few additions and subtractions and a shift or two, which on most hardware is several times cheaper than an integer division, and vectorizes where hardware integer division does not exist. Each quotient is the mathematical quotient truncated toward zero, which is what the built-in / operator computes when both operands have type Scalar_:
int q = n / by;
Eigen::ArrayXi Q = A / by;
n /= by;
A /= by;
Divides integers by a divisor that is fixed at run time, with multiplications and shifts in place of ...
Definition IntegerDivision.h:440
The divisor may have any built-in integer type, and its magnitude may exceed the range of Scalar_. Quotients use the divisor's mathematical value, without the built-in operator's usual arithmetic conversions, so a divisor of the other signedness can give a different quotient: an IntDivider of int constructed from 3u divides -6 to -2, while -6 / 3u is 1431655763. The divisor must be nonzero, and it must be positive when Scalar_ is unsigned. lowest() divided by -1 wraps to lowest(), where the built-in operator overflows for int and wider types.
A scalar numerator of another integer type converts to Scalar_ first. Its type may be no wider than the wider of Scalar_ and int, so that literals and arithmetic promoted to int, such as (a + b) / by, divide by an IntDivider of a narrower type. A wider integer or a floating-point numerator does not compile, since it would be truncated.
A default-constructed IntDivider divides by 1; compute() sets the divisor later, for example in a member that is initialized after construction.
Use IntDivider when one divisor that is not a compile-time constant divides many numerators: constructing it costs about as much as a few divisions. For a constant divisor, write the constant; the compiler then derives a similar sequence itself.