Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
IntegerDivision.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2025 Charlie Schlosser <cs.schlosser@gmail.com>
5// Copyright (C) 2026 Rasmus Munk Larsen <rmlarsen@gmail.com>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10// SPDX-License-Identifier: MPL-2.0
11
12#ifndef EIGEN_INTEGER_DIVISION_H
13#define EIGEN_INTEGER_DIVISION_H
14
15// IWYU pragma: private
16#include "./InternalHeaderCheck.h"
17
18namespace Eigen {
19
20namespace internal {
21
22// Division of k-bit integers by an invariant divisor d != 0.
23//
24// Unsigned, multiply-add (Robison, "N-Bit Unsigned Division Via N-Bit Multiply-Add", ARITH 2005). With
25// f = floor(log2(d)), q = floor(2^(k+f) / d) and e = d - (2^(k+f) mod d),
26//
27// floor(n / d) = (m n + a) >> (k + s), where
28//
29// (m, a, s) = (2^k - 1, 2^k - 1, 0) for d = 1,
30// (2^(k-1), 0, f - 1) for d = 2^f, f >= 1,
31// (q + 1, 0, f) for e <= 2^f,
32// (q, q, f) otherwise.
33//
34// m n + a <= (2^k - 1) 2^k, so the double-width sum cannot overflow. A divisor d >= 2^k is stored as (0, 0, 0).
35//
36// Unsigned, subtract (Granlund and Montgomery, "Division by invariant integers using multiplication", PLDI 1994,
37// Fig. 4.1). With l = ceil(log2(d)) and m = floor(2^k (2^l - d) / d) + 1, t = floor(m n / 2^k), s1 = min(l, 1):
38//
39// floor(n / d) = (t + ((n - t) >> s1)) >> (l - s1).
40//
41// A divisor d >= 2^k is stored as m = 1, l = k. This needs only the high half of m n, where multiply-add also needs
42// the low half to see its carry. Double-width products up to 64 bits and x86's mul return both halves at once, so
43// those use multiply-add; 64-bit scalars elsewhere take the two halves from two multiplications, and use this form.
44//
45// Signed k < 64, after Granlund and Montgomery, Fig. 5.2. With D = |d|, l = max(ceil(log2(D)), 1) - 1 and
46// M = floor(2^(k+l) / D) + 1, every |n| <= 2^(k-1) satisfies
47//
48// trunc(n / D) = (n M >> (k + l)) - (n >> (k - 1)),
49//
50// arithmetic shifts in double width. 2^(k-1) < M < 2^k for D >= 2 and M = 2^k + 1 for D = 1, so M - 2^k fits in k
51// signed bits and the high half of n M is n + mulhs(M - 2^k, n), one sequence for every d. Only n = lowest() with D = 1
52// wraps, in n M or in that sum; there l = 0, so no shift sees the wrapped sign and the result is exact modulo 2^k.
53// A divisor D > 2^k divides like D = 2^k: every quotient is 0.
54namespace int_div {
55
56template <typename T>
57EIGEN_DEVICE_FUNC constexpr bool is_negative(const T& x, std::true_type) {
58 return x < 0;
59}
60template <typename T>
61EIGEN_DEVICE_FUNC constexpr bool is_negative(const T&, std::false_type) {
62 return false;
63}
64template <typename T>
65EIGEN_DEVICE_FUNC constexpr bool is_negative(const T& x) {
66 return is_negative(x, std::is_signed<T>());
67}
68
69// |x| as the unsigned type of the same width; exact for lowest().
70template <typename T>
71EIGEN_DEVICE_FUNC constexpr std::make_unsigned_t<T> unsigned_abs(const T& x) {
72 using Unsigned = std::make_unsigned_t<T>;
73 return is_negative(x) ? static_cast<Unsigned>(Unsigned(0) - static_cast<Unsigned>(x)) : static_cast<Unsigned>(x);
74}
75
76// floor(a b / 2^64).
77template <typename Unsigned, std::enable_if_t<sizeof(Unsigned) == 8, int> = 0>
78EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Unsigned umulhi(Unsigned a, Unsigned b) {
79#if defined(EIGEN_GPU_COMPILE_PHASE)
80 return __umul64hi(a, b);
81#elif defined(EIGEN_SYCL_DEVICE_ONLY)
82 return cl::sycl::mul_hi(static_cast<uint64_t>(a), static_cast<uint64_t>(b));
83#elif EIGEN_COMP_MSVC && (EIGEN_ARCH_x86_64 || EIGEN_ARCH_ARM64)
84 return __umulh(a, b);
85#elif EIGEN_HAS_BUILTIN_INT128
86 return static_cast<Unsigned>((static_cast<__uint128_t>(a) * b) >> 64);
87#else
88 // Hacker's Delight, 2nd ed., Fig. 8-2: with 32-bit digits, u and w stay below 2^64.
89 Unsigned a0 = a & 0xffffffffu, a1 = a >> 32, b0 = b & 0xffffffffu, b1 = b >> 32;
90 Unsigned u = a1 * b0 + ((a0 * b0) >> 32);
91 Unsigned w = a0 * b1 + (u & 0xffffffffu);
92 return a1 * b1 + (u >> 32) + (w >> 32);
93#endif
94}
95
96// floor(r 2^k / d) for r < d, which fits in k bits.
97template <typename Unsigned, std::enable_if_t<(sizeof(Unsigned) < 8), int> = 0>
98EIGEN_DEVICE_FUNC Unsigned div_shifted(Unsigned r, Unsigned d) {
99 return static_cast<Unsigned>((static_cast<uint64_t>(r) << (CHAR_BIT * sizeof(Unsigned))) / d);
100}
101template <typename Unsigned, std::enable_if_t<sizeof(Unsigned) == 8, int> = 0>
102EIGEN_DEVICE_FUNC Unsigned div_shifted(Unsigned r, Unsigned d) {
103#if EIGEN_HAS_BUILTIN_INT128 && !defined(EIGEN_GPU_COMPILE_PHASE) && !defined(__SYCL_DEVICE_ONLY__)
104 return static_cast<Unsigned>((static_cast<__uint128_t>(r) << 64) / d);
105#else
106 // Restoring division, one quotient bit per step, keeping r < d.
107 Unsigned q = 0;
108 for (int i = 0; i < 64; ++i) {
109 bool carry = (r >> 63) != 0;
110 r <<= 1;
111 q <<= 1;
112 if (carry || r >= d) {
113 r -= d;
114 q |= 1;
115 }
116 }
117 return q;
118#endif
119}
120
121// Whether k-bit division uses multiply-add: see above. Depends on the architecture alone, not on the vectorization
122// flags, so the constants of a divider mean the same in translation units compiled for different instruction sets.
123template <typename Unsigned>
124struct use_multiply_add : std::integral_constant<bool, (sizeof(Unsigned) < 8) || EIGEN_ARCH_i386_OR_x86_64> {};
125
126// The unsigned type holding the double-width product of k < 64 bit operands. It is never narrower than 32 bits: 16-bit
127// operands would promote to int, where the product can overflow.
128template <typename Unsigned>
129using double_width_t = std::conditional_t<(sizeof(Unsigned) < 4), uint32_t, uint64_t>;
130
131// floor((a b + c) / 2^64), which fits in 64 bits for a, b, c < 2^64.
132template <typename Unsigned, std::enable_if_t<sizeof(Unsigned) == 8, int> = 0>
133EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Unsigned umuladdhi(Unsigned a, Unsigned b, Unsigned c) {
134#if EIGEN_HAS_BUILTIN_INT128 && !defined(EIGEN_GPU_COMPILE_PHASE) && !defined(__SYCL_DEVICE_ONLY__)
135 return static_cast<Unsigned>((static_cast<__uint128_t>(a) * b + c) >> 64);
136#else
137 Unsigned low = a * b;
138 return umulhi(a, b) + static_cast<Unsigned>(static_cast<Unsigned>(low + c) < low);
139#endif
140}
141
142// floor(a / d) for the constants of d computed by params. Below 64 bits one shift of the whole sum: compilers that
143// vectorize this keep the lanes wide until the end, where narrowing to the high half first costs more shuffles.
144template <typename Unsigned, std::enable_if_t<(sizeof(Unsigned) < 8), int> = 0>
145EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Unsigned udiv(Unsigned a, Unsigned magic, Unsigned addend, int shift) {
146 using Wide = double_width_t<Unsigned>;
147 return static_cast<Unsigned>((static_cast<Wide>(a) * static_cast<Wide>(magic) + static_cast<Wide>(addend)) >>
148 (CHAR_BIT * sizeof(Unsigned) + shift));
149}
150template <typename Unsigned, std::enable_if_t<sizeof(Unsigned) == 8 && use_multiply_add<Unsigned>::value, int> = 0>
151EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Unsigned udiv(Unsigned a, Unsigned magic, Unsigned addend, int shift) {
152 return umuladdhi(a, magic, addend) >> shift;
153}
154template <typename Unsigned, std::enable_if_t<!use_multiply_add<Unsigned>::value, int> = 0>
155EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Unsigned udiv(Unsigned a, Unsigned magic, Unsigned /*addend*/, int shift) {
156 Unsigned t = umulhi(a, magic);
157 int shift1 = shift > 0 ? 1 : 0;
158 return (t + ((a - t) >> shift1)) >> (shift - shift1);
159}
160
161template <typename Unsigned, bool MultiplyAdd = use_multiply_add<Unsigned>::value>
162struct params;
163
164template <typename Unsigned>
165struct params<Unsigned, true> {
166 static_assert(std::is_unsigned<Unsigned>::value, "params takes the unsigned type of the numerators");
167 static constexpr int kBits = CHAR_BIT * sizeof(Unsigned);
168
169 template <typename UnsignedDivisor>
170 EIGEN_DEVICE_FUNC explicit params(UnsignedDivisor d) : magic(0), addend(0), shift(0) {
171 eigen_assert(d != 0 && "IntDivider: division by zero");
172 // A wider divisor type can hold d >= 2^k, which keeps the defaults above.
173 if (d <= static_cast<UnsignedDivisor>(NumTraits<Unsigned>::highest())) {
174 using Wide = std::conditional_t<(sizeof(Unsigned) < 4), uint32_t, Unsigned>;
175 Unsigned du = static_cast<Unsigned>(d);
176 int f = log_2_impl<Unsigned>::run_floor(du);
177 Wide power = Wide(1) << f;
178 if (du == 1) {
179 magic = addend = NumTraits<Unsigned>::highest();
180 } else if (Wide(du) == power) {
181 magic = static_cast<Unsigned>(Wide(1) << (kBits - 1));
182 shift = f - 1;
183 } else {
184 Unsigned q = div_shifted(static_cast<Unsigned>(power), du);
185 // r = 2^(k+f) mod d = -q d mod 2^k, since r < d < 2^k.
186 Unsigned r = static_cast<Unsigned>(Wide(0) - Wide(q) * Wide(du));
187 bool round_up = Wide(du) - Wide(r) <= power;
188 magic = static_cast<Unsigned>(q + (round_up ? 1 : 0));
189 addend = round_up ? Unsigned(0) : q;
190 shift = f;
191 }
192 }
193 }
194
195 Unsigned magic;
196 Unsigned addend;
197 int shift;
198};
199
200template <typename Unsigned>
201struct params<Unsigned, false> {
202 static_assert(std::is_unsigned<Unsigned>::value, "params takes the unsigned type of the numerators");
203 static constexpr int kBits = CHAR_BIT * sizeof(Unsigned);
204
205 template <typename UnsignedDivisor>
206 EIGEN_DEVICE_FUNC explicit params(UnsignedDivisor d) : magic(1), addend(0), shift(kBits) {
207 eigen_assert(d != 0 && "IntDivider: division by zero");
208 // A wider divisor type can hold d >= 2^k, which keeps the defaults above.
209 if (d <= static_cast<UnsignedDivisor>(NumTraits<Unsigned>::highest())) {
210 Unsigned du = static_cast<Unsigned>(d);
211 shift = log_2_impl<Unsigned>::run_ceil(du);
212 // r = 2^l - d < d, reduced mod 2^k for l = k.
213 Unsigned r =
214 shift == kBits ? static_cast<Unsigned>(Unsigned(0) - du) : static_cast<Unsigned>((Unsigned(1) << shift) - du);
215 magic = static_cast<Unsigned>(div_shifted(r, du) + 1);
216 }
217 }
218
219 Unsigned magic;
220 Unsigned addend;
221 int shift;
222};
223
224// The constants of signed division for k < 64. magic holds M modulo 2^32, which only |d| = 1 with k = 32 reduces. It is
225// not wider so that compilers see a 32 x 32 -> 64-bit product: GCC vectorizes a truncated 64-bit multiplier as a
226// 64 x 64-bit product, three times the multiplications. shift is l, and total_shift is k + l, or 0 for |d| = 1 with
227// k = 32, where the low bits of M are 1.
228template <typename Scalar>
229struct signed_params {
230 static constexpr int kBits = CHAR_BIT * sizeof(Scalar);
231
232 template <typename UnsignedDivisor>
233 EIGEN_DEVICE_FUNC explicit signed_params(UnsignedDivisor d) {
234 eigen_assert(d != 0 && "IntDivider: division by zero");
235 using Unsigned = std::make_unsigned_t<Scalar>;
236 // Only a wider divisor type can hold d > 2^k - 1; 2^k gives the same quotients, all 0.
237 bool fits = d <= static_cast<UnsignedDivisor>(NumTraits<Unsigned>::highest());
238 uint64_t magnitude = fits ? static_cast<uint64_t>(d) : uint64_t(1) << kBits;
239 shift = !fits ? kBits - 1 : numext::maxi(log_2_impl<Unsigned>::run_ceil(static_cast<Unsigned>(d)), 1) - 1;
240 uint64_t full_magic = (uint64_t(1) << (kBits + shift)) / magnitude + 1;
241 magic = static_cast<uint32_t>(full_magic);
242 total_shift = (full_magic >> 32) != 0 ? 0 : kBits + shift;
243 }
244
245 uint32_t magic;
246 int shift;
247 int total_shift;
248};
249
250// trunc(a / d) for the constants of signed_params and divisor_sign = -[d < 0]. lowest() / -1 wraps to lowest().
251//
252// 32 bits: compilers vectorize a loop over this badly in the signed form, which needs a 64-bit arithmetic shift.
253// Multiply the magnitude and shift the whole product instead. The masks avoid a select, which mispredicts on random
254// signs.
255template <typename Scalar, std::enable_if_t<sizeof(Scalar) == 4, int> = 0>
256EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar sdiv(Scalar a, uint32_t magic, int total_shift, Scalar divisor_sign) {
257 uint32_t a_sign = static_cast<uint32_t>(a >> 31);
258 uint32_t abs_a = (static_cast<uint32_t>(a) ^ a_sign) - a_sign;
259 uint32_t quotient = static_cast<uint32_t>((static_cast<uint64_t>(abs_a) * magic) >> total_shift);
260 uint32_t sign = a_sign ^ static_cast<uint32_t>(divisor_sign);
261 return static_cast<Scalar>((quotient ^ sign) - sign);
262}
263template <typename Scalar, std::enable_if_t<(sizeof(Scalar) < 4), int> = 0>
264EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar sdiv(Scalar a, uint32_t magic, int total_shift, Scalar divisor_sign) {
265 using Unsigned = std::make_unsigned_t<Scalar>;
266 int32_t wide = static_cast<int32_t>(a);
267 int32_t product = static_cast<int32_t>(static_cast<uint32_t>(wide) * magic);
268 Unsigned quotient =
269 static_cast<Unsigned>(static_cast<Unsigned>(product >> total_shift) - static_cast<Unsigned>(wide >> 31));
270 Unsigned sign = static_cast<Unsigned>(divisor_sign);
271 return static_cast<Scalar>(static_cast<Unsigned>((quotient ^ sign) - sign));
272}
273
274} // namespace int_div
275
279template <typename Packet>
280EIGEN_DEVICE_FUNC inline Packet pudiv_magic(const Packet& a,
281 std::make_unsigned_t<typename unpacket_traits<Packet>::type> magic,
282 std::make_unsigned_t<typename unpacket_traits<Packet>::type> addend,
283 int shift) {
284 using Scalar = typename unpacket_traits<Packet>::type;
285 using Unsigned = std::make_unsigned_t<Scalar>;
286 constexpr int kSize = unpacket_traits<Packet>::size;
287 Scalar lanes[kSize];
288 pstoreu(lanes, a);
289 for (int i = 0; i < kSize; ++i) {
290 lanes[i] = static_cast<Scalar>(int_div::udiv(static_cast<Unsigned>(lanes[i]), magic, addend, shift));
291 }
292 return ploadu<Packet>(lanes);
293}
294
297template <typename Packet>
298EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet psdiv_magic(
299 const Packet& a, std::make_unsigned_t<typename unpacket_traits<Packet>::type> magic,
300 std::make_unsigned_t<typename unpacket_traits<Packet>::type> addend, int shift, const Packet& divisor_sign) {
301 // For a real packet, pabs(lowest()) wraps to lowest(), whose unsigned reading is |lowest()|.
302 Packet abs_quotient = pudiv_magic(pabs(a), magic, addend, shift);
303 Packet quotient_sign = pxor(psignbit(a), divisor_sign);
304 return psub(pxor(abs_quotient, quotient_sign), quotient_sign);
305}
306
311template <typename Packet>
312EIGEN_DEVICE_FUNC inline Packet psdiv_signed_magic(const Packet& a, uint32_t magic, int /*shift*/, int total_shift,
313 const Packet& divisor_sign) {
314 using Scalar = typename unpacket_traits<Packet>::type;
315 constexpr int kSize = unpacket_traits<Packet>::size;
316 Scalar sign = pfirst(divisor_sign);
317 Scalar lanes[kSize];
318 pstoreu(lanes, a);
319 for (int i = 0; i < kSize; ++i) lanes[i] = int_div::sdiv(lanes[i], magic, total_shift, sign);
320 return ploadu<Packet>(lanes);
321}
322
323template <typename Scalar, bool IsSigned = std::is_signed<Scalar>::value, bool SignedMultiply = (sizeof(Scalar) < 8)>
324struct scalar_int_div_op : int_div::params<Scalar> {
325 using Base = int_div::params<Scalar>;
326 using result_type = Scalar;
327
328 template <typename Divisor>
329 EIGEN_DEVICE_FUNC explicit scalar_int_div_op(const Divisor& d) : Base(int_div::unsigned_abs(d)) {
330 eigen_assert(!int_div::is_negative(d) && "IntDivider: an unsigned numerator needs a positive divisor");
331 }
332
333 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar operator()(const Scalar& a) const {
334 return int_div::udiv(a, this->magic, this->addend, this->shift);
335 }
336 template <typename Packet>
337 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a) const {
338 return pudiv_magic(a, this->magic, this->addend, this->shift);
339 }
340};
341
342// Signed 64-bit: there is no double-width type to multiply in, so divide the magnitudes.
343template <typename Scalar>
344struct scalar_int_div_op<Scalar, true, false> : int_div::params<std::make_unsigned_t<Scalar>> {
345 using Unsigned = std::make_unsigned_t<Scalar>;
346 using Base = int_div::params<Unsigned>;
347 using result_type = Scalar;
348
349 template <typename Divisor>
350 EIGEN_DEVICE_FUNC explicit scalar_int_div_op(const Divisor& d)
351 : Base(int_div::unsigned_abs(d)), divisor_sign(int_div::is_negative(d) ? Scalar(-1) : Scalar(0)) {}
352
353 // q = (|q| ^ s) - s for the mask s = -[q < 0]: a select here compiles to a mispredicted branch with some
354 // compilers (GCC 12 for AArch64) when numerator signs are random. The conversion back to Scalar is modular; only
355 // lowest() / -1 wraps.
356 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar operator()(const Scalar& a) const {
357 Unsigned abs_quotient = int_div::udiv(int_div::unsigned_abs(a), this->magic, this->addend, this->shift);
358 Unsigned quotient_sign = (Unsigned(0) - Unsigned(a < 0)) ^ static_cast<Unsigned>(divisor_sign);
359 return static_cast<Scalar>((abs_quotient ^ quotient_sign) - quotient_sign);
360 }
361 template <typename Packet>
362 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a) const {
363 return psdiv_magic(a, this->magic, this->addend, this->shift, pset1<Packet>(divisor_sign));
364 }
365
366 Scalar divisor_sign;
367};
368
369// Signed below 64 bits: one multiplication and no absolute value, for every divisor.
370template <typename Scalar>
371struct scalar_int_div_op<Scalar, true, true> : int_div::signed_params<Scalar> {
372 using Base = int_div::signed_params<Scalar>;
373 using result_type = Scalar;
374
375 template <typename Divisor>
376 EIGEN_DEVICE_FUNC explicit scalar_int_div_op(const Divisor& d)
377 : Base(int_div::unsigned_abs(d)), divisor_sign(int_div::is_negative(d) ? Scalar(-1) : Scalar(0)) {}
378
379 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar operator()(const Scalar& a) const {
380 return int_div::sdiv(a, this->magic, this->total_shift, divisor_sign);
381 }
382 template <typename Packet>
383 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a) const {
384 return psdiv_signed_magic(a, this->magic, this->shift, this->total_shift, pset1<Packet>(divisor_sign));
385 }
386
387 Scalar divisor_sign;
388};
389
390template <typename Scalar, bool IsSigned, bool SignedMultiply>
391struct functor_traits<scalar_int_div_op<Scalar, IsSigned, SignedMultiply>> {
392 static constexpr int Cost = 2 * NumTraits<Scalar>::MulCost + 5 * NumTraits<Scalar>::AddCost;
393 static constexpr bool PacketAccess = packet_traits<Scalar>::HasFastIntDiv;
394};
395
396} // namespace internal
397
439template <typename Scalar_>
441 EIGEN_STATIC_ASSERT((std::is_integral<Scalar_>::value && !std::is_same<Scalar_, bool>::value),
442 THE_NUMERATOR_TYPE_MUST_BE_A_BUILT_IN_INTEGER)
443
444 public:
445 using Scalar = Scalar_;
446 using Functor = internal::scalar_int_div_op<Scalar>;
447
449 EIGEN_DEVICE_FUNC IntDivider() : m_functor(Scalar(1)) {}
450
452 template <typename Divisor>
453 EIGEN_DEVICE_FUNC explicit IntDivider(const Divisor& divisor) : m_functor(divisor) {
454 EIGEN_STATIC_ASSERT((std::is_integral<Divisor>::value && !std::is_same<Divisor, bool>::value),
455 THE_DIVISOR_TYPE_MUST_BE_A_BUILT_IN_INTEGER)
456 }
457
459 template <typename Divisor>
460 EIGEN_DEVICE_FUNC IntDivider& compute(const Divisor& divisor) {
461 EIGEN_STATIC_ASSERT((std::is_integral<Divisor>::value && !std::is_same<Divisor, bool>::value),
462 THE_DIVISOR_TYPE_MUST_BE_A_BUILT_IN_INTEGER)
463 m_functor = Functor(divisor);
464 return *this;
465 }
466
468 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar divide(const Scalar& numerator) const { return m_functor(numerator); }
469
471 template <typename Derived>
472 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CwiseUnaryOp<Functor, const Derived> divide(
473 const DenseBase<Derived>& numerators) const {
474 EIGEN_STATIC_ASSERT(
475 (std::is_same<typename Derived::Scalar, Scalar>::value),
476 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
477 return CwiseUnaryOp<Functor, const Derived>(numerators.derived(), m_functor);
478 }
479
481 EIGEN_DEVICE_FUNC const Functor& functor() const { return m_functor; }
482
483 private:
484 Functor m_functor;
485};
486
490template <typename Numerator, typename Scalar, std::enable_if_t<std::is_arithmetic<Numerator>::value, int> = 0>
491EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar operator/(const Numerator& numerator, const IntDivider<Scalar>& divider) {
492 // Conversions would truncate these silently: 7.5 / IntDivider<int>(2) == 3, (1LL << 40) / IntDivider<int>(1) == 0.
493 // int stays allowed for literals and integer promotion.
494 EIGEN_STATIC_ASSERT(
495 (std::is_integral<Numerator>::value && (sizeof(Numerator) <= sizeof(Scalar) || sizeof(Numerator) <= sizeof(int))),
496 THE_NUMERATOR_MUST_BE_AN_INTEGER_NO_WIDER_THAN_THE_DIVIDER_SCALAR_OR_INT)
497 return divider.divide(static_cast<Scalar>(numerator));
498}
499
502template <typename Scalar>
503EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator/=(Scalar& numerator, const IntDivider<Scalar>& divider) {
504 numerator = divider.divide(numerator);
505 return numerator;
506}
507
510template <typename Derived, typename Scalar>
511EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CwiseUnaryOp<internal::scalar_int_div_op<Scalar>, const Derived> operator/(
512 const DenseBase<Derived>& numerators, const IntDivider<Scalar>& divider) {
513 return divider.divide(numerators);
514}
515
516} // namespace Eigen
517
518#endif // EIGEN_INTEGER_DIVISION_H
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition CwiseUnaryOp.h:55
Base class for all dense matrices, vectors, and arrays.
Definition DenseBase.h:45
IntDivider()
Definition IntegerDivision.h:449
const Functor & functor() const
Definition IntegerDivision.h:481
Scalar & operator/=(Scalar &numerator, const IntDivider< Scalar > &divider)
Definition IntegerDivision.h:503
CwiseUnaryOp< internal::scalar_int_div_op< Scalar >, const Derived > operator/(const DenseBase< Derived > &numerators, const IntDivider< Scalar > &divider)
Definition IntegerDivision.h:511
IntDivider(const Divisor &divisor)
Definition IntegerDivision.h:453
IntDivider & compute(const Divisor &divisor)
Definition IntegerDivision.h:460
Scalar operator/(const Numerator &numerator, const IntDivider< Scalar > &divider)
Definition IntegerDivision.h:491
Scalar divide(const Scalar &numerator) const
Definition IntegerDivision.h:468
CwiseUnaryOp< Functor, const Derived > divide(const DenseBase< Derived > &numerators) const
Definition IntegerDivision.h:472