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: 714 715 99.9 %
Date: 2015-03-20 Functions: 77 77 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Fri, 20 Mar 2015 23:40:27 +0000 (5 days ago)

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