31#ifndef ETL_ROUNDED_INTEGRAL_DIVISION_INCLUDED
32#define ETL_ROUNDED_INTEGRAL_DIVISION_INCLUDED
41 namespace private_rounded_integral_division
49 typename etl::enable_if<etl::is_integral<T>::value && etl::is_signed<T>::value,
bool>::type
50 are_same_sign(T a, T b) ETL_NOEXCEPT
52 return ((a ^ b) >= 0);
61 typename etl::enable_if<etl::is_integral<T>::value && etl::is_unsigned<T>::value,
bool>::type
62 are_same_sign(T , T ) ETL_NOEXCEPT
78 typename etl::enable_if<etl::is_integral<T>::value && etl::is_signed<T>::value, T>::type
81 const T remainder = numerator % denominator;
82 const T quotient = numerator / denominator;
91 return private_rounded_integral_division::are_same_sign(numerator, denominator) ? quotient + 1 : quotient;
103 template <
typename T1,
typename T2>
106 etl::is_integral<T2>::value &&
107 etl::is_signed<T1>::value &&
108 etl::is_signed<T2>::value,
109 typename etl::common_type<T1, T2>::type>::type
112 typedef typename etl::common_type<T1, T2>::type
type;
125 template <
typename T>
132 const T remainder = numerator % denominator;
133 const T quotient = numerator / denominator;
136 return remainder == 0U ? quotient : quotient + 1U;
148 template <
typename T1,
typename T2>
151 etl::is_integral<T2>::value &&
154 typename etl::common_type<T1, T2>::type>::type
157 typedef typename etl::common_type<T1, T2>::type
type;
170 template <
typename T>
173 etl::is_signed<T>::value,
177 const T remainder = numerator % denominator;
178 const T quotient = numerator / denominator;
187 return private_rounded_integral_division::are_same_sign(numerator, denominator) ? quotient : quotient - 1;
199 template <
typename T1,
typename T2>
202 etl::is_integral<T2>::value &&
203 etl::is_signed<T1>::value &&
204 etl::is_signed<T2>::value,
205 typename etl::common_type<T1, T2>::type>::type
208 typedef typename etl::common_type<T1, T2>::type
type;
221 template <
typename T>
228 return numerator / denominator;
240 template <
typename T1,
typename T2>
243 etl::is_integral<T2>::value &&
246 typename etl::common_type<T1, T2>::type>::type
249 typedef typename etl::common_type<T1, T2>::type
type;
251 const type common_numerator = numerator;
252 const type common_denominator = denominator;
254 return common_numerator / common_denominator;
265 template <
typename T>
268 etl::is_signed<T>::value,
272 const T remainder = numerator % denominator;
273 const T quotient = numerator / denominator;
275 if (private_rounded_integral_division::are_same_sign(numerator, denominator))
278 return (remainder != 0) ? quotient + 1 : quotient;
283 return (remainder != 0) ? quotient - 1 : quotient;
296 template <
typename T1,
typename T2>
299 etl::is_integral<T2>::value &&
300 etl::is_signed<T1>::value &&
301 etl::is_signed<T2>::value,
302 typename etl::common_type<T1, T2>::type>::type
305 typedef typename etl::common_type<T1, T2>::type
type;
318 template <
typename T>
325 const T remainder = numerator % denominator;
326 const T quotient = numerator / denominator;
328 return remainder ? quotient + 1U : quotient;
340 template <
typename T1,
typename T2>
343 etl::is_integral<T2>::value &&
346 typename etl::common_type<T1, T2>::type>::type
349 typedef typename etl::common_type<T1, T2>::type
type;
362 template <
typename T>
369 return numerator / denominator;
381 template <
typename T1,
typename T2>
384 etl::is_integral<T2>::value,
385 typename etl::common_type<T1, T2>::type>::type
388 typedef typename etl::common_type<T1, T2>::type
type;
391 const type common_numerator = numerator;
392 const type common_denominator = denominator;
394 return common_numerator / common_denominator;
405 template <
typename T>
408 etl::is_signed<T>::value,
413 const T remainder = numerator % denominator;
414 const T quotient = numerator / denominator;
417 typedef typename std::make_unsigned<T>::type utype;
418 utype abs_remainder = remainder < 0 ? utype(0) - utype(remainder) : utype(remainder);
419 utype abs_denominator = denominator < 0 ? utype(0) - utype(denominator) : utype(denominator);
422 utype half_denominator = (abs_denominator + 1) / 2;
424 if (abs_remainder >= half_denominator)
427 if (private_rounded_integral_division::are_same_sign(numerator, denominator))
449 template <
typename T1,
typename T2>
452 etl::is_integral<T2>::value &&
453 etl::is_signed<T1>::value &&
454 etl::is_signed<T2>::value,
455 typename etl::common_type<T1, T2>::type>::type
458 typedef typename etl::common_type<T1, T2>::type
type;
471 template <
typename T>
478 const T remainder = numerator % denominator;
479 const T quotient = numerator / denominator;
482 return (remainder >= (denominator / 2U) + (denominator % 2U)) ? quotient + 1U : quotient;
494 template <
typename T1,
typename T2>
497 etl::is_integral<T2>::value &&
502 typedef typename etl::common_type<T1, T2>::type
type;
515 template <
typename T>
518 etl::is_signed<T>::value,
522 const T quotient = numerator / denominator;
523 const T remainder = numerator % denominator;
524 const T abs_denominator = etl::absolute(denominator);
525 const T abs_remainderainder = etl::absolute(remainder);
528 const T direction = ((numerator >= 0) == (denominator >= 0)) ? 1 : -1;
531 return abs_remainderainder > (abs_denominator / 2) ? quotient + direction : quotient;
543 template <
typename T1,
typename T2>
546 etl::is_integral<T2>::value &&
547 etl::is_signed<T1>::value &&
548 etl::is_signed<T2>::value,
549 typename etl::common_type<T1, T2>::type>::type
552 typedef typename etl::common_type<T1, T2>::type
type;
565 template <
typename T>
572 const T remainder = numerator % denominator;
573 const T quotient = numerator / denominator;
576 return (remainder > (denominator / 2U)) ? quotient + 1U : quotient;
588 template <
typename T1,
typename T2>
591 etl::is_integral<T2>::value &&
596 typedef typename etl::common_type<T1, T2>::type
type;
609 template <
typename T>
612 etl::is_signed<T>::value,
616 const T quotient = numerator / denominator;
617 const T remainder = numerator % denominator;
618 const T abs_denominator = etl::absolute(denominator);
619 const T abs_remainderainder = etl::absolute(remainder);
620 const T direction = ((numerator >= 0) == (denominator >= 0)) ? 1 : -1;
622 if ((abs_remainderainder * 2) < abs_denominator)
626 else if ((abs_remainderainder * 2) > abs_denominator)
628 return quotient + direction;
633 return (quotient & 1) == 0 ? quotient : quotient + direction;
646 template <
typename T1,
typename T2>
649 etl::is_integral<T2>::value &&
650 etl::is_signed<T1>::value &&
651 etl::is_signed<T2>::value,
652 typename etl::common_type<T1, T2>::type>::type
655 typedef typename etl::common_type<T1, T2>::type
type;
668 template <
typename T>
675 const T quotient = numerator / denominator;
676 const T remainder = numerator % denominator;
678 if ((remainder * 2U) < denominator)
683 else if ((remainder * 2U) > denominator)
686 return quotient + 1U;
691 return (quotient & 1U) == 0U ? quotient : quotient + 1;
704 template <
typename T1,
typename T2>
707 etl::is_integral<T2>::value &&
710 typename etl::common_type<T1, T2>::type>::type
713 typedef typename etl::common_type<T1, T2>::type
type;
726 template <
typename T>
729 etl::is_signed<T>::value,
733 const T quotient = numerator / denominator;
734 const T remainder = numerator % denominator;
735 const T abs_denominator = etl::absolute(denominator);
736 const T abs_remainderainder = etl::absolute(remainder);
737 const T direction = ((numerator >= 0) == (denominator >= 0)) ? 1 : -1;
739 if ((abs_remainderainder * 2) < abs_denominator)
743 else if ((abs_remainderainder * 2) > abs_denominator)
745 return quotient + direction;
750 return (quotient & 1) != 0 ? quotient : quotient + direction;
763 template <
typename T1,
typename T2>
766 etl::is_integral<T2>::value &&
767 etl::is_signed<T1>::value &&
768 etl::is_signed<T2>::value,
769 typename etl::common_type<T1, T2>::type>::type
772 typedef typename etl::common_type<T1, T2>::type
type;
785 template <
typename T>
792 const T quotient = numerator / denominator;
793 const T remainder = numerator % denominator;
794 const T direction = ((numerator >= 0U) == (denominator >= 0U)) ? 1 : -1;
796 if ((remainder * 2U) < denominator)
800 else if ((remainder * 2U) > denominator)
802 return quotient + direction;
807 return (quotient & 1U) != 0U ? quotient : quotient + direction;
820 template <
typename T1,
typename T2>
823 etl::is_integral<T2>::value &&
826 typename etl::common_type<T1, T2>::type>::type
829 typedef typename etl::common_type<T1, T2>::type
type;
enable_if
Definition type_traits_generator.h:1254
is_unsigned
Definition type_traits_generator.h:1084
bitset_ext
Definition absolute.h:39
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:731
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:270
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:367
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:520
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:410
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:614
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:175
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:79