Skip to content

Commit 8b2cd8f

Browse files
committed
ext/gmp: Start refactoring operator overloading to use new parsing mechanism
1 parent 2681da4 commit 8b2cd8f

File tree

1 file changed

+18
-30
lines changed

1 file changed

+18
-30
lines changed

ext/gmp/gmp.c

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,11 @@ static zend_result convert_zstr_to_gmp(mpz_t gmp_number, const zend_string *val,
169169
static zend_result convert_to_gmp(mpz_t gmpnumber, zval *val, zend_long base, uint32_t arg_pos);
170170
static void gmp_cmp(zval *return_value, zval *a_arg, zval *b_arg, bool is_operator);
171171

172-
static bool gmp_zend_parse_arg_into_mpz(
172+
static bool gmp_zend_parse_arg_into_mpz_ex(
173173
zval *arg,
174174
mpz_ptr *destination_mpz_ptr,
175-
uint32_t arg_num
175+
uint32_t arg_num,
176+
bool is_operator
176177
) {
177178
if (EXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) {
178179
if (EXPECTED(instanceof_function(Z_OBJCE_P(arg), gmp_ce))) {
@@ -184,7 +185,7 @@ static bool gmp_zend_parse_arg_into_mpz(
184185

185186
*destination_mpz_ptr = GMPG(zpp_arg[arg_num-1]);
186187
if (Z_TYPE_P(arg) == IS_STRING) {
187-
return convert_zstr_to_gmp(*destination_mpz_ptr, Z_STR_P(arg), /* base */ 0, arg_num) != FAILURE;
188+
return convert_zstr_to_gmp(*destination_mpz_ptr, Z_STR_P(arg), /* base */ 0, is_operator ? 0 : arg_num) != FAILURE;
188189
}
189190

190191
if (Z_TYPE_P(arg) == IS_LONG) {
@@ -194,6 +195,11 @@ static bool gmp_zend_parse_arg_into_mpz(
194195
return false;
195196
}
196197

198+
static bool gmp_zend_parse_arg_into_mpz(zval *arg, mpz_ptr *destination_mpz_ptr, uint32_t arg_num)
199+
{
200+
return gmp_zend_parse_arg_into_mpz_ex(arg, destination_mpz_ptr, arg_num, false);
201+
}
202+
197203
#define GMP_Z_PARAM_INTO_MPZ_PTR(destination_mpz_ptr) \
198204
Z_PARAM_PROLOGUE(0, 0); \
199205
if (UNEXPECTED(!gmp_zend_parse_arg_into_mpz(_arg, &destination_mpz_ptr, _i))) { \
@@ -250,7 +256,6 @@ typedef void (*gmp_binary_op2_t)(mpz_ptr, mpz_ptr, mpz_srcptr, mpz_srcptr);
250256
typedef gmp_ulong (*gmp_binary_ui_op2_t)(mpz_ptr, mpz_ptr, mpz_srcptr, gmp_ulong);
251257

252258
static inline void gmp_zval_binary_ui_op(zval *return_value, zval *a_arg, zval *b_arg, gmp_binary_op_t gmp_op, gmp_binary_ui_op_t gmp_ui_op, bool check_b_zero, bool is_operator);
253-
static inline void gmp_zval_unary_op(zval *return_value, zval *a_arg, gmp_unary_op_t gmp_op);
254259

255260
static void gmp_mpz_tdiv_q_ui(mpz_ptr a, mpz_srcptr b, gmp_ulong c) {
256261
mpz_tdiv_q_ui(a, b, c);
@@ -452,15 +457,9 @@ typeof_op_failure: ;
452457
#define DO_BINARY_UI_OP(op) DO_BINARY_UI_OP_EX(op, op ## _ui, 0)
453458
#define DO_BINARY_OP(op) DO_BINARY_UI_OP_EX(op, NULL, 0)
454459

455-
#define DO_UNARY_OP(op) \
456-
gmp_zval_unary_op(result, op1, op); \
457-
if (UNEXPECTED(EG(exception))) { \
458-
return FAILURE; \
459-
} \
460-
return SUCCESS;
461-
462460
static zend_result gmp_do_operation_ex(uint8_t opcode, zval *result, zval *op1, zval *op2) /* {{{ */
463461
{
462+
mpz_ptr gmp_op1, gmp_result;
464463
switch (opcode) {
465464
case ZEND_ADD:
466465
DO_BINARY_UI_OP(mpz_add);
@@ -484,8 +483,14 @@ static zend_result gmp_do_operation_ex(uint8_t opcode, zval *result, zval *op1,
484483
DO_BINARY_OP(mpz_and);
485484
case ZEND_BW_XOR:
486485
DO_BINARY_OP(mpz_xor);
487-
case ZEND_BW_NOT:
488-
DO_UNARY_OP(mpz_com);
486+
case ZEND_BW_NOT: {
487+
if (!gmp_zend_parse_arg_into_mpz_ex(op1, &gmp_op1, 1, false)) {
488+
return FAILURE;
489+
}
490+
gmp_create(result, &gmp_result);
491+
mpz_com(gmp_result, gmp_op1);
492+
return SUCCESS;
493+
}
489494

490495
default:
491496
return FAILURE;
@@ -857,23 +862,6 @@ static inline void gmp_zval_binary_ui_op(zval *return_value, zval *a_arg, zval *
857862
}
858863
/* }}} */
859864

860-
/* Unary operations */
861-
862-
/* {{{ gmp_zval_unary_op */
863-
static inline void gmp_zval_unary_op(zval *return_value, zval *a_arg, gmp_unary_op_t gmp_op)
864-
{
865-
mpz_ptr gmpnum_a, gmpnum_result;
866-
gmp_temp_t temp_a;
867-
868-
FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a, 1);
869-
870-
INIT_GMP_RETVAL(gmpnum_result);
871-
gmp_op(gmpnum_result, gmpnum_a);
872-
873-
FREE_GMP_TEMP(temp_a);
874-
}
875-
/* }}} */
876-
877865
static bool gmp_verify_base(zend_long base, uint32_t arg_num)
878866
{
879867
if (base && (base < 2 || base > GMP_MAX_BASE)) {

0 commit comments

Comments
 (0)