Skip to content

Commit a9e2a96

Browse files
committed
ext/gmp: Refactor generation of unary GMP functions
1 parent 10003a3 commit a9e2a96

File tree

1 file changed

+16
-41
lines changed

1 file changed

+16
-41
lines changed

ext/gmp/gmp.c

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

234+
#define GMP_UNARY_OP_FUNCTION(name) \
235+
ZEND_FUNCTION(gmp_##name) { \
236+
mpz_ptr gmpnum_a, gmpnum_result; \
237+
ZEND_PARSE_PARAMETERS_START(1, 1) \
238+
GMP_Z_PARAM_INTO_MPZ_PTR(gmpnum_a) \
239+
ZEND_PARSE_PARAMETERS_END(); \
240+
INIT_GMP_RETVAL(gmpnum_result); \
241+
mpz_##name(gmpnum_result, gmpnum_a); \
242+
}
243+
234244
typedef void (*gmp_unary_ui_op_t)(mpz_ptr, gmp_ulong);
235245

236246
typedef void (*gmp_binary_op_t)(mpz_ptr, mpz_srcptr, mpz_srcptr);
@@ -274,9 +284,6 @@ static void gmp_mpz_gcd_ui(mpz_ptr a, mpz_srcptr b, gmp_ulong c) {
274284
#define gmp_binary_ui_op_no_zero(op, uop) \
275285
_gmp_binary_ui_op(INTERNAL_FUNCTION_PARAM_PASSTHRU, op, uop, 1)
276286

277-
/* Unary operations */
278-
#define gmp_unary_op(op) _gmp_unary_op(INTERNAL_FUNCTION_PARAM_PASSTHRU, op)
279-
280287
static void gmp_free_object_storage(zend_object *obj) /* {{{ */
281288
{
282289
gmp_object *intern = GET_GMP_OBJECT_FROM_OBJ(obj);
@@ -959,19 +966,6 @@ static inline void gmp_zval_unary_op(zval *return_value, zval *a_arg, gmp_unary_
959966
}
960967
/* }}} */
961968

962-
/* {{{ _gmp_unary_op */
963-
static inline void _gmp_unary_op(INTERNAL_FUNCTION_PARAMETERS, gmp_unary_op_t gmp_op)
964-
{
965-
zval *a_arg;
966-
967-
ZEND_PARSE_PARAMETERS_START(1, 1)
968-
Z_PARAM_ZVAL(a_arg)
969-
ZEND_PARSE_PARAMETERS_END();
970-
971-
gmp_zval_unary_op(return_value, a_arg, gmp_op);
972-
}
973-
/* }}} */
974-
975969
static bool gmp_verify_base(zend_long base, uint32_t arg_num)
976970
{
977971
if (base && (base < 2 || base > GMP_MAX_BASE)) {
@@ -1298,18 +1292,13 @@ ZEND_FUNCTION(gmp_divexact)
12981292
/* }}} */
12991293

13001294
/* {{{ Negates a number */
1301-
ZEND_FUNCTION(gmp_neg)
1302-
{
1303-
gmp_unary_op(mpz_neg);
1304-
}
1305-
/* }}} */
1306-
1295+
GMP_UNARY_OP_FUNCTION(neg);
13071296
/* {{{ Calculates absolute value */
1308-
ZEND_FUNCTION(gmp_abs)
1309-
{
1310-
gmp_unary_op(mpz_abs);
1311-
}
1312-
/* }}} */
1297+
GMP_UNARY_OP_FUNCTION(abs);
1298+
/* {{{ Calculates one's complement of a */
1299+
GMP_UNARY_OP_FUNCTION(com);
1300+
/* {{{ Finds next prime of a */
1301+
GMP_UNARY_OP_FUNCTION(nextprime);
13131302

13141303
/* {{{ Calculates factorial function */
13151304
ZEND_FUNCTION(gmp_fact)
@@ -1847,20 +1836,6 @@ ZEND_FUNCTION(gmp_or)
18471836
}
18481837
/* }}} */
18491838

1850-
/* {{{ Calculates one's complement of a */
1851-
ZEND_FUNCTION(gmp_com)
1852-
{
1853-
gmp_unary_op(mpz_com);
1854-
}
1855-
/* }}} */
1856-
1857-
/* {{{ Finds next prime of a */
1858-
ZEND_FUNCTION(gmp_nextprime)
1859-
{
1860-
gmp_unary_op(mpz_nextprime);
1861-
}
1862-
/* }}} */
1863-
18641839
/* {{{ Calculates logical exclusive OR of a and b */
18651840
ZEND_FUNCTION(gmp_xor)
18661841
{

0 commit comments

Comments
 (0)