Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_FILL_H
12#define EIGEN_FILL_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20
21template <typename Xpr>
22struct eigen_fill_helper : std::false_type {};
23
24// Only enable std::fill_n for trivially copyable scalars. GCC's libstdc++
25// fill_n pessimizes non-trivially-copyable types (extra moves per iteration),
26// causing measurable regressions for types like AutoDiffScalar (issue #2956).
27template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
28struct eigen_fill_helper<Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols>> : std::is_trivially_copyable<Scalar> {};
29
30template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
31struct eigen_fill_helper<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols>> : std::is_trivially_copyable<Scalar> {};
32
33template <typename Xpr, int BlockRows, int BlockCols>
34struct eigen_fill_helper<Block<Xpr, BlockRows, BlockCols, /*InnerPanel*/ true>> : eigen_fill_helper<Xpr> {};
35
36template <typename Xpr, int BlockRows, int BlockCols>
37struct eigen_fill_helper<Block<Xpr, BlockRows, BlockCols, /*InnerPanel*/ false>>
38 : bool_constant<eigen_fill_helper<Xpr>::value && (Xpr::IsRowMajor ? (BlockRows == 1) : (BlockCols == 1))> {};
39
40template <typename Xpr, int Options>
41struct eigen_fill_helper<Map<Xpr, Options, Stride<0, 0>>> : eigen_fill_helper<Xpr> {};
42
43template <typename Xpr, int Options, int OuterStride_>
44struct eigen_fill_helper<Map<Xpr, Options, Stride<OuterStride_, 0>>>
45 : bool_constant<eigen_fill_helper<Xpr>::value && enum_eq_not_dynamic(OuterStride_, Xpr::InnerSizeAtCompileTime)> {};
46
47template <typename Xpr, int Options, int OuterStride_>
48struct eigen_fill_helper<Map<Xpr, Options, Stride<OuterStride_, 1>>>
49 : eigen_fill_helper<Map<Xpr, Options, Stride<OuterStride_, 0>>> {};
50
51template <typename Xpr, int Options, int InnerStride_>
52struct eigen_fill_helper<Map<Xpr, Options, InnerStride<InnerStride_>>>
53 : eigen_fill_helper<Map<Xpr, Options, Stride<0, InnerStride_>>> {};
54
55template <typename Xpr, int Options, int OuterStride_>
56struct eigen_fill_helper<Map<Xpr, Options, OuterStride<OuterStride_>>>
57 : eigen_fill_helper<Map<Xpr, Options, Stride<OuterStride_, 0>>> {};
58
59template <typename Xpr>
60struct eigen_fill_impl<Xpr, /*use_fill*/ false> {
61 using Scalar = typename Xpr::Scalar;
62 using PlainObject = typename Xpr::PlainObject;
63 using Constant = typename PlainObject::ConstantReturnType;
64 static EIGEN_DEVICE_FUNC constexpr void run(Xpr& dst, const Scalar& val) {
65 const Constant src(dst.rows(), dst.cols(), val);
66 run(dst, src);
67 }
68 template <typename SrcXpr>
69 static EIGEN_DEVICE_FUNC constexpr void run(Xpr& dst, const SrcXpr& src) {
70 call_dense_assignment_loop(dst, src, assign_op<Scalar, Scalar>());
71 }
72};
73
74#if EIGEN_COMP_MSVC || defined(EIGEN_GPU_COMPILE_PHASE)
75template <typename Xpr>
76struct eigen_fill_impl<Xpr, /*use_fill*/ true> : eigen_fill_impl<Xpr, /*use_fill*/ false> {};
77#else
78template <typename Xpr>
79struct eigen_fill_impl<Xpr, /*use_fill*/ true> {
80 using Scalar = typename Xpr::Scalar;
81 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Xpr& dst, const Scalar& val) {
82 const Scalar val_copy = val;
83#if EIGEN_GCC15_SLP_BUG
84 // GCC 15.1/15.2 SLP-vectorize the stores of fill_n into wrong code when val is a complex product computed in the
85 // same function and the size is visible, e.g. Vector2cd::Constant(a * b); see EIGEN_GCC15_SLP_BUG. Copying val as
86 // one unit of sizeof(Scalar) bytes stores VIEW_CONVERT<uint64_t or uint128_t>(COMPLEX_EXPR <re, im>), so no group
87 // of float lanes {re, im, re, im} forms.
88 EIGEN_IF_CONSTEXPR (NumTraits<Scalar>::IsComplex) {
89 EIGEN_USING_STD(memcpy);
90 Scalar* data = dst.data();
91 const Index size = dst.size();
92 for (Index i = 0; i < size; ++i) memcpy(static_cast<void*>(data + i), &val_copy, sizeof(Scalar));
93 return;
94 }
95#endif
96 using std::fill_n;
97 fill_n(dst.data(), dst.size(), val_copy);
98 }
99 template <typename SrcXpr>
100 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Xpr& dst, const SrcXpr& src) {
101 resize_if_allowed(dst, src, assign_op<Scalar, Scalar>());
102 const Scalar& val = src.functor()();
103 run(dst, val);
104 }
105};
106#endif
107
108template <typename Xpr>
109struct eigen_memset_helper {
110 using Scalar = typename Xpr::Scalar;
111 static constexpr bool value = std::is_trivially_copyable<Scalar>::value &&
112 !static_cast<bool>(NumTraits<Scalar>::RequireInitialization) &&
113 eigen_fill_helper<Xpr>::value;
114};
115
116template <typename Xpr>
117struct eigen_zero_impl<Xpr, /*use_memset*/ false> {
118 using Scalar = typename Xpr::Scalar;
119 using PlainObject = typename Xpr::PlainObject;
120 using Zero = typename PlainObject::ZeroReturnType;
121 static EIGEN_DEVICE_FUNC constexpr void run(Xpr& dst) {
122 const Zero src(dst.rows(), dst.cols());
123 run(dst, src);
124 }
125 template <typename SrcXpr>
126 static EIGEN_DEVICE_FUNC constexpr void run(Xpr& dst, const SrcXpr& src) {
127 call_dense_assignment_loop(dst, src, assign_op<Scalar, Scalar>());
128 }
129};
130
131template <typename Xpr>
132struct eigen_zero_impl<Xpr, /*use_memset*/ true> {
133 using Scalar = typename Xpr::Scalar;
134 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Xpr& dst) {
135 Index size = dst.size();
136 if (size <= 0) return;
137 EIGEN_IF_CONSTEXPR (Xpr::MaxSizeAtCompileTime != Dynamic) {
138 if (size > Xpr::MaxSizeAtCompileTime) {
139 eigen_internal_assert(false && "invalid dense object size");
140 return;
141 }
142 }
143 const std::size_t num_bytes = static_cast<std::size_t>(size) * sizeof(Scalar);
144 void* dst_ptr = static_cast<void*>(dst.data());
145#ifndef EIGEN_NO_DEBUG
146 eigen_assert((dst_ptr != nullptr) && "null pointer dereference error!");
147#endif
148 EIGEN_USING_STD(memset);
149 memset(dst_ptr, 0, num_bytes);
150 }
151 template <typename SrcXpr>
152 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Xpr& dst, const SrcXpr& src) {
153 resize_if_allowed(dst, src, assign_op<Scalar, Scalar>());
154 run(dst);
155 }
156};
157
158} // namespace internal
159} // namespace Eigen
160
161#endif // EIGEN_FILL_H