Skip to content

Commit 88bea32

Browse files
committed
ext/gmp: Start refactoring operator overloading to use new parsing mechanism
1 parent adbdc04 commit 88bea32

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

167-
static bool gmp_zend_parse_arg_into_mpz(
167+
static bool gmp_zend_parse_arg_into_mpz_ex(
168168
zval *arg,
169169
mpz_ptr *destination_mpz_ptr,
170-
uint32_t arg_num
170+
uint32_t arg_num,
171+
bool is_operator
171172
) {
172173
if (EXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) {
173174
if (EXPECTED(instanceof_function(Z_OBJCE_P(arg), gmp_ce))) {
@@ -179,7 +180,7 @@ static bool gmp_zend_parse_arg_into_mpz(
179180

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

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

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

247253
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);
248-
static inline void gmp_zval_unary_op(zval *return_value, zval *a_arg, gmp_unary_op_t gmp_op);
249254

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

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

485490
default:
486491
return FAILURE;
@@ -848,23 +853,6 @@ static inline void gmp_zval_binary_ui_op(zval *return_value, zval *a_arg, zval *
848853
}
849854
/* }}} */
850855

851-
/* Unary operations */
852-
853-
/* {{{ gmp_zval_unary_op */
854-
static inline void gmp_zval_unary_op(zval *return_value, zval *a_arg, gmp_unary_op_t gmp_op)
855-
{
856-
mpz_ptr gmpnum_a, gmpnum_result;
857-
gmp_temp_t temp_a;
858-
859-
FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a, 1);
860-
861-
INIT_GMP_RETVAL(gmpnum_result);
862-
gmp_op(gmpnum_result, gmpnum_a);
863-
864-
FREE_GMP_TEMP(temp_a);
865-
}
866-
/* }}} */
867-
868856
static bool gmp_verify_base(zend_long base, uint32_t arg_num)
869857
{
870858
if (base && (base < 2 || base > GMP_MAX_BASE)) {

0 commit comments

Comments
 (0)