$darkmode
Eigen  5.0.1-dev
Fill.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2024 Charles Schlosser <cs.schlosser@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 
10 #ifndef EIGEN_FILL_H
11 #define EIGEN_FILL_H
12 
13 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal {
19 
20 template <typename Xpr>
21 struct eigen_fill_helper : std::false_type {};
22 
23 template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
24 struct eigen_fill_helper<Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols>> : std::true_type {};
25 
26 template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
27 struct eigen_fill_helper<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols>> : std::true_type {};
28 
29 template <typename Xpr, int BlockRows, int BlockCols>
30 struct eigen_fill_helper<Block<Xpr, BlockRows, BlockCols, /*InnerPanel*/ true>> : eigen_fill_helper<Xpr> {};
31 
32 template <typename Xpr, int BlockRows, int BlockCols>
33 struct eigen_fill_helper<Block<Xpr, BlockRows, BlockCols, /*InnerPanel*/ false>>
34  : std::integral_constant<bool, eigen_fill_helper<Xpr>::value &&
35  (Xpr::IsRowMajor ? (BlockRows == 1) : (BlockCols == 1))> {};
36 
37 template <typename Xpr, int Options>
38 struct eigen_fill_helper<Map<Xpr, Options, Stride<0, 0>>> : eigen_fill_helper<Xpr> {};
39 
40 template <typename Xpr, int Options, int OuterStride_>
41 struct eigen_fill_helper<Map<Xpr, Options, Stride<OuterStride_, 0>>>
42  : std::integral_constant<bool, eigen_fill_helper<Xpr>::value &&
43  enum_eq_not_dynamic(OuterStride_, Xpr::InnerSizeAtCompileTime)> {};
44 
45 template <typename Xpr, int Options, int OuterStride_>
46 struct eigen_fill_helper<Map<Xpr, Options, Stride<OuterStride_, 1>>>
47  : eigen_fill_helper<Map<Xpr, Options, Stride<OuterStride_, 0>>> {};
48 
49 template <typename Xpr, int Options, int InnerStride_>
50 struct eigen_fill_helper<Map<Xpr, Options, InnerStride<InnerStride_>>>
51  : eigen_fill_helper<Map<Xpr, Options, Stride<0, InnerStride_>>> {};
52 
53 template <typename Xpr, int Options, int OuterStride_>
54 struct eigen_fill_helper<Map<Xpr, Options, OuterStride<OuterStride_>>>
55  : eigen_fill_helper<Map<Xpr, Options, Stride<OuterStride_, 0>>> {};
56 
57 template <typename Xpr>
58 struct eigen_fill_impl<Xpr, /*use_fill*/ false> {
59  using Scalar = typename Xpr::Scalar;
60  using Func = scalar_constant_op<Scalar>;
61  using PlainObject = typename Xpr::PlainObject;
62  using Constant = typename PlainObject::ConstantReturnType;
63  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr void run(Xpr& dst, const Scalar& val) {
64  const Constant src(dst.rows(), dst.cols(), val);
65  run(dst, src);
66  }
67  template <typename SrcXpr>
68  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr void run(Xpr& dst, const SrcXpr& src) {
69  call_dense_assignment_loop(dst, src, assign_op<Scalar, Scalar>());
70  }
71 };
72 
73 #if EIGEN_COMP_MSVC || defined(EIGEN_GPU_COMPILE_PHASE)
74 template <typename Xpr>
75 struct eigen_fill_impl<Xpr, /*use_fill*/ true> : eigen_fill_impl<Xpr, /*use_fill*/ false> {};
76 #else
77 template <typename Xpr>
78 struct eigen_fill_impl<Xpr, /*use_fill*/ true> {
79  using Scalar = typename Xpr::Scalar;
80  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Xpr& dst, const Scalar& val) {
81  const Scalar val_copy = val;
82  using std::fill_n;
83  fill_n(dst.data(), dst.size(), val_copy);
84  }
85  template <typename SrcXpr>
86  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Xpr& dst, const SrcXpr& src) {
87  resize_if_allowed(dst, src, assign_op<Scalar, Scalar>());
88  const Scalar& val = src.functor()();
89  run(dst, val);
90  }
91 };
92 #endif
93 
94 template <typename Xpr>
95 struct eigen_memset_helper {
96  static constexpr bool value =
97  std::is_trivially_copyable<typename Xpr::Scalar>::value && eigen_fill_helper<Xpr>::value;
98 };
99 
100 template <typename Xpr>
101 struct eigen_zero_impl<Xpr, /*use_memset*/ false> {
102  using Scalar = typename Xpr::Scalar;
103  using PlainObject = typename Xpr::PlainObject;
104  using Zero = typename PlainObject::ZeroReturnType;
105  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr void run(Xpr& dst) {
106  const Zero src(dst.rows(), dst.cols());
107  run(dst, src);
108  }
109  template <typename SrcXpr>
110  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr void run(Xpr& dst, const SrcXpr& src) {
111  call_dense_assignment_loop(dst, src, assign_op<Scalar, Scalar>());
112  }
113 };
114 
115 template <typename Xpr>
116 struct eigen_zero_impl<Xpr, /*use_memset*/ true> {
117  using Scalar = typename Xpr::Scalar;
118  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Xpr& dst) {
119  const std::ptrdiff_t num_bytes = dst.size() * static_cast<std::ptrdiff_t>(sizeof(Scalar));
120  if (num_bytes <= 0) return;
121  void* dst_ptr = static_cast<void*>(dst.data());
122 #ifndef EIGEN_NO_DEBUG
123  eigen_assert((dst_ptr != nullptr) && "null pointer dereference error!");
124 #endif
125  EIGEN_USING_STD(memset);
126  memset(dst_ptr, 0, static_cast<std::size_t>(num_bytes));
127  }
128  template <typename SrcXpr>
129  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Xpr& dst, const SrcXpr& src) {
130  resize_if_allowed(dst, src, assign_op<Scalar, Scalar>());
131  run(dst);
132  }
133 };
134 
135 } // namespace internal
136 } // namespace Eigen
137 
138 #endif // EIGEN_FILL_H
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1