Eigen-Contrib  5.0.1-dev+master
 
Loading...
Searching...
No Matches
TensorIntDiv.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_TENSOR_TENSOR_INTDIV_H
12#define EIGEN_TENSOR_TENSOR_INTDIV_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20
21namespace tensor_int_div {
22
23// floor(m n / 2^(k-1+l)) for n < 2^(k-1). shift is l for k = 64, where 2 n < 2^64, and k - 1 + l for k < 64.
24template <typename Unsigned, std::enable_if_t<sizeof(Unsigned) == 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;
27}
28template <typename Unsigned, std::enable_if_t<(sizeof(Unsigned) < 8), int> = 0>
29EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Unsigned quotient(Unsigned n, Unsigned m, int shift) {
30 using Wide = int_div::double_width_t<Unsigned>;
31 return static_cast<Unsigned>((static_cast<Wide>(m) * static_cast<Wide>(n)) >> shift);
32}
33
34} // namespace tensor_int_div
35
55template <typename T, bool div_gt_one = false>
56struct TensorIntDivisor {
57 public:
58 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorIntDivisor() = default;
59
60 // Must have 0 < divider < 2^(k-1).
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);
65 // 2^(k-1+l) = r 2^k with r = 2^(l-1) < d. d = 1 has l = 0 and keeps m = 2^(k-1).
66 if (l > 0) {
67 const UnsignedType r = static_cast<UnsignedType>(UnsignedType(1) << (l - 1));
68 m_multiplier = static_cast<UnsignedType>(int_div::div_shifted(r, d) + 1);
69 }
70 m_shift = kBaseShift + l;
71 }
72
73 // Must have 0 <= numerator < 2^(k-1).
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));
77 }
78
79 private:
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;
83
84 EIGEN_DEVICE_FUNC static constexpr bool in_range(const T x) {
85 return (static_cast<UnsignedType>(x) >> (kBits - 1)) == 0;
86 }
87
88 UnsignedType m_multiplier = static_cast<UnsignedType>(UnsignedType(1) << (kBits - 1));
89 int m_shift = kBaseShift;
90};
91
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);
95}
96
97} // end namespace internal
98} // end namespace Eigen
99
100#endif // EIGEN_TENSOR_TENSOR_INTDIV_H
Namespace containing all symbols from the Eigen library.