tesseract++ 0.0.1
N-dimensional tensor library for embedded systems
Loading...
Searching...
No Matches
ScalarExpr.h
Go to the documentation of this file.
1#pragma once
2#include "fused/BaseExpr.h"
3#include "fused/Operations.h"
5
6// ===============================
7// Scalar Expression Template
8// ===============================
9template <
10 typename EXPR,
11 typename ScalarT,
12 template <typename, my_size_t, typename> class Op>
13class ScalarExprRHS : public BaseExpr<ScalarExprRHS<EXPR, ScalarT, Op>>
14{
15 // Compile-time check that EXPR value_type and ScalarT are the same
16 static_assert(is_same_v<typename EXPR::value_type, ScalarT>,
17 "ScalarExprRHS: EXPR value_type and ScalarT must be the same");
18
19 const EXPR &_expr;
20 ScalarT _scalar;
21
22public:
23 static constexpr my_size_t NumDims = EXPR::NumDims;
24 static constexpr const my_size_t *Dim = EXPR::Dim;
25 static constexpr my_size_t TotalSize = EXPR::TotalSize;
26 using value_type = typename EXPR::value_type;
27 using Layout = typename EXPR::Layout;
28
29 ScalarExprRHS(const EXPR &expr, ScalarT scalar) : _expr(expr), _scalar(scalar) {}
30
31 const EXPR &expr() const noexcept { return _expr; }
32 ScalarT scalar() const noexcept { return _scalar; }
33
34 template <typename Output>
35 bool may_alias(const Output &output) const noexcept
36 {
37 return _expr.may_alias(output);
38 }
39
40 template <my_size_t length>
41 inline auto operator()(my_size_t (&indices)[length]) const noexcept
42 {
43 using T = std::decay_t<decltype(_expr(indices))>;
44 return Op<T, 0, GENERICARCH>::apply(
45 _expr(indices),
46 _scalar);
47 }
48
49 template <typename T, my_size_t Bits, typename Arch>
50 inline typename Op<T, Bits, Arch>::type evalu(const my_size_t flat) const noexcept
51 {
52 return Op<T, Bits, Arch>::apply(
53 _expr.template evalu<T, Bits, Arch>(flat),
54 _scalar);
55 }
56
57 template <typename T, my_size_t Bits, typename Arch>
58 inline typename Op<T, Bits, Arch>::type logical_evalu(my_size_t logical_flat) const noexcept
59 {
60 return Op<T, Bits, Arch>::apply(
61 _expr.template logical_evalu<T, Bits, Arch>(logical_flat),
62 _scalar);
63 }
64
65 my_size_t getNumDims() const noexcept
66 {
67 return _expr.getNumDims();
68 }
69
70 my_size_t getDim(my_size_t i) const // TODO: conditionally noexcept
71 {
72 return _expr.getDim(i);
73 }
74
75 my_size_t getTotalSize() const noexcept
76 {
77 return _expr.getTotalSize();
78 }
79
80protected:
81 inline auto operator()(const my_size_t *indices) const noexcept
82 {
83 using T = std::decay_t<decltype(_expr(indices))>;
84 return Op<T, 0, GENERICARCH>::apply(
85 _expr(indices),
86 _scalar);
87 }
88};
89
90template <
91 typename EXPR,
92 typename ScalarT,
93 template <typename, my_size_t, typename> class Op>
94class ScalarExprLHS : public BaseExpr<ScalarExprLHS<EXPR, ScalarT, Op>>
95{
96 // Compile-time check that EXPR value_type and ScalarT are the same
97 static_assert(is_same_v<typename EXPR::value_type, ScalarT>,
98 "ScalarExprRHS: EXPR value_type and ScalarT must be the same");
99
100 const EXPR &_expr;
101 ScalarT _scalar;
102
103public:
104 static constexpr my_size_t NumDims = EXPR::NumDims;
105 static constexpr const my_size_t *Dim = EXPR::Dim;
106 static constexpr my_size_t TotalSize = EXPR::TotalSize;
107 using value_type = typename EXPR::value_type;
108 using Layout = typename EXPR::Layout;
109
110 ScalarExprLHS(const EXPR &expr, ScalarT scalar) : _expr(expr), _scalar(scalar) {}
111
112 const EXPR &expr() const noexcept { return _expr; }
113 ScalarT scalar() const noexcept { return _scalar; }
114
115 template <typename Output>
116 bool may_alias(const Output &output) const noexcept
117 {
118 return _expr.may_alias(output);
119 }
120
121 template <my_size_t length>
122 auto operator()(my_size_t (&indices)[length]) const noexcept
123 {
124 using T = std::decay_t<decltype(_expr(indices))>;
125 return Op<T, 0, GENERICARCH>::apply(
126 _scalar,
127 _expr(indices));
128 }
129
130 template <typename T, my_size_t Bits, typename Arch>
131 inline typename Op<T, Bits, Arch>::type evalu(const my_size_t flat) const noexcept
132 {
133 return Op<T, Bits, Arch>::apply(
134 _scalar,
135 _expr.template evalu<T, Bits, Arch>(flat));
136 }
137
138 template <typename T, my_size_t Bits, typename Arch>
139 inline typename Op<T, Bits, Arch>::type logical_evalu(my_size_t logical_flat) const noexcept
140 {
141 return Op<T, Bits, Arch>::apply(
142 _scalar,
143 _expr.template logical_evalu<T, Bits, Arch>(logical_flat));
144 }
145
146 my_size_t getNumDims() const noexcept
147 {
148 return _expr.getNumDims();
149 }
150
151 my_size_t getDim(my_size_t i) const // TODO: conditionally noexcept
152 {
153 return _expr.getDim(i);
154 }
155
156 my_size_t getTotalSize() const noexcept
157 {
158 return _expr.getTotalSize();
159 }
160
161// protected:
162// inline auto operator()(const my_size_t *indices) const noexcept
163// {
164// using T = std::decay_t<decltype(_expr(indices))>;
165// return Op<T, 0, GENERICARCH>::apply(
166// _scalar,
167// _expr(indices));
168// }
169};
Definition BaseExpr.h:15
Definition ScalarExpr.h:95
ScalarExprLHS(const EXPR &expr, ScalarT scalar)
Definition ScalarExpr.h:110
const EXPR & expr() const noexcept
Definition ScalarExpr.h:112
my_size_t getNumDims() const noexcept
Definition ScalarExpr.h:146
my_size_t getDim(my_size_t i) const
Definition ScalarExpr.h:151
typename EXPR::Layout Layout
Definition ScalarExpr.h:108
bool may_alias(const Output &output) const noexcept
Definition ScalarExpr.h:116
auto operator()(my_size_t(&indices)[length]) const noexcept
Definition ScalarExpr.h:122
my_size_t getTotalSize() const noexcept
Definition ScalarExpr.h:156
ScalarT scalar() const noexcept
Definition ScalarExpr.h:113
static constexpr my_size_t NumDims
Definition ScalarExpr.h:104
Op< T, Bits, Arch >::type logical_evalu(my_size_t logical_flat) const noexcept
Definition ScalarExpr.h:139
static constexpr const my_size_t * Dim
Definition ScalarExpr.h:105
Op< T, Bits, Arch >::type evalu(const my_size_t flat) const noexcept
Definition ScalarExpr.h:131
typename EXPR::value_type value_type
Definition ScalarExpr.h:107
static constexpr my_size_t TotalSize
Definition ScalarExpr.h:106
Definition ScalarExpr.h:14
static constexpr const my_size_t * Dim
Definition ScalarExpr.h:24
static constexpr my_size_t NumDims
Definition ScalarExpr.h:23
typename EXPR::value_type value_type
Definition ScalarExpr.h:26
static constexpr my_size_t TotalSize
Definition ScalarExpr.h:25
auto operator()(const my_size_t *indices) const noexcept
Definition ScalarExpr.h:81
my_size_t getDim(my_size_t i) const
Definition ScalarExpr.h:70
auto operator()(my_size_t(&indices)[length]) const noexcept
Definition ScalarExpr.h:41
const EXPR & expr() const noexcept
Definition ScalarExpr.h:31
my_size_t getNumDims() const noexcept
Definition ScalarExpr.h:65
ScalarT scalar() const noexcept
Definition ScalarExpr.h:32
Op< T, Bits, Arch >::type logical_evalu(my_size_t logical_flat) const noexcept
Definition ScalarExpr.h:58
Op< T, Bits, Arch >::type evalu(const my_size_t flat) const noexcept
Definition ScalarExpr.h:50
bool may_alias(const Output &output) const noexcept
Definition ScalarExpr.h:35
my_size_t getTotalSize() const noexcept
Definition ScalarExpr.h:75
typename EXPR::Layout Layout
Definition ScalarExpr.h:27
ScalarExprRHS(const EXPR &expr, ScalarT scalar)
Definition ScalarExpr.h:29
#define my_size_t
Size/index type used throughout the library.
Definition config.h:126