Skip to content

Commit 73318d3

Browse files
committed
ext/gmp: Refactor gmp_divexact() and gmp_mod() to use custom ZPP specifier
1 parent 53fe3e9 commit 73318d3

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

ext/gmp/gmp.c

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,6 @@ static void gmp_mpz_mod_ui(mpz_ptr a, mpz_srcptr b, gmp_ulong c) {
271271
mpz_mod_ui(a, b, c);
272272
}
273273

274-
/* Binary operations */
275-
#define gmp_binary_op(op) _gmp_binary_ui_op(INTERNAL_FUNCTION_PARAM_PASSTHRU, op, NULL, 0)
276-
#define gmp_binary_ui_op_no_zero(op, uop) \
277-
_gmp_binary_ui_op(INTERNAL_FUNCTION_PARAM_PASSTHRU, op, uop, 1)
278-
279274
static void gmp_free_object_storage(zend_object *obj) /* {{{ */
280275
{
281276
gmp_object *intern = GET_GMP_OBJECT_FROM_OBJ(obj);
@@ -874,21 +869,6 @@ static inline void gmp_zval_binary_ui_op(zval *return_value, zval *a_arg, zval *
874869
}
875870
/* }}} */
876871

877-
/* {{{ _gmp_binary_ui_op */
878-
static inline void _gmp_binary_ui_op(INTERNAL_FUNCTION_PARAMETERS, gmp_binary_op_t gmp_op, gmp_binary_ui_op_t gmp_ui_op, int check_b_zero)
879-
{
880-
zval *a_arg, *b_arg;
881-
882-
ZEND_PARSE_PARAMETERS_START(2, 2)
883-
Z_PARAM_ZVAL(a_arg)
884-
Z_PARAM_ZVAL(b_arg)
885-
ZEND_PARSE_PARAMETERS_END();
886-
887-
gmp_zval_binary_ui_op(
888-
return_value, a_arg, b_arg, gmp_op, gmp_ui_op, check_b_zero, /* is_operator */ false);
889-
}
890-
/* }}} */
891-
892872
/* Unary operations */
893873

894874
/* {{{ gmp_zval_unary_op */
@@ -1228,14 +1208,40 @@ ZEND_FUNCTION(gmp_div_q)
12281208
/* {{{ Computes a modulo b */
12291209
ZEND_FUNCTION(gmp_mod)
12301210
{
1231-
gmp_binary_ui_op_no_zero(mpz_mod, gmp_mpz_mod_ui);
1211+
mpz_ptr gmpnum_a, gmpnum_b, gmpnum_result;
1212+
1213+
ZEND_PARSE_PARAMETERS_START(2, 2)
1214+
GMP_Z_PARAM_INTO_MPZ_PTR(gmpnum_a)
1215+
GMP_Z_PARAM_INTO_MPZ_PTR(gmpnum_b)
1216+
ZEND_PARSE_PARAMETERS_END();
1217+
1218+
if (mpz_cmp_ui(gmpnum_b, 0) == 0) {
1219+
zend_argument_error(zend_ce_division_by_zero_error, 2, "Modulo by zero");
1220+
RETURN_THROWS();
1221+
}
1222+
1223+
INIT_GMP_RETVAL(gmpnum_result);
1224+
mpz_mod(gmpnum_result, gmpnum_a, gmpnum_b);
12321225
}
12331226
/* }}} */
12341227

12351228
/* {{{ Divide a by b using exact division algorithm */
12361229
ZEND_FUNCTION(gmp_divexact)
12371230
{
1238-
gmp_binary_ui_op_no_zero(mpz_divexact, NULL);
1231+
mpz_ptr gmpnum_a, gmpnum_b, gmpnum_result;
1232+
1233+
ZEND_PARSE_PARAMETERS_START(2, 2)
1234+
GMP_Z_PARAM_INTO_MPZ_PTR(gmpnum_a)
1235+
GMP_Z_PARAM_INTO_MPZ_PTR(gmpnum_b)
1236+
ZEND_PARSE_PARAMETERS_END();
1237+
1238+
if (mpz_cmp_ui(gmpnum_b, 0) == 0) {
1239+
zend_argument_error(zend_ce_division_by_zero_error, 2, "Division by zero");
1240+
RETURN_THROWS();
1241+
}
1242+
1243+
INIT_GMP_RETVAL(gmpnum_result);
1244+
mpz_divexact(gmpnum_result, gmpnum_a, gmpnum_b);
12391245
}
12401246
/* }}} */
12411247

ext/gmp/tests/gmp_divexact.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ echo "Done\n";
4242
?>
4343
--EXPECT--
4444
string(1) "0"
45-
Division by zero
45+
gmp_divexact(): Argument #2 ($num2) Division by zero
4646
string(2) "10"
4747
string(3) "512"
4848
string(19) "5000000000000000000"

ext/gmp/tests/gmp_mod.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ object(GMP)#2 (1) {
4242
["num"]=>
4343
string(1) "0"
4444
}
45-
Modulo by zero
45+
gmp_mod(): Argument #2 ($num2) Modulo by zero
4646
gmp_mod(): Argument #1 ($num1) must be of type GMP|string|int, array given
4747
object(GMP)#4 (1) {
4848
["num"]=>

0 commit comments

Comments
 (0)