API Documentation
Loading...
Searching...
No Matches
BaseFunctions.hpp
Go to the documentation of this file.
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Base
28File: BaseFunctions
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include <NDEVR/BaseValues.h>
34#include <NDEVR/LibAssert.h>
35namespace NDEVR
36{
37
38 /**
39 \brief Finds the minimum of the given arguments based on the < operator
40 Author: Tyler Parke
41 Date: 2017-11-05
42 \param[in] left A t_type to process.
43 \param[in] right A t_type to process.
44
45 The only requirement is that t_type have > operator defined.
46
47 For Vector-based structures a new object is created that, for each dimension stores the max for that
48 dimension.
49
50 Note integers, use same as the templated getMax, however, we can optimize specially for integers using
51 some bit twiddling tricks. This removes a branch statement entirely and is likely faster on a modern CPU
52
53 \return The calculated minimum.
54 **/
55 template <typename t_type>
56 constexpr t_type getMin(const t_type& left, const t_type& right) { return left < right ? left : right; }
57 template <typename t_type>
58 constexpr t_type getMin(const t_type& left, const t_type& middle, const t_type& right) { return getMin(getMin(left, middle), right); }
59
60 template<>
61 constexpr sint01 getMin(const sint01& left, const sint01& right) { return left ^ ((right ^ left) & -(right < left)); }
62 template<>
63 constexpr uint01 getMin(const uint01& left, const uint01& right) { return static_cast<uint01>(left ^ ((right ^ left) & -(right < left))); }
64
65 template<>
66 constexpr sint02 getMin(const sint02& left, const sint02& right) { return left ^ ((right ^ left) & -(right < left)); }
67 template<>
68 constexpr uint02 getMin(const uint02& left, const uint02& right) { return static_cast<uint02>(left ^ ((right ^ left) & -(right < left))); }
69
70 template<>
71 constexpr sint04 getMin(const sint04& left, const sint04& right) { return left ^ ((right ^ left) & -(right < left)); }
72 template<>
73 constexpr uint04 getMin(const uint04& left, const uint04& right) { return static_cast<uint04>(left ^ ((right ^ left) & -(right < left))); }
74
75 template<>
76 constexpr sint08 getMin(const sint08& left, const sint08& right) { return left ^ ((right ^ left) & -(right < left)); }
77 template<>
78 constexpr uint08 getMin(const uint08& left, const uint08& right) { return static_cast<uint08>(left ^ ((right ^ left) & -(right < left))); }
79
80 /**
81 \brief Finds the max of the given arguments using the > operator
82 The only requirement is that t_type have > operator defined.
83
84 For Vector-based structures a new object is created that, for each dimension stores the max for that
85 dimension.
86
87 Note integers, use same as the templated getMax, however, we can optimize specially for integers using
88 some bit twiddling tricks. This removes a branch statement entirely and is likely faster on a modern CPU
89 \param[in] left - One of two comparators to determine the max of.
90 \param[in] right - One of two comparators to determine the max of.
91 \return The calculated maximum.
92 **/
93 template <typename t_type>
94 constexpr t_type getMax(const t_type& left, const t_type& right) { return left > right ? left : right; }
95 template <typename t_type>
96 constexpr t_type getMax(const t_type& left, const t_type& middle, const t_type& right) { return getMax(getMax(left, middle), right); }
97
98 template<>
99 constexpr sint01 getMax(const sint01& left, const sint01& right) { return left ^ ((left ^ right) & -(left < right)); }
100 template<>
101 constexpr uint01 getMax(const uint01& left, const uint01& right) { return static_cast<uint01>(left ^ ((left ^ right) & -(left < right))); }
102
103 template<>
104 constexpr sint02 getMax(const sint02& left, const sint02& right) { return left ^ ((left ^ right) & -(left < right)); }
105 template<>
106 constexpr uint02 getMax(const uint02& left, const uint02& right) { return static_cast<uint02>(left ^ ((left ^ right) & -(left < right))); }
107
108 template<>
109 constexpr sint04 getMax(const sint04& left, const sint04& right) { return left ^ ((left ^ right) & -(left < right)); }
110 template<>
111 constexpr uint04 getMax(const uint04& left, const uint04& right) { return static_cast<uint04>(left ^ ((left ^ right) & -(left < right))); }
112
113 template<>
114 constexpr sint08 getMax(const sint08& left, const sint08& right) { return left ^ ((left ^ right) & -(left < right)); }
115 template<>
116 constexpr uint08 getMax(const uint08& left, const uint08& right) { return static_cast<uint08>(left ^ ((left ^ right) & -(left < right))); }
117
118
119
120 /**
121 \brief Rounds the value to the nearest multiple given and returns that value.
122
123 For example quantize(.8, .25) would return .75.
124 quantize(.8, .1) would return .8 and quantize(.8, .5) would return 1.0
125 Author: Tyler Parke
126
127 Date: 2017-11-05
128
129 \param[in] value A t_type to process.
130 \param[in] d (Optional) A t_type to process. Default value is 1.
131 \return The nearest multiple from the given input.
132 **/
133 template <typename t_type>
134 constexpr t_type quantize(t_type value, t_type d = cast<t_type>(1))
135 {
136 return cast<t_type>(cast<sint08>(value / d + cast<t_type>(value < 0 ? -0.5 : 0.5))) * d;
137 }
138
139 /**
140 \brief A simple function that returns 1 for all values greater than or equal to 0 and -1 for all values less than 0
141
142 Author: Tyler Parke
143
144 Date: 2017-11-05
145
146 \param[in] value - The value to check the sign of.
147
148 \return 1 for all values greater than or equal to 0 and -1 for all values less than 0.
149 **/
150 template <typename t_type>
151 constexpr t_type sign(t_type value)
152 {
153 return value >= 0 ? cast<t_type>(1) : cast<t_type>(-1);
154 }
155
156
157 /**
158 \brief Query if 'value' is valid or invalid. Invalid values should return invalid if used for
159 calculations or false for operator checks, when possible.
160
161 Author: Tyler Parke
162
163 Date: 2017-11-05
164
165 \param[in] value The value.
166
167 \return True if the value is invalid, false if the value is valid.
168 **/
169 template<class t_type>
170 constexpr bool IsInvalid(const t_type& value)
171 {
172 return value == Constant<t_type>::Invalid;
173 }
174 template<>
175 constexpr bool IsInvalid(const fltp04& value)
176 {
177 // ReSharper disable once CppIdenticalOperandsInBinaryExpression
178 return value != value;
179 }
180 template<>
181 constexpr bool IsInvalid(const fltp08& value)
182 {
183 // ReSharper disable once CppIdenticalOperandsInBinaryExpression
184 return value != value;
185 }
186
187 /**
188 \brief Clips the value given so that that the returned value falls between upper and lower bound.
189
190 For example, clip(-1, 0, 10) would return 0, clip(.5, 0, 10) would return .5, and clip(11.5, 0, 10)
191 would return 10.
192
193 Author: Tyler Parke
194
195 Date: 2017-11-05
196
197 \param[in] value The value. If this falls between the upper and lower bound it is returned.
198 \param[in] lower_bound The lower bound. It is assumed that this will always be a value less than or equal to
199 the upper bound.
200 \param[in] upper_bound The upper bound. It is assumed that this will always be a value greater than or equal to
201 the lower bound.
202
203 \return the value if it falls between the upper or lower bound, otherwise if less than lower bound.
204 returns lower bound and if greater than upper bound returns upper bound.
205 **/
206 template <typename t_type>
207 constexpr t_type clip(const t_type& value, const t_type& lower_bound, const t_type& upper_bound)
208 {
209 lib_assert(IsInvalid(lower_bound) || IsInvalid(upper_bound) || (lower_bound <= upper_bound), "Invalid clip: upper bound is less than lower bound");
210 return getMax(lower_bound, getMin(value, upper_bound));
211 }
212};
213
214
#define lib_assert(expression, message)
Definition LibAssert.h:61
Definition ACIColor.h:37
int32_t sint04
-Defines an alias representing a 4 byte, signed integer. -Can represent exact integer values -2147483...
Definition BaseValues.hpp:64
constexpr bool IsInvalid(const t_type &value)
Query if 'value' is valid or invalid. Invalid values should return invalid if used for calculations o...
Definition BaseFunctions.hpp:170
constexpr t_type getMax(const t_type &left, const t_type &right)
Finds the max of the given arguments using the > operator The only requirement is that t_type have > ...
Definition BaseFunctions.hpp:94
int64_t sint08
-Defines an alias representing an 8 byte, signed integer -Can represent exact integer values -9223372...
Definition BaseValues.hpp:71
constexpr t_type clip(const t_type &value, const t_type &lower_bound, const t_type &upper_bound)
Clips the value given so that that the returned value falls between upper and lower bound.
Definition BaseFunctions.hpp:207
float fltp04
Defines an alias representing a 4 byte floating-point number Bit layout is as follows: -Sign: 1 bit a...
Definition BaseValues.hpp:127
constexpr Vector< t_dims, Angle< t_angle_type > > quantize(const Vector< t_dims, Angle< t_angle_type > > &value, Angle< t_angle_type > d=Angle< t_angle_type >(DEGREES, 1.0))
Definition AngleFunctions.h:828
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:80
int8_t sint01
-Defines an alias representing a 1 byte, signed integer. -Can represent exact integer values -127 thr...
Definition BaseValues.hpp:50
int16_t sint02
-Defines an alias representing a 2 byte, signed integer. -Can represent exact integer values -32767 t...
Definition BaseValues.hpp:57
uint64_t uint08
-Defines an alias representing an 8 byte, unsigned integer
Definition BaseValues.hpp:106
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:96
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:375
uint16_t uint02
-Defines an alias representing a 2 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:88
constexpr t_type sign(t_type value)
A simple function that returns 1 for all values greater than or equal to 0 and -1 for all values less...
Definition BaseFunctions.hpp:151
double fltp08
Defines an alias representing an 8 byte floating-point number.
Definition BaseValues.hpp:149
constexpr t_type getMin(const t_type &left, const t_type &right)
Finds the minimum of the given arguments based on the < operator Author: Tyler Parke Date: 2017-11-05...
Definition BaseFunctions.hpp:56
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved 'invali...
Definition BaseValues.hpp:233