31#ifndef ETL_NUMERIC_INCLUDED
32#define ETL_NUMERIC_INCLUDED
57 template <
typename TIterator,
typename T>
58 ETL_CONSTEXPR14
void iota(TIterator first, TIterator last, T value)
71 ETL_CONSTEXPR14
typename etl::enable_if< !etl::is_pointer<T>::value && !etl::is_integral<T>::value && etl::is_floating_point<T>::value, T>
::type
77 return ((
abs(a) <= hi) && (
abs(b) <= hi)) ? (a + b) / T(2)
78 : (
abs(a) < lo) ? a + (b / T(2))
79 : (
abs(b) < lo) ? ((a / T(2)) + b)
80 : (a / T(2)) + (b / T(2));
88 ETL_CONSTEXPR14
typename etl::enable_if<
89 !etl::is_pointer<T>::value && etl::is_integral<T>::value && !etl::is_floating_point<T>::value && etl::is_unsigned<T>::value, T>
::type
94 return a - ((a - b) >> 1);
98 return a + ((b - a) >> 1);
106 template <
typename T>
107 ETL_CONSTEXPR14
typename etl::enable_if<
108 !etl::is_pointer<T>::value && etl::is_integral<T>::value && !etl::is_floating_point<T>::value && etl::is_signed<T>::value, T>
::type
111 typedef typename etl::make_unsigned<T>::type utype;
115 return a - T(utype(utype(a) - utype(b)) >> 1);
119 return a + T((utype(b) - utype(a)) >> 1);
127 template <
typename T>
128 ETL_CONSTEXPR14
typename etl::enable_if< etl::is_pointer<T>::value && !etl::is_integral<T>::value && !etl::is_floating_point<T>::value, T>
::type
133 return b + (etl::distance(b, a) / 2);
137 return a + (etl::distance(a, b) / 2);
145 template <
typename T>
148 typename etl::enable_if< !etl::is_pointer<T>::value && !etl::is_integral<T>::value && !etl::is_floating_point<T>::value
149 && etl::is_same<
typename etl::iterator_traits<T>::iterator_category, ETL_OR_STD::random_access_iterator_tag>::value,
154 return b + (etl::distance(b, a) / 2);
158 return a + (etl::distance(a, b) / 2);
167 template <
typename T>
169 typename etl::enable_if<
170 (!etl::is_pointer<T>::value && !etl::is_integral<T>::value && !etl::is_floating_point<T>::value
171 && (etl::is_same<
typename etl::iterator_traits<T>::iterator_category, ETL_OR_STD::forward_iterator_tag>::value
172 || etl::is_same<
typename etl::iterator_traits<T>::iterator_category, ETL_OR_STD::bidirectional_iterator_tag>::value)),
175 etl::advance(a, etl::distance(a, b) / 2);
183 template <
typename T>
184 ETL_CONSTEXPR
typename etl::enable_if<etl::is_floating_point<T>::value, T>
::type lerp(T a, T b, T t) ETL_NOEXCEPT
186 return a + (t * (b - a));
193 template <
typename TArithmetic1,
typename TArithmetic2,
typename TArithmetic3>
194 ETL_CONSTEXPR
typename etl::enable_if<
195 !etl::is_floating_point<TArithmetic1>::value || !etl::is_floating_point<TArithmetic2>::value || !etl::is_floating_point<TArithmetic3>::value,
196 typename etl::conditional< etl::is_same<TArithmetic1, long double>::value || etl::is_same<TArithmetic2, long double>::value
197 || etl::is_same<TArithmetic3, long double>::value,
199 lerp(TArithmetic1 a, TArithmetic2 b, TArithmetic3 t) ETL_NOEXCEPT
201 typedef typename etl::conditional<etl::is_integral<TArithmetic1>::value, double, TArithmetic1>
::type typecast_a;
202 typedef typename etl::conditional<etl::is_integral<TArithmetic2>::value, double, TArithmetic2>
::type typecast_b;
203 typedef typename etl::conditional<etl::is_integral<TArithmetic3>::value, double, TArithmetic3>
::type typecast_t;
205 return typecast_a(a) + (typecast_t(t) * (typecast_b(b) - typecast_a(a)));
212 template <
typename T>
213 ETL_CONSTEXPR14
typename etl::enable_if<etl::is_integral<T>::value && etl::is_unsigned<T>::value, T>
::type add_sat(T x, T y) ETL_NOEXCEPT
215 T result =
static_cast<T
>(x + y);
228 template <
typename T>
229 ETL_CONSTEXPR14
typename etl::enable_if<etl::is_integral<T>::value && etl::is_signed<T>::value, T>
::type add_sat(T x, T y) ETL_NOEXCEPT
249 return static_cast<T
>(x + y);
256 template <
typename T>
257 ETL_CONSTEXPR14
typename etl::enable_if<etl::is_integral<T>::value && etl::is_unsigned<T>::value, T>
::type sub_sat(T x, T y) ETL_NOEXCEPT
264 return static_cast<T
>(x - y);
271 template <
typename T>
272 ETL_CONSTEXPR14
typename etl::enable_if<etl::is_integral<T>::value && etl::is_signed<T>::value, T>
::type sub_sat(T x, T y) ETL_NOEXCEPT
292 return static_cast<T
>(x - y);
299 template <
typename T>
300 ETL_CONSTEXPR14
typename etl::enable_if<etl::is_integral<T>::value && etl::is_unsigned<T>::value, T>
::type mul_sat(T x, T y) ETL_NOEXCEPT
302 if ((x == T(0)) || (y == T(0)))
313 return static_cast<T
>(x * y);
320 template <
typename T>
321 ETL_CONSTEXPR14
typename etl::enable_if<etl::is_integral<T>::value && etl::is_signed<T>::value, T>
::type mul_sat(T x, T y) ETL_NOEXCEPT
323 if ((x == T(0)) || (y == T(0)))
329 if ((x > T(0)) && (y > T(0)))
337 else if ((x < T(0)) && (y < T(0)))
345 else if ((x > T(0)) && (y < T(0)))
361 return static_cast<T
>(x * y);
369 template <
typename T>
370 ETL_CONSTEXPR14
typename etl::enable_if<etl::is_integral<T>::value && etl::is_unsigned<T>::value, T>
::type div_sat(T x, T y) ETL_NOEXCEPT
372 return static_cast<T
>(x / y);
381 template <
typename T>
382 ETL_CONSTEXPR14
typename etl::enable_if<etl::is_integral<T>::value && etl::is_signed<T>::value, T>
::type div_sat(T x, T y) ETL_NOEXCEPT
390 return static_cast<T
>(x / y);
405 template <
typename R,
typename T>
407 typename etl::enable_if<etl::is_integral<R>::value && etl::is_integral<T>::value && etl::is_unsigned<R>::value && etl::is_unsigned<T>::value,
418 return static_cast<R
>(value);
422 template <
typename R,
typename T>
424 typename etl::enable_if<etl::is_integral<R>::value && etl::is_integral<T>::value && etl::is_signed<R>::value && etl::is_signed<T>::value, R>
::type
429 if (
sizeof(R) <
sizeof(T))
435 if (value <
static_cast<T
>(etl::numeric_limits<R>::min()))
437 return etl::numeric_limits<R>::min();
440 return static_cast<R
>(value);
444 template <
typename R,
typename T>
446 typename etl::enable_if<etl::is_integral<R>::value && etl::is_integral<T>::value && etl::is_unsigned<R>::value && etl::is_signed<T>::value,
455 typedef typename etl::make_unsigned<T>::type unsigned_t;
456 unsigned_t uvalue =
static_cast<unsigned_t
>(value);
461 if ((
sizeof(R) <
sizeof(T)) && (uvalue >
static_cast<unsigned_t
>(etl::numeric_limits<R>::max())))
463 return etl::numeric_limits<R>::max();
465 return static_cast<R
>(value);
469 template <
typename R,
typename T>
471 typename etl::enable_if<etl::is_integral<R>::value && etl::is_integral<T>::value && etl::is_signed<R>::value && etl::is_unsigned<T>::value,
477 typedef typename etl::make_unsigned<R>::type unsigned_r;
479 if (value >
static_cast<T
>(
static_cast<unsigned_r
>(etl::numeric_limits<R>::max())))
481 return etl::numeric_limits<R>::max();
483 return static_cast<R
>(value);
ETL_CONSTEXPR14 void iota(TIterator first, TIterator last, T value)
Definition numeric.h:58
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value &&etl::is_unsigned< T >::value, T >::type add_sat(T x, T y) ETL_NOEXCEPT
Definition numeric.h:213
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value &&etl::is_unsigned< T >::value, T >::type mul_sat(T x, T y) ETL_NOEXCEPT
Definition numeric.h:300
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value &&etl::is_unsigned< T >::value, T >::type div_sat(T x, T y) ETL_NOEXCEPT
Definition numeric.h:370
ETL_CONSTEXPR14 etl::chrono::duration< TRep, TPeriod > abs(etl::chrono::duration< TRep, TPeriod > d) ETL_NOEXCEPT
Returns the absolute value of a duration.
Definition duration.h:688
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value &&etl::is_unsigned< T >::value, T >::type sub_sat(T x, T y) ETL_NOEXCEPT
Definition numeric.h:257
ETL_CONSTEXPR etl::enable_if< etl::is_floating_point< T >::value, T >::type lerp(T a, T b, T t) ETL_NOEXCEPT
Definition numeric.h:184
ETL_CONSTEXPR14 etl::enable_if<!etl::is_pointer< T >::value &&!etl::is_integral< T >::value &&etl::is_floating_point< T >::value, T >::type midpoint(T a, T b) ETL_NOEXCEPT
Definition numeric.h:72
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< R >::value &&etl::is_integral< T >::value &&etl::is_unsigned< R >::value &&etl::is_unsigned< T >::value, R >::type saturate_cast(T value) ETL_NOEXCEPT
Definition numeric.h:409