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

Generated by: LCOV version 1.10

Generated at Fri, 31 Jul 2015 08:58:48 +0000 (37 hours ago)

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