11// //////////////////////////////////////////////////////////////////////////////////////////////////
22//
33// Simple Long Integer Math for C++
4- // version 1.0
4+ // version 1.3
55//
66// //////////////////////////////////////////////////////////////////////////////////////////////////
77//
@@ -118,17 +118,6 @@ inline constexpr bool is_signed_v<long_int_t<native_t, size>> = true;
118118
119119
120120
121- // //////////////////////////////////////////////////////////////////////////////////////////////////
122- // make_unsigned_t
123- // //////////////////////////////////////////////////////////////////////////////////////////////////
124-
125- template <typename native_t , uint_t size>
126- struct make_unsigned <long_int_t <native_t , size>> {
127- using type = long_uint_t <native_t , size>;
128- };
129-
130-
131-
132121// //////////////////////////////////////////////////////////////////////////////////////////////////
133122// standalone methods
134123// //////////////////////////////////////////////////////////////////////////////////////////////////
@@ -217,20 +206,13 @@ constexpr bool long_int_t<native_t, size>::sign() const noexcept
217206template <typename native_t , uint_t size>
218207constexpr long_int_t <native_t , size>& long_int_t <native_t , size>::negate() noexcept
219208{
220- bool borrow = true ;
221-
222- for (uint_t n = 0 ; n < std::size (digits); ++n) {
223-
224- borrow = subb<native_t >(digits[n], 0 , borrow);
225- digits[n] = ~digits[n];
226- }
209+ slim::negate (digits);
227210
228211 return *this ;
229212}
230213
231214
232215
233-
234216// //////////////////////////////////////////////////////////////////////////////////////////////////
235217template <typename native_t , uint_t size>
236218template <uint_t other_size, std::enable_if_t <(other_size < size), int >>
@@ -398,10 +380,7 @@ constexpr long_int_t<native_t, size> long_int_t<native_t, size>::operator-() con
398380template <typename native_t , uint_t size>
399381constexpr long_int_t <native_t , size>& long_int_t <native_t , size>::operator *=(const long_int_t & that) noexcept
400382{
401- long_uint_t <native_t , size>::operator *=(that.sign () ? -that : that);
402-
403- if (that.sign ())
404- negate ();
383+ long_uint_t <native_t , size>::operator *=(that);
405384
406385 return *this ;
407386}
@@ -421,15 +400,15 @@ constexpr long_int_t<native_t, size> long_int_t<native_t, size>::operator*(const
421400template <typename native_t , uint_t size>
422401constexpr long_int_t <native_t , size>& long_int_t <native_t , size>::operator /=(const long_int_t & that) noexcept
423402{
424- const long_uint_t <native_t , size> value1 = this ->sign () ? -*this : *this ;
425- const long_uint_t <native_t , size> value2 = that.sign () ? -that : that;
403+ const bool sing = sign ();
426404
427- long_uint_t <native_t , size> result = value1 / value2;
405+ if (sing)
406+ negate ();
428407
429- if (this ->sign () ^ that.sign ())
430- result = -result;
408+ long_uint_t <native_t , size>::operator /=(that.sign () ? -that : that);
431409
432- *this = result;
410+ if (sing != that.sign ())
411+ negate ();
433412
434413 return *this ;
435414}
@@ -449,15 +428,15 @@ constexpr long_int_t<native_t, size> long_int_t<native_t, size>::operator/(const
449428template <typename native_t , uint_t size>
450429constexpr long_int_t <native_t , size>& long_int_t <native_t , size>::operator %=(const long_int_t & that) noexcept
451430{
452- const long_uint_t <native_t , size> value1 = this ->sign () ? -*this : *this ;
453- const long_uint_t <native_t , size> value2 = that.sign () ? -that : that;
431+ const bool sing = sign ();
454432
455- long_uint_t <native_t , size> result = value1 % value2;
433+ if (sing)
434+ negate ();
456435
457- if (this ->sign () ^ that.sign ())
458- result = -result;
436+ long_uint_t <native_t , size>::operator %=(that.sign () ? -that : that);
459437
460- *this = result;
438+ if (sing != that.sign ())
439+ negate ();
461440
462441 return *this ;
463442}
@@ -538,13 +517,18 @@ template<typename type_t, std::enable_if_t<is_signed_v<type_t>, int>>
538517constexpr type_t muldiv (const type_t & value, const type_t & multiplier, const type_t & divider) noexcept
539518{
540519 using unsigned_t = make_unsigned_t <type_t >;
541- const unsigned_t uvalue = value >= 0 ? value : -value;
542- const unsigned_t umultiplier = multiplier >= 0 ? multiplier : -multiplier;
543- const unsigned_t udivider = divider >= 0 ? divider : -divider;
520+
521+ const bool value_sign = sign (value);
522+ const bool multiplier_sign = sign (multiplier);
523+ const bool divider_sign = sign (divider);
524+
525+ const unsigned_t uvalue = value_sign ? -value : value;
526+ const unsigned_t umultiplier = multiplier_sign ? -multiplier : multiplier;
527+ const unsigned_t udivider = divider_sign ? -divider : divider;
544528
545529 type_t result = muldiv<unsigned_t >(uvalue, umultiplier, udivider);
546530
547- if ((value < 0 ) ^ (multiplier < 0 ) ^ (divider < 0 ) )
531+ if (value_sign != multiplier_sign != divider_sign )
548532 result = -result;
549533
550534 return result;
0 commit comments