Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
Eigen::IntDivider< Scalar_ > Class Template Reference

#include <Eigen/src/Core/IntegerDivision.h>

Detailed Description

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_:

Eigen::IntDivider<int> by(d); // d is known only at run time
int q = n / by; // == n / d
Eigen::ArrayXi Q = A / by; // == A / d, coefficient-wise
n /= by; // n = n / by
A /= by; // A = 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.

Public Member Functions

template<typename Divisor>
IntDivider & compute (const Divisor &divisor)
 
template<typename Derived>
CwiseUnaryOp< Functor, const Derived > divide (const DenseBase< Derived > &numerators) const
 
Scalar divide (const Scalar &numerator) const
 
const Functor & functor () const
 
 IntDivider ()
 
template<typename Divisor>
 IntDivider (const Divisor &divisor)
 

Related Symbols

(Note that these are not member symbols.)

template<typename Derived, typename Scalar>
CwiseUnaryOp< internal::scalar_int_div_op< Scalar >, const Derived > operator/ (const DenseBase< Derived > &numerators, const IntDivider< Scalar > &divider)
 
template<typename Numerator, typename Scalar, std::enable_if_t< std::is_arithmetic< Numerator >::value, int > = 0>
Scalar operator/ (const Numerator &numerator, const IntDivider< Scalar > &divider)
 
template<typename Scalar>
Scalar & operator/= (Scalar &numerator, const IntDivider< Scalar > &divider)
 

Constructor & Destructor Documentation

◆ IntDivider() [1/2]

template<typename Scalar_>
Eigen::IntDivider< Scalar_ >::IntDivider ( )
inline

Default constructor: divides by 1 until compute() sets another divisor.

◆ IntDivider() [2/2]

template<typename Scalar_>
template<typename Divisor>
Eigen::IntDivider< Scalar_ >::IntDivider ( const Divisor & divisor)
inlineexplicit

Precomputes the division by divisor.

Member Function Documentation

◆ compute()

template<typename Scalar_>
template<typename Divisor>
IntDivider & Eigen::IntDivider< Scalar_ >::compute ( const Divisor & divisor)
inline

Precomputes the division by divisor, replacing the previous divisor.

Returns
a reference to *this.

◆ divide() [1/2]

template<typename Scalar_>
template<typename Derived>
CwiseUnaryOp< Functor, const Derived > Eigen::IntDivider< Scalar_ >::divide ( const DenseBase< Derived > & numerators) const
inline
Returns
an expression of the coefficient-wise quotients of numerators by the divisor.

◆ divide() [2/2]

template<typename Scalar_>
Scalar Eigen::IntDivider< Scalar_ >::divide ( const Scalar & numerator) const
inline
Returns
numerator divided by the divisor, truncated toward zero.

◆ functor()

template<typename Scalar_>
const Functor & Eigen::IntDivider< Scalar_ >::functor ( ) const
inline
Returns
the functor that divides one numerator.

Friends And Related Symbol Documentation

◆ operator/() [1/2]

template<typename Derived, typename Scalar>
CwiseUnaryOp< internal::scalar_int_div_op< Scalar >, const Derived > operator/ ( const DenseBase< Derived > & numerators,
const IntDivider< Scalar > & divider )
related
Returns
an expression of the coefficient-wise quotients of numerators by the divisor of divider.

◆ operator/() [2/2]

template<typename Numerator, typename Scalar, std::enable_if_t< std::is_arithmetic< Numerator >::value, int > = 0>
Scalar operator/ ( const Numerator & numerator,
const IntDivider< Scalar > & divider )
related
Returns
numerator divided by the divisor of divider, truncated toward zero.

numerator is an integer no wider than the wider of Scalar and int, and converts to Scalar first.

◆ operator/=()

template<typename Scalar>
Scalar & operator/= ( Scalar & numerator,
const IntDivider< Scalar > & divider )
related

Divides numerator by the divisor of divider, truncated toward zero.

Returns
a reference to numerator.

The documentation for this class was generated from the following file: