From 2bdfafadeb229b8cc7189011c43f567f382eeab8 Mon Sep 17 00:00:00 2001 From: Antony Dovgal Date: Mon, 23 Apr 2007 14:57:37 +0000 Subject: [PATCH] [DOC] add gmp_testbit() function tests if the specified bit is set and returns false/true --- NEWS | 1 + ext/gmp/gmp.c | 36 +++++++++++++++++++ ext/gmp/php_gmp.h | 1 + ext/gmp/tests/039.phpt | 79 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+) create mode 100644 ext/gmp/tests/039.phpt diff --git a/NEWS b/NEWS index a1be27f842d..82c9faa85c9 100644 --- a/NEWS +++ b/NEWS @@ -57,6 +57,7 @@ PHP NEWS set params during context creation. (Sara) . "context" and "binary_pipes" params in "other_options" arg. (Sara) . stream_resolve_include_path(). (Sara) +- Added gmp_testbit() function. (Tony) - Added shm_has_var() function. (Mike) - Added str_getcsv() function. (Sara) - Added ext/hash support to ext/session's ID generator. (Sara) diff --git a/ext/gmp/gmp.c b/ext/gmp/gmp.c index dca90ac1a47..ef1f44214bc 100644 --- a/ext/gmp/gmp.c +++ b/ext/gmp/gmp.c @@ -242,6 +242,12 @@ ZEND_BEGIN_ARG_INFO(arginfo_gmp_clrbit, 0) ZEND_ARG_INFO(0, index) ZEND_END_ARG_INFO() + static +ZEND_BEGIN_ARG_INFO(arginfo_gmp_testbit, 0) + ZEND_ARG_INFO(0, a) + ZEND_ARG_INFO(0, index) +ZEND_END_ARG_INFO() + static ZEND_BEGIN_ARG_INFO(arginfo_gmp_popcount, 0) ZEND_ARG_INFO(0, a) @@ -308,6 +314,7 @@ zend_function_entry gmp_functions[] = { ZEND_FE(gmp_xor, arginfo_gmp_xor) ZEND_FE(gmp_setbit, arginfo_gmp_setbit) ZEND_FE(gmp_clrbit, arginfo_gmp_clrbit) + ZEND_FE(gmp_testbit, arginfo_gmp_testbit) ZEND_FE(gmp_scan0, arginfo_gmp_scan0) ZEND_FE(gmp_scan1, arginfo_gmp_scan1) ZEND_FE(gmp_popcount, arginfo_gmp_popcount) @@ -1531,6 +1538,35 @@ ZEND_FUNCTION(gmp_clrbit) } /* }}} */ +/* {{{ proto bool gmp_testbit(resource a, int index) U + Tests if bit is set in a */ +ZEND_FUNCTION(gmp_testbit) +{ + zval **a_arg, **ind_arg; + int index; + mpz_t *gmpnum_a; + + if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &a_arg, &ind_arg) == FAILURE){ + WRONG_PARAM_COUNT; + } + + ZEND_FETCH_RESOURCE(gmpnum_a, mpz_t *, a_arg, -1, GMP_RESOURCE_NAME, le_gmp); + + convert_to_long_ex(ind_arg); + index = Z_LVAL_PP(ind_arg); + + if (index < 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Index must be greater than or equal to zero"); + RETURN_FALSE; + } + + if (mpz_tstbit(*gmpnum_a, index)) { + RETURN_TRUE; + } + RETURN_FALSE; +} +/* }}} */ + /* {{{ proto int gmp_popcount(resource a) U Calculates the population count of a */ ZEND_FUNCTION(gmp_popcount) diff --git a/ext/gmp/php_gmp.h b/ext/gmp/php_gmp.h index 22b4abcf487..ce208c3d4df 100644 --- a/ext/gmp/php_gmp.h +++ b/ext/gmp/php_gmp.h @@ -70,6 +70,7 @@ ZEND_FUNCTION(gmp_xor); ZEND_FUNCTION(gmp_random); ZEND_FUNCTION(gmp_setbit); ZEND_FUNCTION(gmp_clrbit); +ZEND_FUNCTION(gmp_testbit); ZEND_FUNCTION(gmp_scan0); ZEND_FUNCTION(gmp_scan1); ZEND_FUNCTION(gmp_popcount); diff --git a/ext/gmp/tests/039.phpt b/ext/gmp/tests/039.phpt new file mode 100644 index 00000000000..084772288c0 --- /dev/null +++ b/ext/gmp/tests/039.phpt @@ -0,0 +1,79 @@ +--TEST-- +gmp_testbit() basic tests +--FILE-- + +--EXPECTF-- +Warning: gmp_testbit(): Index must be greater than or equal to zero in %s on line %d +bool(false) +bool(false) +bool(false) +bool(false) +bool(true) + +Warning: gmp_testbit(): Index must be greater than or equal to zero in %s on line %d +bool(false) +bool(false) +bool(true) +string(7) "1000002" +bool(true) +string(7) "1000034" +bool(false) +bool(true) +string(30) "238462734628347239571823641266" +bool(false) +string(30) "238462734628347239571823641234" +Done +--UEXPECTF-- +Warning: gmp_testbit(): Index must be greater than or equal to zero in %s on line %d +bool(false) +bool(false) +bool(false) +bool(false) +bool(true) + +Warning: gmp_testbit(): Index must be greater than or equal to zero in %s on line %d +bool(false) +bool(false) +bool(true) +unicode(7) "1000002" +bool(true) +unicode(7) "1000034" +bool(false) +bool(true) +unicode(30) "238462734628347239571823641266" +bool(false) +unicode(30) "238462734628347239571823641234" +Done