VCTR
Loading...
Searching...
No Matches
Map.h
1/*
2 ==============================================================================
3 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
5 Copyright 2023 by sonible GmbH.
6
7 This file is part of VCTR - Versatile Container Templates Reconceptualized.
8
9 VCTR is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License version 3
11 only, as published by the Free Software Foundation.
12
13 VCTR is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License version 3 for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 version 3 along with VCTR. If not, see <https://www.gnu.org/licenses/>.
20 ==============================================================================
21*/
22
23namespace vctr::expressions
24{
25
26template <size_t extent, class SrcType, is::rangeWithValueType<ValueType<SrcType>> RangeType>
27requires is::realFloatNumber<ValueType<SrcType>>
29{
30public:
31 using value_type = ValueType<SrcType>;
32
33 VCTR_COMMON_UNARY_EXPRESSION_MEMBERS (Map, src)
34
35 void applyRuntimeArgs (RangeType newSrcRange, RangeType newDstRange)
36 {
37 srcRangeStartNegated = -newSrcRange.getStart();
38 srcDstLenRatio = newDstRange.getLength() / newSrcRange.getLength();
39 dstRangeStart = newDstRange.getStart();
40 }
41
42 VCTR_FORCEDINLINE constexpr value_type operator[] (size_t i) const
43 {
44 return (src[i] + srcRangeStartNegated) * srcDstLenRatio + dstRangeStart;
45 }
46
47 //==============================================================================
48 // Platform Vector Operation Implementation
49 VCTR_FORCEDINLINE const value_type* evalNextVectorOpInExpressionChain (value_type* dst) const
51 {
52 const auto s = size();
53
54 Expression::Accelerate::add (src.evalNextVectorOpInExpressionChain (dst), srcRangeStartNegated, dst, s);
55 Expression::Accelerate::mul (dst, srcDstLenRatio, dst, s);
56 Expression::Accelerate::add (dst, dstRangeStart, dst, s);
57
58 return dst;
59 }
60
61 VCTR_FORCEDINLINE const value_type* evalNextVectorOpInExpressionChain (value_type* dst) const
63 {
64 const auto s = size();
65
66 Expression::IPP::add (src.evalNextVectorOpInExpressionChain (dst), srcRangeStartNegated, dst, s);
67 Expression::IPP::mul (dst, srcDstLenRatio, dst, s);
68 Expression::IPP::add (dst, dstRangeStart, dst, s);
69
70 return dst;
71 }
72
73private:
74 //==============================================================================
75 value_type srcRangeStartNegated = 0;
76 value_type srcDstLenRatio = 0;
77 value_type dstRangeStart = 0;
78};
79
80template <size_t extent, class SrcType, is::rangeWithValueType<ValueType<SrcType>> RangeType>
83{
84public:
85 using value_type = ValueType<SrcType>;
86
87 VCTR_COMMON_UNARY_EXPRESSION_MEMBERS (MapFrom0To1, src)
88
89 void applyRuntimeArgs (RangeType newDstRange)
90 {
91 dstRangeStart = newDstRange.getStart();
92 dstRangeLen = newDstRange.getLength();
93 }
94
95 VCTR_FORCEDINLINE constexpr value_type operator[] (size_t i) const
96 {
97 return src[i] * dstRangeLen + dstRangeStart;
98 }
99
100 //==============================================================================
101 // Platform Vector Operation Implementation
102 VCTR_FORCEDINLINE const value_type* evalNextVectorOpInExpressionChain (value_type* dst) const
104 {
105 const auto s = size();
106
107 Expression::Accelerate::mul (src.evalNextVectorOpInExpressionChain (dst), dstRangeLen, dst, s);
108 Expression::Accelerate::add (dst, dstRangeStart, dst, s);
109
110 return dst;
111 }
112
113 VCTR_FORCEDINLINE const value_type* evalNextVectorOpInExpressionChain (value_type* dst) const
115 {
116 const auto s = size();
117
118 Expression::IPP::mul (src.evalNextVectorOpInExpressionChain (dst), dstRangeLen, dst, s);
119 Expression::IPP::add (dst, dstRangeStart, dst, s);
120
121 return dst;
122 }
123
124private:
125 //==============================================================================
126 value_type dstRangeStart = 0;
127 value_type dstRangeLen = 0;
128};
129
130template <size_t extent, class SrcType, is::rangeWithValueType<ValueType<SrcType>> RangeType>
133{
134public:
135 using value_type = ValueType<SrcType>;
136
137 VCTR_COMMON_UNARY_EXPRESSION_MEMBERS (MapTo0To1, src)
138
139 void applyRuntimeArgs (RangeType newSrcRange)
140 {
141 srcRangeStartNegated = -newSrcRange.getStart();
142 srcRangeLen = newSrcRange.getLength();
143 }
144
145 VCTR_FORCEDINLINE constexpr value_type operator[] (size_t i) const
146 {
147 return (src[i] + srcRangeStartNegated) / srcRangeLen;
148 }
149
150 //==============================================================================
151 // Platform Vector Operation Implementation
152 VCTR_FORCEDINLINE const value_type* evalNextVectorOpInExpressionChain (value_type* dst) const
154 {
155 const auto s = size();
156
157 Expression::Accelerate::add (src.evalNextVectorOpInExpressionChain (dst), srcRangeStartNegated, dst, s);
158 Expression::Accelerate::div (dst, srcRangeLen, dst, s);
159
160 return dst;
161 }
162
163 VCTR_FORCEDINLINE const value_type* evalNextVectorOpInExpressionChain (value_type* dst) const
165 {
166 const auto s = size();
167
168 Expression::IPP::add (src.evalNextVectorOpInExpressionChain (dst), srcRangeStartNegated, dst, s);
169 Expression::IPP::div (dst, srcRangeLen, dst, s);
170
171 return dst;
172 }
173
174private:
175 //==============================================================================
176 value_type srcRangeStartNegated = 0;
177 value_type srcRangeLen = 0;
178};
179
180} // namespace vctr::expressions
181
182namespace vctr
183{
184
191template <is::range RangeType>
192auto map (RangeType&& srcValueRange, RangeType&& dstValueRange)
193{
194 return makeTemplateExpressionChainBuilderWithRuntimeArgs<expressions::Map, std::remove_cvref_t<RangeType>> (std::forward<RangeType> (srcValueRange), std::forward<RangeType> (dstValueRange));
195}
196
203template <is::realFloatNumber T>
204auto map (T srcValueRangeStart, T srcValueRangeEnd, T dstValueRangeStart, T dstValueRangeEnd)
205{
206 return map (Range (srcValueRangeStart, srcValueRangeEnd), Range (dstValueRangeStart, dstValueRangeEnd));
207}
208
215template <is::range RangeType>
216auto mapFrom0To1 (RangeType&& dstValueRange)
217{
218 return makeTemplateExpressionChainBuilderWithRuntimeArgs<expressions::MapFrom0To1, std::remove_cvref_t<RangeType>> (std::forward<RangeType> (dstValueRange));
219}
220
227template <is::realFloatNumber T>
228auto mapFrom0To1 (T dstValueRangeStart, T dstValueRangeEnd)
229{
230 return mapFrom0To1 (vctr::Range (dstValueRangeStart, dstValueRangeEnd));
231}
232
239template <is::range RangeType>
240auto mapTo0To1 (RangeType&& srcValueRange)
241{
242 return makeTemplateExpressionChainBuilderWithRuntimeArgs<expressions::MapTo0To1, std::remove_cvref_t<RangeType>> (std::forward<RangeType> (srcValueRange));
243}
244
251template <is::realFloatNumber T>
252auto mapTo0To1 (T srcValueRangeStart, T srcValueRangeEnd)
253{
254 return mapTo0To1 (Range (srcValueRangeStart, srcValueRangeEnd));
255}
256
257}
A simple range class.
Definition: Range.h:32
Definition: Map.h:83
Definition: Map.h:133
Definition: Map.h:29
Constrains a type to represent a real valued floating point number.
Definition: NumericTypeConcepts.h:79
A combined concept to check if Apple Accelerate is a suitable option for a real valued floating point...
Definition: ContainerAndExpressionConcepts.h:242
A combined concept to check if Intel IPP is a suitable option for a real valued floating point vector...
Definition: ContainerAndExpressionConcepts.h:266
auto mapFrom0To1(RangeType &&dstValueRange)
Maps all source element values from the range [0.0 to 1.0] to values in dstValueRange.
Definition: Map.h:216
auto mapTo0To1(RangeType &&srcValueRange)
Maps all source element values from srcValueRange to values in the range [0.0 to 1....
Definition: Map.h:240
auto map(RangeType &&srcValueRange, RangeType &&dstValueRange)
Maps all source element values from srcValueRange to values in dstValueRange.
Definition: Map.h:192
The main namespace of the VCTR project.
Definition: Array.h:24
typename detail::ValueType< std::remove_cvref_t< T > >::Type ValueType
If T is an expression template, it equals its return type, if it's a type that defines value_type as ...
Definition: Traits.h:201
The base class to every expression template.
Definition: ExpressionTemplate.h:37