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: 716 717 99.9 %
Date: 2015-01-18 Functions: 77 77 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sun, 18 Jan 2015 09:00:39 +0000 (6 days ago)

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