@@ -204,7 +204,56 @@ def test_division(self):
204204 self .check_division (710031681576388032 , 26769404391308 )
205205 self .check_division (1933622614268221 , 30212853348836 )
206206
207+ def test_divmod_full_limbs (self ):
208+ # Use saturated limbs and q near BASE to maximize |z| (~BASE*q)
209+ # in the x_divrem inner loop: z = vk[i] + zhi - q*w0[i].
210+ for n_div in (2 , 3 , 5 , 8 ):
211+ # n_div MASK limbs for the divisor (w0)
212+ w = (1 << (n_div * SHIFT )) - 1
213+ for n_num in (n_div , n_div + 1 , n_div + 4 ):
214+ # n_num MASK limbs for the dividend (vk)
215+ v = (1 << (n_num * SHIFT )) - 1
216+ with self .subTest (n_div = n_div , n_num = n_num ):
217+ self .check_division (v , w )
218+
219+ # Known quotient and remainder: q includes values
220+ # near BASE and r spans 0 .. w-1.
221+ for q in (1 , 2 , MASK , BASE - 1 , BASE , BASE + 1 ):
222+ for r in (0 , 1 , MASK , w - 1 ):
223+ with self .subTest (n_div = n_div , q = q , r = r ):
224+ v = q * w + r
225+ self .assertEqual (divmod (v , w ), (q , r ))
226+ self .check_division (v , w )
207227
228+ @support .requires_IEEE_754
229+ def test_intradigit_shift (self ):
230+ # Unit tests for v_lshift and v_rshift in longobject.c.
231+ # These two functions are not used by Python << and >>,
232+ # so it is different with tests of test_xxx_l|rshift.
233+ # We test them with other functions that use them.
234+
235+ # Full limb values.
236+ one = (1 << SHIFT ) - 1
237+ two = (1 << (2 * SHIFT )) - 1
238+ three = (1 << (3 * SHIFT )) - 1
239+ four = (1 << (4 * SHIFT )) - 1
240+ # Powers of 10.
241+ ten_to_40 = 10 ** 40
242+ ten_to_20 = 10 ** 20
243+
244+ # Test with "_PyLong_Frexp" (n -> float):
245+ # - n.bit_length() <= 55 => v_lshift,
246+ # - n.bit_length() > 55 => v_rshift.
247+ self .check_float_conversion (one )
248+ self .check_float_conversion (two )
249+ self .check_float_conversion (ten_to_40 )
250+
251+ # Test with "long_true_divide" (a / b):
252+ # - (a.bit_length() - b.bit_length()) <= 55 => v_lshift,
253+ # - (a.bit_length() - b.bit_length()) > 55 => v_rshift.
254+ self .check_truediv (three , two )
255+ self .check_truediv (four , two )
256+ self .check_truediv (ten_to_40 , ten_to_20 )
208257
209258 def test_karatsuba (self ):
210259 digits = list (range (1 , 5 )) + list (range (KARATSUBA_CUTOFF ,
0 commit comments