Skip to content

Commit 9178f53

Browse files
authored
bpo-45636: Merge all numeric operators (GH-29482)
1 parent 1cbaa50 commit 9178f53

File tree

16 files changed

+611
-1079
lines changed

16 files changed

+611
-1079
lines changed

Doc/library/dis.rst

Lines changed: 7 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -406,156 +406,29 @@ result back on the stack.
406406
.. versionadded:: 3.5
407407

408408

409-
**Binary operations**
409+
**Binary and in-place operations**
410410

411411
Binary operations remove the top of the stack (TOS) and the second top-most
412412
stack item (TOS1) from the stack. They perform the operation, and put the
413413
result back on the stack.
414414

415-
.. opcode:: BINARY_POWER
416-
417-
Implements ``TOS = TOS1 ** TOS``.
418-
419-
420-
.. opcode:: BINARY_MULTIPLY
421-
422-
Implements ``TOS = TOS1 * TOS``.
423-
424-
425-
.. opcode:: BINARY_MATRIX_MULTIPLY
426-
427-
Implements ``TOS = TOS1 @ TOS``.
428-
429-
.. versionadded:: 3.5
430-
431-
432-
.. opcode:: BINARY_FLOOR_DIVIDE
433-
434-
Implements ``TOS = TOS1 // TOS``.
435-
436-
437-
.. opcode:: BINARY_TRUE_DIVIDE
438-
439-
Implements ``TOS = TOS1 / TOS``.
440-
441-
442-
.. opcode:: BINARY_MODULO
443-
444-
Implements ``TOS = TOS1 % TOS``.
445-
446-
447-
.. opcode:: BINARY_ADD
448-
449-
Implements ``TOS = TOS1 + TOS``.
450-
451-
452-
.. opcode:: BINARY_SUBTRACT
453-
454-
Implements ``TOS = TOS1 - TOS``.
455-
456-
457-
.. opcode:: BINARY_SUBSCR
458-
459-
Implements ``TOS = TOS1[TOS]``.
460-
461-
462-
.. opcode:: BINARY_LSHIFT
463-
464-
Implements ``TOS = TOS1 << TOS``.
465-
466-
467-
.. opcode:: BINARY_RSHIFT
468-
469-
Implements ``TOS = TOS1 >> TOS``.
470-
471-
472-
.. opcode:: BINARY_AND
473-
474-
Implements ``TOS = TOS1 & TOS``.
475-
476-
477-
.. opcode:: BINARY_XOR
478-
479-
Implements ``TOS = TOS1 ^ TOS``.
480-
481-
482-
.. opcode:: BINARY_OR
483-
484-
Implements ``TOS = TOS1 | TOS``.
485-
486-
487-
**In-place operations**
488-
489415
In-place operations are like binary operations, in that they remove TOS and
490416
TOS1, and push the result back on the stack, but the operation is done in-place
491417
when TOS1 supports it, and the resulting TOS may be (but does not have to be)
492418
the original TOS1.
493419

494-
.. opcode:: INPLACE_POWER
495-
496-
Implements in-place ``TOS = TOS1 ** TOS``.
497-
498-
499-
.. opcode:: INPLACE_MULTIPLY
500-
501-
Implements in-place ``TOS = TOS1 * TOS``.
502-
503-
504-
.. opcode:: INPLACE_MATRIX_MULTIPLY
505-
506-
Implements in-place ``TOS = TOS1 @ TOS``.
507-
508-
.. versionadded:: 3.5
509-
510-
511-
.. opcode:: INPLACE_FLOOR_DIVIDE
512-
513-
Implements in-place ``TOS = TOS1 // TOS``.
514-
515-
516-
.. opcode:: INPLACE_TRUE_DIVIDE
517-
518-
Implements in-place ``TOS = TOS1 / TOS``.
519-
520420

521-
.. opcode:: INPLACE_MODULO
421+
.. opcode:: BINARY_OP (op)
522422

523-
Implements in-place ``TOS = TOS1 % TOS``.
423+
Implements the binary and in-place operators (depending on the value of
424+
*op*).
524425

525-
526-
.. opcode:: INPLACE_ADD
527-
528-
Implements in-place ``TOS = TOS1 + TOS``.
529-
530-
531-
.. opcode:: INPLACE_SUBTRACT
532-
533-
Implements in-place ``TOS = TOS1 - TOS``.
534-
535-
536-
.. opcode:: INPLACE_LSHIFT
537-
538-
Implements in-place ``TOS = TOS1 << TOS``.
539-
540-
541-
.. opcode:: INPLACE_RSHIFT
542-
543-
Implements in-place ``TOS = TOS1 >> TOS``.
544-
545-
546-
.. opcode:: INPLACE_AND
547-
548-
Implements in-place ``TOS = TOS1 & TOS``.
549-
550-
551-
.. opcode:: INPLACE_XOR
552-
553-
Implements in-place ``TOS = TOS1 ^ TOS``.
426+
.. versionadded:: 3.11
554427

555428

556-
.. opcode:: INPLACE_OR
429+
.. opcode:: BINARY_SUBSCR
557430

558-
Implements in-place ``TOS = TOS1 | TOS``.
431+
Implements ``TOS = TOS1[TOS]``.
559432

560433

561434
.. opcode:: STORE_SUBSCR

Doc/whatsnew/3.11.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,9 @@ Optimizations
315315
CPython bytecode changes
316316
========================
317317

318+
* Replaced all numeric ``BINARY_*`` and ``INPLACE_*`` instructions with a single
319+
:opcode:`BINARY_OP` implementation.
320+
318321
* Added a new :opcode:`CALL_METHOD_KW` opcode. Calls a method in a similar
319322
fashion as :opcode:`CALL_METHOD`, but also supports keyword arguments. Works
320323
in tandem with :opcode:`LOAD_METHOD`.

Include/internal/pycore_code.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,9 @@ int _Py_Specialize_StoreAttr(PyObject *owner, _Py_CODEUNIT *instr, PyObject *nam
267267
int _Py_Specialize_LoadGlobal(PyObject *globals, PyObject *builtins, _Py_CODEUNIT *instr, PyObject *name, SpecializedCacheEntry *cache);
268268
int _Py_Specialize_LoadMethod(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name, SpecializedCacheEntry *cache);
269269
int _Py_Specialize_BinarySubscr(PyObject *sub, PyObject *container, _Py_CODEUNIT *instr);
270-
int _Py_Specialize_BinaryAdd(PyObject *left, PyObject *right, _Py_CODEUNIT *instr);
271-
int _Py_Specialize_BinaryMultiply(PyObject *left, PyObject *right, _Py_CODEUNIT *instr);
272270
int _Py_Specialize_CallFunction(PyObject *callable, _Py_CODEUNIT *instr, int nargs, SpecializedCacheEntry *cache, PyObject *builtins);
271+
void _Py_Specialize_BinaryOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr,
272+
SpecializedCacheEntry *cache);
273273

274274
#define PRINT_SPECIALIZATION_STATS 0
275275
#define PRINT_SPECIALIZATION_STATS_DETAILED 0

0 commit comments

Comments
 (0)