Skip to content

Commit fabfb6f

Browse files
committed
ext/gmp: Refactor gmp_divexact() and gmp_mod() to use custom ZPP specifier
1 parent 8e9b944 commit fabfb6f

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
@@ -275,11 +275,6 @@ static void gmp_mpz_mod_ui(mpz_ptr a, mpz_srcptr b, gmp_ulong c) {
275275
mpz_mod_ui(a, b, c);
276276
}
277277

278-
/* Binary operations */
279-
#define gmp_binary_op(op) _gmp_binary_ui_op(INTERNAL_FUNCTION_PARAM_PASSTHRU, op, NULL, 0)
280-
#define gmp_binary_ui_op_no_zero(op, uop) \
281-
_gmp_binary_ui_op(INTERNAL_FUNCTION_PARAM_PASSTHRU, op, uop, 1)
282-
283278
static void gmp_free_object_storage(zend_object *obj) /* {{{ */
284279
{
285280
gmp_object *intern = GET_GMP_OBJECT_FROM_OBJ(obj);
@@ -878,21 +873,6 @@ static inline void gmp_zval_binary_ui_op(zval *return_value, zval *a_arg, zval *
878873
}
879874
/* }}} */
880875

881-
/* {{{ _gmp_binary_ui_op */
882-
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)
883-
{
884-
zval *a_arg, *b_arg;
885-
886-
ZEND_PARSE_PARAMETERS_START(2, 2)
887-
Z_PARAM_ZVAL(a_arg)
888-
Z_PARAM_ZVAL(b_arg)
889-
ZEND_PARSE_PARAMETERS_END();
890-
891-
gmp_zval_binary_ui_op(
892-
return_value, a_arg, b_arg, gmp_op, gmp_ui_op, check_b_zero, /* is_operator */ false);
893-
}
894-
/* }}} */
895-
896876
/* Unary operations */
897877

898878
/* {{{ gmp_zval_unary_op */
@@ -1232,14 +1212,40 @@ ZEND_FUNCTION(gmp_div_q)
12321212
/* {{{ Computes a modulo b */
12331213
ZEND_FUNCTION(gmp_mod)
12341214
{
1235-
gmp_binary_ui_op_no_zero(mpz_mod, gmp_mpz_mod_ui);
1215+
mpz_ptr gmpnum_a, gmpnum_b, gmpnum_result;
1216+
1217+
ZEND_PARSE_PARAMETERS_START(2, 2)
1218+
GMP_Z_PARAM_INTO_MPZ_PTR(gmpnum_a)
1219+
GMP_Z_PARAM_INTO_MPZ_PTR(gmpnum_b)
1220+
ZEND_PARSE_PARAMETERS_END();
1221+
1222+
if (mpz_cmp_ui(gmpnum_b, 0) == 0) {
1223+
zend_argument_error(zend_ce_division_by_zero_error, 2, "Modulo by zero");
1224+
RETURN_THROWS();
1225+
}
1226+
1227+
INIT_GMP_RETVAL(gmpnum_result);
1228+
mpz_mod(gmpnum_result, gmpnum_a, gmpnum_b);
12361229
}
12371230
/* }}} */
12381231

12391232
/* {{{ Divide a by b using exact division algorithm */
12401233
ZEND_FUNCTION(gmp_divexact)
12411234
{
1242-
gmp_binary_ui_op_no_zero(mpz_divexact, NULL);
1235+
mpz_ptr gmpnum_a, gmpnum_b, gmpnum_result;
1236+
1237+
ZEND_PARSE_PARAMETERS_START(2, 2)
1238+
GMP_Z_PARAM_INTO_MPZ_PTR(gmpnum_a)
1239+
GMP_Z_PARAM_INTO_MPZ_PTR(gmpnum_b)
1240+
ZEND_PARSE_PARAMETERS_END();
1241+
1242+
if (mpz_cmp_ui(gmpnum_b, 0) == 0) {
1243+
zend_argument_error(zend_ce_division_by_zero_error, 2, "Division by zero");
1244+
RETURN_THROWS();
1245+
}
1246+
1247+
INIT_GMP_RETVAL(gmpnum_result);
1248+
mpz_divexact(gmpnum_result, gmpnum_a, gmpnum_b);
12431249
}
12441250
/* }}} */
12451251

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)