11#ifndef EIGEN_TENSOR_TENSOR_INTDIV_H
12#define EIGEN_TENSOR_TENSOR_INTDIV_H
15#include "./InternalHeaderCheck.h"
21namespace tensor_int_div {
24template <
typename Un
signed, std::enable_if_t<sizeof(Un
signed) == 8,
int> = 0>
25EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Unsigned quotient(Unsigned n, Unsigned m,
int shift) {
26 return int_div::umulhi(m,
static_cast<Unsigned
>(n << 1)) >> shift;
28template <
typename Un
signed, std::enable_if_t<(sizeof(Un
signed) < 8),
int> = 0>
29EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Un
signed quotient(Un
signed n, Un
signed m,
int shift) {
30 using W
ide =
int_div::
double_w
idth_t<Un
signed>;
31 return static_cast<Un
signed>((static_cast<W
ide>(m) * static_cast<W
ide>(n)) >> shift);
55template <
typename T,
bool div_gt_one = false>
56struct TensorIntDivisor {
58 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorIntDivisor() =
default;
61 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorIntDivisor(
const T divider) {
62 eigen_assert(divider > 0 && in_range(divider));
63 const UnsignedType d =
static_cast<UnsignedType
>(divider);
64 const int l = log_2_impl<UnsignedType>::run_ceil(d);
67 const UnsignedType r =
static_cast<UnsignedType
>(UnsignedType(1) << (l - 1));
68 m_multiplier =
static_cast<UnsignedType
>(int_div::div_shifted(r, d) + 1);
70 m_shift = kBaseShift + l;
74 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T divide(
const T numerator)
const {
75 eigen_assert(in_range(numerator));
76 return static_cast<T
>(tensor_int_div::quotient(
static_cast<UnsignedType
>(numerator), m_multiplier, m_shift));
80 using UnsignedType = std::make_unsigned_t<T>;
81 static constexpr int kBits = CHAR_BIT *
sizeof(T);
82 static constexpr int kBaseShift =
sizeof(T) == 8 ? 0 : kBits - 1;
84 EIGEN_DEVICE_FUNC
static constexpr bool in_range(
const T x) {
85 return (
static_cast<UnsignedType
>(x) >> (kBits - 1)) == 0;
88 UnsignedType m_multiplier =
static_cast<UnsignedType
>(UnsignedType(1) << (kBits - 1));
89 int m_shift = kBaseShift;
92template <
typename T,
bool div_gt_one>
93EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T operator/(
const T& numerator,
const TensorIntDivisor<T, div_gt_one>& divisor) {
94 return divisor.divide(numerator);
Namespace containing all symbols from the Eigen library.