PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/gmp - gmp.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 674 674 100.0 %
Date: 2014-09-19 Functions: 76 76 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2014 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Author: Stanislav Malyshev <stas@php.net>                            |
      16             :    +----------------------------------------------------------------------+
      17             :  */
      18             : 
      19             : #ifdef HAVE_CONFIG_H
      20             : #include "config.h"
      21             : #endif
      22             : 
      23             : #include "php.h"
      24             : #include "php_ini.h"
      25             : #include "php_gmp.h"
      26             : #include "ext/standard/info.h"
      27             : #include "ext/standard/php_var.h"
      28             : #include "ext/standard/php_smart_str_public.h"
      29             : #include "zend_exceptions.h"
      30             : 
      31             : #if HAVE_GMP
      32             : 
      33             : #include <gmp.h>
      34             : 
      35             : /* Needed for gmp_random() */
      36             : #include "ext/standard/php_rand.h"
      37             : #include "ext/standard/php_lcg.h"
      38             : #define GMP_ABS(x) ((x) >= 0 ? (x) : -(x))
      39             : 
      40             : /* {{{ arginfo */
      41             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_init, 0, 0, 1)
      42             :         ZEND_ARG_INFO(0, number)
      43             :         ZEND_ARG_INFO(0, base)
      44             : ZEND_END_ARG_INFO()
      45             : 
      46             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_import, 0, 0, 1)
      47             :         ZEND_ARG_INFO(0, data)
      48             :         ZEND_ARG_INFO(0, word_size)
      49             :         ZEND_ARG_INFO(0, options)
      50             : ZEND_END_ARG_INFO()
      51             : 
      52             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_export, 0, 0, 1)
      53             :         ZEND_ARG_INFO(0, gmpnumber)
      54             :         ZEND_ARG_INFO(0, word_size)
      55             :         ZEND_ARG_INFO(0, options)
      56             : ZEND_END_ARG_INFO()
      57             : 
      58             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_intval, 0, 0, 1)
      59             :         ZEND_ARG_INFO(0, gmpnumber)
      60             : ZEND_END_ARG_INFO()
      61             : 
      62             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_strval, 0, 0, 1)
      63             :         ZEND_ARG_INFO(0, gmpnumber)
      64             :         ZEND_ARG_INFO(0, base)
      65             : ZEND_END_ARG_INFO()
      66             : 
      67             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_unary, 0, 0, 1)
      68             :         ZEND_ARG_INFO(0, a)
      69             : ZEND_END_ARG_INFO()
      70             : 
      71             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_binary, 0, 0, 2)
      72             :         ZEND_ARG_INFO(0, a)
      73             :         ZEND_ARG_INFO(0, b)
      74             : ZEND_END_ARG_INFO()
      75             : 
      76             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_div, 0, 0, 2)
      77             :         ZEND_ARG_INFO(0, a)
      78             :         ZEND_ARG_INFO(0, b)
      79             :         ZEND_ARG_INFO(0, round)
      80             : ZEND_END_ARG_INFO()
      81             : 
      82             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_pow, 0, 0, 2)
      83             :         ZEND_ARG_INFO(0, base)
      84             :         ZEND_ARG_INFO(0, exp)
      85             : ZEND_END_ARG_INFO()
      86             : 
      87             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_powm, 0, 0, 3)
      88             :         ZEND_ARG_INFO(0, base)
      89             :         ZEND_ARG_INFO(0, exp)
      90             :         ZEND_ARG_INFO(0, mod)
      91             : ZEND_END_ARG_INFO()
      92             : 
      93             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_root, 0, 0, 2)
      94             :         ZEND_ARG_INFO(0, a)
      95             :         ZEND_ARG_INFO(0, nth)
      96             : ZEND_END_ARG_INFO()
      97             : 
      98             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_prob_prime, 0, 0, 1)
      99             :         ZEND_ARG_INFO(0, a)
     100             :         ZEND_ARG_INFO(0, reps)
     101             : ZEND_END_ARG_INFO()
     102             : 
     103             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_random, 0, 0, 0)
     104             :         ZEND_ARG_INFO(0, limiter)
     105             : ZEND_END_ARG_INFO()
     106             : 
     107             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_setbit, 0, 0, 2)
     108             :         ZEND_ARG_INFO(0, a)
     109             :         ZEND_ARG_INFO(0, index)
     110             :         ZEND_ARG_INFO(0, set_clear)
     111             : ZEND_END_ARG_INFO()
     112             : 
     113             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_bit, 0, 0, 2)
     114             :         ZEND_ARG_INFO(0, a)
     115             :         ZEND_ARG_INFO(0, index)
     116             : ZEND_END_ARG_INFO()
     117             : 
     118             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_scan, 0, 0, 2)
     119             :         ZEND_ARG_INFO(0, a)
     120             :         ZEND_ARG_INFO(0, start)
     121             : ZEND_END_ARG_INFO()
     122             : 
     123             : /* }}} */
     124             : 
     125             : ZEND_DECLARE_MODULE_GLOBALS(gmp)
     126             : static ZEND_GINIT_FUNCTION(gmp);
     127             : 
     128             : /* {{{ gmp_functions[]
     129             :  */
     130             : const zend_function_entry gmp_functions[] = {
     131             :         ZEND_FE(gmp_init,               arginfo_gmp_init)
     132             :         ZEND_FE(gmp_import,             arginfo_gmp_import)
     133             :         ZEND_FE(gmp_export,             arginfo_gmp_export)
     134             :         ZEND_FE(gmp_intval,             arginfo_gmp_intval)
     135             :         ZEND_FE(gmp_strval,             arginfo_gmp_strval)
     136             :         ZEND_FE(gmp_add,                arginfo_gmp_binary)
     137             :         ZEND_FE(gmp_sub,                arginfo_gmp_binary)
     138             :         ZEND_FE(gmp_mul,                arginfo_gmp_binary)
     139             :         ZEND_FE(gmp_div_qr,             arginfo_gmp_div)
     140             :         ZEND_FE(gmp_div_q,              arginfo_gmp_div)
     141             :         ZEND_FE(gmp_div_r,              arginfo_gmp_div)
     142             :         ZEND_FALIAS(gmp_div, gmp_div_q, arginfo_gmp_div)
     143             :         ZEND_FE(gmp_mod,                arginfo_gmp_binary)
     144             :         ZEND_FE(gmp_divexact,   arginfo_gmp_binary)
     145             :         ZEND_FE(gmp_neg,                arginfo_gmp_unary)
     146             :         ZEND_FE(gmp_abs,                arginfo_gmp_unary)
     147             :         ZEND_FE(gmp_fact,               arginfo_gmp_unary)
     148             :         ZEND_FE(gmp_sqrt,               arginfo_gmp_unary)
     149             :         ZEND_FE(gmp_sqrtrem,    arginfo_gmp_unary)
     150             :         ZEND_FE(gmp_root,               arginfo_gmp_root)
     151             :         ZEND_FE(gmp_rootrem,    arginfo_gmp_root)
     152             :         ZEND_FE(gmp_pow,                arginfo_gmp_pow)
     153             :         ZEND_FE(gmp_powm,               arginfo_gmp_powm)
     154             :         ZEND_FE(gmp_perfect_square,     arginfo_gmp_unary)
     155             :         ZEND_FE(gmp_prob_prime, arginfo_gmp_prob_prime)
     156             :         ZEND_FE(gmp_gcd,                arginfo_gmp_binary)
     157             :         ZEND_FE(gmp_gcdext,             arginfo_gmp_binary)
     158             :         ZEND_FE(gmp_invert,             arginfo_gmp_binary)
     159             :         ZEND_FE(gmp_jacobi,             arginfo_gmp_binary)
     160             :         ZEND_FE(gmp_legendre,   arginfo_gmp_binary)
     161             :         ZEND_FE(gmp_cmp,                arginfo_gmp_binary)
     162             :         ZEND_FE(gmp_sign,               arginfo_gmp_unary)
     163             :         ZEND_FE(gmp_random,             arginfo_gmp_random)
     164             :         ZEND_FE(gmp_and,                arginfo_gmp_binary)
     165             :         ZEND_FE(gmp_or,                 arginfo_gmp_binary)
     166             :         ZEND_FE(gmp_com,                arginfo_gmp_unary)
     167             :         ZEND_FE(gmp_xor,                arginfo_gmp_binary)
     168             :         ZEND_FE(gmp_setbit,             arginfo_gmp_setbit)
     169             :         ZEND_FE(gmp_clrbit,             arginfo_gmp_bit)
     170             :         ZEND_FE(gmp_testbit,    arginfo_gmp_bit)
     171             :         ZEND_FE(gmp_scan0,      arginfo_gmp_scan)
     172             :         ZEND_FE(gmp_scan1,      arginfo_gmp_scan)
     173             :         ZEND_FE(gmp_popcount,   arginfo_gmp_unary)
     174             :         ZEND_FE(gmp_hamdist,    arginfo_gmp_binary)
     175             :         ZEND_FE(gmp_nextprime,  arginfo_gmp_unary)
     176             :         PHP_FE_END
     177             : };
     178             : /* }}} */
     179             : 
     180             : /* {{{ gmp_module_entry
     181             :  */
     182             : zend_module_entry gmp_module_entry = {
     183             :         STANDARD_MODULE_HEADER,
     184             :         "gmp",
     185             :         gmp_functions,
     186             :         ZEND_MODULE_STARTUP_N(gmp),
     187             :         NULL,
     188             :         NULL,
     189             :         ZEND_MODULE_DEACTIVATE_N(gmp),
     190             :         ZEND_MODULE_INFO_N(gmp),
     191             :         NO_VERSION_YET,
     192             :         ZEND_MODULE_GLOBALS(gmp),
     193             :         ZEND_GINIT(gmp),
     194             :         NULL,
     195             :         NULL,
     196             :         STANDARD_MODULE_PROPERTIES_EX
     197             : };
     198             : /* }}} */
     199             : 
     200             : #ifdef COMPILE_DL_GMP
     201             : ZEND_GET_MODULE(gmp)
     202             : #endif
     203             : 
     204             : zend_class_entry *gmp_ce;
     205             : static zend_object_handlers gmp_object_handlers;
     206             : 
     207             : typedef struct _gmp_object {
     208             :         mpz_t num;
     209             :         zend_object std;
     210             : } gmp_object;
     211             : 
     212             : typedef struct _gmp_temp {
     213             :         mpz_t num;
     214             :         zend_bool is_used;
     215             : } gmp_temp_t;
     216             : 
     217             : #define GMP_ROUND_ZERO      0
     218             : #define GMP_ROUND_PLUSINF   1
     219             : #define GMP_ROUND_MINUSINF  2
     220             : 
     221             : #define GMP_MSW_FIRST     (1 << 0)
     222             : #define GMP_LSW_FIRST     (1 << 1)
     223             : #define GMP_LITTLE_ENDIAN (1 << 2)
     224             : #define GMP_BIG_ENDIAN    (1 << 3)
     225             : #define GMP_NATIVE_ENDIAN (1 << 4)
     226             : 
     227             : #define GMP_42_OR_NEWER \
     228             :         ((__GNU_MP_VERSION >= 5) || (__GNU_MP_VERSION >= 4 && __GNU_MP_VERSION_MINOR >= 2))
     229             : 
     230             : /* The maximum base for input and output conversions is 62 from GMP 4.2
     231             :  * onwards. */
     232             : #if GMP_42_OR_NEWER
     233             : #       define MAX_BASE 62
     234             : #else
     235             : #       define MAX_BASE 36
     236             : #endif
     237             : 
     238             : #define IS_GMP(zval) \
     239             :         (Z_TYPE_P(zval) == IS_OBJECT && instanceof_function(Z_OBJCE_P(zval), gmp_ce TSRMLS_CC))
     240             : 
     241             : #define GET_GMP_OBJECT_FROM_OBJ(obj) \
     242             :         ((gmp_object *) ((char *) (obj) - XtOffsetOf(gmp_object, std)))
     243             : #define GET_GMP_OBJECT_FROM_ZVAL(zv) \
     244             :         GET_GMP_OBJECT_FROM_OBJ(Z_OBJ_P(zv))
     245             : 
     246             : #define GET_GMP_FROM_ZVAL(zval) \
     247             :         GET_GMP_OBJECT_FROM_OBJ(Z_OBJ_P(zval))->num
     248             : 
     249             : /* The FETCH_GMP_ZVAL_* family of macros is used to fetch a gmp number
     250             :  * (mpz_ptr) from a zval. If the zval is not a GMP instance, then we
     251             :  * try to convert the value to a temporary gmp number using convert_to_gmp.
     252             :  * This temporary number is stored in the temp argument, which is of type
     253             :  * gmp_temp_t. This temporary value needs to be freed lateron using the
     254             :  * FREE_GMP_TEMP macro.
     255             :  *
     256             :  * If the conversion to a gmp number fails, the macros return false.
     257             :  * The _DEP / _DEP_DEP variants additionally free the temporary values
     258             :  * passed in the last / last two arguments.
     259             :  *
     260             :  * If one zval can sometimes be fetched as a long you have to set the
     261             :  * is_used member of the corresponding gmp_temp_t value to 0, otherwise
     262             :  * the FREE_GMP_TEMP and *_DEP macros will not work properly.
     263             :  *
     264             :  * The three FETCH_GMP_ZVAL_* macros below are mostly copy & paste code
     265             :  * as I couldn't find a way to combine them.
     266             :  */
     267             : 
     268             : #define FREE_GMP_TEMP(temp)  \
     269             :         if (temp.is_used) {      \
     270             :                 mpz_clear(temp.num); \
     271             :         }
     272             : 
     273             : #define FETCH_GMP_ZVAL_DEP_DEP(gmpnumber, zval, temp, dep1, dep2) \
     274             : if (IS_GMP(zval)) {                                               \
     275             :         gmpnumber = GET_GMP_FROM_ZVAL(zval);                          \
     276             :         temp.is_used = 0;                                             \
     277             : } else {                                                          \
     278             :         mpz_init(temp.num);                                           \
     279             :         if (convert_to_gmp(temp.num, zval, 0 TSRMLS_CC) == FAILURE) { \
     280             :                 mpz_clear(temp.num);                                      \
     281             :                 FREE_GMP_TEMP(dep1);                                      \
     282             :                 FREE_GMP_TEMP(dep2);                                      \
     283             :                 RETURN_FALSE;                                             \
     284             :         }                                                             \
     285             :         temp.is_used = 1;                                             \
     286             :         gmpnumber = temp.num;                                         \
     287             : }
     288             : 
     289             : #define FETCH_GMP_ZVAL_DEP(gmpnumber, zval, temp, dep)            \
     290             : if (IS_GMP(zval)) {                                               \
     291             :         gmpnumber = GET_GMP_FROM_ZVAL(zval);                          \
     292             :         temp.is_used = 0;                                             \
     293             : } else {                                                          \
     294             :         mpz_init(temp.num);                                           \
     295             :         if (convert_to_gmp(temp.num, zval, 0 TSRMLS_CC) == FAILURE) { \
     296             :                 mpz_clear(temp.num);                                      \
     297             :                 FREE_GMP_TEMP(dep);                                       \
     298             :                 RETURN_FALSE;                                             \
     299             :         }                                                             \
     300             :         temp.is_used = 1;                                             \
     301             :         gmpnumber = temp.num;                                         \
     302             : }
     303             : 
     304             : #define FETCH_GMP_ZVAL(gmpnumber, zval, temp)                     \
     305             : if (IS_GMP(zval)) {                                               \
     306             :         gmpnumber = GET_GMP_FROM_ZVAL(zval);                          \
     307             :         temp.is_used = 0;                                             \
     308             : } else {                                                          \
     309             :         mpz_init(temp.num);                                           \
     310             :         if (convert_to_gmp(temp.num, zval, 0 TSRMLS_CC) == FAILURE) { \
     311             :                 mpz_clear(temp.num);                                      \
     312             :                 RETURN_FALSE;                                             \
     313             :         }                                                             \
     314             :         temp.is_used = 1;                                             \
     315             :         gmpnumber = temp.num;                                         \
     316             : }
     317             : 
     318             : #define INIT_GMP_RETVAL(gmpnumber) \
     319             :         gmp_create(return_value, &gmpnumber TSRMLS_CC)
     320             : 
     321             : static void gmp_strval(zval *result, mpz_t gmpnum, zend_long base);
     322             : static int convert_to_gmp(mpz_t gmpnumber, zval *val, zend_long base TSRMLS_DC);
     323             : static void gmp_cmp(zval *return_value, zval *a_arg, zval *b_arg TSRMLS_DC);
     324             : 
     325             : /*
     326             :  * The gmp_*_op functions provide an implementation for several common types
     327             :  * of GMP functions. The gmp_zval_(unary|binary)_*_op functions have to be manually
     328             :  * passed zvals to work on, whereas the gmp_(unary|binary)_*_op macros already
     329             :  * include parameter parsing.
     330             :  */
     331             : typedef void (*gmp_unary_op_t)(mpz_ptr, mpz_srcptr);
     332             : typedef int (*gmp_unary_opl_t)(mpz_srcptr);
     333             : 
     334             : typedef void (*gmp_unary_ui_op_t)(mpz_ptr, gmp_ulong);
     335             : 
     336             : typedef void (*gmp_binary_op_t)(mpz_ptr, mpz_srcptr, mpz_srcptr);
     337             : typedef int (*gmp_binary_opl_t)(mpz_srcptr, mpz_srcptr);
     338             : 
     339             : typedef void (*gmp_binary_ui_op_t)(mpz_ptr, mpz_srcptr, gmp_ulong);
     340             : typedef void (*gmp_binary_op2_t)(mpz_ptr, mpz_ptr, mpz_srcptr, mpz_srcptr);
     341             : typedef void (*gmp_binary_ui_op2_t)(mpz_ptr, mpz_ptr, mpz_srcptr, gmp_ulong);
     342             : 
     343             : static inline void gmp_zval_binary_ui_op(zval *return_value, zval *a_arg, zval *b_arg, gmp_binary_op_t gmp_op, gmp_binary_ui_op_t gmp_ui_op, int check_b_zero TSRMLS_DC);
     344             : static inline void gmp_zval_binary_ui_op2(zval *return_value, zval *a_arg, zval *b_arg, gmp_binary_op2_t gmp_op, gmp_binary_ui_op2_t gmp_ui_op, int check_b_zero TSRMLS_DC);
     345             : static inline void gmp_zval_unary_op(zval *return_value, zval *a_arg, gmp_unary_op_t gmp_op TSRMLS_DC);
     346             : static inline void gmp_zval_unary_ui_op(zval *return_value, zval *a_arg, gmp_unary_ui_op_t gmp_op TSRMLS_DC);
     347             : 
     348             : /* Binary operations */
     349             : #define gmp_binary_ui_op(op, uop) _gmp_binary_ui_op(INTERNAL_FUNCTION_PARAM_PASSTHRU, op, uop, 0)
     350             : #define gmp_binary_op(op)         _gmp_binary_ui_op(INTERNAL_FUNCTION_PARAM_PASSTHRU, op, NULL, 0)
     351             : #define gmp_binary_opl(op) _gmp_binary_opl(INTERNAL_FUNCTION_PARAM_PASSTHRU, op)
     352             : #define gmp_binary_ui_op_no_zero(op, uop) \
     353             :         _gmp_binary_ui_op(INTERNAL_FUNCTION_PARAM_PASSTHRU, op, uop, 1)
     354             : 
     355             : /* Unary operations */
     356             : #define gmp_unary_op(op)         _gmp_unary_op(INTERNAL_FUNCTION_PARAM_PASSTHRU, op)
     357             : #define gmp_unary_opl(op)         _gmp_unary_opl(INTERNAL_FUNCTION_PARAM_PASSTHRU, op)
     358             : #define gmp_unary_ui_op(op)      _gmp_unary_ui_op(INTERNAL_FUNCTION_PARAM_PASSTHRU, op)
     359             : 
     360             : /* {{{ gmp_emalloc
     361             :  */
     362        5043 : static void *gmp_emalloc(size_t size)
     363             : {
     364        5043 :         return emalloc(size);
     365             : }
     366             : /* }}} */
     367             : 
     368             : /* {{{ gmp_erealloc
     369             :  */
     370        2274 : static void *gmp_erealloc(void *ptr, size_t old_size, size_t new_size)
     371             : {
     372        2274 :         return erealloc(ptr, new_size);
     373             : }
     374             : /* }}} */
     375             : 
     376             : /* {{{ gmp_efree
     377             :  */
     378        5043 : static void gmp_efree(void *ptr, size_t size)
     379             : {
     380        5043 :         efree(ptr);
     381        5043 : }
     382             : /* }}} */
     383             : 
     384        3478 : static void gmp_free_object_storage(zend_object *obj TSRMLS_DC) /* {{{ */
     385             : {
     386        3478 :         gmp_object *intern = GET_GMP_OBJECT_FROM_OBJ(obj);
     387             : 
     388        3478 :         mpz_clear(intern->num);
     389        3478 :         zend_object_std_dtor(&intern->std TSRMLS_CC);
     390        3478 : }
     391             : /* }}} */
     392             : 
     393        3478 : static inline zend_object *gmp_create_object_ex(zend_class_entry *ce, mpz_ptr *gmpnum_target TSRMLS_DC) /* {{{ */
     394             : {
     395        3478 :         gmp_object *intern = emalloc(sizeof(gmp_object)
     396             :                         + sizeof(zval) * (ce->default_properties_count - 1));
     397             : 
     398        3478 :         zend_object_std_init(&intern->std, ce TSRMLS_CC);
     399        3478 :         object_properties_init(&intern->std, ce);
     400             : 
     401        3478 :         mpz_init(intern->num);
     402        3478 :         *gmpnum_target = intern->num;
     403        3478 :         intern->std.handlers = &gmp_object_handlers;
     404             : 
     405        3478 :         return &intern->std;
     406             : }
     407             : /* }}} */
     408             : 
     409           1 : static zend_object *gmp_create_object(zend_class_entry *ce TSRMLS_DC) /* {{{ */
     410             : {
     411             :         mpz_ptr gmpnum_dummy;
     412           1 :         return gmp_create_object_ex(ce, &gmpnum_dummy TSRMLS_CC);
     413             : }
     414             : /* }}} */
     415             : 
     416        3477 : static inline void gmp_create(zval *target, mpz_ptr *gmpnum_target TSRMLS_DC) /* {{{ */
     417             : {
     418        3477 :         ZVAL_OBJ(target, gmp_create_object_ex(gmp_ce, gmpnum_target TSRMLS_CC));
     419        3477 : }
     420             : /* }}} */
     421             : 
     422          15 : static int gmp_cast_object(zval *readobj, zval *writeobj, int type TSRMLS_DC) /* {{{ */
     423             : {
     424             :         mpz_ptr gmpnum;
     425          15 :         switch (type) {
     426             :         case IS_STRING:
     427           7 :                 gmpnum = GET_GMP_FROM_ZVAL(readobj);
     428           7 :                 gmp_strval(writeobj, gmpnum, 10);
     429           7 :                 return SUCCESS;
     430             :         case IS_LONG:
     431           6 :                 gmpnum = GET_GMP_FROM_ZVAL(readobj);
     432           6 :                 ZVAL_LONG(writeobj, mpz_get_si(gmpnum));
     433           6 :                 return SUCCESS;
     434             :         case IS_DOUBLE:
     435           1 :                 gmpnum = GET_GMP_FROM_ZVAL(readobj);
     436           1 :                 ZVAL_DOUBLE(writeobj, mpz_get_d(gmpnum));
     437           1 :                 return SUCCESS;
     438             :         default:
     439           1 :                 return FAILURE;
     440             :         }
     441             : }
     442             : /* }}} */
     443             : 
     444         116 : static HashTable *gmp_get_debug_info(zval *obj, int *is_temp TSRMLS_DC) /* {{{ */
     445             : {
     446         116 :         HashTable *ht, *props = zend_std_get_properties(obj TSRMLS_CC);
     447         116 :         mpz_ptr gmpnum = GET_GMP_FROM_ZVAL(obj);
     448             :         zval zv;
     449             : 
     450         116 :         *is_temp = 1;
     451         116 :         ALLOC_HASHTABLE(ht);
     452         116 :         zend_array_dup(ht, props);
     453             : 
     454         116 :         gmp_strval(&zv, gmpnum, 10);
     455         116 :         zend_hash_str_update(ht, "num", sizeof("num")-1, &zv);
     456             : 
     457         116 :         return ht;
     458             : }
     459             : /* }}} */
     460             : 
     461           1 : static zend_object *gmp_clone_obj(zval *obj TSRMLS_DC) /* {{{ */
     462             : {
     463           1 :         gmp_object *old_object = GET_GMP_OBJECT_FROM_ZVAL(obj);
     464           1 :         gmp_object *new_object = GET_GMP_OBJECT_FROM_OBJ(gmp_create_object(Z_OBJCE_P(obj) TSRMLS_CC));
     465             : 
     466           1 :         zend_objects_clone_members( &new_object->std, &old_object->std TSRMLS_CC);
     467             : 
     468           1 :         mpz_set(new_object->num, old_object->num);
     469             : 
     470           1 :         return &new_object->std;
     471             : }
     472             : /* }}} */
     473             : 
     474        1010 : static void shift_operator_helper(gmp_binary_ui_op_t op, zval *return_value, zval *op1, zval *op2 TSRMLS_DC) {
     475        1010 :         zend_long shift = zval_get_long(op2);
     476             : 
     477        1010 :         if (shift < 0) {
     478           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Shift cannot be negative");
     479           2 :                 RETVAL_FALSE;
     480             :         } else {
     481             :                 mpz_ptr gmpnum_op, gmpnum_result;
     482             :                 gmp_temp_t temp;
     483             : 
     484        1008 :                 FETCH_GMP_ZVAL(gmpnum_op, op1, temp);
     485        1008 :                 INIT_GMP_RETVAL(gmpnum_result);
     486        1008 :                 op(gmpnum_result, gmpnum_op, (gmp_ulong) shift);
     487        1008 :                 FREE_GMP_TEMP(temp);
     488             :         }
     489             : }
     490             : 
     491             : #define DO_BINARY_UI_OP_EX(op, uop, check_b_zero)       \
     492             :         gmp_zval_binary_ui_op(                              \
     493             :                 result, op1, op2, op, (gmp_binary_ui_op_t) uop, \
     494             :                 check_b_zero TSRMLS_CC                          \
     495             :         );                                                  \
     496             :         return SUCCESS;
     497             : 
     498             : #define DO_BINARY_UI_OP(op) DO_BINARY_UI_OP_EX(op, op ## _ui, 0)
     499             : #define DO_BINARY_OP(op) DO_BINARY_UI_OP_EX(op, NULL, 0)
     500             : 
     501             : #define DO_UNARY_OP(op) \
     502             :         gmp_zval_unary_op(result, op1, op TSRMLS_CC); \
     503             :         return SUCCESS;
     504             : 
     505        1050 : static int gmp_do_operation_ex(zend_uchar opcode, zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
     506             : {
     507        1050 :         switch (opcode) {
     508             :         case ZEND_ADD:
     509           7 :                 DO_BINARY_UI_OP(mpz_add);
     510             :         case ZEND_SUB:
     511           8 :                 DO_BINARY_UI_OP(mpz_sub);
     512             :         case ZEND_MUL:
     513           3 :                 DO_BINARY_UI_OP(mpz_mul);
     514             :         case ZEND_POW:
     515           3 :                 shift_operator_helper(mpz_pow_ui, result, op1, op2 TSRMLS_CC);
     516           3 :                 return SUCCESS;
     517             :         case ZEND_DIV:
     518           4 :                 DO_BINARY_UI_OP_EX(mpz_tdiv_q, mpz_tdiv_q_ui, 1);
     519             :         case ZEND_MOD:
     520           4 :                 DO_BINARY_UI_OP_EX(mpz_mod, mpz_mod_ui, 1);
     521             :         case ZEND_SL:
     522        1004 :                 shift_operator_helper(mpz_mul_2exp, result, op1, op2 TSRMLS_CC);
     523        1004 :                 return SUCCESS;
     524             :         case ZEND_SR:
     525           3 :                 shift_operator_helper(mpz_fdiv_q_2exp, result, op1, op2 TSRMLS_CC);
     526           3 :                 return SUCCESS;
     527             :         case ZEND_BW_OR:
     528           3 :                 DO_BINARY_OP(mpz_ior);
     529             :         case ZEND_BW_AND:
     530           3 :                 DO_BINARY_OP(mpz_and);
     531             :         case ZEND_BW_XOR:
     532           3 :                 DO_BINARY_OP(mpz_xor);
     533             :         case ZEND_BW_NOT:
     534           1 :                 DO_UNARY_OP(mpz_com);
     535             : 
     536             :         default:
     537           4 :                 return FAILURE;
     538             :         }
     539             : }
     540             : /* }}} */
     541             : 
     542        1050 : static int gmp_do_operation(zend_uchar opcode, zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
     543             : {
     544             :         zval op1_copy;
     545             :         int retval;
     546             : 
     547        1050 :         if (result == op1) {
     548        1007 :                 ZVAL_COPY_VALUE(&op1_copy, op1);
     549        1007 :                 op1 = &op1_copy;
     550             :         }
     551             : 
     552        1050 :         retval = gmp_do_operation_ex(opcode, result, op1, op2 TSRMLS_CC);
     553             : 
     554        1050 :         if (retval == SUCCESS && op1 == &op1_copy) {
     555             :                 zval_dtor(op1);
     556             :         }
     557             : 
     558        1050 :         return retval;
     559             : }
     560             : /* }}} */
     561             : 
     562          23 : static int gmp_compare(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
     563             : {
     564          23 :         gmp_cmp(result, op1, op2 TSRMLS_CC);
     565          23 :         if (Z_TYPE_P(result) == IS_FALSE) {
     566           1 :                 ZVAL_LONG(result, 1);
     567             :         }
     568          23 :         return SUCCESS;
     569             : }
     570             : /* }}} */
     571             : 
     572           2 : static int gmp_serialize(zval *object, unsigned char **buffer, uint32_t *buf_len, zend_serialize_data *data TSRMLS_DC) /* {{{ */
     573             : {
     574           2 :         mpz_ptr gmpnum = GET_GMP_FROM_ZVAL(object);
     575           2 :         smart_str buf = {0};
     576             :         zval zv;
     577           2 :         php_serialize_data_t serialize_data = (php_serialize_data_t) data;
     578             :         zend_array tmp_arr;
     579             : 
     580           2 :         PHP_VAR_SERIALIZE_INIT(serialize_data);
     581             :     
     582           2 :         gmp_strval(&zv, gmpnum, 10);
     583           2 :         php_var_serialize(&buf, &zv, &serialize_data TSRMLS_CC);
     584             :         zval_dtor(&zv);
     585             : 
     586           2 :         ZVAL_ARR(&zv, &tmp_arr);
     587           2 :         tmp_arr.ht = *zend_std_get_properties(object TSRMLS_CC);
     588           2 :         php_var_serialize(&buf, &zv, &serialize_data TSRMLS_CC);
     589             : 
     590           2 :         PHP_VAR_SERIALIZE_DESTROY(serialize_data);
     591           2 :         *buffer = (unsigned char *) estrndup(buf.s->val, buf.s->len);
     592           2 :         *buf_len = buf.s->len;
     593           2 :         zend_string_release(buf.s);
     594             : 
     595           2 :         return SUCCESS;
     596             : }
     597             : /* }}} */
     598             : 
     599           4 : static int gmp_unserialize(zval *object, zend_class_entry *ce, const unsigned char *buf, uint32_t buf_len, zend_unserialize_data *data TSRMLS_DC) /* {{{ */
     600             : {
     601             :         mpz_ptr gmpnum;
     602             :         const unsigned char *p, *max;
     603             :         zval zv;
     604           4 :         int retval = FAILURE;
     605           4 :         php_unserialize_data_t unserialize_data = (php_unserialize_data_t) data;
     606             : 
     607           4 :         ZVAL_UNDEF(&zv);
     608           4 :         PHP_VAR_UNSERIALIZE_INIT(unserialize_data);
     609           4 :         gmp_create(object, &gmpnum TSRMLS_CC);
     610             : 
     611           4 :         p = buf;
     612           4 :         max = buf + buf_len;
     613             : 
     614          10 :         if (!php_var_unserialize(&zv, &p, max, &unserialize_data TSRMLS_CC)
     615             :                 || Z_TYPE(zv) != IS_STRING
     616           6 :                 || convert_to_gmp(gmpnum, &zv, 10 TSRMLS_CC) == FAILURE
     617             :         ) {
     618           1 :                 zend_throw_exception(NULL, "Could not unserialize number", 0 TSRMLS_CC);
     619           1 :                 goto exit;
     620             :         }
     621             :         zval_dtor(&zv);
     622           3 :         ZVAL_UNDEF(&zv);
     623             : 
     624           5 :         if (!php_var_unserialize(&zv, &p, max, &unserialize_data TSRMLS_CC)
     625           2 :                 || Z_TYPE(zv) != IS_ARRAY
     626             :         ) {
     627           1 :                 zend_throw_exception(NULL, "Could not unserialize properties", 0 TSRMLS_CC);
     628           1 :                 goto exit;
     629             :         }
     630             : 
     631           2 :         if (zend_hash_num_elements(Z_ARRVAL(zv)) != 0) {
     632           1 :                 zend_hash_copy(
     633           1 :                         zend_std_get_properties(object TSRMLS_CC), Z_ARRVAL(zv),
     634             :                         (copy_ctor_func_t) zval_add_ref
     635             :                 );
     636             :         }
     637             : 
     638           2 :         retval = SUCCESS;
     639             : exit:
     640             :         zval_dtor(&zv);
     641           4 :         PHP_VAR_UNSERIALIZE_DESTROY(unserialize_data);
     642           4 :         return retval;
     643             : }
     644             : /* }}} */
     645             : 
     646             : /* {{{ ZEND_GINIT_FUNCTION
     647             :  */
     648       20335 : static ZEND_GINIT_FUNCTION(gmp)
     649             : {
     650       20335 :         gmp_globals->rand_initialized = 0;
     651       20335 : }
     652             : /* }}} */
     653             : 
     654             : /* {{{ ZEND_MINIT_FUNCTION
     655             :  */
     656       20335 : ZEND_MINIT_FUNCTION(gmp)
     657             : {
     658             :         zend_class_entry tmp_ce;
     659       20335 :         INIT_CLASS_ENTRY(tmp_ce, "GMP", NULL);
     660       20335 :         gmp_ce = zend_register_internal_class(&tmp_ce TSRMLS_CC);
     661       20335 :         gmp_ce->create_object = gmp_create_object;
     662       20335 :         gmp_ce->serialize = gmp_serialize;
     663       20335 :         gmp_ce->unserialize = gmp_unserialize;
     664             : 
     665       20335 :         memcpy(&gmp_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
     666       20335 :         gmp_object_handlers.offset = XtOffsetOf(gmp_object, std);
     667       20335 :         gmp_object_handlers.free_obj = gmp_free_object_storage;
     668       20335 :         gmp_object_handlers.cast_object = gmp_cast_object;
     669       20335 :         gmp_object_handlers.get_debug_info = gmp_get_debug_info;
     670       20335 :         gmp_object_handlers.clone_obj = gmp_clone_obj;
     671       20335 :         gmp_object_handlers.do_operation = gmp_do_operation;
     672       20335 :         gmp_object_handlers.compare = gmp_compare;
     673             : 
     674       20335 :         REGISTER_LONG_CONSTANT("GMP_ROUND_ZERO", GMP_ROUND_ZERO, CONST_CS | CONST_PERSISTENT);
     675       20335 :         REGISTER_LONG_CONSTANT("GMP_ROUND_PLUSINF", GMP_ROUND_PLUSINF, CONST_CS | CONST_PERSISTENT);
     676       20335 :         REGISTER_LONG_CONSTANT("GMP_ROUND_MINUSINF", GMP_ROUND_MINUSINF, CONST_CS | CONST_PERSISTENT);
     677             : #ifdef mpir_version
     678             :         REGISTER_STRING_CONSTANT("GMP_MPIR_VERSION", (char *)mpir_version, CONST_CS | CONST_PERSISTENT);
     679             : #endif
     680       20335 :         REGISTER_STRING_CONSTANT("GMP_VERSION", (char *)gmp_version, CONST_CS | CONST_PERSISTENT);
     681             : 
     682       20335 :         REGISTER_LONG_CONSTANT("GMP_MSW_FIRST", GMP_MSW_FIRST, CONST_CS | CONST_PERSISTENT);
     683       20335 :         REGISTER_LONG_CONSTANT("GMP_LSW_FIRST", GMP_LSW_FIRST, CONST_CS | CONST_PERSISTENT);
     684       20335 :         REGISTER_LONG_CONSTANT("GMP_LITTLE_ENDIAN", GMP_LITTLE_ENDIAN, CONST_CS | CONST_PERSISTENT);
     685       20335 :         REGISTER_LONG_CONSTANT("GMP_BIG_ENDIAN", GMP_BIG_ENDIAN, CONST_CS | CONST_PERSISTENT);
     686       20335 :         REGISTER_LONG_CONSTANT("GMP_NATIVE_ENDIAN", GMP_NATIVE_ENDIAN, CONST_CS | CONST_PERSISTENT);
     687             : 
     688       20335 :         mp_set_memory_functions(gmp_emalloc, gmp_erealloc, gmp_efree);
     689             : 
     690       20335 :         return SUCCESS;
     691             : }
     692             : /* }}} */
     693             : 
     694             : /* {{{ ZEND_RSHUTDOWN_FUNCTION
     695             :  */
     696       20328 : ZEND_MODULE_DEACTIVATE_D(gmp)
     697             : {
     698       20328 :         if (GMPG(rand_initialized)) {
     699           1 :                 gmp_randclear(GMPG(rand_state));
     700           1 :                 GMPG(rand_initialized) = 0;
     701             :         }
     702             : 
     703       20328 :         return SUCCESS;
     704             : }
     705             : /* }}} */
     706             : 
     707             : /* {{{ ZEND_MINFO_FUNCTION
     708             :  */
     709         143 : ZEND_MODULE_INFO_D(gmp)
     710             : {
     711         143 :         php_info_print_table_start();
     712         143 :         php_info_print_table_row(2, "gmp support", "enabled");
     713             : #ifdef mpir_version
     714             :         php_info_print_table_row(2, "MPIR version", mpir_version);
     715             : #else
     716         143 :         php_info_print_table_row(2, "GMP version", gmp_version);
     717             : #endif
     718         143 :         php_info_print_table_end();
     719         143 : }
     720             : /* }}} */
     721             : 
     722             : 
     723             : /* {{{ convert_to_gmp
     724             :  * Convert zval to be gmp number */
     725        2686 : static int convert_to_gmp(mpz_t gmpnumber, zval *val, zend_long base TSRMLS_DC)
     726             : {
     727        2686 :         switch (Z_TYPE_P(val)) {
     728             :         case IS_LONG:
     729             :         case IS_FALSE:
     730             :         case IS_TRUE: {
     731        2410 :                 mpz_set_si(gmpnumber, zval_get_long(val));
     732        2410 :                 return SUCCESS;
     733             :         }
     734             :         case IS_STRING: {
     735         213 :                 char *numstr = Z_STRVAL_P(val);
     736         213 :                 zend_bool skip_lead = 0;
     737             :                 int ret;
     738             : 
     739         213 :                 if (Z_STRLEN_P(val) > 2 && numstr[0] == '0') {
     740          15 :                         if ((base == 0 || base == 16) && (numstr[1] == 'x' || numstr[1] == 'X')) {
     741           3 :                                 base = 16;
     742           3 :                                 skip_lead = 1;
     743           9 :                         } else if ((base == 0 || base == 2) && (numstr[1] == 'b' || numstr[1] == 'B')) {
     744           2 :                                 base = 2;
     745           2 :                                 skip_lead = 1;
     746             :                         }
     747             :                 }
     748             : 
     749         213 :                 ret = mpz_set_str(gmpnumber, (skip_lead ? &numstr[2] : numstr), (int) base);
     750         213 :                 if (-1 == ret) {
     751          19 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING,
     752             :                                 "Unable to convert variable to GMP - string is not an integer");
     753          19 :                         return FAILURE;
     754             :                 }
     755             : 
     756         194 :                 return SUCCESS;
     757             :         }
     758             :         default:
     759          63 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING,
     760             :                         "Unable to convert variable to GMP - wrong type");
     761          63 :                 return FAILURE;
     762             :         }
     763             : }
     764             : /* }}} */
     765             : 
     766         429 : static void gmp_strval(zval *result, mpz_t gmpnum, zend_long base) /* {{{ */
     767             : {
     768             :         size_t num_len;
     769             :         zend_string *str;
     770             : 
     771         429 :         num_len = mpz_sizeinbase(gmpnum, abs(base));
     772         429 :         if (mpz_sgn(gmpnum) < 0) {
     773          52 :                 num_len++;
     774             :         }
     775             : 
     776         429 :         str = zend_string_alloc(num_len, 0);
     777         429 :         mpz_get_str(str->val, base, gmpnum);
     778             :         
     779             :         /* 
     780             :          * From GMP documentation for mpz_sizeinbase():
     781             :          * The returned value will be exact or 1 too big.  If base is a power of
     782             :          * 2, the returned value will always be exact.
     783             :          *
     784             :          * So let's check to see if we already have a \0 byte...
     785             :          */
     786             : 
     787         429 :         if (str->val[str->len - 1] == '\0') {
     788          51 :                 str->len--;
     789             :         } else {
     790         378 :                 str->val[str->len] = '\0';
     791             :         }
     792             : 
     793         429 :         ZVAL_STR(result, str);
     794         429 : }
     795             : /* }}} */
     796             : 
     797          32 : static void gmp_cmp(zval *return_value, zval *a_arg, zval *b_arg TSRMLS_DC) /* {{{ */
     798             : {
     799             :         mpz_ptr gmpnum_a, gmpnum_b;
     800             :         gmp_temp_t temp_a, temp_b;
     801          32 :         zend_bool use_si = 0;
     802             :         zend_long res;
     803             : 
     804          32 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
     805             : 
     806          31 :         if (Z_TYPE_P(b_arg) == IS_LONG) {
     807          11 :                 use_si = 1;
     808          11 :                 temp_b.is_used = 0;
     809             :         } else {
     810          20 :                 FETCH_GMP_ZVAL_DEP(gmpnum_b, b_arg, temp_b, temp_a);
     811             :         }
     812             : 
     813          30 :         if (use_si) {
     814          11 :                 res = mpz_cmp_si(gmpnum_a, Z_LVAL_P(b_arg));
     815             :         } else {
     816          19 :                 res = mpz_cmp(gmpnum_a, gmpnum_b);
     817             :         }
     818             : 
     819          30 :         FREE_GMP_TEMP(temp_a);
     820          30 :         FREE_GMP_TEMP(temp_b);
     821             : 
     822          30 :         RETURN_LONG(res);
     823             : }
     824             : /* }}} */
     825             : 
     826             : /* {{{ gmp_zval_binary_ui_op
     827             :    Execute GMP binary operation.
     828             : */
     829        1156 : static inline void gmp_zval_binary_ui_op(zval *return_value, zval *a_arg, zval *b_arg, gmp_binary_op_t gmp_op, gmp_binary_ui_op_t gmp_ui_op, int check_b_zero TSRMLS_DC)
     830             : {
     831             :         mpz_ptr gmpnum_a, gmpnum_b, gmpnum_result;
     832        1156 :         int use_ui = 0;
     833             :         gmp_temp_t temp_a, temp_b;
     834             : 
     835        1156 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
     836             : 
     837        1199 :         if (gmp_ui_op && Z_TYPE_P(b_arg) == IS_LONG && Z_LVAL_P(b_arg) >= 0) {
     838          61 :                 use_ui = 1;
     839          61 :                 temp_b.is_used = 0;
     840             :         } else {
     841        1077 :                 FETCH_GMP_ZVAL_DEP(gmpnum_b, b_arg, temp_b, temp_a);
     842             :         }
     843             : 
     844        1134 :         if (check_b_zero) {
     845          56 :                 int b_is_zero = 0;
     846          56 :                 if (use_ui) {
     847          30 :                         b_is_zero = (Z_LVAL_P(b_arg) == 0);
     848             :                 } else {
     849          26 :                         b_is_zero = !mpz_cmp_ui(gmpnum_b, 0);
     850             :                 }
     851             : 
     852          56 :                 if (b_is_zero) {
     853           7 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Zero operand not allowed");
     854           7 :                         FREE_GMP_TEMP(temp_a);
     855           7 :                         FREE_GMP_TEMP(temp_b);
     856           7 :                         RETURN_FALSE;
     857             :                 }
     858             :         }
     859             : 
     860        1127 :         INIT_GMP_RETVAL(gmpnum_result);
     861             : 
     862        1127 :         if (use_ui) {
     863          55 :                 gmp_ui_op(gmpnum_result, gmpnum_a, (gmp_ulong) Z_LVAL_P(b_arg));
     864             :         } else {
     865        1072 :                 gmp_op(gmpnum_result, gmpnum_a, gmpnum_b);
     866             :         }
     867             : 
     868        1127 :         FREE_GMP_TEMP(temp_a);
     869        1127 :         FREE_GMP_TEMP(temp_b);
     870             : }
     871             : /* }}} */
     872             : 
     873             : /* {{{ gmp_zval_binary_ui_op2
     874             :    Execute GMP binary operation which returns 2 values.
     875             : */
     876          14 : static inline void gmp_zval_binary_ui_op2(zval *return_value, zval *a_arg, zval *b_arg, gmp_binary_op2_t gmp_op, gmp_binary_ui_op2_t gmp_ui_op, int check_b_zero TSRMLS_DC)
     877             : {
     878             :         mpz_ptr gmpnum_a, gmpnum_b, gmpnum_result1, gmpnum_result2;
     879          14 :         int use_ui = 0;
     880             :         gmp_temp_t temp_a, temp_b;
     881             :         zval result1, result2;
     882             : 
     883          14 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
     884             : 
     885          22 :         if (gmp_ui_op && Z_TYPE_P(b_arg) == IS_LONG && Z_LVAL_P(b_arg) >= 0) {
     886             :                 /* use _ui function */
     887          10 :                 use_ui = 1;
     888          10 :                 temp_b.is_used = 0;
     889             :         } else {
     890           2 :                 FETCH_GMP_ZVAL_DEP(gmpnum_b, b_arg, temp_b, temp_a);
     891             :         }
     892             : 
     893          12 :         if (check_b_zero) {
     894          12 :                 int b_is_zero = 0;
     895          12 :                 if (use_ui) {
     896          10 :                         b_is_zero = (Z_LVAL_P(b_arg) == 0);
     897             :                 } else {
     898           2 :                         b_is_zero = !mpz_cmp_ui(gmpnum_b, 0);
     899             :                 }
     900             : 
     901          12 :                 if (b_is_zero) {
     902           3 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Zero operand not allowed");
     903           3 :                         FREE_GMP_TEMP(temp_a);
     904           3 :                         FREE_GMP_TEMP(temp_b);
     905           3 :                         RETURN_FALSE;
     906             :                 }
     907             :         }
     908             : 
     909           9 :         gmp_create(&result1, &gmpnum_result1 TSRMLS_CC);
     910           9 :         gmp_create(&result2, &gmpnum_result2 TSRMLS_CC);
     911             : 
     912           9 :         array_init(return_value);
     913           9 :         add_next_index_zval(return_value, &result1);
     914           9 :         add_next_index_zval(return_value, &result2);
     915             : 
     916           9 :         if (use_ui) {
     917           8 :                 gmp_ui_op(gmpnum_result1, gmpnum_result2, gmpnum_a, (gmp_ulong) Z_LVAL_P(b_arg));
     918             :         } else {
     919           1 :                 gmp_op(gmpnum_result1, gmpnum_result2, gmpnum_a, gmpnum_b);
     920             :         }
     921             : 
     922           9 :         FREE_GMP_TEMP(temp_a);
     923           9 :         FREE_GMP_TEMP(temp_b);
     924             : }
     925             : /* }}} */
     926             : 
     927             : /* {{{ _gmp_binary_ui_op
     928             :  */
     929        1114 : 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)
     930             : {
     931             :         zval *a_arg, *b_arg;
     932             : 
     933        1114 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &a_arg, &b_arg) == FAILURE){
     934          16 :                 return;
     935             :         }
     936             : 
     937        1098 :         gmp_zval_binary_ui_op(return_value, a_arg, b_arg, gmp_op, gmp_ui_op, check_b_zero TSRMLS_CC);
     938             : }
     939             : /* }}} */
     940             : 
     941             : /* Unary operations */
     942             : 
     943             : /* {{{ gmp_zval_unary_op
     944             :  */
     945          38 : static inline void gmp_zval_unary_op(zval *return_value, zval *a_arg, gmp_unary_op_t gmp_op TSRMLS_DC)
     946             : {
     947             :         mpz_ptr gmpnum_a, gmpnum_result;
     948             :         gmp_temp_t temp_a;
     949             : 
     950          38 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
     951             : 
     952          26 :         INIT_GMP_RETVAL(gmpnum_result);
     953          26 :         gmp_op(gmpnum_result, gmpnum_a);
     954             : 
     955          26 :         FREE_GMP_TEMP(temp_a);
     956             : }
     957             : /* }}} */
     958             : 
     959             : /* {{{ gmp_zval_unary_ui_op
     960             :  */
     961          11 : static inline void gmp_zval_unary_ui_op(zval *return_value, zval *a_arg, gmp_unary_ui_op_t gmp_op TSRMLS_DC)
     962             : {
     963             :         mpz_ptr gmpnum_result;
     964             : 
     965          11 :         INIT_GMP_RETVAL(gmpnum_result);
     966          11 :         gmp_op(gmpnum_result, zval_get_long(a_arg));
     967          11 : }
     968             : /* }}} */
     969             : 
     970             : /* {{{ _gmp_unary_ui_op
     971             :    Execute GMP unary operation.
     972             : */
     973             : static inline void _gmp_unary_ui_op(INTERNAL_FUNCTION_PARAMETERS, gmp_unary_ui_op_t gmp_op)
     974             : {
     975             :         zval *a_arg;
     976             : 
     977             :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &a_arg) == FAILURE){
     978             :                 return;
     979             :         }
     980             : 
     981             :         gmp_zval_unary_ui_op(return_value, a_arg, gmp_op TSRMLS_CC);
     982             : }
     983             : /* }}} */
     984             : 
     985             : /* {{{ _gmp_unary_op
     986             :  */
     987          42 : static inline void _gmp_unary_op(INTERNAL_FUNCTION_PARAMETERS, gmp_unary_op_t gmp_op)
     988             : {
     989             :         zval *a_arg;
     990             : 
     991          42 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &a_arg) == FAILURE){
     992           5 :                 return;
     993             :         }
     994             : 
     995          37 :         gmp_zval_unary_op(return_value, a_arg, gmp_op TSRMLS_CC);
     996             : }
     997             : /* }}} */
     998             : 
     999             : /* {{{ _gmp_unary_opl
    1000             :  */
    1001           8 : static inline void _gmp_unary_opl(INTERNAL_FUNCTION_PARAMETERS, gmp_unary_opl_t gmp_op)
    1002             : {
    1003             :         zval *a_arg;
    1004             :         mpz_ptr gmpnum_a;
    1005             :         gmp_temp_t temp_a;
    1006             : 
    1007           8 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &a_arg) == FAILURE){
    1008           1 :                 return;
    1009             :         }
    1010             : 
    1011           7 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
    1012           6 :         RETVAL_LONG(gmp_op(gmpnum_a));
    1013           6 :         FREE_GMP_TEMP(temp_a);
    1014             : }
    1015             : /* }}} */
    1016             : 
    1017             : /* {{{ _gmp_binary_opl
    1018             :  */
    1019          58 : static inline void _gmp_binary_opl(INTERNAL_FUNCTION_PARAMETERS, gmp_binary_opl_t gmp_op)
    1020             : {
    1021             :         zval *a_arg, *b_arg;
    1022             :         mpz_ptr gmpnum_a, gmpnum_b;
    1023             :         gmp_temp_t temp_a, temp_b;
    1024             : 
    1025          58 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &a_arg, &b_arg) == FAILURE){
    1026           8 :                 return;
    1027             :         }
    1028             : 
    1029          50 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
    1030          44 :         FETCH_GMP_ZVAL_DEP(gmpnum_b, b_arg, temp_b, temp_a);
    1031             : 
    1032          41 :         RETVAL_LONG(gmp_op(gmpnum_a, gmpnum_b));
    1033             : 
    1034          41 :         FREE_GMP_TEMP(temp_a);
    1035          41 :         FREE_GMP_TEMP(temp_b);
    1036             : }
    1037             : /* }}} */
    1038             : 
    1039             : /* {{{ proto GMP gmp_init(mixed number [, int base])
    1040             :    Initializes GMP number */
    1041        1161 : ZEND_FUNCTION(gmp_init)
    1042             : {
    1043             :         zval *number_arg;
    1044             :         mpz_ptr gmpnumber;
    1045        1161 :         zend_long base = 0;
    1046             : 
    1047        1161 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &number_arg, &base) == FAILURE) {
    1048           3 :                 return;
    1049             :         }
    1050             : 
    1051        1158 :         if (base && (base < 2 || base > MAX_BASE)) {
    1052           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad base for conversion: %pd (should be between 2 and %d)", base, MAX_BASE);
    1053           1 :                 RETURN_FALSE;
    1054             :         }
    1055             : 
    1056        1157 :         INIT_GMP_RETVAL(gmpnumber);
    1057        1157 :         if (convert_to_gmp(gmpnumber, number_arg, base TSRMLS_CC) == FAILURE) {
    1058             :                 zval_dtor(return_value);
    1059           5 :                 RETURN_FALSE;
    1060             :         }
    1061             : }
    1062             : /* }}} */
    1063             : 
    1064          51 : int gmp_import_export_validate(zend_long size, zend_long options, int *order, int *endian TSRMLS_DC)
    1065             : {
    1066          51 :         if (size < 1) {
    1067           4 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING,
    1068             :                         "Word size must be positive, %pd given", size);
    1069           4 :                 return FAILURE;
    1070             :         }
    1071             : 
    1072          47 :         switch (options & (GMP_LSW_FIRST | GMP_MSW_FIRST)) {
    1073             :                 case GMP_LSW_FIRST:
    1074          18 :                         *order = -1;
    1075          18 :                         break;
    1076             :                 case GMP_MSW_FIRST:
    1077             :                 case 0: /* default */
    1078          27 :                         *order = 1;
    1079          27 :                         break;
    1080             :                 default:
    1081           2 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING,
    1082             :                                 "Invalid options: Conflicting word orders");
    1083           2 :                         return FAILURE;
    1084             :         }
    1085             : 
    1086          45 :         switch (options & (GMP_LITTLE_ENDIAN | GMP_BIG_ENDIAN | GMP_NATIVE_ENDIAN)) {
    1087             :                 case GMP_LITTLE_ENDIAN:
    1088          18 :                         *endian = -1;
    1089          18 :                         break;
    1090             :                 case GMP_BIG_ENDIAN:
    1091          22 :                         *endian = 1;
    1092          22 :                         break;
    1093             :                 case GMP_NATIVE_ENDIAN:
    1094             :                 case 0: /* default */
    1095           3 :                         *endian = 0;
    1096           3 :                         break;
    1097             :                 default:
    1098           2 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING,
    1099             :                                 "Invalid options: Conflicting word endianness");
    1100           2 :                         return FAILURE;
    1101             :         }
    1102             : 
    1103          43 :         return SUCCESS;
    1104             : }
    1105             : 
    1106             : /* {{{ proto GMP gmp_import(string data [, int word_size = 1, int options = GMP_MSW_FIRST | GMP_NATIVE_ENDIAN])
    1107             :    Imports a GMP number from a binary string */
    1108          27 : ZEND_FUNCTION(gmp_import)
    1109             : {
    1110             :         char *data;
    1111             :         size_t data_len;
    1112          27 :         zend_long size = 1;
    1113          27 :         zend_long options = GMP_MSW_FIRST | GMP_NATIVE_ENDIAN;
    1114             :         int order, endian;
    1115             :         mpz_ptr gmpnumber;
    1116             : 
    1117          27 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ll", &data, &data_len, &size, &options) == FAILURE) {
    1118           1 :                 return;
    1119             :         }
    1120             : 
    1121          26 :         if (gmp_import_export_validate(size, options, &order, &endian TSRMLS_CC) == FAILURE) {
    1122           4 :                 RETURN_FALSE;
    1123             :         }
    1124             : 
    1125          22 :         if ((data_len % size) != 0) {
    1126           3 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING,
    1127             :                         "Input length must be a multiple of word size");
    1128           3 :                 RETURN_FALSE;
    1129             :         }
    1130             : 
    1131          19 :         INIT_GMP_RETVAL(gmpnumber);
    1132             : 
    1133          19 :         mpz_import(gmpnumber, data_len / size, order, size, endian, 0, data);
    1134             : }
    1135             : /* }}} */
    1136             : 
    1137             : /* {{{ proto string gmp_export(GMP gmpnumber [, int word_size = 1, int options = GMP_MSW_FIRST | GMP_NATIVE_ENDIAN])
    1138             :    Exports a GMP number to a binary string */
    1139          26 : ZEND_FUNCTION(gmp_export)
    1140             : {
    1141             :         zval *gmpnumber_arg;
    1142          26 :         zend_long size = 1;
    1143          26 :         zend_long options = GMP_MSW_FIRST | GMP_NATIVE_ENDIAN;
    1144             :         int order, endian;
    1145             :         mpz_ptr gmpnumber;
    1146             :         gmp_temp_t temp_a;
    1147             : 
    1148          26 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ll", &gmpnumber_arg, &size, &options) == FAILURE) {
    1149           1 :                 return;
    1150             :         }
    1151             : 
    1152          25 :         if (gmp_import_export_validate(size, options, &order, &endian TSRMLS_CC) == FAILURE) {
    1153           4 :                 RETURN_FALSE;
    1154             :         }
    1155             : 
    1156          21 :         FETCH_GMP_ZVAL(gmpnumber, gmpnumber_arg, temp_a);
    1157             : 
    1158          21 :         if (mpz_sgn(gmpnumber) == 0) {
    1159           3 :                 RETURN_EMPTY_STRING();
    1160             :         } else {
    1161          18 :                 size_t bits_per_word = size * 8;
    1162          18 :                 size_t count = (mpz_sizeinbase(gmpnumber, 2) + bits_per_word - 1) / bits_per_word;
    1163          18 :                 size_t out_len = count * size;
    1164             : 
    1165          18 :                 zend_string *out_string = zend_string_alloc(out_len, 0);
    1166          18 :                 mpz_export(out_string->val, NULL, order, size, endian, 0, gmpnumber);
    1167          18 :                 out_string->val[out_len] = '\0';
    1168             : 
    1169          18 :                 RETURN_STR(out_string);
    1170             :         }
    1171             : 
    1172             :         FREE_GMP_TEMP(temp_a);
    1173             : }
    1174             : /* }}} */
    1175             : 
    1176             : /* {{{ proto int gmp_intval(mixed gmpnumber)
    1177             :    Gets signed long value of GMP number */
    1178          19 : ZEND_FUNCTION(gmp_intval)
    1179             : {
    1180             :         zval *gmpnumber_arg;
    1181             : 
    1182          19 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &gmpnumber_arg) == FAILURE){
    1183           1 :                 return;
    1184             :         }
    1185             : 
    1186          25 :         if (IS_GMP(gmpnumber_arg)) {
    1187           7 :                 RETVAL_LONG(mpz_get_si(GET_GMP_FROM_ZVAL(gmpnumber_arg)));
    1188             :         } else {
    1189          22 :                 RETVAL_LONG(zval_get_long(gmpnumber_arg));
    1190             :         }
    1191             : }
    1192             : /* }}} */
    1193             : 
    1194             : /* {{{ proto string gmp_strval(mixed gmpnumber [, int base])
    1195             :    Gets string representation of GMP number  */
    1196         328 : ZEND_FUNCTION(gmp_strval)
    1197             : {
    1198             :         zval *gmpnumber_arg;
    1199         328 :         zend_long base = 10;
    1200             :         mpz_ptr gmpnum;
    1201             :         gmp_temp_t temp_a;
    1202             : 
    1203         328 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &gmpnumber_arg, &base) == FAILURE) {
    1204           5 :                 return;
    1205             :         }
    1206             : 
    1207             : #if MAX_BASE == 62
    1208             :         /* Although the maximum base in general in GMP >= 4.2 is 62, mpz_get_str()
    1209             :          * is explicitly limited to -36 when dealing with negative bases. */
    1210         323 :         if ((base < 2 && base > -2) || base > MAX_BASE || base < -36) {
    1211           9 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad base for conversion: %pd (should be between 2 and %d or -2 and -36)", base, MAX_BASE);
    1212             : #else
    1213             :         if (base < 2 || base > MAX_BASE) {
    1214             :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad base for conversion: %pd (should be between 2 and %d)", base, MAX_BASE);
    1215             : #endif
    1216           9 :                 RETURN_FALSE;
    1217             :         }
    1218             : 
    1219         314 :         FETCH_GMP_ZVAL(gmpnum, gmpnumber_arg, temp_a);
    1220             : 
    1221         304 :         gmp_strval(return_value, gmpnum, base);
    1222             : 
    1223         304 :         FREE_GMP_TEMP(temp_a);
    1224             : }
    1225             : /* }}} */
    1226             : 
    1227             : /* {{{ proto GMP gmp_add(mixed a, mixed b)
    1228             :    Add a and b */
    1229          16 : ZEND_FUNCTION(gmp_add)
    1230             : {
    1231          16 :         gmp_binary_ui_op(mpz_add, mpz_add_ui);
    1232          16 : }
    1233             : /* }}} */
    1234             : 
    1235             : /* {{{ proto GMP gmp_sub(mixed a, mixed b)
    1236             :    Subtract b from a */
    1237           9 : ZEND_FUNCTION(gmp_sub)
    1238             : {
    1239           9 :         gmp_binary_ui_op(mpz_sub, mpz_sub_ui);
    1240           9 : }
    1241             : /* }}} */
    1242             : 
    1243             : /* {{{ proto GMP gmp_mul(mixed a, mixed b)
    1244             :    Multiply a and b */
    1245        1001 : ZEND_FUNCTION(gmp_mul)
    1246             : {
    1247        1001 :         gmp_binary_ui_op(mpz_mul, mpz_mul_ui);
    1248        1001 : }
    1249             : /* }}} */
    1250             : 
    1251             : /* {{{ proto array gmp_div_qr(mixed a, mixed b [, int round])
    1252             :    Divide a by b, returns quotient and reminder */
    1253          17 : ZEND_FUNCTION(gmp_div_qr)
    1254             : {
    1255             :         zval *a_arg, *b_arg;
    1256          17 :         zend_long round = GMP_ROUND_ZERO;
    1257             : 
    1258          17 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|l", &a_arg, &b_arg, &round) == FAILURE) {
    1259           2 :                 return;
    1260             :         }
    1261             : 
    1262          15 :         switch (round) {
    1263             :         case GMP_ROUND_ZERO:
    1264          10 :                 gmp_zval_binary_ui_op2(return_value, a_arg, b_arg, mpz_tdiv_qr, (gmp_binary_ui_op2_t) mpz_tdiv_qr_ui, 1 TSRMLS_CC);
    1265          10 :                 break;
    1266             :         case GMP_ROUND_PLUSINF:
    1267           2 :                 gmp_zval_binary_ui_op2(return_value, a_arg, b_arg, mpz_cdiv_qr, (gmp_binary_ui_op2_t) mpz_cdiv_qr_ui, 1 TSRMLS_CC);
    1268           2 :                 break;
    1269             :         case GMP_ROUND_MINUSINF:
    1270           2 :                 gmp_zval_binary_ui_op2(return_value, a_arg, b_arg, mpz_fdiv_qr, (gmp_binary_ui_op2_t) mpz_fdiv_qr_ui, 1 TSRMLS_CC);
    1271           2 :                 break;
    1272             :         default:
    1273           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid rounding mode");
    1274           1 :                 RETURN_FALSE;
    1275             :         }
    1276             : }
    1277             : /* }}} */
    1278             : 
    1279             : /* {{{ proto GMP gmp_div_r(mixed a, mixed b [, int round])
    1280             :    Divide a by b, returns reminder only */
    1281          14 : ZEND_FUNCTION(gmp_div_r)
    1282             : {
    1283             :         zval *a_arg, *b_arg;
    1284          14 :         zend_long round = GMP_ROUND_ZERO;
    1285             : 
    1286          14 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|l", &a_arg, &b_arg, &round) == FAILURE) {
    1287           2 :                 return;
    1288             :         }
    1289             : 
    1290          12 :         switch (round) {
    1291             :         case GMP_ROUND_ZERO:
    1292           7 :                 gmp_zval_binary_ui_op(return_value, a_arg, b_arg, mpz_tdiv_r, (gmp_binary_ui_op_t) mpz_tdiv_r_ui, 1 TSRMLS_CC);
    1293           7 :                 break;
    1294             :         case GMP_ROUND_PLUSINF:
    1295           2 :                 gmp_zval_binary_ui_op(return_value, a_arg, b_arg, mpz_cdiv_r, (gmp_binary_ui_op_t) mpz_cdiv_r_ui, 1 TSRMLS_CC);
    1296           2 :                 break;
    1297             :         case GMP_ROUND_MINUSINF:
    1298           2 :                 gmp_zval_binary_ui_op(return_value, a_arg, b_arg, mpz_fdiv_r, (gmp_binary_ui_op_t) mpz_fdiv_r_ui, 1 TSRMLS_CC);
    1299           2 :                 break;
    1300             :         default:
    1301           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid rounding mode");
    1302           1 :                 RETURN_FALSE;
    1303             :         }
    1304             : }
    1305             : /* }}} */
    1306             : 
    1307             : /* {{{ proto GMP gmp_div_q(mixed a, mixed b [, int round])
    1308             :    Divide a by b, returns quotient only */
    1309          15 : ZEND_FUNCTION(gmp_div_q)
    1310             : {
    1311             :         zval *a_arg, *b_arg;
    1312          15 :         zend_long round = GMP_ROUND_ZERO;
    1313             : 
    1314          15 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|l", &a_arg, &b_arg, &round) == FAILURE) {
    1315           2 :                 return;
    1316             :         }
    1317             : 
    1318          13 :         switch (round) {
    1319             :         case GMP_ROUND_ZERO:
    1320           8 :                 gmp_zval_binary_ui_op(return_value, a_arg, b_arg, mpz_tdiv_q, (gmp_binary_ui_op_t) mpz_tdiv_q_ui, 1 TSRMLS_CC);
    1321           8 :                 break;
    1322             :         case GMP_ROUND_PLUSINF:
    1323           2 :                 gmp_zval_binary_ui_op(return_value, a_arg, b_arg, mpz_cdiv_q, (gmp_binary_ui_op_t) mpz_cdiv_q_ui, 1 TSRMLS_CC);
    1324           2 :                 break;
    1325             :         case GMP_ROUND_MINUSINF:
    1326           2 :                 gmp_zval_binary_ui_op(return_value, a_arg, b_arg, mpz_fdiv_q, (gmp_binary_ui_op_t) mpz_fdiv_q_ui, 1 TSRMLS_CC);
    1327           2 :                 break;
    1328             :         default:
    1329           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid rounding mode");
    1330           1 :                 RETURN_FALSE;
    1331             :         }
    1332             : 
    1333             : }
    1334             : /* }}} */
    1335             : 
    1336             : /* {{{ proto GMP gmp_mod(mixed a, mixed b)
    1337             :    Computes a modulo b */
    1338          26 : ZEND_FUNCTION(gmp_mod)
    1339             : {
    1340          26 :         gmp_binary_ui_op_no_zero(mpz_mod, (gmp_binary_ui_op_t) mpz_mod_ui);
    1341          26 : }
    1342             : /* }}} */
    1343             : 
    1344             : /* {{{ proto GMP gmp_divexact(mixed a, mixed b)
    1345             :    Divide a by b using exact division algorithm */
    1346           9 : ZEND_FUNCTION(gmp_divexact)
    1347             : {
    1348           9 :         gmp_binary_ui_op_no_zero(mpz_divexact, NULL);
    1349           9 : }
    1350             : /* }}} */
    1351             : 
    1352             : /* {{{ proto GMP gmp_neg(mixed a)
    1353             :    Negates a number */
    1354          11 : ZEND_FUNCTION(gmp_neg)
    1355             : {
    1356          11 :         gmp_unary_op(mpz_neg);
    1357          11 : }
    1358             : /* }}} */
    1359             : 
    1360             : /* {{{ proto GMP gmp_abs(mixed a)
    1361             :    Calculates absolute value */
    1362          12 : ZEND_FUNCTION(gmp_abs)
    1363             : {
    1364          12 :         gmp_unary_op(mpz_abs);
    1365          12 : }
    1366             : /* }}} */
    1367             : 
    1368             : /* {{{ proto GMP gmp_fact(int a)
    1369             :    Calculates factorial function */
    1370          16 : ZEND_FUNCTION(gmp_fact)
    1371             : {
    1372             :         zval *a_arg;
    1373             : 
    1374          16 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &a_arg) == FAILURE){
    1375           2 :                 return;
    1376             :         }
    1377             : 
    1378          15 :         if (IS_GMP(a_arg)) {
    1379           2 :                 mpz_ptr gmpnum_tmp = GET_GMP_FROM_ZVAL(a_arg);
    1380           2 :                 if (mpz_sgn(gmpnum_tmp) < 0) {
    1381           1 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number has to be greater than or equal to 0");
    1382           1 :                         RETURN_FALSE;
    1383             :                 }
    1384             :         } else {
    1385          24 :                 if (zval_get_long(a_arg) < 0) {
    1386           2 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number has to be greater than or equal to 0");
    1387           2 :                         RETURN_FALSE;
    1388             :                 }
    1389             :         }
    1390             : 
    1391          11 :         gmp_zval_unary_ui_op(return_value, a_arg, mpz_fac_ui TSRMLS_CC);
    1392             : }
    1393             : /* }}} */
    1394             : 
    1395             : /* {{{ proto GMP gmp_pow(mixed base, int exp)
    1396             :    Raise base to power exp */
    1397          18 : ZEND_FUNCTION(gmp_pow)
    1398             : {
    1399             :         zval *base_arg;
    1400             :         mpz_ptr gmpnum_result, gmpnum_base;
    1401             :         gmp_temp_t temp_base;
    1402             :         zend_long exp;
    1403             : 
    1404          18 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zl", &base_arg, &exp) == FAILURE) {
    1405           5 :                 return;
    1406             :         }
    1407             : 
    1408          13 :         if (exp < 0) {
    1409           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Negative exponent not supported");
    1410           2 :                 RETURN_FALSE;
    1411             :         }
    1412             : 
    1413          11 :         INIT_GMP_RETVAL(gmpnum_result);
    1414          14 :         if (Z_TYPE_P(base_arg) == IS_LONG && Z_LVAL_P(base_arg) >= 0) {
    1415           3 :                 mpz_ui_pow_ui(gmpnum_result, Z_LVAL_P(base_arg), exp);
    1416             :         } else {
    1417           8 :                 FETCH_GMP_ZVAL(gmpnum_base, base_arg, temp_base);
    1418           7 :                 mpz_pow_ui(gmpnum_result, gmpnum_base, exp);
    1419           7 :                 FREE_GMP_TEMP(temp_base);
    1420             :         }
    1421             : }
    1422             : /* }}} */
    1423             : 
    1424             : /* {{{ proto GMP gmp_powm(mixed base, mixed exp, mixed mod)
    1425             :    Raise base to power exp and take result modulo mod */
    1426          20 : ZEND_FUNCTION(gmp_powm)
    1427             : {
    1428             :         zval *base_arg, *exp_arg, *mod_arg;
    1429             :         mpz_ptr gmpnum_base, gmpnum_exp, gmpnum_mod, gmpnum_result;
    1430          20 :         int use_ui = 0;
    1431             :         gmp_temp_t temp_base, temp_exp, temp_mod;
    1432             : 
    1433          20 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zzz", &base_arg, &exp_arg, &mod_arg) == FAILURE){
    1434           3 :                 return;
    1435             :         }
    1436             : 
    1437          17 :         FETCH_GMP_ZVAL(gmpnum_base, base_arg, temp_base);
    1438             : 
    1439          23 :         if (Z_TYPE_P(exp_arg) == IS_LONG && Z_LVAL_P(exp_arg) >= 0) {
    1440           8 :                 use_ui = 1;
    1441           8 :                 temp_exp.is_used = 0;
    1442             :         } else {
    1443           7 :                 FETCH_GMP_ZVAL_DEP(gmpnum_exp, exp_arg, temp_exp, temp_base);
    1444           6 :                 if (mpz_sgn(gmpnum_exp) < 0) {
    1445           1 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Second parameter cannot be less than 0");
    1446           1 :                         FREE_GMP_TEMP(temp_base);
    1447           1 :                         FREE_GMP_TEMP(temp_exp);
    1448           1 :                         RETURN_FALSE;
    1449             :                 }
    1450             :         }
    1451          13 :         FETCH_GMP_ZVAL_DEP_DEP(gmpnum_mod, mod_arg, temp_mod, temp_exp, temp_base);
    1452             : 
    1453          12 :         if (!mpz_cmp_ui(gmpnum_mod, 0)) {
    1454           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Modulus may not be zero");
    1455           2 :                 FREE_GMP_TEMP(temp_base);
    1456           2 :                 FREE_GMP_TEMP(temp_exp);
    1457           2 :                 FREE_GMP_TEMP(temp_mod);
    1458           2 :                 RETURN_FALSE;
    1459             :         }
    1460             : 
    1461          10 :         INIT_GMP_RETVAL(gmpnum_result);
    1462          10 :         if (use_ui) {
    1463           7 :                 mpz_powm_ui(gmpnum_result, gmpnum_base, (zend_ulong) Z_LVAL_P(exp_arg), gmpnum_mod);
    1464             :         } else {
    1465           3 :                 mpz_powm(gmpnum_result, gmpnum_base, gmpnum_exp, gmpnum_mod);
    1466           3 :                 FREE_GMP_TEMP(temp_exp);
    1467             :         }
    1468             : 
    1469          10 :         FREE_GMP_TEMP(temp_base);
    1470          10 :         FREE_GMP_TEMP(temp_mod);
    1471             : }
    1472             : /* }}} */
    1473             : 
    1474             : /* {{{ proto GMP gmp_sqrt(mixed a)
    1475             :    Takes integer part of square root of a */
    1476          11 : ZEND_FUNCTION(gmp_sqrt)
    1477             : {
    1478             :         zval *a_arg;
    1479             :         mpz_ptr gmpnum_a, gmpnum_result;
    1480             :         gmp_temp_t temp_a;
    1481             : 
    1482          11 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &a_arg) == FAILURE){
    1483           2 :                 return;
    1484             :         }
    1485             : 
    1486           9 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
    1487             : 
    1488           8 :         if (mpz_sgn(gmpnum_a) < 0) {
    1489           3 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number has to be greater than or equal to 0");
    1490           3 :                 FREE_GMP_TEMP(temp_a);
    1491           3 :                 RETURN_FALSE;
    1492             :         }
    1493             : 
    1494           5 :         INIT_GMP_RETVAL(gmpnum_result);
    1495           5 :         mpz_sqrt(gmpnum_result, gmpnum_a);
    1496           5 :         FREE_GMP_TEMP(temp_a);
    1497             : }
    1498             : /* }}} */
    1499             : 
    1500             : /* {{{ proto array gmp_sqrtrem(mixed a)
    1501             :    Square root with remainder */
    1502          13 : ZEND_FUNCTION(gmp_sqrtrem)
    1503             : {
    1504             :         zval *a_arg;
    1505             :         mpz_ptr gmpnum_a, gmpnum_result1, gmpnum_result2;
    1506             :         gmp_temp_t temp_a;
    1507             :         zval result1, result2;
    1508             : 
    1509          13 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &a_arg) == FAILURE){
    1510           1 :                 return;
    1511             :         }
    1512             : 
    1513          12 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
    1514             : 
    1515          11 :         if (mpz_sgn(gmpnum_a) < 0) {
    1516           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number has to be greater than or equal to 0");
    1517           2 :                 FREE_GMP_TEMP(temp_a);
    1518           2 :                 RETURN_FALSE;
    1519             :         }
    1520             :         
    1521           9 :         gmp_create(&result1, &gmpnum_result1 TSRMLS_CC);
    1522           9 :         gmp_create(&result2, &gmpnum_result2 TSRMLS_CC);
    1523             : 
    1524           9 :         array_init(return_value);
    1525           9 :         add_next_index_zval(return_value, &result1);
    1526           9 :         add_next_index_zval(return_value, &result2);
    1527             : 
    1528           9 :         mpz_sqrtrem(gmpnum_result1, gmpnum_result2, gmpnum_a);
    1529           9 :         FREE_GMP_TEMP(temp_a);
    1530             : }
    1531             : /* }}} */
    1532             : 
    1533             : /* {{{ proto GMP gmp_root(mixed a, int nth)
    1534             :    Takes integer part of nth root */
    1535          10 : ZEND_FUNCTION(gmp_root)
    1536             : {
    1537             :         zval *a_arg;
    1538             :         zend_long nth;
    1539             :         mpz_ptr gmpnum_a, gmpnum_result;
    1540             :         gmp_temp_t temp_a;
    1541             : 
    1542          10 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zl", &a_arg, &nth) == FAILURE) {
    1543           1 :                 return;
    1544             :         }
    1545             : 
    1546           9 :         if (nth <= 0) {
    1547           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "The root must be positive");
    1548           2 :                 RETURN_FALSE;
    1549             :         }
    1550             : 
    1551           7 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
    1552             : 
    1553           7 :         if (nth % 2 == 0 && mpz_sgn(gmpnum_a) < 0) {
    1554           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can't take even root of negative number");
    1555           1 :                 FREE_GMP_TEMP(temp_a);
    1556           1 :                 RETURN_FALSE;
    1557             :         }
    1558             : 
    1559           6 :         INIT_GMP_RETVAL(gmpnum_result);
    1560           6 :         mpz_root(gmpnum_result, gmpnum_a, (gmp_ulong) nth);
    1561           6 :         FREE_GMP_TEMP(temp_a);
    1562             : }
    1563             : /* }}} */
    1564             : 
    1565             : /* {{{ proto GMP gmp_rootrem(mixed a, int nth)
    1566             :    Calculates integer part of nth root and remainder */
    1567          10 : ZEND_FUNCTION(gmp_rootrem)
    1568             : {
    1569             :         zval *a_arg;
    1570             :         zend_long nth;
    1571             :         mpz_ptr gmpnum_a, gmpnum_result1, gmpnum_result2;
    1572             :         gmp_temp_t temp_a;
    1573             :         zval result1, result2;
    1574             : 
    1575          10 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zl", &a_arg, &nth) == FAILURE) {
    1576           1 :                 return;
    1577             :         }
    1578             : 
    1579           9 :         if (nth <= 0) {
    1580           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "The root must be positive");
    1581           2 :                 RETURN_FALSE;
    1582             :         }
    1583             : 
    1584           7 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
    1585             : 
    1586           7 :         if (nth % 2 == 0 && mpz_sgn(gmpnum_a) < 0) {
    1587           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can't take even root of negative number");
    1588           1 :                 FREE_GMP_TEMP(temp_a);
    1589           1 :                 RETURN_FALSE;
    1590             :         }
    1591             : 
    1592           6 :         gmp_create(&result1, &gmpnum_result1 TSRMLS_CC);
    1593           6 :         gmp_create(&result2, &gmpnum_result2 TSRMLS_CC);
    1594             : 
    1595           6 :         array_init(return_value);
    1596           6 :         add_next_index_zval(return_value, &result1);
    1597           6 :         add_next_index_zval(return_value, &result2);
    1598             : 
    1599             : #if GMP_42_OR_NEWER
    1600           6 :         mpz_rootrem(gmpnum_result1, gmpnum_result2, gmpnum_a, (gmp_ulong) nth);
    1601             : #else
    1602             :         mpz_root(gmpnum_result1, gmpnum_a, (gmp_ulong) nth);
    1603             :         mpz_pow_ui(gmpnum_result2, gmpnum_result1, (gmp_ulong) nth);
    1604             :         mpz_sub(gmpnum_result2, gmpnum_a, gmpnum_result2);
    1605             :         mpz_abs(gmpnum_result2, gmpnum_result2);
    1606             : #endif
    1607             : 
    1608           6 :         FREE_GMP_TEMP(temp_a);
    1609             : }
    1610             : /* }}} */
    1611             : 
    1612             : /* {{{ proto bool gmp_perfect_square(mixed a)
    1613             :    Checks if a is an exact square */
    1614          13 : ZEND_FUNCTION(gmp_perfect_square)
    1615             : {
    1616             :         zval *a_arg;
    1617             :         mpz_ptr gmpnum_a;
    1618             :         gmp_temp_t temp_a;
    1619             : 
    1620          13 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &a_arg) == FAILURE){
    1621           1 :                 return;
    1622             :         }
    1623             : 
    1624          12 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
    1625             : 
    1626          11 :         RETVAL_BOOL((mpz_perfect_square_p(gmpnum_a) != 0));
    1627          11 :         FREE_GMP_TEMP(temp_a);
    1628             : }
    1629             : /* }}} */
    1630             : 
    1631             : /* {{{ proto int gmp_prob_prime(mixed a[, int reps])
    1632             :    Checks if a is "probably prime" */
    1633          40 : ZEND_FUNCTION(gmp_prob_prime)
    1634             : {
    1635             :         zval *gmpnumber_arg;
    1636             :         mpz_ptr gmpnum_a;
    1637          40 :         zend_long reps = 10;
    1638             :         gmp_temp_t temp_a;
    1639             : 
    1640          40 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &gmpnumber_arg, &reps) == FAILURE) {
    1641           1 :                 return;
    1642             :         }
    1643             : 
    1644          39 :         FETCH_GMP_ZVAL(gmpnum_a, gmpnumber_arg, temp_a);
    1645             : 
    1646          38 :         RETVAL_LONG(mpz_probab_prime_p(gmpnum_a, reps));
    1647          38 :         FREE_GMP_TEMP(temp_a);
    1648             : }
    1649             : /* }}} */
    1650             : 
    1651             : /* {{{ proto GMP gmp_gcd(mixed a, mixed b)
    1652             :    Computes greatest common denominator (gcd) of a and b */
    1653          14 : ZEND_FUNCTION(gmp_gcd)
    1654             : {
    1655          14 :         gmp_binary_ui_op(mpz_gcd, (gmp_binary_ui_op_t) mpz_gcd_ui);
    1656          14 : }
    1657             : /* }}} */
    1658             : 
    1659             : /* {{{ proto array gmp_gcdext(mixed a, mixed b)
    1660             :    Computes G, S, and T, such that AS + BT = G = `gcd' (A, B) */
    1661          15 : ZEND_FUNCTION(gmp_gcdext)
    1662             : {
    1663             :         zval *a_arg, *b_arg;
    1664             :         mpz_ptr gmpnum_a, gmpnum_b, gmpnum_t, gmpnum_s, gmpnum_g;
    1665             :         gmp_temp_t temp_a, temp_b;
    1666             :         zval result_g, result_s, result_t;
    1667             : 
    1668          15 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &a_arg, &b_arg) == FAILURE){
    1669           3 :                 return;
    1670             :         }
    1671             : 
    1672          12 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
    1673          11 :         FETCH_GMP_ZVAL_DEP(gmpnum_b, b_arg, temp_b, temp_a);
    1674             : 
    1675          10 :         gmp_create(&result_g, &gmpnum_g TSRMLS_CC);
    1676          10 :         gmp_create(&result_s, &gmpnum_s TSRMLS_CC);
    1677          10 :         gmp_create(&result_t, &gmpnum_t TSRMLS_CC);
    1678             : 
    1679          10 :         array_init(return_value);
    1680          10 :         add_assoc_zval(return_value, "g", &result_g);
    1681          10 :         add_assoc_zval(return_value, "s", &result_s);
    1682          10 :         add_assoc_zval(return_value, "t", &result_t);
    1683             : 
    1684          10 :         mpz_gcdext(gmpnum_g, gmpnum_s, gmpnum_t, gmpnum_a, gmpnum_b);
    1685          10 :         FREE_GMP_TEMP(temp_a);
    1686          10 :         FREE_GMP_TEMP(temp_b);
    1687             : }
    1688             : /* }}} */
    1689             : 
    1690             : /* {{{ proto GMP gmp_invert(mixed a, mixed b)
    1691             :    Computes the inverse of a modulo b */
    1692          14 : ZEND_FUNCTION(gmp_invert)
    1693             : {
    1694             :         zval *a_arg, *b_arg;
    1695             :         mpz_ptr gmpnum_a, gmpnum_b, gmpnum_result;
    1696             :         gmp_temp_t temp_a, temp_b;
    1697             :         int res;
    1698             : 
    1699          14 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &a_arg, &b_arg) == FAILURE){
    1700           2 :                 return;
    1701             :         }
    1702             : 
    1703          12 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
    1704          10 :         FETCH_GMP_ZVAL_DEP(gmpnum_b, b_arg, temp_b, temp_a);
    1705             : 
    1706           9 :         INIT_GMP_RETVAL(gmpnum_result);
    1707           9 :         res = mpz_invert(gmpnum_result, gmpnum_a, gmpnum_b);
    1708           9 :         FREE_GMP_TEMP(temp_a);
    1709           9 :         FREE_GMP_TEMP(temp_b);
    1710           9 :         if (!res) {
    1711             :                 zval_dtor(return_value);
    1712           5 :                 RETURN_FALSE;
    1713             :         }
    1714             : }
    1715             : /* }}} */
    1716             : 
    1717             : /* {{{ proto int gmp_jacobi(mixed a, mixed b)
    1718             :    Computes Jacobi symbol */
    1719          23 : ZEND_FUNCTION(gmp_jacobi)
    1720             : {
    1721          23 :         gmp_binary_opl(mpz_jacobi);
    1722          23 : }
    1723             : /* }}} */
    1724             : 
    1725             : /* {{{ proto int gmp_legendre(mixed a, mixed b)
    1726             :    Computes Legendre symbol */
    1727          23 : ZEND_FUNCTION(gmp_legendre)
    1728             : {
    1729          23 :         gmp_binary_opl(mpz_legendre);
    1730          23 : }
    1731             : /* }}} */
    1732             : 
    1733             : /* {{{ proto int gmp_cmp(mixed a, mixed b)
    1734             :    Compares two numbers */
    1735          12 : ZEND_FUNCTION(gmp_cmp)
    1736             : {
    1737             :         zval *a_arg, *b_arg;
    1738             : 
    1739          12 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &a_arg, &b_arg) == FAILURE){
    1740           3 :                 return;
    1741             :         }
    1742             : 
    1743           9 :         gmp_cmp(return_value, a_arg, b_arg TSRMLS_CC);
    1744             : }
    1745             : /* }}} */
    1746             : 
    1747             : /* {{{ proto int gmp_sign(mixed a)
    1748             :    Gets the sign of the number */
    1749          10 : ZEND_FUNCTION(gmp_sign)
    1750             : {
    1751             :         /* Can't use gmp_unary_opl here, because mpz_sgn is a macro */
    1752             :         zval *a_arg;
    1753             :         mpz_ptr gmpnum_a;
    1754             :         gmp_temp_t temp_a;
    1755             : 
    1756          10 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &a_arg) == FAILURE){
    1757           2 :                 return;
    1758             :         }
    1759             : 
    1760           8 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
    1761             : 
    1762           6 :         RETVAL_LONG(mpz_sgn(gmpnum_a));
    1763           6 :         FREE_GMP_TEMP(temp_a);
    1764             : }
    1765             : /* }}} */
    1766             : 
    1767             : /* {{{ proto GMP gmp_random([int limiter])
    1768             :    Gets random number */
    1769          10 : ZEND_FUNCTION(gmp_random)
    1770             : {
    1771          10 :         zend_long limiter = 20;
    1772             :         mpz_ptr gmpnum_result;
    1773             : 
    1774          10 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &limiter) == FAILURE) {
    1775           4 :                 return;
    1776             :         }
    1777             : 
    1778           6 :         INIT_GMP_RETVAL(gmpnum_result);
    1779             : 
    1780           6 :         if (!GMPG(rand_initialized)) {
    1781             :                 /* Initialize */
    1782           1 :                 gmp_randinit_lc_2exp_size(GMPG(rand_state), 32L);
    1783             : 
    1784             :                 /* Seed */
    1785           1 :                 gmp_randseed_ui(GMPG(rand_state), GENERATE_SEED());
    1786             : 
    1787           1 :                 GMPG(rand_initialized) = 1;
    1788             :         }
    1789             : #ifdef GMP_LIMB_BITS
    1790           6 :         mpz_urandomb(gmpnum_result, GMPG(rand_state), GMP_ABS (limiter) * GMP_LIMB_BITS);
    1791             : #else
    1792             :         mpz_urandomb(gmpnum_result, GMPG(rand_state), GMP_ABS (limiter) * __GMP_BITS_PER_MP_LIMB);
    1793             : #endif
    1794             : }
    1795             : /* }}} */
    1796             : 
    1797             : /* {{{ proto GMP gmp_and(mixed a, mixed b)
    1798             :    Calculates logical AND of a and b */
    1799          13 : ZEND_FUNCTION(gmp_and)
    1800             : {
    1801          13 :         gmp_binary_op(mpz_and);
    1802          13 : }
    1803             : /* }}} */
    1804             : 
    1805             : /* {{{ proto GMP gmp_or(mixed a, mixed b)
    1806             :    Calculates logical OR of a and b */
    1807          13 : ZEND_FUNCTION(gmp_or)
    1808             : {
    1809          13 :         gmp_binary_op(mpz_ior);
    1810          13 : }
    1811             : /* }}} */
    1812             : 
    1813             : /* {{{ proto GMP gmp_com(mixed a)
    1814             :    Calculates one's complement of a */
    1815          11 : ZEND_FUNCTION(gmp_com)
    1816             : {
    1817          11 :         gmp_unary_op(mpz_com);
    1818          11 : }
    1819             : /* }}} */
    1820             : 
    1821             : /* {{{ proto GMP gmp_nextprime(mixed a)
    1822             :    Finds next prime of a */
    1823           8 : ZEND_FUNCTION(gmp_nextprime)
    1824             : {
    1825           8 :    gmp_unary_op(mpz_nextprime);
    1826           8 : }
    1827             : /* }}} */
    1828             : 
    1829             : /* {{{ proto GMP gmp_xor(mixed a, mixed b)
    1830             :    Calculates logical exclusive OR of a and b */
    1831          13 : ZEND_FUNCTION(gmp_xor)
    1832             : {
    1833          13 :         gmp_binary_op(mpz_xor);
    1834          13 : }
    1835             : /* }}} */
    1836             : 
    1837             : /* {{{ proto void gmp_setbit(GMP a, int index[, bool set_clear])
    1838             :    Sets or clear bit in a */
    1839          15 : ZEND_FUNCTION(gmp_setbit)
    1840             : {
    1841             :         zval *a_arg;
    1842             :         zend_long index;
    1843          15 :         zend_bool set = 1;
    1844             :         mpz_ptr gmpnum_a;
    1845             : 
    1846          15 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Ol|b", &a_arg, gmp_ce, &index, &set) == FAILURE) {
    1847           5 :                 return;
    1848             :         }
    1849             : 
    1850          10 :         if (index < 0) {
    1851           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Index must be greater than or equal to zero");
    1852           1 :                 return;
    1853             :         }
    1854             : 
    1855           9 :         gmpnum_a = GET_GMP_FROM_ZVAL(a_arg);
    1856             : 
    1857           9 :         if (set) {
    1858           7 :                 mpz_setbit(gmpnum_a, index);
    1859             :         } else {
    1860           2 :                 mpz_clrbit(gmpnum_a, index);
    1861             :         }
    1862             : }
    1863             : /* }}} */
    1864             : 
    1865             : /* {{{ proto void gmp_clrbit(GMP a, int index)
    1866             :    Clears bit in a */
    1867          13 : ZEND_FUNCTION(gmp_clrbit)
    1868             : {
    1869             :         zval *a_arg;
    1870             :         zend_long index;
    1871             :         mpz_ptr gmpnum_a;
    1872             : 
    1873          13 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Ol", &a_arg, gmp_ce, &index) == FAILURE){
    1874           4 :                 return;
    1875             :         }
    1876             : 
    1877           9 :         if (index < 0) {
    1878           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Index must be greater than or equal to zero");
    1879           2 :                 return;
    1880             :         }
    1881             : 
    1882           7 :         gmpnum_a = GET_GMP_FROM_ZVAL(a_arg);
    1883           7 :         mpz_clrbit(gmpnum_a, index);
    1884             : }
    1885             : /* }}} */
    1886             : 
    1887             : /* {{{ proto bool gmp_testbit(mixed a, int index)
    1888             :    Tests if bit is set in a */
    1889          14 : ZEND_FUNCTION(gmp_testbit)
    1890             : {
    1891             :         zval *a_arg;
    1892             :         zend_long index;
    1893             :         mpz_ptr gmpnum_a;
    1894             :         gmp_temp_t temp_a;
    1895             : 
    1896          14 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zl", &a_arg, &index) == FAILURE){
    1897           1 :                 return;
    1898             :         }
    1899             : 
    1900          13 :         if (index < 0) {
    1901           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Index must be greater than or equal to zero");
    1902           2 :                 RETURN_FALSE;
    1903             :         }
    1904             : 
    1905          11 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
    1906          10 :         RETVAL_BOOL(mpz_tstbit(gmpnum_a, index));
    1907          10 :         FREE_GMP_TEMP(temp_a);
    1908             : }
    1909             : /* }}} */
    1910             : 
    1911             : /* {{{ proto int gmp_popcount(mixed a)
    1912             :    Calculates the population count of a */
    1913           8 : ZEND_FUNCTION(gmp_popcount)
    1914             : {
    1915           8 :         gmp_unary_opl((gmp_unary_opl_t) mpz_popcount);
    1916           8 : }
    1917             : /* }}} */
    1918             : 
    1919             : /* {{{ proto int gmp_hamdist(mixed a, mixed b)
    1920             :    Calculates hamming distance between a and b */
    1921          12 : ZEND_FUNCTION(gmp_hamdist)
    1922             : {
    1923          12 :         gmp_binary_opl((gmp_binary_opl_t) mpz_hamdist);
    1924          12 : }
    1925             : /* }}} */
    1926             : 
    1927             : /* {{{ proto int gmp_scan0(mixed a, int start)
    1928             :    Finds first zero bit */
    1929           9 : ZEND_FUNCTION(gmp_scan0)
    1930             : {
    1931             :         zval *a_arg;
    1932             :         mpz_ptr gmpnum_a;
    1933             :         gmp_temp_t temp_a;
    1934             :         zend_long start;
    1935             : 
    1936           9 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zl", &a_arg, &start) == FAILURE){
    1937           2 :                 return;
    1938             :         }
    1939             : 
    1940           7 :         if (start < 0) {
    1941           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Starting index must be greater than or equal to zero");
    1942           1 :                 RETURN_FALSE;
    1943             :         }
    1944             : 
    1945           6 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
    1946             : 
    1947           5 :         RETVAL_LONG(mpz_scan0(gmpnum_a, start));
    1948           5 :         FREE_GMP_TEMP(temp_a);
    1949             : }
    1950             : /* }}} */
    1951             : 
    1952             : /* {{{ proto int gmp_scan1(mixed a, int start)
    1953             :    Finds first non-zero bit */
    1954           9 : ZEND_FUNCTION(gmp_scan1)
    1955             : {
    1956             :         zval *a_arg;
    1957             :         mpz_ptr gmpnum_a;
    1958             :         gmp_temp_t temp_a;
    1959             :         zend_long start;
    1960             : 
    1961           9 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zl", &a_arg, &start) == FAILURE){
    1962           2 :                 return;
    1963             :         }
    1964             : 
    1965           7 :         if (start < 0) {
    1966           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Starting index must be greater than or equal to zero");
    1967           1 :                 RETURN_FALSE;
    1968             :         }
    1969             : 
    1970           6 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
    1971             : 
    1972           5 :         RETVAL_LONG(mpz_scan1(gmpnum_a, start));
    1973           5 :         FREE_GMP_TEMP(temp_a);
    1974             : }
    1975             : /* }}} */
    1976             : 
    1977             : #endif  /* HAVE_GMP */
    1978             : 
    1979             : /*
    1980             :  * Local variables:
    1981             :  * tab-width: 4
    1982             :  * c-basic-offset: 4
    1983             :  * End:
    1984             :  * vim600: noet sw=4 ts=4 fdm=marker
    1985             :  * vim<600: noet sw=4 ts=4
    1986             :  */

Generated by: LCOV version 1.10

Generated at Fri, 19 Sep 2014 17:11:07 +0000 (2 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.