Skip to content

Commit 1e38760

Browse files
committed
ext/gmp: Refactor generation of some binary GMP functions
1 parent a9e2a96 commit 1e38760

File tree

1 file changed

+34
-61
lines changed

1 file changed

+34
-61
lines changed

ext/gmp/gmp.c

Lines changed: 34 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,32 @@ static bool gmp_zend_parse_arg_into_mpz(
231231
*/
232232
typedef void (*gmp_unary_op_t)(mpz_ptr, mpz_srcptr);
233233

234+
#define GMP_FN_NAME(name) gmp_##name
235+
#define GMP_MPZ_FN_NAME(name) mpz_##name
236+
234237
#define GMP_UNARY_OP_FUNCTION(name) \
235238
ZEND_FUNCTION(gmp_##name) { \
236239
mpz_ptr gmpnum_a, gmpnum_result; \
237240
ZEND_PARSE_PARAMETERS_START(1, 1) \
238241
GMP_Z_PARAM_INTO_MPZ_PTR(gmpnum_a) \
239242
ZEND_PARSE_PARAMETERS_END(); \
240243
INIT_GMP_RETVAL(gmpnum_result); \
241-
mpz_##name(gmpnum_result, gmpnum_a); \
244+
GMP_MPZ_FN_NAME(name)(gmpnum_result, gmpnum_a); \
245+
}
246+
247+
#define GMP_BINARY_OP_FUNCTION_EX(gmp_name, mpz_name) \
248+
ZEND_FUNCTION(gmp_name) { \
249+
mpz_ptr gmpnum_a, gmpnum_b, gmpnum_result; \
250+
ZEND_PARSE_PARAMETERS_START(2, 2) \
251+
GMP_Z_PARAM_INTO_MPZ_PTR(gmpnum_a) \
252+
GMP_Z_PARAM_INTO_MPZ_PTR(gmpnum_b) \
253+
ZEND_PARSE_PARAMETERS_END(); \
254+
INIT_GMP_RETVAL(gmpnum_result); \
255+
mpz_name(gmpnum_result, gmpnum_a, gmpnum_b); \
242256
}
243257

258+
#define GMP_BINARY_OP_FUNCTION(name) GMP_BINARY_OP_FUNCTION_EX(GMP_FN_NAME(name), GMP_MPZ_FN_NAME(name))
259+
244260
typedef void (*gmp_unary_ui_op_t)(mpz_ptr, gmp_ulong);
245261

246262
typedef void (*gmp_binary_op_t)(mpz_ptr, mpz_srcptr, mpz_srcptr);
@@ -274,12 +290,8 @@ static void gmp_mpz_cdiv_q_ui(mpz_ptr a, mpz_srcptr b, gmp_ulong c) {
274290
static void gmp_mpz_mod_ui(mpz_ptr a, mpz_srcptr b, gmp_ulong c) {
275291
mpz_mod_ui(a, b, c);
276292
}
277-
static void gmp_mpz_gcd_ui(mpz_ptr a, mpz_srcptr b, gmp_ulong c) {
278-
mpz_gcd_ui(a, b, c);
279-
}
280293

281294
/* Binary operations */
282-
#define gmp_binary_ui_op(op, uop) _gmp_binary_ui_op(INTERNAL_FUNCTION_PARAM_PASSTHRU, op, uop, 0)
283295
#define gmp_binary_op(op) _gmp_binary_ui_op(INTERNAL_FUNCTION_PARAM_PASSTHRU, op, NULL, 0)
284296
#define gmp_binary_ui_op_no_zero(op, uop) \
285297
_gmp_binary_ui_op(INTERNAL_FUNCTION_PARAM_PASSTHRU, op, uop, 1)
@@ -1159,27 +1171,6 @@ ZEND_FUNCTION(gmp_strval)
11591171
}
11601172
/* }}} */
11611173

1162-
/* {{{ Add a and b */
1163-
ZEND_FUNCTION(gmp_add)
1164-
{
1165-
gmp_binary_ui_op(mpz_add, mpz_add_ui);
1166-
}
1167-
/* }}} */
1168-
1169-
/* {{{ Subtract b from a */
1170-
ZEND_FUNCTION(gmp_sub)
1171-
{
1172-
gmp_binary_ui_op(mpz_sub, mpz_sub_ui);
1173-
}
1174-
/* }}} */
1175-
1176-
/* {{{ Multiply a and b */
1177-
ZEND_FUNCTION(gmp_mul)
1178-
{
1179-
gmp_binary_ui_op(mpz_mul, mpz_mul_ui);
1180-
}
1181-
/* }}} */
1182-
11831174
/* {{{ Divide a by b, returns quotient and reminder */
11841175
ZEND_FUNCTION(gmp_div_qr)
11851176
{
@@ -1300,6 +1291,23 @@ GMP_UNARY_OP_FUNCTION(com);
13001291
/* {{{ Finds next prime of a */
13011292
GMP_UNARY_OP_FUNCTION(nextprime);
13021293

1294+
/* Add a and b */
1295+
GMP_BINARY_OP_FUNCTION(add);
1296+
/* Subtract b from a */
1297+
GMP_BINARY_OP_FUNCTION(sub);
1298+
/* Multiply a and b */
1299+
GMP_BINARY_OP_FUNCTION(mul);
1300+
/* Computes greatest common denominator (gcd) of a and b */
1301+
GMP_BINARY_OP_FUNCTION(gcd);
1302+
/* Computes least common multiple (lcm) of a and b */
1303+
GMP_BINARY_OP_FUNCTION(lcm);
1304+
/* {Calculates logical AND of a and b */
1305+
GMP_BINARY_OP_FUNCTION(and);
1306+
/* Calculates logical exclusive OR of a and b */
1307+
GMP_BINARY_OP_FUNCTION(xor);
1308+
/* Calculates logical OR of a and b */
1309+
GMP_BINARY_OP_FUNCTION_EX(gmp_or, mpz_ior);
1310+
13031311
/* {{{ Calculates factorial function */
13041312
ZEND_FUNCTION(gmp_fact)
13051313
{
@@ -1564,20 +1572,6 @@ ZEND_FUNCTION(gmp_prob_prime)
15641572
}
15651573
/* }}} */
15661574

1567-
/* {{{ Computes greatest common denominator (gcd) of a and b */
1568-
ZEND_FUNCTION(gmp_gcd)
1569-
{
1570-
gmp_binary_ui_op(mpz_gcd, gmp_mpz_gcd_ui);
1571-
}
1572-
/* }}} */
1573-
1574-
/* {{{ Computes least common multiple (lcm) of a and b */
1575-
ZEND_FUNCTION(gmp_lcm)
1576-
{
1577-
gmp_binary_ui_op(mpz_lcm, mpz_lcm_ui);
1578-
}
1579-
/* }}} */
1580-
15811575
/* {{{ Computes G, S, and T, such that AS + BT = G = `gcd' (A, B) */
15821576
ZEND_FUNCTION(gmp_gcdext)
15831577
{
@@ -1822,27 +1816,6 @@ ZEND_FUNCTION(gmp_random_range)
18221816
}
18231817
/* }}} */
18241818

1825-
/* {{{ Calculates logical AND of a and b */
1826-
ZEND_FUNCTION(gmp_and)
1827-
{
1828-
gmp_binary_op(mpz_and);
1829-
}
1830-
/* }}} */
1831-
1832-
/* {{{ Calculates logical OR of a and b */
1833-
ZEND_FUNCTION(gmp_or)
1834-
{
1835-
gmp_binary_op(mpz_ior);
1836-
}
1837-
/* }}} */
1838-
1839-
/* {{{ Calculates logical exclusive OR of a and b */
1840-
ZEND_FUNCTION(gmp_xor)
1841-
{
1842-
gmp_binary_op(mpz_xor);
1843-
}
1844-
/* }}} */
1845-
18461819
/* {{{ Sets or clear bit in a */
18471820
ZEND_FUNCTION(gmp_setbit)
18481821
{

0 commit comments

Comments
 (0)