Skip to content

Commit 987d9b9

Browse files
committed
prevent duplicated code generation
1 parent e50ee53 commit 987d9b9

File tree

1 file changed

+32
-47
lines changed

1 file changed

+32
-47
lines changed

vm/src/builtins/int.rs

Lines changed: 32 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -157,33 +157,50 @@ fn inner_divmod(int1: &BigInt, int2: &BigInt, vm: &VirtualMachine) -> PyResult {
157157
Ok(vm.new_tuple((div, modulo)).into())
158158
}
159159

160-
fn overflow_shift(int2: &BigInt, vm: &VirtualMachine) -> PyResult<usize> {
161-
int2.to_usize().ok_or_else(|| {
162-
vm.new_overflow_error("the number is too large to convert to int".to_owned())
163-
})
160+
fn inner_lshift(base: &BigInt, bits: &BigInt, vm: &VirtualMachine) -> PyResult {
161+
inner_shift(
162+
base,
163+
bits,
164+
|base, bits| base << bits,
165+
|bits, vm| {
166+
bits.to_usize().ok_or_else(|| {
167+
vm.new_overflow_error("the number is too large to convert to int".to_owned())
168+
})
169+
},
170+
vm,
171+
)
172+
}
173+
174+
fn inner_rshift(base: &BigInt, bits: &BigInt, vm: &VirtualMachine) -> PyResult {
175+
inner_shift(
176+
base,
177+
bits,
178+
|base, bits| base >> bits,
179+
|bits, _vm| Ok(bits.to_usize().unwrap_or(usize::MAX)),
180+
vm,
181+
)
164182
}
165183

166184
fn inner_shift<F, S>(
167-
int1: &BigInt,
168-
int2: &BigInt,
185+
base: &BigInt,
186+
bits: &BigInt,
169187
shift_op: F,
170-
overflow_shift: S,
188+
shift_bits: S,
171189
vm: &VirtualMachine,
172190
) -> PyResult
173191
where
174192
F: Fn(&BigInt, usize) -> BigInt,
175193
S: Fn(&BigInt, &VirtualMachine) -> PyResult<usize>,
176194
{
177-
if int2.is_negative() {
195+
if bits.is_negative() {
178196
Err(vm.new_value_error("negative shift count".to_owned()))
179-
} else if int1.is_zero() {
197+
} else if base.is_zero() {
180198
Ok(vm.ctx.new_int(0).into())
181199
} else {
182-
overflow_shift(int2, vm).map(|v| vm.ctx.new_int(shift_op(int1, v)).into())
200+
shift_bits(bits, vm).map(|bits| vm.ctx.new_int(shift_op(base, bits)).into())
183201
}
184202
}
185203

186-
#[inline]
187204
fn inner_truediv(i1: &BigInt, i2: &BigInt, vm: &VirtualMachine) -> PyResult {
188205
if i2.is_zero() {
189206
return Err(vm.new_zero_division_error("integer division by zero".to_owned()));
@@ -372,54 +389,22 @@ impl PyInt {
372389

373390
#[pymethod(magic)]
374391
fn lshift(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
375-
self.general_op(
376-
other,
377-
|a, b| inner_shift(a, b, |a, b| a << b, overflow_shift, vm),
378-
vm,
379-
)
392+
self.general_op(other, |a, b| inner_lshift(a, b, vm), vm)
380393
}
381394

382395
#[pymethod(magic)]
383396
fn rlshift(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
384-
self.general_op(
385-
other,
386-
|a, b| inner_shift(b, a, |a, b| a << b, overflow_shift, vm),
387-
vm,
388-
)
397+
self.general_op(other, |a, b| inner_lshift(b, a, vm), vm)
389398
}
390399

391400
#[pymethod(magic)]
392401
fn rshift(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
393-
self.general_op(
394-
other,
395-
|a, b| {
396-
inner_shift(
397-
a,
398-
b,
399-
|a, b| a >> b,
400-
|a, _vm| Ok(a.to_usize().unwrap_or(usize::MAX)),
401-
vm,
402-
)
403-
},
404-
vm,
405-
)
402+
self.general_op(other, |a, b| inner_rshift(a, b, vm), vm)
406403
}
407404

408405
#[pymethod(magic)]
409406
fn rrshift(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
410-
self.general_op(
411-
other,
412-
|a, b| {
413-
inner_shift(
414-
b,
415-
a,
416-
|a, b| a >> b,
417-
|a, _vm| Ok(a.to_usize().unwrap_or(usize::MAX)),
418-
vm,
419-
)
420-
},
421-
vm,
422-
)
407+
self.general_op(other, |a, b| inner_rshift(b, a, vm), vm)
423408
}
424409

425410
#[pymethod(name = "__rxor__")]

0 commit comments

Comments
 (0)