tesseract++ 0.0.1
N-dimensional tensor library for embedded systems
Loading...
Searching...
No Matches
BinaryExpr.h
Go to the documentation of this file.
1#pragma once
2#include "config.h"
3#include "fused/BaseExpr.h"
4#include "fused/Operations.h"
5#include "helper_traits.h"
7
8// ===============================
9// Binary Expression Template
10// ===============================
11template <
12 typename LHS, typename RHS,
13 template <typename, my_size_t, typename> class Op>
14class BinaryExpr : public BaseExpr<BinaryExpr<LHS, RHS, Op>>
15{
16 // Compile-time check that LHS and RHS have the same value_type
17 static_assert(is_same_v<typename LHS::value_type, typename RHS::value_type>,
18 "BinaryExpr: LHS and RHS must have the same value_type");
19
20#ifdef COMPILETIME_CHECK_DIMENSIONS_COUNT_MISMATCH
21 // Compile-time check that both expressions have the same number of dimensions
22 static_assert(LHS::NumDims == RHS::NumDims,
23 "BinaryExpr: number of dimensions mismatch");
24#endif
25#ifdef COMPILETIME_CHECK_DIMENSIONS_SIZE_MISMATCH
26 // Compile-time check that both expressions have the same dimensions
27 static_assert(dims_match<LHS::NumDims>(LHS::Dim, RHS::Dim),
28 "BinaryExpr: there is at least one dimension mismatch");
29#endif
30 // TODO: runtime checks should be here and not in the operators (in this constructor?)
31 // Or not, because in the case of the permuted views? We don't want checks there...
32
33 const LHS &_lhs;
34 const RHS &_rhs;
35
36public:
37 // Expose compile-time shape constants
38 static constexpr my_size_t NumDims = LHS::NumDims;
39 static constexpr const my_size_t *Dim = LHS::Dim;
40 static constexpr my_size_t TotalSize = LHS::TotalSize;
41 using value_type = typename LHS::value_type;
42 using Layout = typename LHS::Layout;
43
44 BinaryExpr(const LHS &lhs, const RHS &rhs) : _lhs(lhs), _rhs(rhs) {}
45
46 const LHS &lhs() const noexcept { return _lhs; }
47 const RHS &rhs() const noexcept { return _rhs; }
48
49 template <typename Output>
50 bool may_alias(const Output &output) const noexcept
51 {
52 return _lhs.may_alias(output) || _rhs.may_alias(output);
53 }
54
55 template <my_size_t length>
56 inline auto operator()(my_size_t (&indices)[length]) const noexcept
57 {
58 using T = std::decay_t<decltype(_lhs(indices))>; // TODO: get rid of std
59 return Op<T, 0, GENERICARCH>::apply(_lhs(indices), _rhs(indices));
60 }
61
62 template <typename T, my_size_t Bits, typename Arch>
63 typename Op<T, Bits, Arch>::type evalu(my_size_t flat) const noexcept
64 {
65 return Op<T, Bits, Arch>::apply(
66 _lhs.template evalu<T, Bits, Arch>(flat),
67 _rhs.template evalu<T, Bits, Arch>(flat));
68 }
69
70 template <typename T, my_size_t Bits, typename Arch>
71 typename Op<T, Bits, Arch>::type logical_evalu(my_size_t logical_flat) const noexcept
72 {
73 return Op<T, Bits, Arch>::apply(
74 _lhs.template logical_evalu<T, Bits, Arch>(logical_flat),
75 _rhs.template logical_evalu<T, Bits, Arch>(logical_flat));
76 }
77
78 // Forward getNumDims to _lhs
79 inline my_size_t getNumDims() const noexcept
80 {
81 return _lhs.getNumDims();
82 }
83
84 // Forward getDim(i) to _lhs
85 inline my_size_t getDim(my_size_t i) const // TODO: conditionally noexcept
86 {
87 return _lhs.getDim(i);
88 }
89
90 my_size_t getTotalSize() const noexcept
91 {
92 return _lhs.getTotalSize();
93 }
94
95// protected:
96 // inline auto operator()(const my_size_t *indices) const noexcept
97 // {
98 // using T = std::decay_t<decltype(_lhs(indices))>; // TODO: get rid of std
99 // return Op<T, 0, GENERICARCH>::apply(_lhs(indices), _rhs(indices));
100 // }
101};
Definition BaseExpr.h:15
Definition BinaryExpr.h:15
static constexpr my_size_t TotalSize
Definition BinaryExpr.h:40
Op< T, Bits, Arch >::type evalu(my_size_t flat) const noexcept
Definition BinaryExpr.h:63
BinaryExpr(const LHS &lhs, const RHS &rhs)
Definition BinaryExpr.h:44
bool may_alias(const Output &output) const noexcept
Definition BinaryExpr.h:50
my_size_t getTotalSize() const noexcept
Definition BinaryExpr.h:90
my_size_t getNumDims() const noexcept
Definition BinaryExpr.h:79
my_size_t getDim(my_size_t i) const
Definition BinaryExpr.h:85
const LHS & lhs() const noexcept
Definition BinaryExpr.h:46
typename LHS::Layout Layout
Definition BinaryExpr.h:42
typename LHS::value_type value_type
Definition BinaryExpr.h:41
static constexpr my_size_t NumDims
Definition BinaryExpr.h:38
Op< T, Bits, Arch >::type logical_evalu(my_size_t logical_flat) const noexcept
Definition BinaryExpr.h:71
static constexpr const my_size_t * Dim
Definition BinaryExpr.h:39
const RHS & rhs() const noexcept
Definition BinaryExpr.h:47
auto operator()(my_size_t(&indices)[length]) const noexcept
Definition BinaryExpr.h:56
Global configuration for the tesseract tensor library.
#define my_size_t
Size/index type used throughout the library.
Definition config.h:126