@@ -4416,9 +4416,9 @@ long_bool(PyLongObject *v)
4416
4416
4417
4417
/* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */
4418
4418
static int
4419
- divmod_shift (PyLongObject * shiftby , Py_ssize_t * wordshift , digit * remshift )
4419
+ divmod_shift (PyObject * shiftby , Py_ssize_t * wordshift , digit * remshift )
4420
4420
{
4421
- assert (PyLong_Check (( PyObject * ) shiftby ));
4421
+ assert (PyLong_Check (shiftby ));
4422
4422
assert (Py_SIZE (shiftby ) >= 0 );
4423
4423
Py_ssize_t lshiftby = PyLong_AsSsize_t ((PyObject * )shiftby );
4424
4424
if (lshiftby >= 0 ) {
@@ -4430,7 +4430,7 @@ divmod_shift(PyLongObject *shiftby, Py_ssize_t *wordshift, digit *remshift)
4430
4430
be that PyLong_AsSsize_t raised an OverflowError. */
4431
4431
assert (PyErr_ExceptionMatches (PyExc_OverflowError ));
4432
4432
PyErr_Clear ();
4433
- PyLongObject * wordshift_obj = divrem1 (shiftby , PyLong_SHIFT , remshift );
4433
+ PyLongObject * wordshift_obj = divrem1 (( PyLongObject * ) shiftby , PyLong_SHIFT , remshift );
4434
4434
if (wordshift_obj == NULL ) {
4435
4435
return -1 ;
4436
4436
}
@@ -4448,47 +4448,37 @@ divmod_shift(PyLongObject *shiftby, Py_ssize_t *wordshift, digit *remshift)
4448
4448
}
4449
4449
4450
4450
static PyObject *
4451
- long_rshift (PyLongObject * a , PyLongObject * b )
4451
+ long_rshift1 (PyLongObject * a , Py_ssize_t wordshift , digit remshift )
4452
4452
{
4453
4453
PyLongObject * z = NULL ;
4454
- Py_ssize_t newsize , wordshift , hishift , i , j ;
4455
- digit loshift , lomask , himask ;
4456
-
4457
- CHECK_BINOP (a , b );
4458
-
4459
- if (Py_SIZE (b ) < 0 ) {
4460
- PyErr_SetString (PyExc_ValueError ,
4461
- "negative shift count" );
4462
- return NULL ;
4463
- }
4454
+ Py_ssize_t newsize , hishift , i , j ;
4455
+ digit lomask , himask ;
4464
4456
4465
4457
if (Py_SIZE (a ) < 0 ) {
4466
4458
/* Right shifting negative numbers is harder */
4467
4459
PyLongObject * a1 , * a2 ;
4468
4460
a1 = (PyLongObject * ) long_invert (a );
4469
4461
if (a1 == NULL )
4470
4462
return NULL ;
4471
- a2 = (PyLongObject * ) long_rshift (a1 , b );
4463
+ a2 = (PyLongObject * ) long_rshift1 (a1 , wordshift , remshift );
4472
4464
Py_DECREF (a1 );
4473
4465
if (a2 == NULL )
4474
4466
return NULL ;
4475
4467
z = (PyLongObject * ) long_invert (a2 );
4476
4468
Py_DECREF (a2 );
4477
4469
}
4478
4470
else {
4479
- if (divmod_shift (b , & wordshift , & loshift ) < 0 )
4480
- return NULL ;
4481
4471
newsize = Py_SIZE (a ) - wordshift ;
4482
4472
if (newsize <= 0 )
4483
4473
return PyLong_FromLong (0 );
4484
- hishift = PyLong_SHIFT - loshift ;
4474
+ hishift = PyLong_SHIFT - remshift ;
4485
4475
lomask = ((digit )1 << hishift ) - 1 ;
4486
4476
himask = PyLong_MASK ^ lomask ;
4487
4477
z = _PyLong_New (newsize );
4488
4478
if (z == NULL )
4489
4479
return NULL ;
4490
4480
for (i = 0 , j = wordshift ; i < newsize ; i ++ , j ++ ) {
4491
- z -> ob_digit [i ] = (a -> ob_digit [j ] >> loshift ) & lomask ;
4481
+ z -> ob_digit [i ] = (a -> ob_digit [j ] >> remshift ) & lomask ;
4492
4482
if (i + 1 < newsize )
4493
4483
z -> ob_digit [i ] |= (a -> ob_digit [j + 1 ] << hishift ) & himask ;
4494
4484
}
@@ -4498,15 +4488,10 @@ long_rshift(PyLongObject *a, PyLongObject *b)
4498
4488
}
4499
4489
4500
4490
static PyObject *
4501
- long_lshift (PyObject * v , PyObject * w )
4491
+ long_rshift (PyObject * a , PyObject * b )
4502
4492
{
4503
- /* This version due to Tim Peters */
4504
- PyLongObject * a = (PyLongObject * )v ;
4505
- PyLongObject * b = (PyLongObject * )w ;
4506
- PyLongObject * z = NULL ;
4507
- Py_ssize_t oldsize , newsize , wordshift , i , j ;
4493
+ Py_ssize_t wordshift ;
4508
4494
digit remshift ;
4509
- twodigits accum ;
4510
4495
4511
4496
CHECK_BINOP (a , b );
4512
4497
@@ -4517,9 +4502,35 @@ long_lshift(PyObject *v, PyObject *w)
4517
4502
if (Py_SIZE (a ) == 0 ) {
4518
4503
return PyLong_FromLong (0 );
4519
4504
}
4520
-
4521
4505
if (divmod_shift (b , & wordshift , & remshift ) < 0 )
4522
4506
return NULL ;
4507
+ return long_rshift1 ((PyLongObject * )a , wordshift , remshift );
4508
+ }
4509
+
4510
+ /* Return a >> shiftby. */
4511
+ PyObject *
4512
+ _PyLong_Rshift (PyObject * a , size_t shiftby )
4513
+ {
4514
+ Py_ssize_t wordshift ;
4515
+ digit remshift ;
4516
+
4517
+ assert (PyLong_Check (a ));
4518
+ if (Py_SIZE (a ) == 0 ) {
4519
+ return PyLong_FromLong (0 );
4520
+ }
4521
+ wordshift = shiftby / PyLong_SHIFT ;
4522
+ remshift = shiftby % PyLong_SHIFT ;
4523
+ return long_rshift1 ((PyLongObject * )a , wordshift , remshift );
4524
+ }
4525
+
4526
+ static PyObject *
4527
+ long_lshift1 (PyLongObject * a , Py_ssize_t wordshift , digit remshift )
4528
+ {
4529
+ /* This version due to Tim Peters */
4530
+ PyLongObject * z = NULL ;
4531
+ Py_ssize_t oldsize , newsize , i , j ;
4532
+ twodigits accum ;
4533
+
4523
4534
oldsize = Py_ABS (Py_SIZE (a ));
4524
4535
newsize = oldsize + wordshift ;
4525
4536
if (remshift )
@@ -4547,6 +4558,42 @@ long_lshift(PyObject *v, PyObject *w)
4547
4558
return (PyObject * ) maybe_small_long (z );
4548
4559
}
4549
4560
4561
+ static PyObject *
4562
+ long_lshift (PyObject * a , PyObject * b )
4563
+ {
4564
+ Py_ssize_t wordshift ;
4565
+ digit remshift ;
4566
+
4567
+ CHECK_BINOP (a , b );
4568
+
4569
+ if (Py_SIZE (b ) < 0 ) {
4570
+ PyErr_SetString (PyExc_ValueError , "negative shift count" );
4571
+ return NULL ;
4572
+ }
4573
+ if (Py_SIZE (a ) == 0 ) {
4574
+ return PyLong_FromLong (0 );
4575
+ }
4576
+ if (divmod_shift (b , & wordshift , & remshift ) < 0 )
4577
+ return NULL ;
4578
+ return long_lshift1 ((PyLongObject * )a , wordshift , remshift );
4579
+ }
4580
+
4581
+ /* Return a << shiftby. */
4582
+ PyObject *
4583
+ _PyLong_Lshift (PyObject * a , size_t shiftby )
4584
+ {
4585
+ Py_ssize_t wordshift ;
4586
+ digit remshift ;
4587
+
4588
+ assert (PyLong_Check (a ));
4589
+ if (Py_SIZE (a ) == 0 ) {
4590
+ return PyLong_FromLong (0 );
4591
+ }
4592
+ wordshift = shiftby / PyLong_SHIFT ;
4593
+ remshift = shiftby % PyLong_SHIFT ;
4594
+ return long_lshift1 ((PyLongObject * )a , wordshift , remshift );
4595
+ }
4596
+
4550
4597
/* Compute two's complement of digit vector a[0:m], writing result to
4551
4598
z[0:m]. The digit vector a need not be normalized, but should not
4552
4599
be entirely zero. a and z may point to the same digit vector. */
@@ -5552,7 +5599,7 @@ static PyNumberMethods long_as_number = {
5552
5599
(inquiry )long_bool , /*tp_bool*/
5553
5600
(unaryfunc )long_invert , /*nb_invert*/
5554
5601
long_lshift , /*nb_lshift*/
5555
- ( binaryfunc ) long_rshift , /*nb_rshift*/
5602
+ long_rshift , /*nb_rshift*/
5556
5603
long_and , /*nb_and*/
5557
5604
long_xor , /*nb_xor*/
5558
5605
long_or , /*nb_or*/
0 commit comments