@@ -4491,6 +4491,38 @@ nm_##MPDFUNC(PyObject *self) \
44914491 return result; \
44924492}
44934493
4494+ typedef void (* mpd_binaryfunc )(mpd_t * , const mpd_t * , const mpd_t * ,
4495+ const mpd_context_t * , uint32_t * );
4496+
4497+ static PyObject *
4498+ dec_binop_int64 (decimal_state * state , PyObject * context , mpd_binaryfunc op ,
4499+ PyObject * decimal , int64_t value , int reflected )
4500+ {
4501+ mpd_uint_t data [_Py_DEC_MINALLOC ];
4502+ mpd_t integer = {
4503+ .flags = MPD_STATIC | MPD_STATIC_DATA ,
4504+ .alloc = _Py_DEC_MINALLOC ,
4505+ .data = data ,
4506+ };
4507+ uint32_t status = 0 ;
4508+ mpd_qset_i64_exact (& integer , value , & status );
4509+ if (status && dec_addstatus (context , status )) {
4510+ mpd_del (& integer );
4511+ return NULL ;
4512+ }
4513+ PyObject * result = dec_alloc (state );
4514+ if (result != NULL ) {
4515+ const mpd_t * a = reflected ? & integer : MPD (decimal );
4516+ const mpd_t * b = reflected ? MPD (decimal ) : & integer ;
4517+ op (MPD (result ), a , b , CTX (context ), & status );
4518+ if (dec_addstatus (context , status )) {
4519+ Py_CLEAR (result );
4520+ }
4521+ }
4522+ mpd_del (& integer );
4523+ return result ;
4524+ }
4525+
44944526/* Binary number method that uses default module context. */
44954527#define Dec_BinaryNumberMethod (MPDFUNC ) \
44964528static PyObject * \
@@ -4503,6 +4535,18 @@ nm_##MPDFUNC(PyObject *self, PyObject *other) \
45034535 \
45044536 decimal_state *state = find_state_left_or_right(self, other); \
45054537 CURRENT_CONTEXT(state, context) ; \
4538+ int reflected = PyLong_CheckExact(self); \
4539+ if (reflected || PyLong_CheckExact(other)) { \
4540+ PyObject *integer = reflected ? self : other; \
4541+ int overflow; \
4542+ long long value = \
4543+ PyLong_AsLongLongAndOverflow(integer, &overflow); \
4544+ if (!overflow) { \
4545+ return dec_binop_int64(state, context, MPDFUNC, \
4546+ reflected ? other : self, \
4547+ value, reflected); \
4548+ } \
4549+ } \
45064550 CONVERT_BINOP(&a, &b, self, other, context); \
45074551 \
45084552 if ((result = dec_alloc(state)) == NULL) { \
0 commit comments