31#ifndef ETL_ROUNDED_INTEGRAL_DIVISION_INCLUDED
32#define ETL_ROUNDED_INTEGRAL_DIVISION_INCLUDED
41 namespace private_rounded_integral_division
48 ETL_CONSTEXPR
typename etl::enable_if< etl::is_integral<T>::value && etl::is_signed<T>::value,
bool>::type are_same_sign(T a, T b) ETL_NOEXCEPT
50 return ((a ^ b) >= 0);
58 ETL_CONSTEXPR
typename etl::enable_if< etl::is_integral<T>::value && etl::is_unsigned<T>::value,
bool>::type are_same_sign(T , T )
78 const T remainder = numerator % denominator;
79 const T quotient = numerator / denominator;
88 return private_rounded_integral_division::are_same_sign(numerator, denominator) ? quotient + 1 : quotient;
100 template <
typename T1,
typename T2>
102 typename etl::enable_if< etl::is_integral<T1>::value && etl::is_integral<T2>::value && etl::is_signed<T1>::value && etl::is_signed<T2>::value,
103 typename etl::common_type<T1, T2>::type>
::type
106 typedef typename etl::common_type<T1, T2>::type
type;
119 template <
typename T>
124 const T remainder = numerator % denominator;
125 const T quotient = numerator / denominator;
128 return remainder == 0U ? quotient : quotient + 1U;
140 template <
typename T1,
typename T2>
142 typename etl::enable_if< etl::is_integral<T1>::value && etl::is_integral<T2>::value && etl::is_unsigned<T1>::value && etl::is_unsigned<T2>::value,
143 typename etl::common_type<T1, T2>::type>
::type
146 typedef typename etl::common_type<T1, T2>::type
type;
159 template <
typename T>
164 const T remainder = numerator % denominator;
165 const T quotient = numerator / denominator;
174 return private_rounded_integral_division::are_same_sign(numerator, denominator) ? quotient : quotient - 1;
186 template <
typename T1,
typename T2>
188 typename etl::enable_if< etl::is_integral<T1>::value && etl::is_integral<T2>::value && etl::is_signed<T1>::value && etl::is_signed<T2>::value,
189 typename etl::common_type<T1, T2>::type>
::type
192 typedef typename etl::common_type<T1, T2>::type
type;
205 template <
typename T>
210 return numerator / denominator;
222 template <
typename T1,
typename T2>
224 typename etl::enable_if< etl::is_integral<T1>::value && etl::is_integral<T2>::value && etl::is_unsigned<T1>::value && etl::is_unsigned<T2>::value,
225 typename etl::common_type<T1, T2>::type>
::type
228 typedef typename etl::common_type<T1, T2>::type
type;
230 const type common_numerator = numerator;
231 const type common_denominator = denominator;
233 return common_numerator / common_denominator;
244 template <
typename T>
249 const T remainder = numerator % denominator;
250 const T quotient = numerator / denominator;
252 if (private_rounded_integral_division::are_same_sign(numerator, denominator))
255 return (remainder != 0) ? quotient + 1 : quotient;
260 return (remainder != 0) ? quotient - 1 : quotient;
273 template <
typename T1,
typename T2>
275 typename etl::enable_if< etl::is_integral<T1>::value && etl::is_integral<T2>::value && etl::is_signed<T1>::value && etl::is_signed<T2>::value,
276 typename etl::common_type<T1, T2>::type>
::type
279 typedef typename etl::common_type<T1, T2>::type
type;
292 template <
typename T>
293 ETL_CONSTEXPR14
typename etl::enable_if< etl::is_integral<T>::value && etl::is_unsigned<T>::value, T>
::type
296 const T remainder = numerator % denominator;
297 const T quotient = numerator / denominator;
299 return remainder ? quotient + 1U : quotient;
311 template <
typename T1,
typename T2>
313 typename etl::enable_if< etl::is_integral<T1>::value && etl::is_integral<T2>::value && etl::is_unsigned<T1>::value && etl::is_unsigned<T2>::value,
314 typename etl::common_type<T1, T2>::type>
::type
317 typedef typename etl::common_type<T1, T2>::type
type;
330 template <
typename T>
331 ETL_CONSTEXPR14
typename etl::enable_if< etl::is_integral<T>::value && etl::is_unsigned<T>::value, T>
::type divide_round_to_zero(T numerator,
335 return numerator / denominator;
347 template <
typename T1,
typename T2>
348 ETL_CONSTEXPR14
typename etl::enable_if<etl::is_integral<T1>::value && etl::is_integral<T2>::value,
typename etl::common_type<T1, T2>::type>
::type
351 typedef typename etl::common_type<T1, T2>::type
type;
354 const type common_numerator = numerator;
355 const type common_denominator = denominator;
357 return common_numerator / common_denominator;
368 template <
typename T>
374 const T remainder = numerator % denominator;
375 const T quotient = numerator / denominator;
378 typedef typename etl::make_unsigned<T>::type utype;
379 utype abs_remainder = remainder < 0 ? utype(0) - utype(remainder) : utype(remainder);
380 utype abs_denominator = denominator < 0 ? utype(0) - utype(denominator) : utype(denominator);
383 utype half_denominator = (abs_denominator + 1) / 2;
385 if (abs_remainder >= half_denominator)
388 if (private_rounded_integral_division::are_same_sign(numerator, denominator))
410 template <
typename T1,
typename T2>
412 typename etl::enable_if< etl::is_integral<T1>::value && etl::is_integral<T2>::value && etl::is_signed<T1>::value && etl::is_signed<T2>::value,
413 typename etl::common_type<T1, T2>::type>
::type
416 typedef typename etl::common_type<T1, T2>::type
type;
429 template <
typename T>
430 ETL_CONSTEXPR14
typename etl::enable_if< etl::is_integral<T>::value && etl::is_unsigned<T>::value, T>
::type divide_round_half_up(T numerator,
434 const T remainder = numerator % denominator;
435 const T quotient = numerator / denominator;
438 return (remainder >= (denominator / 2U) + (denominator % 2U)) ? quotient + 1U : quotient;
450 template <
typename T1,
typename T2>
452 typename etl::enable_if< etl::is_integral<T1>::value && etl::is_integral<T2>::value && etl::is_unsigned<T1>::value && etl::is_unsigned<T2>::value,
453 typename etl::common_type<T1, T2>::type>
::type
456 typedef typename etl::common_type<T1, T2>::type
type;
469 template <
typename T>
474 const T quotient = numerator / denominator;
475 const T remainder = numerator % denominator;
477 typedef typename etl::make_unsigned<T>::type utype;
478 const utype abs_denominator = etl::absolute_unsigned(denominator);
479 const utype abs_remainder = etl::absolute_unsigned(remainder);
482 const T direction = private_rounded_integral_division::are_same_sign(numerator, denominator) ? 1 : -1;
486 return abs_remainder > (abs_denominator / 2U) ? quotient + direction : quotient;
498 template <
typename T1,
typename T2>
500 typename etl::enable_if< etl::is_integral<T1>::value && etl::is_integral<T2>::value && etl::is_signed<T1>::value && etl::is_signed<T2>::value,
501 typename etl::common_type<T1, T2>::type>
::type
504 typedef typename etl::common_type<T1, T2>::type
type;
517 template <
typename T>
522 const T remainder = numerator % denominator;
523 const T quotient = numerator / denominator;
526 return (remainder > (denominator / 2U)) ? quotient + 1U : quotient;
538 template <
typename T1,
typename T2>
540 typename etl::enable_if< etl::is_integral<T1>::value && etl::is_integral<T2>::value && etl::is_unsigned<T1>::value && etl::is_unsigned<T2>::value,
541 typename etl::common_type<T1, T2>::type>
::type
544 typedef typename etl::common_type<T1, T2>::type
type;
557 template <
typename T>
562 const T quotient = numerator / denominator;
563 const T remainder = numerator % denominator;
564 const T direction = ((numerator >= 0) == (denominator >= 0)) ? 1 : -1;
568 typedef typename std::make_unsigned<T>::type utype;
569 const utype abs_denominator = (denominator < 0) ? (utype(0) - utype(denominator)) : utype(denominator);
570 const utype abs_remainder = (remainder < 0) ? (utype(0) - utype(remainder)) : utype(remainder);
571 const utype half_denominator = abs_denominator / 2U;
574 if ((abs_denominator & 1U) == 0U)
577 if (abs_remainder < half_denominator)
581 else if (abs_remainder > half_denominator)
583 return quotient + direction;
588 return (quotient & 1) == 0 ? quotient : quotient + direction;
594 return (abs_remainder <= half_denominator) ? quotient : (quotient + direction);
607 template <
typename T1,
typename T2>
609 typename etl::enable_if< etl::is_integral<T1>::value && etl::is_integral<T2>::value && etl::is_signed<T1>::value && etl::is_signed<T2>::value,
610 typename etl::common_type<T1, T2>::type>
::type
613 typedef typename etl::common_type<T1, T2>::type
type;
626 template <
typename T>
631 const T quotient = numerator / denominator;
632 const T remainder = numerator % denominator;
634 if ((remainder * 2U) < denominator)
639 else if ((remainder * 2U) > denominator)
642 return quotient + 1U;
647 return (quotient & 1U) == 0U ? quotient : quotient + 1;
660 template <
typename T1,
typename T2>
662 typename etl::enable_if< etl::is_integral<T1>::value && etl::is_integral<T2>::value && etl::is_unsigned<T1>::value && etl::is_unsigned<T2>::value,
663 typename etl::common_type<T1, T2>::type>
::type
666 typedef typename etl::common_type<T1, T2>::type
type;
679 template <
typename T>
684 const T quotient = numerator / denominator;
685 const T remainder = numerator % denominator;
687 typedef typename etl::make_unsigned<T>::type utype;
688 const utype abs_denominator = etl::absolute_unsigned(denominator);
689 const utype abs_remainder = etl::absolute_unsigned(remainder);
690 const utype half = abs_denominator / 2U;
691 const T direction = private_rounded_integral_division::are_same_sign(numerator, denominator) ? 1 : -1;
694 if ((abs_denominator & 1U) != 0U)
696 return (abs_remainder > half) ? quotient + direction : quotient;
699 if (abs_remainder < half)
703 else if (abs_remainder > half)
705 return quotient + direction;
710 return (quotient & 1) != 0 ? quotient : quotient + direction;
723 template <
typename T1,
typename T2>
725 typename etl::enable_if< etl::is_integral<T1>::value && etl::is_integral<T2>::value && etl::is_signed<T1>::value && etl::is_signed<T2>::value,
726 typename etl::common_type<T1, T2>::type>
::type
729 typedef typename etl::common_type<T1, T2>::type
type;
742 template <
typename T>
747 const T quotient = numerator / denominator;
748 const T remainder = numerator % denominator;
750 if ((remainder * 2U) < denominator)
754 else if ((remainder * 2U) > denominator)
756 return quotient + 1U;
761 return (quotient & 1U) != 0U ? quotient : quotient + 1U;
774 template <
typename T1,
typename T2>
776 typename etl::enable_if< etl::is_integral<T1>::value && etl::is_integral<T2>::value && etl::is_unsigned<T1>::value && etl::is_unsigned<T2>::value,
777 typename etl::common_type<T1, T2>::type>
::type
780 typedef typename etl::common_type<T1, T2>::type
type;
bitset_ext
Definition absolute.h:40
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value &&etl::is_signed< T >::value, T >::type divide_round_half_odd(T numerator, T denominator) ETL_NOEXCEPT
Integral division with rounding to half odd. For signed integral types. For identical argument types.
Definition rounded_integral_division.h:680
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value &&etl::is_signed< T >::value, T >::type divide_round_to_infinity(T numerator, T denominator) ETL_NOEXCEPT
Integral division with rounding towards infinity. For signed integral types. For identical argument t...
Definition rounded_integral_division.h:245
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value &&etl::is_unsigned< T >::value, T >::type divide_round_to_zero(T numerator, T denominator) ETL_NOEXCEPT
Integral division with rounding towards zero. For integral types. For identical argument types.
Definition rounded_integral_division.h:331
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value &&etl::is_signed< T >::value, T >::type divide_round_half_down(T numerator, T denominator) ETL_NOEXCEPT
Integral division with rounding to half down. For signed integral types. For identical argument types...
Definition rounded_integral_division.h:470
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value &&etl::is_signed< T >::value, T >::type divide_round_half_up(T numerator, T denominator) ETL_NOEXCEPT
Integral division with rounding to half up. For signed integral types. For identical argument types.
Definition rounded_integral_division.h:369
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value &&etl::is_signed< T >::value, T >::type divide_round_half_even(T numerator, T denominator) ETL_NOEXCEPT
Integral division with rounding to half even. For signed integral types. For identical argument types...
Definition rounded_integral_division.h:558
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value &&etl::is_signed< T >::value, T >::type divide_round_to_floor(T numerator, T denominator) ETL_NOEXCEPT
Integral division with rounding to floor. For signed integral types. For identical argument types.
Definition rounded_integral_division.h:160
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value &&etl::is_signed< T >::value, T >::type divide_round_to_ceiling(T numerator, T denominator) ETL_NOEXCEPT
Integral division with rounding to ceiling. For signed integral types. For identical argument types.
Definition rounded_integral_division.h:74