11// //////////////////////////////////////////////////////////////
2- // Copyright 2013 - 2022 John Maddock.
3- // Copyright 2022 Christopher Kormanyos.
2+ // Copyright 2013 - 2025 John Maddock.
3+ // Copyright 2022 - 2025 Christopher Kormanyos.
44// Distributed under the Boost Software License,
55// Version 1.0. (See accompanying file LICENSE_1_0.txt
66// or copy at https://www.boost.org/LICENSE_1_0.txt)
@@ -1626,15 +1626,34 @@ inline void convert_to_unsigned_int(I* res, const cpp_bin_float<Digits, DigitBas
16261626 }
16271627 typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::rep_type man (arg.bits ());
16281628 using shift_type = typename std::conditional<sizeof (typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type) < sizeof (int ), int , typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type>::type;
1629- shift_type shift = (shift_type)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1 - arg.exponent ();
1630- if (shift > (shift_type)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1 )
1629+
1630+ const shift_type shift = (shift_type)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1 - arg.exponent ();
1631+
1632+ if (arg.sign ())
1633+ {
1634+ using si_type = typename boost::multiprecision::detail::make_signed<I>::type;
1635+
1636+ si_type val;
1637+
1638+ convert_to_signed_int (&val, arg);
1639+
1640+ *res = static_cast <I>(val);
1641+
1642+ return ;
1643+ }
1644+ else if (shift > (shift_type)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1 )
16311645 {
16321646 *res = 0 ;
16331647 return ;
16341648 }
1649+ else if (arg.compare (max_val) >= 0 )
1650+ {
1651+ *res = max_val;
1652+ return ;
1653+ }
16351654 else if (shift < 0 )
16361655 {
1637- if (cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - shift <= digits)
1656+ if ((shift_type) cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - shift <= (shift_type) digits)
16381657 {
16391658 // We have more bits in ulong_long_type than the float, so it's OK to left shift:
16401659 eval_convert_to (res, man);
@@ -1644,7 +1663,7 @@ inline void convert_to_unsigned_int(I* res, const cpp_bin_float<Digits, DigitBas
16441663 *res = max_val;
16451664 return ;
16461665 }
1647- eval_right_shift (man, shift);
1666+ eval_right_shift (man, static_cast < unsigned >( shift) );
16481667 eval_convert_to (res, man);
16491668}
16501669
@@ -2035,18 +2054,18 @@ inline void eval_ceil(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE
20352054 }
20362055 bool fractional = (shift_type)eval_lsb (arg.bits ()) < shift;
20372056 res = arg;
2038- eval_right_shift (res.bits (), shift);
2057+ eval_right_shift (res.bits (), static_cast < unsigned >( shift) );
20392058 if (fractional && !res.sign ())
20402059 {
20412060 eval_increment (res.bits ());
2042- if ((std::ptrdiff_t )eval_msb (res.bits ()) != cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1 - shift)
2061+ if ((std::ptrdiff_t )eval_msb (res.bits ()) != (std:: ptrdiff_t )( static_cast <shift_type>( cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count) - 1 - shift) )
20432062 {
20442063 // Must have extended result by one bit in the increment:
20452064 --shift;
20462065 ++res.exponent ();
20472066 }
20482067 }
2049- eval_left_shift (res.bits (), shift);
2068+ eval_left_shift (res.bits (), static_cast < unsigned >( shift) );
20502069}
20512070
20522071template <unsigned D1, backends::digit_base_type B1, class A1 , class E1 , E1 M1, E1 M2>
0 commit comments