Expression Templates Library (ETL)
linear_eval_functors.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c) 2014-2023 Baptiste Wicht
3 // Distributed under the terms of the MIT License.
4 // (See accompanying file LICENSE or copy at
5 // http://opensource.org/licenses/MIT)
6 //=======================================================================
7 
14 #pragma once
15 
16 namespace etl::detail {
17 
24 struct Assign {
28  template <typename L_Expr, typename R_Expr>
29  static void apply(L_Expr&& lhs, R_Expr&& rhs) {
30  const size_t N = etl::size(lhs);
31 
32  auto* lhs_mem = lhs.memory_start();
33 
34  size_t i = 0;
35 
36  if constexpr (unroll_normal_loops) {
37  const size_t iend = prev_multiple(N, 4);
38 
39  for (; i < iend; i += 4) {
40  lhs_mem[i] = rhs.read_flat(i);
41  lhs_mem[i + 1] = rhs.read_flat(i + 1);
42  lhs_mem[i + 2] = rhs.read_flat(i + 2);
43  lhs_mem[i + 3] = rhs.read_flat(i + 3);
44  }
45  }
46 
47  for (; i < N; ++i) {
48  lhs_mem[i] = rhs.read_flat(i);
49  }
50  }
51 };
52 
56 struct AssignAdd {
60  template <typename L_Expr, typename R_Expr>
61  static void apply(L_Expr&& lhs, R_Expr&& rhs) {
62  const size_t N = etl::size(lhs);
63 
64  auto* lhs_mem = lhs.memory_start();
65 
66  size_t i = 0;
67 
68  if constexpr (unroll_normal_loops) {
69  const size_t iend = prev_multiple(N, 4);
70 
71  for (; i < iend; i += 4) {
72  lhs_mem[i] += rhs[i];
73  lhs_mem[i + 1] += rhs[i + 1];
74  lhs_mem[i + 2] += rhs[i + 2];
75  lhs_mem[i + 3] += rhs[i + 3];
76  }
77  }
78 
79  for (; i < N; ++i) {
80  lhs_mem[i] += rhs[i];
81  }
82  }
83 };
84 
88 struct AssignSub {
92  template <typename L_Expr, typename R_Expr>
93  static void apply(L_Expr&& lhs, R_Expr&& rhs) {
94  const size_t N = etl::size(lhs);
95 
96  auto* lhs_mem = lhs.memory_start();
97 
98  size_t i = 0;
99 
100  if constexpr (unroll_normal_loops) {
101  const size_t iend = prev_multiple(N, 4);
102 
103  for (; i < iend; i += 4) {
104  lhs_mem[i] -= rhs[i];
105  lhs_mem[i + 1] -= rhs[i + 1];
106  lhs_mem[i + 2] -= rhs[i + 2];
107  lhs_mem[i + 3] -= rhs[i + 3];
108  }
109  }
110 
111  for (; i < N; ++i) {
112  lhs_mem[i] -= rhs[i];
113  }
114  }
115 };
116 
120 struct AssignMul {
124  template <typename L_Expr, typename R_Expr>
125  static void apply(L_Expr&& lhs, R_Expr&& rhs) {
126  const size_t N = etl::size(lhs);
127 
128  auto* lhs_mem = lhs.memory_start();
129 
130  size_t i = 0;
131 
132  if constexpr (unroll_normal_loops) {
133  const size_t iend = prev_multiple(N, 4);
134 
135  for (; i < iend; i += 4) {
136  lhs_mem[i] *= rhs[i];
137  lhs_mem[i + 1] *= rhs[i + 1];
138  lhs_mem[i + 2] *= rhs[i + 2];
139  lhs_mem[i + 3] *= rhs[i + 3];
140  }
141  }
142 
143  for (; i < N; ++i) {
144  lhs_mem[i] *= rhs[i];
145  }
146  }
147 };
148 
152 struct AssignDiv {
156  template <typename L_Expr, typename R_Expr>
157  static void apply(L_Expr&& lhs, R_Expr&& rhs) {
158  const size_t N = etl::size(lhs);
159 
160  auto* lhs_mem = lhs.memory_start();
161 
162  size_t i = 0;
163 
164  if constexpr (unroll_normal_loops) {
165  const size_t iend = prev_multiple(N, 4);
166 
167  for (; i < iend; i += 4) {
168  lhs_mem[i] /= rhs[i];
169  lhs_mem[i + 1] /= rhs[i + 1];
170  lhs_mem[i + 2] /= rhs[i + 2];
171  lhs_mem[i + 3] /= rhs[i + 3];
172  }
173  }
174 
175  for (; i < N; ++i) {
176  lhs_mem[i] /= rhs[i];
177  }
178  }
179 };
180 
181 } //end of namespace etl::detail
static void apply(L_Expr &&lhs, R_Expr &&rhs)
Assign rhs to lhs.
Definition: linear_eval_functors.hpp:125
static void apply(L_Expr &&lhs, R_Expr &&rhs)
Assign rhs to lhs.
Definition: linear_eval_functors.hpp:93
Definition: expression_builder.hpp:699
static void apply(L_Expr &&lhs, R_Expr &&rhs)
Assign rhs to lhs.
Definition: linear_eval_functors.hpp:29
Functor for compound assign mul.
Definition: linear_eval_functors.hpp:120
Functor for compound assign sub.
Definition: linear_eval_functors.hpp:88
constexpr size_t size(const E &expr) noexcept
Returns the size of the given ETL expression.
Definition: helpers.hpp:108
static void apply(L_Expr &&lhs, R_Expr &&rhs)
Assign rhs to lhs.
Definition: linear_eval_functors.hpp:157
static void apply(L_Expr &&lhs, R_Expr &&rhs)
Assign rhs to lhs.
Definition: linear_eval_functors.hpp:61
Functor for compound assign div.
Definition: linear_eval_functors.hpp:152
Functor for simple assign.
Definition: linear_eval_functors.hpp:24
Functor for simple compound assign add.
Definition: linear_eval_functors.hpp:56
constexpr bool unroll_normal_loops
Indicates if ETL is allowed tor unroll non-vectorized loops.
Definition: config.hpp:153