Skip to content

Commit 226bf32

Browse files
committed
ext/gmp: Start refactoring operator overloading to use new parsing mechanism
1 parent 1b911c1 commit 226bf32

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
@@ -165,10 +165,11 @@ static zend_result convert_zstr_to_gmp(mpz_t gmp_number, const zend_string *val,
165165
static zend_result convert_to_gmp(mpz_t gmpnumber, zval *val, zend_long base, uint32_t arg_pos);
166166
static void gmp_cmp(zval *return_value, zval *a_arg, zval *b_arg, bool is_operator);
167167

168-
static bool gmp_zend_parse_arg_into_mpz(
168+
static bool gmp_zend_parse_arg_into_mpz_ex(
169169
zval *arg,
170170
mpz_ptr *destination_mpz_ptr,
171-
uint32_t arg_num
171+
uint32_t arg_num,
172+
bool is_operator
172173
) {
173174
if (EXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) {
174175
if (EXPECTED(instanceof_function(Z_OBJCE_P(arg), gmp_ce))) {
@@ -180,7 +181,7 @@ static bool gmp_zend_parse_arg_into_mpz(
180181

181182
*destination_mpz_ptr = GMPG(zpp_arg[arg_num-1]);
182183
if (Z_TYPE_P(arg) == IS_STRING) {
183-
return convert_zstr_to_gmp(*destination_mpz_ptr, Z_STR_P(arg), /* base */ 0, arg_num) != FAILURE;
184+
return convert_zstr_to_gmp(*destination_mpz_ptr, Z_STR_P(arg), /* base */ 0, is_operator ? 0 : arg_num) != FAILURE;
184185
}
185186

186187
if (Z_TYPE_P(arg) == IS_LONG) {
@@ -190,6 +191,11 @@ static bool gmp_zend_parse_arg_into_mpz(
190191
return false;
191192
}
192193

194+
static bool gmp_zend_parse_arg_into_mpz(zval *arg, mpz_ptr *destination_mpz_ptr, uint32_t arg_num)
195+
{
196+
return gmp_zend_parse_arg_into_mpz_ex(arg, destination_mpz_ptr, arg_num, false);
197+
}
198+
193199
#define GMP_Z_PARAM_INTO_MPZ_PTR(destination_mpz_ptr) \
194200
Z_PARAM_PROLOGUE(0, 0); \
195201
if (UNEXPECTED(!gmp_zend_parse_arg_into_mpz(_arg, &destination_mpz_ptr, _i))) { \
@@ -246,7 +252,6 @@ typedef void (*gmp_binary_op2_t)(mpz_ptr, mpz_ptr, mpz_srcptr, mpz_srcptr);
246252
typedef gmp_ulong (*gmp_binary_ui_op2_t)(mpz_ptr, mpz_ptr, mpz_srcptr, gmp_ulong);
247253

248254
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);
249-
static inline void gmp_zval_unary_op(zval *return_value, zval *a_arg, gmp_unary_op_t gmp_op);
250255

251256
static void gmp_mpz_tdiv_q_ui(mpz_ptr a, mpz_srcptr b, gmp_ulong c) {
252257
mpz_tdiv_q_ui(a, b, c);
@@ -448,15 +453,9 @@ typeof_op_failure: ;
448453
#define DO_BINARY_UI_OP(op) DO_BINARY_UI_OP_EX(op, op ## _ui, 0)
449454
#define DO_BINARY_OP(op) DO_BINARY_UI_OP_EX(op, NULL, 0)
450455

451-
#define DO_UNARY_OP(op) \
452-
gmp_zval_unary_op(result, op1, op); \
453-
if (UNEXPECTED(EG(exception))) { \
454-
return FAILURE; \
455-
} \
456-
return SUCCESS;
457-
458456
static zend_result gmp_do_operation_ex(uint8_t opcode, zval *result, zval *op1, zval *op2) /* {{{ */
459457
{
458+
mpz_ptr gmp_op1, gmp_result;
460459
switch (opcode) {
461460
case ZEND_ADD:
462461
DO_BINARY_UI_OP(mpz_add);
@@ -480,8 +479,14 @@ static zend_result gmp_do_operation_ex(uint8_t opcode, zval *result, zval *op1,
480479
DO_BINARY_OP(mpz_and);
481480
case ZEND_BW_XOR:
482481
DO_BINARY_OP(mpz_xor);
483-
case ZEND_BW_NOT:
484-
DO_UNARY_OP(mpz_com);
482+
case ZEND_BW_NOT: {
483+
if (!gmp_zend_parse_arg_into_mpz_ex(op1, &gmp_op1, 1, false)) {
484+
return FAILURE;
485+
}
486+
gmp_create(result, &gmp_result);
487+
mpz_com(gmp_result, gmp_op1);
488+
return SUCCESS;
489+
}
485490

486491
default:
487492
return FAILURE;
@@ -853,23 +858,6 @@ static inline void gmp_zval_binary_ui_op(zval *return_value, zval *a_arg, zval *
853858
}
854859
/* }}} */
855860

856-
/* Unary operations */
857-
858-
/* {{{ gmp_zval_unary_op */
859-
static inline void gmp_zval_unary_op(zval *return_value, zval *a_arg, gmp_unary_op_t gmp_op)
860-
{
861-
mpz_ptr gmpnum_a, gmpnum_result;
862-
gmp_temp_t temp_a;
863-
864-
FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a, 1);
865-
866-
INIT_GMP_RETVAL(gmpnum_result);
867-
gmp_op(gmpnum_result, gmpnum_a);
868-
869-
FREE_GMP_TEMP(temp_a);
870-
}
871-
/* }}} */
872-
873861
static bool gmp_verify_base(zend_long base, uint32_t arg_num)
874862
{
875863
if (base && (base < 2 || base > GMP_MAX_BASE)) {

0 commit comments

Comments
 (0)