1999-04-22 07:28:00 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2019-01-30 17:03:12 +08:00
|
|
|
| Copyright (c) The PHP Group |
|
1999-04-22 07:28:00 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2006-01-01 20:51:34 +08:00
|
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
1999-07-16 21:13:16 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-11 04:04:29 +08:00
|
|
|
| available through the world-wide-web at the following url: |
|
2021-05-06 18:16:35 +08:00
|
|
|
| https://www.php.net/license/3_01.txt |
|
1999-07-16 21:13:16 +08:00
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
1999-04-22 07:28:00 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2018-11-02 00:30:28 +08:00
|
|
|
| Author: Andi Gutmans <andi@php.net> |
|
1999-04-22 07:28:00 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2000-07-24 09:40:02 +08:00
|
|
|
*/
|
|
|
|
|
2001-05-24 18:07:29 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2024-06-26 06:26:43 +08:00
|
|
|
#include <config.h>
|
2001-05-24 18:07:29 +08:00
|
|
|
#endif
|
|
|
|
|
1999-04-22 07:28:00 +08:00
|
|
|
#include "php.h"
|
|
|
|
|
2020-05-13 04:24:01 +08:00
|
|
|
#ifdef HAVE_BCMATH
|
1999-04-22 07:28:00 +08:00
|
|
|
|
2003-12-10 07:59:33 +08:00
|
|
|
#include "php_ini.h"
|
2019-10-28 18:59:20 +08:00
|
|
|
#include "zend_exceptions.h"
|
2024-09-04 10:12:51 +08:00
|
|
|
#include "zend_interfaces.h"
|
2019-08-11 00:02:13 +08:00
|
|
|
#include "bcmath_arginfo.h"
|
2000-10-08 19:45:18 +08:00
|
|
|
#include "ext/standard/info.h"
|
1999-12-15 05:44:35 +08:00
|
|
|
#include "php_bcmath.h"
|
2002-08-18 12:33:10 +08:00
|
|
|
#include "libbcmath/src/bcmath.h"
|
1999-04-22 07:28:00 +08:00
|
|
|
|
2024-05-09 01:36:14 +08:00
|
|
|
/* Always pair SETUP with TEARDOWN, and do so in the outer scope!
|
|
|
|
* Should not be used when data can escape the function. */
|
|
|
|
#define BC_ARENA_SETUP \
|
|
|
|
char bc_arena[BC_ARENA_SIZE]; \
|
|
|
|
BCG(arena) = bc_arena;
|
|
|
|
|
|
|
|
#define BC_ARENA_TEARDOWN \
|
|
|
|
BCG(arena) = NULL; \
|
|
|
|
BCG(arena_offset) = 0;
|
|
|
|
|
2007-06-07 06:09:25 +08:00
|
|
|
ZEND_DECLARE_MODULE_GLOBALS(bcmath)
|
2006-06-16 02:33:09 +08:00
|
|
|
static PHP_GINIT_FUNCTION(bcmath);
|
2006-11-10 20:02:10 +08:00
|
|
|
static PHP_GSHUTDOWN_FUNCTION(bcmath);
|
2023-07-24 23:42:34 +08:00
|
|
|
static PHP_MINIT_FUNCTION(bcmath);
|
|
|
|
static PHP_MSHUTDOWN_FUNCTION(bcmath);
|
|
|
|
static PHP_MINFO_FUNCTION(bcmath);
|
2002-11-22 17:25:29 +08:00
|
|
|
|
1999-12-18 04:55:31 +08:00
|
|
|
zend_module_entry bcmath_module_entry = {
|
2001-10-12 07:33:59 +08:00
|
|
|
STANDARD_MODULE_HEADER,
|
2000-10-08 19:45:18 +08:00
|
|
|
"bcmath",
|
2020-04-05 02:41:42 +08:00
|
|
|
ext_functions,
|
2003-12-10 07:59:33 +08:00
|
|
|
PHP_MINIT(bcmath),
|
|
|
|
PHP_MSHUTDOWN(bcmath),
|
2006-11-10 20:02:10 +08:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2000-10-08 19:45:18 +08:00
|
|
|
PHP_MINFO(bcmath),
|
2015-03-24 03:13:59 +08:00
|
|
|
PHP_BCMATH_VERSION,
|
2006-06-16 02:33:09 +08:00
|
|
|
PHP_MODULE_GLOBALS(bcmath),
|
|
|
|
PHP_GINIT(bcmath),
|
2020-04-25 07:18:09 +08:00
|
|
|
PHP_GSHUTDOWN(bcmath),
|
2006-06-16 02:33:09 +08:00
|
|
|
NULL,
|
|
|
|
STANDARD_MODULE_PROPERTIES_EX
|
1999-04-22 07:28:00 +08:00
|
|
|
};
|
|
|
|
|
2000-05-23 17:33:51 +08:00
|
|
|
#ifdef COMPILE_DL_BCMATH
|
2014-10-17 21:51:21 +08:00
|
|
|
#ifdef ZTS
|
2016-03-03 23:46:04 +08:00
|
|
|
ZEND_TSRMLS_CACHE_DEFINE()
|
2014-10-17 21:51:21 +08:00
|
|
|
#endif
|
2000-05-02 08:30:36 +08:00
|
|
|
ZEND_GET_MODULE(bcmath)
|
1999-04-22 07:28:00 +08:00
|
|
|
#endif
|
|
|
|
|
2020-04-25 07:18:09 +08:00
|
|
|
ZEND_INI_MH(OnUpdateScale)
|
|
|
|
{
|
|
|
|
int *p;
|
|
|
|
zend_long tmp;
|
|
|
|
|
2022-06-17 20:12:53 +08:00
|
|
|
tmp = zend_ini_parse_quantity_warn(new_value, entry->name);
|
2020-04-25 07:18:09 +08:00
|
|
|
if (tmp < 0 || tmp > INT_MAX) {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = (int *) ZEND_INI_GET_ADDR();
|
|
|
|
*p = (int) tmp;
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2003-12-10 07:59:33 +08:00
|
|
|
/* {{{ PHP_INI */
|
|
|
|
PHP_INI_BEGIN()
|
2020-04-25 07:18:09 +08:00
|
|
|
STD_PHP_INI_ENTRY("bcmath.scale", "0", PHP_INI_ALL, OnUpdateScale, bc_precision, zend_bcmath_globals, bcmath_globals)
|
2003-12-10 07:59:33 +08:00
|
|
|
PHP_INI_END()
|
|
|
|
/* }}} */
|
1999-04-22 07:28:00 +08:00
|
|
|
|
2020-07-01 21:32:55 +08:00
|
|
|
/* {{{ PHP_GINIT_FUNCTION */
|
2006-06-16 02:33:09 +08:00
|
|
|
static PHP_GINIT_FUNCTION(bcmath)
|
2000-11-23 04:20:02 +08:00
|
|
|
{
|
2014-10-17 21:51:21 +08:00
|
|
|
#if defined(COMPILE_DL_BCMATH) && defined(ZTS)
|
2015-02-17 00:19:32 +08:00
|
|
|
ZEND_TSRMLS_CACHE_UPDATE();
|
2014-10-17 21:51:21 +08:00
|
|
|
#endif
|
2003-12-10 07:59:33 +08:00
|
|
|
bcmath_globals->bc_precision = 0;
|
2024-05-09 01:36:14 +08:00
|
|
|
bcmath_globals->arena = NULL;
|
|
|
|
bcmath_globals->arena_offset = 0;
|
2014-12-14 06:06:14 +08:00
|
|
|
bc_init_numbers();
|
2006-11-10 20:02:10 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2020-07-01 21:32:55 +08:00
|
|
|
/* {{{ PHP_GSHUTDOWN_FUNCTION */
|
2006-11-10 20:02:10 +08:00
|
|
|
static PHP_GSHUTDOWN_FUNCTION(bcmath)
|
|
|
|
{
|
|
|
|
_bc_free_num_ex(&bcmath_globals->_zero_, 1);
|
|
|
|
_bc_free_num_ex(&bcmath_globals->_one_, 1);
|
|
|
|
_bc_free_num_ex(&bcmath_globals->_two_, 1);
|
2024-05-09 01:36:14 +08:00
|
|
|
bcmath_globals->arena = NULL;
|
|
|
|
bcmath_globals->arena_offset = 0;
|
2000-11-23 04:20:02 +08:00
|
|
|
}
|
2003-12-10 07:59:33 +08:00
|
|
|
/* }}} */
|
2000-11-23 04:20:02 +08:00
|
|
|
|
2024-09-04 10:12:51 +08:00
|
|
|
static void bcmath_number_register_class(void);
|
|
|
|
|
2020-07-01 21:32:55 +08:00
|
|
|
/* {{{ PHP_MINIT_FUNCTION */
|
2003-12-10 07:59:33 +08:00
|
|
|
PHP_MINIT_FUNCTION(bcmath)
|
1999-04-22 07:28:00 +08:00
|
|
|
{
|
2003-12-10 07:59:33 +08:00
|
|
|
REGISTER_INI_ENTRIES();
|
2024-09-04 10:12:51 +08:00
|
|
|
bcmath_number_register_class();
|
2003-12-10 07:59:33 +08:00
|
|
|
|
1999-04-22 07:28:00 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
2003-12-10 07:59:33 +08:00
|
|
|
/* }}} */
|
1999-04-22 07:28:00 +08:00
|
|
|
|
2020-07-01 21:32:55 +08:00
|
|
|
/* {{{ PHP_MSHUTDOWN_FUNCTION */
|
2003-12-10 07:59:33 +08:00
|
|
|
PHP_MSHUTDOWN_FUNCTION(bcmath)
|
2004-07-14 08:14:43 +08:00
|
|
|
{
|
|
|
|
UNREGISTER_INI_ENTRIES();
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2020-07-01 21:32:55 +08:00
|
|
|
/* {{{ PHP_MINFO_FUNCTION */
|
2000-10-08 19:45:18 +08:00
|
|
|
PHP_MINFO_FUNCTION(bcmath)
|
|
|
|
{
|
2001-07-30 14:18:13 +08:00
|
|
|
php_info_print_table_start();
|
|
|
|
php_info_print_table_row(2, "BCMath support", "enabled");
|
|
|
|
php_info_print_table_end();
|
2008-12-12 21:07:28 +08:00
|
|
|
DISPLAY_INI_ENTRIES();
|
2000-10-08 19:45:18 +08:00
|
|
|
}
|
2003-12-10 07:59:33 +08:00
|
|
|
/* }}} */
|
2000-10-08 19:45:18 +08:00
|
|
|
|
2024-09-07 03:19:18 +08:00
|
|
|
static zend_always_inline zend_result bcmath_check_scale(zend_long scale, uint32_t arg_num)
|
|
|
|
{
|
|
|
|
if (UNEXPECTED(scale < 0 || scale > INT_MAX)) {
|
|
|
|
zend_argument_value_error(arg_num, "must be between 0 and %d", INT_MAX);
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2024-09-04 10:12:51 +08:00
|
|
|
static void php_long2num(bc_num *num, zend_long lval)
|
|
|
|
{
|
|
|
|
*num = bc_long2num(lval);
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_result php_str2num_ex(bc_num *num, const zend_string *str, size_t *full_scale)
|
2003-02-05 05:07:40 +08:00
|
|
|
{
|
2024-09-04 10:12:51 +08:00
|
|
|
if (!bc_str2num(num, ZSTR_VAL(str), ZSTR_VAL(str) + ZSTR_LEN(str), 0, full_scale, true)) {
|
2020-12-28 04:15:06 +08:00
|
|
|
return FAILURE;
|
2019-04-30 22:33:04 +08:00
|
|
|
}
|
2020-12-28 04:15:06 +08:00
|
|
|
|
|
|
|
return SUCCESS;
|
2003-02-05 03:03:30 +08:00
|
|
|
}
|
2024-09-04 10:12:51 +08:00
|
|
|
|
|
|
|
/* {{{ php_str2num
|
|
|
|
Convert to bc_num detecting scale */
|
|
|
|
static zend_result php_str2num(bc_num *num, const zend_string *str)
|
|
|
|
{
|
|
|
|
return php_str2num_ex(num, str, NULL);
|
|
|
|
}
|
2003-02-05 03:03:30 +08:00
|
|
|
/* }}} */
|
|
|
|
|
2020-07-01 21:32:55 +08:00
|
|
|
/* {{{ Returns the sum of two arbitrary precision numbers */
|
1999-07-27 04:09:08 +08:00
|
|
|
PHP_FUNCTION(bcadd)
|
1999-04-22 07:28:00 +08:00
|
|
|
{
|
2016-12-28 15:35:38 +08:00
|
|
|
zend_string *left, *right;
|
2020-05-02 02:49:47 +08:00
|
|
|
zend_long scale_param;
|
2021-01-15 19:30:54 +08:00
|
|
|
bool scale_param_is_null = 1;
|
2024-05-02 02:59:40 +08:00
|
|
|
bc_num first = NULL, second = NULL, result = NULL;
|
2020-05-02 02:49:47 +08:00
|
|
|
int scale;
|
2008-06-25 00:01:32 +08:00
|
|
|
|
2016-12-28 15:44:18 +08:00
|
|
|
ZEND_PARSE_PARAMETERS_START(2, 3)
|
|
|
|
Z_PARAM_STR(left)
|
|
|
|
Z_PARAM_STR(right)
|
|
|
|
Z_PARAM_OPTIONAL
|
2020-05-02 02:49:47 +08:00
|
|
|
Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
|
2016-12-28 15:44:18 +08:00
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2020-05-02 02:49:47 +08:00
|
|
|
if (scale_param_is_null) {
|
|
|
|
scale = BCG(bc_precision);
|
2024-09-07 03:19:24 +08:00
|
|
|
} else if (bcmath_check_scale(scale_param, 3) == FAILURE) {
|
2020-05-02 02:49:47 +08:00
|
|
|
RETURN_THROWS();
|
|
|
|
} else {
|
2020-04-25 07:18:09 +08:00
|
|
|
scale = (int) scale_param;
|
1999-04-22 07:28:00 +08:00
|
|
|
}
|
2008-06-25 00:01:32 +08:00
|
|
|
|
2024-05-09 01:36:14 +08:00
|
|
|
BC_ARENA_SETUP;
|
|
|
|
|
2024-05-03 23:34:38 +08:00
|
|
|
if (php_str2num(&first, left) == FAILURE) {
|
2020-12-28 04:15:06 +08:00
|
|
|
zend_argument_value_error(1, "is not well-formed");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2024-05-03 23:34:38 +08:00
|
|
|
if (php_str2num(&second, right) == FAILURE) {
|
2020-12-28 04:15:06 +08:00
|
|
|
zend_argument_value_error(2, "is not well-formed");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2024-05-02 02:59:40 +08:00
|
|
|
result = bc_add (first, second, scale);
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2024-05-02 00:24:57 +08:00
|
|
|
RETVAL_NEW_STR(bc_num2str_ex(result, scale));
|
2020-12-28 04:15:06 +08:00
|
|
|
|
|
|
|
cleanup: {
|
|
|
|
bc_free_num(&first);
|
|
|
|
bc_free_num(&second);
|
|
|
|
bc_free_num(&result);
|
2024-05-09 01:36:14 +08:00
|
|
|
BC_ARENA_TEARDOWN;
|
2020-12-28 04:15:06 +08:00
|
|
|
};
|
1999-04-22 07:28:00 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2020-07-01 21:32:55 +08:00
|
|
|
/* {{{ Returns the difference between two arbitrary precision numbers */
|
1999-07-27 04:09:08 +08:00
|
|
|
PHP_FUNCTION(bcsub)
|
1999-04-22 07:28:00 +08:00
|
|
|
{
|
2016-12-28 15:35:38 +08:00
|
|
|
zend_string *left, *right;
|
2020-05-02 02:49:47 +08:00
|
|
|
zend_long scale_param;
|
2021-01-15 19:30:54 +08:00
|
|
|
bool scale_param_is_null = 1;
|
2024-05-02 03:10:43 +08:00
|
|
|
bc_num first = NULL, second = NULL, result = NULL;
|
2020-05-02 02:49:47 +08:00
|
|
|
int scale;
|
2008-06-25 00:01:32 +08:00
|
|
|
|
2016-12-28 15:44:18 +08:00
|
|
|
ZEND_PARSE_PARAMETERS_START(2, 3)
|
|
|
|
Z_PARAM_STR(left)
|
|
|
|
Z_PARAM_STR(right)
|
|
|
|
Z_PARAM_OPTIONAL
|
2020-05-02 02:49:47 +08:00
|
|
|
Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
|
2016-12-28 15:44:18 +08:00
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2020-05-02 02:49:47 +08:00
|
|
|
if (scale_param_is_null) {
|
|
|
|
scale = BCG(bc_precision);
|
2024-09-07 03:19:24 +08:00
|
|
|
} else if (bcmath_check_scale(scale_param, 3) == FAILURE) {
|
2020-05-02 02:49:47 +08:00
|
|
|
RETURN_THROWS();
|
|
|
|
} else {
|
2020-04-25 07:18:09 +08:00
|
|
|
scale = (int) scale_param;
|
1999-04-22 07:28:00 +08:00
|
|
|
}
|
2008-06-25 00:01:32 +08:00
|
|
|
|
2024-05-09 01:36:14 +08:00
|
|
|
BC_ARENA_SETUP;
|
|
|
|
|
2024-05-03 23:34:38 +08:00
|
|
|
if (php_str2num(&first, left) == FAILURE) {
|
2020-12-28 04:15:06 +08:00
|
|
|
zend_argument_value_error(1, "is not well-formed");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2024-05-03 23:34:38 +08:00
|
|
|
if (php_str2num(&second, right) == FAILURE) {
|
2020-12-28 04:15:06 +08:00
|
|
|
zend_argument_value_error(2, "is not well-formed");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2024-05-02 03:10:43 +08:00
|
|
|
result = bc_sub (first, second, scale);
|
2008-06-25 00:01:32 +08:00
|
|
|
|
2024-05-02 00:24:57 +08:00
|
|
|
RETVAL_NEW_STR(bc_num2str_ex(result, scale));
|
2020-12-28 04:15:06 +08:00
|
|
|
|
|
|
|
cleanup: {
|
|
|
|
bc_free_num(&first);
|
|
|
|
bc_free_num(&second);
|
|
|
|
bc_free_num(&result);
|
2024-05-09 01:36:14 +08:00
|
|
|
BC_ARENA_TEARDOWN;
|
2020-12-28 04:15:06 +08:00
|
|
|
};
|
1999-04-22 07:28:00 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2020-07-01 21:32:55 +08:00
|
|
|
/* {{{ Returns the multiplication of two arbitrary precision numbers */
|
1999-07-27 04:09:08 +08:00
|
|
|
PHP_FUNCTION(bcmul)
|
1999-04-22 07:28:00 +08:00
|
|
|
{
|
2016-12-28 15:35:38 +08:00
|
|
|
zend_string *left, *right;
|
2020-05-02 02:49:47 +08:00
|
|
|
zend_long scale_param;
|
2021-01-15 19:30:54 +08:00
|
|
|
bool scale_param_is_null = 1;
|
2024-05-02 03:14:03 +08:00
|
|
|
bc_num first = NULL, second = NULL, result = NULL;
|
2020-05-02 02:49:47 +08:00
|
|
|
int scale;
|
2008-06-25 00:01:32 +08:00
|
|
|
|
2016-12-28 15:44:18 +08:00
|
|
|
ZEND_PARSE_PARAMETERS_START(2, 3)
|
|
|
|
Z_PARAM_STR(left)
|
|
|
|
Z_PARAM_STR(right)
|
|
|
|
Z_PARAM_OPTIONAL
|
2020-05-02 02:49:47 +08:00
|
|
|
Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
|
2016-12-28 15:44:18 +08:00
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2020-05-02 02:49:47 +08:00
|
|
|
if (scale_param_is_null) {
|
|
|
|
scale = BCG(bc_precision);
|
2024-09-07 03:19:24 +08:00
|
|
|
} else if (bcmath_check_scale(scale_param, 3) == FAILURE) {
|
2020-05-02 02:49:47 +08:00
|
|
|
RETURN_THROWS();
|
|
|
|
} else {
|
2020-04-25 07:18:09 +08:00
|
|
|
scale = (int) scale_param;
|
2008-06-25 00:01:32 +08:00
|
|
|
}
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2024-05-09 01:36:14 +08:00
|
|
|
BC_ARENA_SETUP;
|
|
|
|
|
2024-05-03 23:34:38 +08:00
|
|
|
if (php_str2num(&first, left) == FAILURE) {
|
2020-12-28 04:15:06 +08:00
|
|
|
zend_argument_value_error(1, "is not well-formed");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2024-05-03 23:34:38 +08:00
|
|
|
if (php_str2num(&second, right) == FAILURE) {
|
2020-12-28 04:15:06 +08:00
|
|
|
zend_argument_value_error(2, "is not well-formed");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2024-05-02 03:14:03 +08:00
|
|
|
result = bc_multiply (first, second, scale);
|
2008-06-25 00:01:32 +08:00
|
|
|
|
2024-05-02 00:24:57 +08:00
|
|
|
RETVAL_NEW_STR(bc_num2str_ex(result, scale));
|
2020-12-28 04:15:06 +08:00
|
|
|
|
|
|
|
cleanup: {
|
|
|
|
bc_free_num(&first);
|
|
|
|
bc_free_num(&second);
|
|
|
|
bc_free_num(&result);
|
2024-05-09 01:36:14 +08:00
|
|
|
BC_ARENA_TEARDOWN;
|
2020-12-28 04:15:06 +08:00
|
|
|
};
|
1999-04-22 07:28:00 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2020-07-01 21:32:55 +08:00
|
|
|
/* {{{ Returns the quotient of two arbitrary precision numbers (division) */
|
1999-07-27 04:09:08 +08:00
|
|
|
PHP_FUNCTION(bcdiv)
|
1999-04-22 07:28:00 +08:00
|
|
|
{
|
2016-12-28 15:35:38 +08:00
|
|
|
zend_string *left, *right;
|
2020-05-02 02:49:47 +08:00
|
|
|
zend_long scale_param;
|
2021-01-15 19:30:54 +08:00
|
|
|
bool scale_param_is_null = 1;
|
2024-05-01 23:52:06 +08:00
|
|
|
bc_num first = NULL, second = NULL, result;
|
2020-04-25 07:18:09 +08:00
|
|
|
int scale = BCG(bc_precision);
|
2008-06-25 00:01:32 +08:00
|
|
|
|
2016-12-28 15:44:18 +08:00
|
|
|
ZEND_PARSE_PARAMETERS_START(2, 3)
|
|
|
|
Z_PARAM_STR(left)
|
|
|
|
Z_PARAM_STR(right)
|
|
|
|
Z_PARAM_OPTIONAL
|
2020-05-02 02:49:47 +08:00
|
|
|
Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
|
2016-12-28 15:44:18 +08:00
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2020-05-02 02:49:47 +08:00
|
|
|
if (scale_param_is_null) {
|
|
|
|
scale = BCG(bc_precision);
|
2024-09-07 03:19:24 +08:00
|
|
|
} else if (bcmath_check_scale(scale_param, 3) == FAILURE) {
|
2020-05-02 02:49:47 +08:00
|
|
|
RETURN_THROWS();
|
|
|
|
} else {
|
2020-04-25 07:18:09 +08:00
|
|
|
scale = (int) scale_param;
|
1999-04-22 07:28:00 +08:00
|
|
|
}
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2024-05-09 01:36:14 +08:00
|
|
|
BC_ARENA_SETUP;
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
bc_init_num(&result);
|
2020-12-28 04:15:06 +08:00
|
|
|
|
2024-05-03 23:34:38 +08:00
|
|
|
if (php_str2num(&first, left) == FAILURE) {
|
2020-12-28 04:15:06 +08:00
|
|
|
zend_argument_value_error(1, "is not well-formed");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2024-05-03 23:34:38 +08:00
|
|
|
if (php_str2num(&second, right) == FAILURE) {
|
2020-12-28 04:15:06 +08:00
|
|
|
zend_argument_value_error(2, "is not well-formed");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2008-06-25 00:01:32 +08:00
|
|
|
|
2023-07-24 23:42:34 +08:00
|
|
|
if (!bc_divide(first, second, &result, scale)) {
|
|
|
|
zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Division by zero");
|
|
|
|
goto cleanup;
|
1999-04-22 07:28:00 +08:00
|
|
|
}
|
2008-06-25 00:01:32 +08:00
|
|
|
|
2024-05-02 00:24:57 +08:00
|
|
|
RETVAL_NEW_STR(bc_num2str_ex(result, scale));
|
2023-07-24 23:42:34 +08:00
|
|
|
|
2020-12-28 04:15:06 +08:00
|
|
|
cleanup: {
|
|
|
|
bc_free_num(&first);
|
|
|
|
bc_free_num(&second);
|
|
|
|
bc_free_num(&result);
|
2024-05-09 01:36:14 +08:00
|
|
|
BC_ARENA_TEARDOWN;
|
2020-12-28 04:15:06 +08:00
|
|
|
};
|
1999-04-22 07:28:00 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2020-07-01 21:32:55 +08:00
|
|
|
/* {{{ Returns the modulus of the two arbitrary precision operands */
|
1999-07-27 04:09:08 +08:00
|
|
|
PHP_FUNCTION(bcmod)
|
1999-04-22 07:28:00 +08:00
|
|
|
{
|
2016-12-28 15:35:38 +08:00
|
|
|
zend_string *left, *right;
|
2020-05-02 02:49:47 +08:00
|
|
|
zend_long scale_param;
|
2021-01-15 19:30:54 +08:00
|
|
|
bool scale_param_is_null = 1;
|
2024-05-01 23:52:06 +08:00
|
|
|
bc_num first = NULL, second = NULL, result;
|
2020-04-25 07:18:09 +08:00
|
|
|
int scale = BCG(bc_precision);
|
1999-04-22 07:28:00 +08:00
|
|
|
|
2017-09-09 21:43:02 +08:00
|
|
|
ZEND_PARSE_PARAMETERS_START(2, 3)
|
2016-12-28 15:44:18 +08:00
|
|
|
Z_PARAM_STR(left)
|
|
|
|
Z_PARAM_STR(right)
|
2017-09-09 21:43:02 +08:00
|
|
|
Z_PARAM_OPTIONAL
|
2020-05-02 02:49:47 +08:00
|
|
|
Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
|
2016-12-28 15:44:18 +08:00
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2020-05-02 02:49:47 +08:00
|
|
|
if (scale_param_is_null) {
|
|
|
|
scale = BCG(bc_precision);
|
2024-09-07 03:19:24 +08:00
|
|
|
} else if (bcmath_check_scale(scale_param, 3) == FAILURE) {
|
2020-05-02 02:49:47 +08:00
|
|
|
RETURN_THROWS();
|
|
|
|
} else {
|
2020-04-25 07:18:09 +08:00
|
|
|
scale = (int) scale_param;
|
2017-09-09 21:43:02 +08:00
|
|
|
}
|
|
|
|
|
2024-05-09 01:36:14 +08:00
|
|
|
BC_ARENA_SETUP;
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
bc_init_num(&result);
|
2020-12-28 04:15:06 +08:00
|
|
|
|
2024-05-03 23:34:38 +08:00
|
|
|
if (php_str2num(&first, left) == FAILURE) {
|
2020-12-28 04:15:06 +08:00
|
|
|
zend_argument_value_error(1, "is not well-formed");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2024-05-03 23:34:38 +08:00
|
|
|
if (php_str2num(&second, right) == FAILURE) {
|
2020-12-28 04:15:06 +08:00
|
|
|
zend_argument_value_error(2, "is not well-formed");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2023-07-24 23:42:34 +08:00
|
|
|
if (!bc_modulo(first, second, &result, scale)) {
|
|
|
|
zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero");
|
|
|
|
goto cleanup;
|
1999-04-22 07:28:00 +08:00
|
|
|
}
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2024-05-02 00:24:57 +08:00
|
|
|
RETVAL_NEW_STR(bc_num2str_ex(result, scale));
|
2023-07-24 23:42:34 +08:00
|
|
|
|
2020-12-28 04:15:06 +08:00
|
|
|
cleanup: {
|
|
|
|
bc_free_num(&first);
|
|
|
|
bc_free_num(&second);
|
|
|
|
bc_free_num(&result);
|
2024-05-09 01:36:14 +08:00
|
|
|
BC_ARENA_TEARDOWN;
|
2020-12-28 04:15:06 +08:00
|
|
|
};
|
1999-04-22 07:28:00 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2024-09-23 05:43:11 +08:00
|
|
|
PHP_FUNCTION(bcdivmod)
|
|
|
|
{
|
|
|
|
zend_string *left, *right;
|
|
|
|
zend_long scale_param;
|
|
|
|
bool scale_param_is_null = 1;
|
|
|
|
bc_num first = NULL, second = NULL, quot = NULL, rem = NULL;
|
|
|
|
int scale = BCG(bc_precision);
|
|
|
|
|
|
|
|
ZEND_PARSE_PARAMETERS_START(2, 3)
|
|
|
|
Z_PARAM_STR(left)
|
|
|
|
Z_PARAM_STR(right)
|
|
|
|
Z_PARAM_OPTIONAL
|
|
|
|
Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
|
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
|
|
|
|
|
|
if (scale_param_is_null) {
|
|
|
|
scale = BCG(bc_precision);
|
|
|
|
} else if (bcmath_check_scale(scale_param, 3) == FAILURE) {
|
|
|
|
RETURN_THROWS();
|
|
|
|
} else {
|
|
|
|
scale = (int) scale_param;
|
|
|
|
}
|
|
|
|
|
|
|
|
BC_ARENA_SETUP;
|
|
|
|
|
|
|
|
if (php_str2num(&first, left) == FAILURE) {
|
|
|
|
zend_argument_value_error(1, "is not well-formed");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (php_str2num(&second, right) == FAILURE) {
|
|
|
|
zend_argument_value_error(2, "is not well-formed");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bc_divmod(first, second, ", &rem, scale)) {
|
|
|
|
zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Division by zero");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
zval z_quot, z_rem;
|
|
|
|
ZVAL_STR(&z_quot, bc_num2str_ex(quot, 0));
|
|
|
|
ZVAL_STR(&z_rem, bc_num2str_ex(rem, scale));
|
|
|
|
|
|
|
|
RETVAL_ARR(zend_new_pair(&z_quot, &z_rem));
|
|
|
|
|
|
|
|
cleanup: {
|
|
|
|
bc_free_num(&first);
|
|
|
|
bc_free_num(&second);
|
|
|
|
bc_free_num(");
|
|
|
|
bc_free_num(&rem);
|
|
|
|
BC_ARENA_TEARDOWN;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-07-01 21:32:55 +08:00
|
|
|
/* {{{ Returns the value of an arbitrary precision number raised to the power of another reduced by a modulus */
|
2002-12-12 00:28:16 +08:00
|
|
|
PHP_FUNCTION(bcpowmod)
|
2002-12-11 03:04:27 +08:00
|
|
|
{
|
2023-07-24 23:42:34 +08:00
|
|
|
zend_string *base_str, *exponent_str, *modulus_str;
|
2020-05-02 02:49:47 +08:00
|
|
|
zend_long scale_param;
|
2021-01-15 19:30:54 +08:00
|
|
|
bool scale_param_is_null = 1;
|
2024-05-01 23:52:06 +08:00
|
|
|
bc_num bc_base = NULL, bc_expo = NULL, bc_modulus = NULL, result;
|
2020-04-25 07:18:09 +08:00
|
|
|
int scale = BCG(bc_precision);
|
2002-12-11 03:04:27 +08:00
|
|
|
|
2016-12-28 15:44:18 +08:00
|
|
|
ZEND_PARSE_PARAMETERS_START(3, 4)
|
2023-07-24 23:42:34 +08:00
|
|
|
Z_PARAM_STR(base_str)
|
|
|
|
Z_PARAM_STR(exponent_str)
|
|
|
|
Z_PARAM_STR(modulus_str)
|
2016-12-28 15:44:18 +08:00
|
|
|
Z_PARAM_OPTIONAL
|
2020-05-02 02:49:47 +08:00
|
|
|
Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
|
2016-12-28 15:44:18 +08:00
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
2002-12-11 03:04:27 +08:00
|
|
|
|
2020-05-02 02:49:47 +08:00
|
|
|
if (scale_param_is_null) {
|
|
|
|
scale = BCG(bc_precision);
|
2024-09-07 03:19:24 +08:00
|
|
|
} else if (bcmath_check_scale(scale_param, 4) == FAILURE) {
|
2020-05-02 02:49:47 +08:00
|
|
|
RETURN_THROWS();
|
|
|
|
} else {
|
2020-04-25 07:18:09 +08:00
|
|
|
scale = (int) scale_param;
|
|
|
|
}
|
|
|
|
|
2024-05-09 01:36:14 +08:00
|
|
|
BC_ARENA_SETUP;
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
bc_init_num(&result);
|
2020-12-28 04:15:06 +08:00
|
|
|
|
2024-05-03 23:34:38 +08:00
|
|
|
if (php_str2num(&bc_base, base_str) == FAILURE) {
|
2020-12-28 04:15:06 +08:00
|
|
|
zend_argument_value_error(1, "is not well-formed");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2024-05-03 23:34:38 +08:00
|
|
|
if (php_str2num(&bc_expo, exponent_str) == FAILURE) {
|
2020-12-28 04:15:06 +08:00
|
|
|
zend_argument_value_error(2, "is not well-formed");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2024-05-03 23:34:38 +08:00
|
|
|
if (php_str2num(&bc_modulus, modulus_str) == FAILURE) {
|
2020-12-28 04:15:06 +08:00
|
|
|
zend_argument_value_error(3, "is not well-formed");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2007-06-07 06:09:25 +08:00
|
|
|
|
2023-07-24 23:42:34 +08:00
|
|
|
raise_mod_status status = bc_raisemod(bc_base, bc_expo, bc_modulus, &result, scale);
|
|
|
|
switch (status) {
|
|
|
|
case BASE_HAS_FRACTIONAL:
|
|
|
|
zend_argument_value_error(1, "cannot have a fractional part");
|
|
|
|
goto cleanup;
|
|
|
|
case EXPO_HAS_FRACTIONAL:
|
|
|
|
zend_argument_value_error(2, "cannot have a fractional part");
|
|
|
|
goto cleanup;
|
|
|
|
case EXPO_IS_NEGATIVE:
|
|
|
|
zend_argument_value_error(2, "must be greater than or equal to 0");
|
|
|
|
goto cleanup;
|
|
|
|
case MOD_HAS_FRACTIONAL:
|
|
|
|
zend_argument_value_error(3, "cannot have a fractional part");
|
|
|
|
goto cleanup;
|
|
|
|
case MOD_IS_ZERO:
|
|
|
|
zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero");
|
|
|
|
goto cleanup;
|
|
|
|
case OK:
|
2024-05-02 00:24:57 +08:00
|
|
|
RETVAL_NEW_STR(bc_num2str_ex(result, scale));
|
2023-07-24 23:42:34 +08:00
|
|
|
break;
|
|
|
|
EMPTY_SWITCH_DEFAULT_CASE();
|
2003-03-20 08:22:57 +08:00
|
|
|
}
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2020-12-28 04:15:06 +08:00
|
|
|
cleanup: {
|
2023-07-24 23:42:34 +08:00
|
|
|
bc_free_num(&bc_base);
|
|
|
|
bc_free_num(&bc_expo);
|
|
|
|
bc_free_num(&bc_modulus);
|
2020-12-28 04:15:06 +08:00
|
|
|
bc_free_num(&result);
|
2024-05-09 01:36:14 +08:00
|
|
|
BC_ARENA_TEARDOWN;
|
2020-12-28 04:15:06 +08:00
|
|
|
};
|
2002-12-11 03:04:27 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2020-07-01 21:32:55 +08:00
|
|
|
/* {{{ Returns the value of an arbitrary precision number raised to the power of another */
|
1999-07-27 04:09:08 +08:00
|
|
|
PHP_FUNCTION(bcpow)
|
1999-04-22 07:28:00 +08:00
|
|
|
{
|
2023-07-24 23:42:34 +08:00
|
|
|
zend_string *base_str, *exponent_str;
|
2020-05-02 02:49:47 +08:00
|
|
|
zend_long scale_param;
|
2021-01-15 19:30:54 +08:00
|
|
|
bool scale_param_is_null = 1;
|
2024-05-01 23:52:06 +08:00
|
|
|
bc_num first = NULL, bc_exponent = NULL, result;
|
2020-04-25 07:18:09 +08:00
|
|
|
int scale = BCG(bc_precision);
|
2008-06-25 00:01:32 +08:00
|
|
|
|
2016-12-28 15:44:18 +08:00
|
|
|
ZEND_PARSE_PARAMETERS_START(2, 3)
|
2023-07-24 23:42:34 +08:00
|
|
|
Z_PARAM_STR(base_str)
|
|
|
|
Z_PARAM_STR(exponent_str)
|
2016-12-28 15:44:18 +08:00
|
|
|
Z_PARAM_OPTIONAL
|
2020-05-02 02:49:47 +08:00
|
|
|
Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
|
2016-12-28 15:44:18 +08:00
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2020-05-02 02:49:47 +08:00
|
|
|
if (scale_param_is_null) {
|
|
|
|
scale = BCG(bc_precision);
|
2024-09-07 03:19:24 +08:00
|
|
|
} else if (bcmath_check_scale(scale_param, 3) == FAILURE) {
|
2020-05-02 02:49:47 +08:00
|
|
|
RETURN_THROWS();
|
|
|
|
} else {
|
2020-04-25 07:18:09 +08:00
|
|
|
scale = (int) scale_param;
|
1999-04-22 07:28:00 +08:00
|
|
|
}
|
2008-06-25 00:01:32 +08:00
|
|
|
|
2024-05-09 01:36:14 +08:00
|
|
|
BC_ARENA_SETUP;
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
bc_init_num(&result);
|
2020-12-28 04:15:06 +08:00
|
|
|
|
2024-05-03 23:34:38 +08:00
|
|
|
if (php_str2num(&first, base_str) == FAILURE) {
|
2020-12-28 04:15:06 +08:00
|
|
|
zend_argument_value_error(1, "is not well-formed");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2024-05-03 23:34:38 +08:00
|
|
|
if (php_str2num(&bc_exponent, exponent_str) == FAILURE) {
|
2020-12-28 04:15:06 +08:00
|
|
|
zend_argument_value_error(2, "is not well-formed");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2023-07-24 23:42:34 +08:00
|
|
|
/* Check the exponent for scale digits and convert to a long. */
|
|
|
|
if (bc_exponent->n_scale != 0) {
|
|
|
|
zend_argument_value_error(2, "cannot have a fractional part");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
long exponent = bc_num2long(bc_exponent);
|
|
|
|
if (exponent == 0 && (bc_exponent->n_len > 1 || bc_exponent->n_value[0] != 0)) {
|
|
|
|
zend_argument_value_error(2, "is too large");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2024-11-19 23:16:03 +08:00
|
|
|
if (!bc_raise(first, exponent, &result, scale)) {
|
|
|
|
zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Negative power of zero");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2008-06-25 00:01:32 +08:00
|
|
|
|
2024-05-02 00:24:57 +08:00
|
|
|
RETVAL_NEW_STR(bc_num2str_ex(result, scale));
|
2020-12-28 04:15:06 +08:00
|
|
|
|
|
|
|
cleanup: {
|
|
|
|
bc_free_num(&first);
|
2023-07-24 23:42:34 +08:00
|
|
|
bc_free_num(&bc_exponent);
|
2020-12-28 04:15:06 +08:00
|
|
|
bc_free_num(&result);
|
2024-05-09 01:36:14 +08:00
|
|
|
BC_ARENA_TEARDOWN;
|
2020-12-28 04:15:06 +08:00
|
|
|
};
|
1999-04-22 07:28:00 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2020-07-01 21:32:55 +08:00
|
|
|
/* {{{ Returns the square root of an arbitrary precision number */
|
1999-07-27 04:09:08 +08:00
|
|
|
PHP_FUNCTION(bcsqrt)
|
1999-04-22 07:28:00 +08:00
|
|
|
{
|
2016-12-28 15:35:38 +08:00
|
|
|
zend_string *left;
|
2020-05-02 02:49:47 +08:00
|
|
|
zend_long scale_param;
|
2021-01-15 19:30:54 +08:00
|
|
|
bool scale_param_is_null = 1;
|
2024-05-01 23:52:06 +08:00
|
|
|
bc_num result = NULL;
|
2020-04-25 07:18:09 +08:00
|
|
|
int scale = BCG(bc_precision);
|
2008-06-25 00:01:32 +08:00
|
|
|
|
2016-12-28 15:44:18 +08:00
|
|
|
ZEND_PARSE_PARAMETERS_START(1, 2)
|
|
|
|
Z_PARAM_STR(left)
|
|
|
|
Z_PARAM_OPTIONAL
|
2020-05-02 02:49:47 +08:00
|
|
|
Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
|
2016-12-28 15:44:18 +08:00
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2020-05-02 02:49:47 +08:00
|
|
|
if (scale_param_is_null) {
|
|
|
|
scale = BCG(bc_precision);
|
2024-09-07 03:19:24 +08:00
|
|
|
} else if (bcmath_check_scale(scale_param, 2) == FAILURE) {
|
2020-05-02 02:49:47 +08:00
|
|
|
RETURN_THROWS();
|
|
|
|
} else {
|
2020-04-25 07:18:09 +08:00
|
|
|
scale = (int) scale_param;
|
1999-04-22 07:28:00 +08:00
|
|
|
}
|
2008-06-25 00:01:32 +08:00
|
|
|
|
2024-05-09 01:36:14 +08:00
|
|
|
BC_ARENA_SETUP;
|
|
|
|
|
2024-05-03 23:34:38 +08:00
|
|
|
if (php_str2num(&result, left) == FAILURE) {
|
2020-12-28 04:15:06 +08:00
|
|
|
zend_argument_value_error(1, "is not well-formed");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
if (bc_sqrt (&result, scale) != 0) {
|
2024-05-02 00:24:57 +08:00
|
|
|
RETVAL_NEW_STR(bc_num2str_ex(result, scale));
|
1999-04-22 07:28:00 +08:00
|
|
|
} else {
|
2020-09-11 23:40:06 +08:00
|
|
|
zend_argument_value_error(1, "must be greater than or equal to 0");
|
1999-04-22 07:28:00 +08:00
|
|
|
}
|
2008-06-25 00:01:32 +08:00
|
|
|
|
2020-12-28 04:15:06 +08:00
|
|
|
cleanup: {
|
|
|
|
bc_free_num(&result);
|
2024-05-09 01:36:14 +08:00
|
|
|
BC_ARENA_TEARDOWN;
|
2020-12-28 04:15:06 +08:00
|
|
|
};
|
1999-04-22 07:28:00 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2020-07-01 21:32:55 +08:00
|
|
|
/* {{{ Compares two arbitrary precision numbers */
|
1999-07-27 04:09:08 +08:00
|
|
|
PHP_FUNCTION(bccomp)
|
1999-04-22 07:28:00 +08:00
|
|
|
{
|
2016-12-28 15:35:38 +08:00
|
|
|
zend_string *left, *right;
|
2020-05-02 02:49:47 +08:00
|
|
|
zend_long scale_param;
|
2021-01-15 19:30:54 +08:00
|
|
|
bool scale_param_is_null = 1;
|
2024-05-01 23:52:06 +08:00
|
|
|
bc_num first = NULL, second = NULL;
|
2020-04-25 07:18:09 +08:00
|
|
|
int scale = BCG(bc_precision);
|
2008-06-25 00:01:32 +08:00
|
|
|
|
2016-12-28 15:44:18 +08:00
|
|
|
ZEND_PARSE_PARAMETERS_START(2, 3)
|
|
|
|
Z_PARAM_STR(left)
|
|
|
|
Z_PARAM_STR(right)
|
|
|
|
Z_PARAM_OPTIONAL
|
2020-05-02 02:49:47 +08:00
|
|
|
Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
|
2016-12-28 15:44:18 +08:00
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2020-05-02 02:49:47 +08:00
|
|
|
if (scale_param_is_null) {
|
|
|
|
scale = BCG(bc_precision);
|
2024-09-07 03:19:24 +08:00
|
|
|
} else if (bcmath_check_scale(scale_param, 3) == FAILURE) {
|
2020-05-02 02:49:47 +08:00
|
|
|
RETURN_THROWS();
|
|
|
|
} else {
|
2020-04-25 07:18:09 +08:00
|
|
|
scale = (int) scale_param;
|
1999-04-22 07:28:00 +08:00
|
|
|
}
|
|
|
|
|
2024-05-09 01:36:14 +08:00
|
|
|
BC_ARENA_SETUP;
|
|
|
|
|
2024-09-04 10:12:51 +08:00
|
|
|
if (!bc_str2num(&first, ZSTR_VAL(left), ZSTR_VAL(left) + ZSTR_LEN(left), scale, NULL, false)) {
|
2020-12-28 04:15:06 +08:00
|
|
|
zend_argument_value_error(1, "is not well-formed");
|
|
|
|
goto cleanup;
|
2019-04-30 22:33:04 +08:00
|
|
|
}
|
2020-12-28 04:15:06 +08:00
|
|
|
|
2024-09-04 10:12:51 +08:00
|
|
|
if (!bc_str2num(&second, ZSTR_VAL(right), ZSTR_VAL(right) + ZSTR_LEN(right), scale, NULL, false)) {
|
2020-12-28 04:15:06 +08:00
|
|
|
zend_argument_value_error(2, "is not well-formed");
|
|
|
|
goto cleanup;
|
2019-04-30 22:33:04 +08:00
|
|
|
}
|
2020-12-28 04:15:06 +08:00
|
|
|
|
2024-07-11 23:08:47 +08:00
|
|
|
RETVAL_LONG(bc_compare(first, second, scale));
|
1999-04-22 07:28:00 +08:00
|
|
|
|
2020-12-28 04:15:06 +08:00
|
|
|
cleanup: {
|
|
|
|
bc_free_num(&first);
|
|
|
|
bc_free_num(&second);
|
2024-05-09 01:36:14 +08:00
|
|
|
BC_ARENA_TEARDOWN;
|
2020-12-28 04:15:06 +08:00
|
|
|
};
|
1999-04-22 07:28:00 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2024-05-01 01:32:33 +08:00
|
|
|
/* {{{ floor or ceil */
|
|
|
|
static void bcfloor_or_bcceil(INTERNAL_FUNCTION_PARAMETERS, bool is_floor)
|
|
|
|
{
|
|
|
|
zend_string *numstr;
|
2024-05-02 03:23:39 +08:00
|
|
|
bc_num num = NULL, result = NULL;
|
2024-05-01 01:32:33 +08:00
|
|
|
|
|
|
|
ZEND_PARSE_PARAMETERS_START(1, 1)
|
|
|
|
Z_PARAM_STR(numstr)
|
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
|
|
|
2024-05-09 01:36:14 +08:00
|
|
|
BC_ARENA_SETUP;
|
|
|
|
|
2024-05-03 23:34:38 +08:00
|
|
|
if (php_str2num(&num, numstr) == FAILURE) {
|
2024-05-01 01:32:33 +08:00
|
|
|
zend_argument_value_error(1, "is not well-formed");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2024-05-02 03:23:39 +08:00
|
|
|
result = bc_floor_or_ceil(num, is_floor);
|
2024-05-02 00:24:57 +08:00
|
|
|
RETVAL_NEW_STR(bc_num2str_ex(result, 0));
|
2024-05-01 01:32:33 +08:00
|
|
|
|
|
|
|
cleanup: {
|
|
|
|
bc_free_num(&num);
|
|
|
|
bc_free_num(&result);
|
2024-05-09 01:36:14 +08:00
|
|
|
BC_ARENA_TEARDOWN;
|
2024-05-01 01:32:33 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ Returns floor of num */
|
|
|
|
PHP_FUNCTION(bcfloor)
|
|
|
|
{
|
|
|
|
bcfloor_or_bcceil(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ Returns ceil of num */
|
|
|
|
PHP_FUNCTION(bcceil)
|
|
|
|
{
|
|
|
|
bcfloor_or_bcceil(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ Returns num rounded to the digits specified by precision. */
|
|
|
|
PHP_FUNCTION(bcround)
|
|
|
|
{
|
|
|
|
zend_string *numstr;
|
|
|
|
zend_long precision = 0;
|
|
|
|
zend_long mode = PHP_ROUND_HALF_UP;
|
2024-07-19 02:44:30 +08:00
|
|
|
zend_object *mode_object = NULL;
|
2024-05-01 23:52:06 +08:00
|
|
|
bc_num num = NULL, result;
|
2024-05-01 01:32:33 +08:00
|
|
|
|
|
|
|
ZEND_PARSE_PARAMETERS_START(1, 3)
|
|
|
|
Z_PARAM_STR(numstr)
|
|
|
|
Z_PARAM_OPTIONAL
|
|
|
|
Z_PARAM_LONG(precision)
|
2024-07-19 02:44:30 +08:00
|
|
|
Z_PARAM_OBJ_OF_CLASS(mode_object, rounding_mode_ce)
|
2024-05-01 01:32:33 +08:00
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
|
|
|
2024-07-19 02:44:30 +08:00
|
|
|
if (mode_object != NULL) {
|
|
|
|
mode = php_math_round_mode_from_enum(mode_object);
|
|
|
|
}
|
|
|
|
|
2024-05-01 01:32:33 +08:00
|
|
|
switch (mode) {
|
|
|
|
case PHP_ROUND_HALF_UP:
|
|
|
|
case PHP_ROUND_HALF_DOWN:
|
|
|
|
case PHP_ROUND_HALF_EVEN:
|
|
|
|
case PHP_ROUND_HALF_ODD:
|
|
|
|
case PHP_ROUND_CEILING:
|
|
|
|
case PHP_ROUND_FLOOR:
|
|
|
|
case PHP_ROUND_TOWARD_ZERO:
|
|
|
|
case PHP_ROUND_AWAY_FROM_ZERO:
|
|
|
|
break;
|
|
|
|
default:
|
2024-07-19 02:44:30 +08:00
|
|
|
/* This is currently unreachable, but might become reachable when new modes are added. */
|
|
|
|
zend_argument_value_error(3, "is an unsupported rounding mode");
|
2024-05-01 01:32:33 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-09 01:36:14 +08:00
|
|
|
BC_ARENA_SETUP;
|
|
|
|
|
2024-05-01 01:32:33 +08:00
|
|
|
bc_init_num(&result);
|
|
|
|
|
2024-05-03 23:34:38 +08:00
|
|
|
if (php_str2num(&num, numstr) == FAILURE) {
|
2024-05-01 01:32:33 +08:00
|
|
|
zend_argument_value_error(1, "is not well-formed");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
bc_round(num, precision, mode, &result);
|
2024-05-02 00:24:57 +08:00
|
|
|
RETVAL_NEW_STR(bc_num2str_ex(result, result->n_scale));
|
2024-05-01 01:32:33 +08:00
|
|
|
|
|
|
|
cleanup: {
|
|
|
|
bc_free_num(&num);
|
|
|
|
bc_free_num(&result);
|
2024-05-09 01:36:14 +08:00
|
|
|
BC_ARENA_TEARDOWN;
|
2024-05-01 01:32:33 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2020-07-01 21:32:55 +08:00
|
|
|
/* {{{ Sets default scale parameter for all bc math functions */
|
1999-07-27 04:09:08 +08:00
|
|
|
PHP_FUNCTION(bcscale)
|
1999-04-22 07:28:00 +08:00
|
|
|
{
|
2017-09-06 20:28:51 +08:00
|
|
|
zend_long old_scale, new_scale;
|
2021-01-15 19:30:54 +08:00
|
|
|
bool new_scale_is_null = 1;
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2017-09-06 20:28:51 +08:00
|
|
|
ZEND_PARSE_PARAMETERS_START(0, 1)
|
|
|
|
Z_PARAM_OPTIONAL
|
2020-05-02 02:49:47 +08:00
|
|
|
Z_PARAM_LONG_OR_NULL(new_scale, new_scale_is_null)
|
2016-12-28 15:44:18 +08:00
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
2008-06-25 00:01:32 +08:00
|
|
|
|
2014-08-18 20:49:28 +08:00
|
|
|
old_scale = BCG(bc_precision);
|
|
|
|
|
2020-05-02 02:49:47 +08:00
|
|
|
if (!new_scale_is_null) {
|
2024-09-07 03:19:24 +08:00
|
|
|
if (bcmath_check_scale(new_scale, 1) == FAILURE) {
|
2020-04-25 07:18:09 +08:00
|
|
|
RETURN_THROWS();
|
|
|
|
}
|
2020-05-02 02:49:47 +08:00
|
|
|
|
2023-03-20 23:19:05 +08:00
|
|
|
zend_string *ini_name = ZSTR_INIT_LITERAL("bcmath.scale", 0);
|
2020-10-21 23:03:54 +08:00
|
|
|
zend_string *new_scale_str = zend_long_to_str(new_scale);
|
|
|
|
zend_alter_ini_entry(ini_name, new_scale_str, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
|
|
|
|
zend_string_release(new_scale_str);
|
|
|
|
zend_string_release(ini_name);
|
2014-08-18 20:49:28 +08:00
|
|
|
}
|
2003-04-03 07:51:52 +08:00
|
|
|
|
2014-08-18 20:49:28 +08:00
|
|
|
RETURN_LONG(old_scale);
|
1999-04-22 07:28:00 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
|
2024-09-04 10:12:51 +08:00
|
|
|
|
|
|
|
static zend_class_entry *bcmath_number_ce;
|
|
|
|
static zend_object_handlers bcmath_number_obj_handlers;
|
|
|
|
|
|
|
|
static zend_result bcmath_number_do_operation(uint8_t opcode, zval *ret_val, zval *op1, zval *op2);
|
|
|
|
static int bcmath_number_compare(zval *op1, zval *op2);
|
|
|
|
|
|
|
|
#if SIZEOF_SIZE_T >= 8
|
|
|
|
# define CHECK_RET_SCALE_OVERFLOW(scale, origin_scale) (scale > INT_MAX)
|
|
|
|
#else
|
|
|
|
# define CHECK_RET_SCALE_OVERFLOW(scale, origin_scale) (scale > INT_MAX || scale < origin_scale)
|
|
|
|
#endif
|
2024-09-22 05:59:06 +08:00
|
|
|
#define CHECK_SCALE_OVERFLOW(scale) (scale > INT_MAX)
|
2024-09-04 10:12:51 +08:00
|
|
|
|
|
|
|
static zend_always_inline bcmath_number_obj_t *get_bcmath_number_from_obj(const zend_object *obj)
|
|
|
|
{
|
|
|
|
return (bcmath_number_obj_t*)((char*)(obj) - XtOffsetOf(bcmath_number_obj_t, std));
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_always_inline bcmath_number_obj_t *get_bcmath_number_from_zval(const zval *zv)
|
|
|
|
{
|
|
|
|
return get_bcmath_number_from_obj(Z_OBJ_P(zv));
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_always_inline zend_string *bcmath_number_value_to_str(bcmath_number_obj_t *intern)
|
|
|
|
{
|
|
|
|
if (intern->value == NULL) {
|
|
|
|
intern->value = bc_num2str_ex(intern->num, intern->scale);
|
|
|
|
}
|
|
|
|
return intern->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_object *bcmath_number_create(zend_class_entry *ce)
|
|
|
|
{
|
|
|
|
bcmath_number_obj_t *intern = zend_object_alloc(sizeof(bcmath_number_obj_t), ce);
|
|
|
|
|
|
|
|
zend_object_std_init(&intern->std, ce);
|
|
|
|
object_properties_init(&intern->std, ce);
|
|
|
|
|
|
|
|
intern->scale = 1;
|
|
|
|
|
|
|
|
return &intern->std;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bcmath_number_free(zend_object *obj)
|
|
|
|
{
|
|
|
|
bcmath_number_obj_t *intern = get_bcmath_number_from_obj(obj);
|
|
|
|
if (intern->num) {
|
|
|
|
bc_free_num(&intern->num);
|
|
|
|
intern->num = NULL;
|
|
|
|
}
|
|
|
|
if (intern->value) {
|
|
|
|
zend_string_release(intern->value);
|
|
|
|
intern->value = NULL;
|
|
|
|
}
|
|
|
|
zend_object_std_dtor(&intern->std);
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_object *bcmath_number_clone(zend_object *obj)
|
|
|
|
{
|
|
|
|
bcmath_number_obj_t *original = get_bcmath_number_from_obj(obj);
|
|
|
|
bcmath_number_obj_t *clone = get_bcmath_number_from_obj(bcmath_number_create(bcmath_number_ce));
|
|
|
|
|
|
|
|
clone->num = bc_copy_num(original->num);
|
|
|
|
if (original->value) {
|
|
|
|
clone->value = zend_string_copy(original->value);
|
|
|
|
}
|
|
|
|
clone->scale = original->scale;
|
|
|
|
|
|
|
|
return &clone->std;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HashTable *bcmath_number_get_properties_for(zend_object *obj, zend_prop_purpose purpose)
|
|
|
|
{
|
|
|
|
zval zv;
|
|
|
|
bcmath_number_obj_t *intern = get_bcmath_number_from_obj(obj);
|
|
|
|
HashTable *props = zend_array_dup(zend_std_get_properties(obj));
|
|
|
|
|
|
|
|
ZVAL_STR_COPY(&zv, bcmath_number_value_to_str(intern));
|
|
|
|
zend_hash_update(props, ZSTR_KNOWN(ZEND_STR_VALUE), &zv);
|
|
|
|
ZVAL_LONG(&zv, intern->scale);
|
|
|
|
zend_hash_str_update(props, ZEND_STRL("scale"), &zv);
|
|
|
|
|
|
|
|
return props;
|
|
|
|
}
|
|
|
|
|
|
|
|
static zval *bcmath_number_write_property(zend_object *obj, zend_string *name, zval *value, void **cache_slot)
|
|
|
|
{
|
|
|
|
if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_VALUE)) || zend_string_equals_literal(name, "scale")) {
|
|
|
|
zend_readonly_property_modification_error_ex(ZSTR_VAL(obj->ce->name), ZSTR_VAL(name));
|
|
|
|
return &EG(error_zval);
|
|
|
|
}
|
|
|
|
|
|
|
|
return zend_std_write_property(obj, name, value, cache_slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bcmath_number_unset_property(zend_object *obj, zend_string *name, void **cache_slot)
|
|
|
|
{
|
|
|
|
if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_VALUE)) || zend_string_equals_literal(name, "scale")) {
|
|
|
|
zend_throw_error(NULL, "Cannot unset readonly property %s::$%s", ZSTR_VAL(obj->ce->name), ZSTR_VAL(name));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
zend_std_unset_property(obj, name, cache_slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
static zval *bcmath_number_read_property(zend_object *obj, zend_string *name, int type, void **cache_slot, zval *rv)
|
|
|
|
{
|
|
|
|
bcmath_number_obj_t *intern = get_bcmath_number_from_obj(obj);
|
|
|
|
|
|
|
|
if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_VALUE))) {
|
|
|
|
ZVAL_STR_COPY(rv, bcmath_number_value_to_str(intern));
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (zend_string_equals_literal(name, "scale")) {
|
|
|
|
ZVAL_LONG(rv, intern->scale);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return zend_std_read_property(obj, name, type, cache_slot, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bcmath_number_has_property(zend_object *obj, zend_string *name, int check_empty, void **cache_slot)
|
|
|
|
{
|
|
|
|
if (check_empty == ZEND_PROPERTY_NOT_EMPTY) {
|
|
|
|
bcmath_number_obj_t *intern = get_bcmath_number_from_obj(obj);
|
|
|
|
|
|
|
|
if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_VALUE))) {
|
|
|
|
return !bc_is_zero(intern->num);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (zend_string_equals_literal(name, "scale")) {
|
|
|
|
return intern->scale != 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_VALUE)) || zend_string_equals_literal(name, "scale");
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_result bcmath_number_cast_object(zend_object *obj, zval *ret, int type)
|
|
|
|
{
|
|
|
|
if (type == _IS_BOOL) {
|
|
|
|
bcmath_number_obj_t *intern = get_bcmath_number_from_obj(obj);
|
|
|
|
ZVAL_BOOL(ret, !bc_is_zero(intern->num));
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return zend_std_cast_object_tostring(obj, ret, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bcmath_number_register_class(void)
|
|
|
|
{
|
|
|
|
bcmath_number_ce = register_class_BcMath_Number(zend_ce_stringable);
|
|
|
|
bcmath_number_ce->create_object = bcmath_number_create;
|
|
|
|
bcmath_number_ce->default_object_handlers = &bcmath_number_obj_handlers;
|
|
|
|
|
|
|
|
memcpy(&bcmath_number_obj_handlers, &std_object_handlers, sizeof(zend_object_handlers));
|
|
|
|
bcmath_number_obj_handlers.offset = XtOffsetOf(bcmath_number_obj_t, std);
|
|
|
|
bcmath_number_obj_handlers.free_obj = bcmath_number_free;
|
|
|
|
bcmath_number_obj_handlers.clone_obj = bcmath_number_clone;
|
|
|
|
bcmath_number_obj_handlers.do_operation = bcmath_number_do_operation;
|
|
|
|
bcmath_number_obj_handlers.compare = bcmath_number_compare;
|
|
|
|
bcmath_number_obj_handlers.write_property = bcmath_number_write_property;
|
|
|
|
bcmath_number_obj_handlers.unset_property = bcmath_number_unset_property;
|
|
|
|
bcmath_number_obj_handlers.has_property = bcmath_number_has_property;
|
|
|
|
bcmath_number_obj_handlers.read_property = bcmath_number_read_property;
|
|
|
|
bcmath_number_obj_handlers.get_properties_for = bcmath_number_get_properties_for;
|
|
|
|
bcmath_number_obj_handlers.cast_object = bcmath_number_cast_object;
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_always_inline void bcmath_number_add_internal(
|
|
|
|
bc_num n1, bc_num n2, bc_num *ret,
|
|
|
|
size_t n1_full_scale, size_t n2_full_scale, size_t *scale, bool auto_scale
|
|
|
|
) {
|
|
|
|
if (auto_scale) {
|
|
|
|
*scale = MAX(n1_full_scale, n2_full_scale);
|
|
|
|
}
|
|
|
|
*ret = bc_add(n1, n2, *scale);
|
|
|
|
(*ret)->n_scale = MIN(*scale, (*ret)->n_scale);
|
|
|
|
bc_rm_trailing_zeros(*ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_always_inline void bcmath_number_sub_internal(
|
|
|
|
bc_num n1, bc_num n2, bc_num *ret,
|
|
|
|
size_t n1_full_scale, size_t n2_full_scale, size_t *scale, bool auto_scale
|
|
|
|
) {
|
|
|
|
if (auto_scale) {
|
|
|
|
*scale = MAX(n1_full_scale, n2_full_scale);
|
|
|
|
}
|
|
|
|
*ret = bc_sub(n1, n2, *scale);
|
|
|
|
(*ret)->n_scale = MIN(*scale, (*ret)->n_scale);
|
|
|
|
bc_rm_trailing_zeros(*ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_always_inline zend_result bcmath_number_mul_internal(
|
|
|
|
bc_num n1, bc_num n2, bc_num *ret,
|
|
|
|
size_t n1_full_scale, size_t n2_full_scale, size_t *scale, bool auto_scale
|
|
|
|
) {
|
|
|
|
if (auto_scale) {
|
|
|
|
*scale = n1_full_scale + n2_full_scale;
|
|
|
|
if (UNEXPECTED(CHECK_RET_SCALE_OVERFLOW(*scale, n1_full_scale))) {
|
|
|
|
zend_value_error("scale of the result is too large");
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*ret = bc_multiply(n1, n2, *scale);
|
|
|
|
(*ret)->n_scale = MIN(*scale, (*ret)->n_scale);
|
|
|
|
bc_rm_trailing_zeros(*ret);
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_always_inline zend_result bcmath_number_div_internal(
|
|
|
|
bc_num n1, bc_num n2, bc_num *ret,
|
|
|
|
size_t n1_full_scale, size_t *scale, bool auto_scale
|
|
|
|
) {
|
|
|
|
if (auto_scale) {
|
|
|
|
*scale = n1_full_scale + BC_MATH_NUMBER_EXPAND_SCALE;
|
|
|
|
if (UNEXPECTED(CHECK_RET_SCALE_OVERFLOW(*scale, n1_full_scale))) {
|
|
|
|
zend_value_error("scale of the result is too large");
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!bc_divide(n1, n2, ret, *scale)) {
|
|
|
|
zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Division by zero");
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
bc_rm_trailing_zeros(*ret);
|
|
|
|
if (auto_scale) {
|
|
|
|
size_t diff = *scale - (*ret)->n_scale;
|
|
|
|
*scale -= diff > BC_MATH_NUMBER_EXPAND_SCALE ? BC_MATH_NUMBER_EXPAND_SCALE : diff;
|
|
|
|
}
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_always_inline zend_result bcmath_number_mod_internal(
|
|
|
|
bc_num n1, bc_num n2, bc_num *ret,
|
|
|
|
size_t n1_full_scale, size_t n2_full_scale, size_t *scale, bool auto_scale
|
|
|
|
) {
|
|
|
|
if (auto_scale) {
|
|
|
|
*scale = MAX(n1_full_scale, n2_full_scale);
|
|
|
|
}
|
|
|
|
if (!bc_modulo(n1, n2, ret, *scale)) {
|
|
|
|
zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero");
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
bc_rm_trailing_zeros(*ret);
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_result bcmath_number_pow_internal(
|
|
|
|
bc_num n1, bc_num n2, bc_num *ret,
|
|
|
|
size_t n1_full_scale, size_t *scale, bool auto_scale, bool is_op
|
|
|
|
) {
|
|
|
|
/* Check the exponent for scale digits and convert to a long. */
|
|
|
|
if (UNEXPECTED(n2->n_scale != 0)) {
|
|
|
|
if (is_op) {
|
|
|
|
zend_value_error("exponent cannot have a fractional part");
|
|
|
|
} else {
|
|
|
|
zend_argument_value_error(1, "exponent cannot have a fractional part");
|
|
|
|
}
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
long exponent = bc_num2long(n2);
|
|
|
|
|
|
|
|
bool scale_expand = false;
|
|
|
|
if (auto_scale) {
|
|
|
|
if (exponent > 0) {
|
|
|
|
*scale = n1_full_scale * exponent;
|
|
|
|
if (UNEXPECTED(*scale > INT_MAX || *scale < n1_full_scale)) {
|
|
|
|
zend_value_error("scale of the result is too large");
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
} else if (exponent < 0) {
|
|
|
|
*scale = n1_full_scale + BC_MATH_NUMBER_EXPAND_SCALE;
|
|
|
|
if (UNEXPECTED(CHECK_RET_SCALE_OVERFLOW(*scale, n1_full_scale))) {
|
|
|
|
zend_value_error("scale of the result is too large");
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
scale_expand = true;
|
|
|
|
} else {
|
|
|
|
*scale = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bc_num2long() returns 0 if exponent is too large.
|
|
|
|
* Here, if n2->n_value is not 0 but exponent is 0, it is considered too large.
|
|
|
|
*/
|
|
|
|
if (UNEXPECTED(exponent == 0 && (n2->n_len > 1 || n2->n_value[0] != 0))) {
|
|
|
|
if (is_op) {
|
|
|
|
zend_value_error("exponent is too large");
|
|
|
|
} else {
|
|
|
|
zend_argument_value_error(1, "exponent is too large");
|
|
|
|
}
|
|
|
|
return FAILURE;
|
|
|
|
}
|
2024-11-19 23:16:03 +08:00
|
|
|
if (!bc_raise(n1, exponent, ret, *scale)) {
|
|
|
|
zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Negative power of zero");
|
|
|
|
return FAILURE;
|
|
|
|
}
|
2024-09-04 10:12:51 +08:00
|
|
|
bc_rm_trailing_zeros(*ret);
|
|
|
|
if (scale_expand) {
|
|
|
|
size_t diff = *scale - (*ret)->n_scale;
|
|
|
|
*scale -= diff > BC_MATH_NUMBER_EXPAND_SCALE ? BC_MATH_NUMBER_EXPAND_SCALE : diff;
|
|
|
|
}
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_always_inline bcmath_number_obj_t *bcmath_number_new_obj(bc_num ret, size_t scale)
|
|
|
|
{
|
|
|
|
bcmath_number_obj_t *intern = get_bcmath_number_from_obj(bcmath_number_create(bcmath_number_ce));
|
|
|
|
intern->num = ret;
|
|
|
|
intern->scale = scale;
|
|
|
|
return intern;
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_result bcmath_number_parse_num(zval *zv, zend_object **obj, zend_string **str, zend_long *lval)
|
|
|
|
{
|
|
|
|
if (Z_TYPE_P(zv) == IS_OBJECT && instanceof_function(Z_OBJCE_P(zv), bcmath_number_ce)) {
|
|
|
|
*obj = Z_OBJ_P(zv);
|
|
|
|
return SUCCESS;
|
|
|
|
} else {
|
|
|
|
switch (Z_TYPE_P(zv)) {
|
|
|
|
case IS_LONG:
|
|
|
|
*lval = Z_LVAL_P(zv);
|
|
|
|
return SUCCESS;
|
|
|
|
|
|
|
|
case IS_STRING:
|
|
|
|
*str = Z_STR_P(zv);
|
|
|
|
return SUCCESS;
|
|
|
|
|
|
|
|
case IS_NULL:
|
|
|
|
*lval = 0;
|
2024-09-23 18:49:33 +08:00
|
|
|
return FAILURE;
|
2024-09-04 10:12:51 +08:00
|
|
|
|
|
|
|
default:
|
2024-09-24 21:33:36 +08:00
|
|
|
return zend_parse_arg_long_slow(zv, lval, 1 /* dummy */) ? SUCCESS : FAILURE;
|
2024-09-04 10:12:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_result bc_num_from_obj_or_str_or_long(
|
|
|
|
bc_num *num, size_t *full_scale, const zend_object *obj, const zend_string *str, zend_long lval)
|
|
|
|
{
|
|
|
|
if (obj) {
|
|
|
|
bcmath_number_obj_t *intern = get_bcmath_number_from_obj(obj);
|
|
|
|
*num = intern->num;
|
|
|
|
if (full_scale) {
|
|
|
|
*full_scale = intern->scale;
|
|
|
|
}
|
|
|
|
return SUCCESS;
|
|
|
|
} else if (str) {
|
|
|
|
return php_str2num_ex(num, str, full_scale);
|
|
|
|
} else {
|
|
|
|
php_long2num(num, lval);
|
|
|
|
if (full_scale) {
|
|
|
|
*full_scale = 0;
|
|
|
|
}
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_result bcmath_number_do_operation(uint8_t opcode, zval *ret_val, zval *op1, zval *op2)
|
|
|
|
{
|
|
|
|
switch (opcode) {
|
|
|
|
case ZEND_ADD:
|
|
|
|
case ZEND_SUB:
|
|
|
|
case ZEND_MUL:
|
|
|
|
case ZEND_DIV:
|
|
|
|
case ZEND_MOD:
|
|
|
|
case ZEND_POW:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
zend_object *obj1 = NULL;
|
|
|
|
zend_string *str1 = NULL;
|
|
|
|
zend_long lval1 = 0;
|
|
|
|
|
|
|
|
zend_object *obj2 = NULL;
|
|
|
|
zend_string *str2 = NULL;
|
|
|
|
zend_long lval2 = 0;
|
|
|
|
|
|
|
|
if (UNEXPECTED(bcmath_number_parse_num(op1, &obj1, &str1, &lval1) == FAILURE || bcmath_number_parse_num(op2, &obj2, &str2, &lval2) == FAILURE)) {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bc_num n1 = NULL;
|
|
|
|
bc_num n2 = NULL;
|
|
|
|
size_t n1_full_scale;
|
|
|
|
size_t n2_full_scale;
|
2024-09-23 18:49:33 +08:00
|
|
|
if (UNEXPECTED(bc_num_from_obj_or_str_or_long(&n1, &n1_full_scale, obj1, str1, lval1) == FAILURE)) {
|
|
|
|
zend_value_error("Left string operand cannot be converted to BcMath\\Number");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (UNEXPECTED(bc_num_from_obj_or_str_or_long(&n2, &n2_full_scale, obj2, str2, lval2) == FAILURE)) {
|
|
|
|
zend_value_error("Right string operand cannot be converted to BcMath\\Number");
|
2024-09-04 10:12:51 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2024-09-22 05:59:06 +08:00
|
|
|
if (UNEXPECTED(CHECK_SCALE_OVERFLOW(n1_full_scale) || CHECK_SCALE_OVERFLOW(n2_full_scale))) {
|
|
|
|
zend_value_error("scale must be between 0 and %d", INT_MAX);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2024-09-04 10:12:51 +08:00
|
|
|
bc_num ret = NULL;
|
|
|
|
size_t scale;
|
|
|
|
switch (opcode) {
|
|
|
|
case ZEND_ADD:
|
|
|
|
bcmath_number_add_internal(n1, n2, &ret, n1_full_scale, n2_full_scale, &scale, true);
|
|
|
|
break;
|
|
|
|
case ZEND_SUB:
|
|
|
|
bcmath_number_sub_internal(n1, n2, &ret, n1_full_scale, n2_full_scale, &scale, true);
|
|
|
|
break;
|
|
|
|
case ZEND_MUL:
|
|
|
|
if (UNEXPECTED(bcmath_number_mul_internal(n1, n2, &ret, n1_full_scale, n2_full_scale, &scale, true) == FAILURE)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ZEND_DIV:
|
|
|
|
if (UNEXPECTED(bcmath_number_div_internal(n1, n2, &ret, n1_full_scale, &scale, true) == FAILURE)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ZEND_MOD:
|
|
|
|
if (UNEXPECTED(bcmath_number_mod_internal(n1, n2, &ret, n1_full_scale, n2_full_scale, &scale, true) == FAILURE)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ZEND_POW:
|
|
|
|
if (UNEXPECTED(bcmath_number_pow_internal(n1, n2, &ret, n1_full_scale, &scale, true, true) == FAILURE)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
EMPTY_SWITCH_DEFAULT_CASE();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Z_TYPE_P(op1) != IS_OBJECT) {
|
|
|
|
bc_free_num(&n1);
|
|
|
|
}
|
|
|
|
if (Z_TYPE_P(op2) != IS_OBJECT) {
|
|
|
|
bc_free_num(&n2);
|
|
|
|
}
|
|
|
|
|
|
|
|
bcmath_number_obj_t *intern = bcmath_number_new_obj(ret, scale);
|
|
|
|
|
|
|
|
/* For increment and decrement, etc */
|
|
|
|
if (ret_val == op1) {
|
|
|
|
zval_ptr_dtor(ret_val);
|
|
|
|
}
|
|
|
|
ZVAL_OBJ(ret_val, &intern->std);
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (Z_TYPE_P(op1) != IS_OBJECT) {
|
|
|
|
bc_free_num(&n1);
|
|
|
|
}
|
|
|
|
if (Z_TYPE_P(op2) != IS_OBJECT) {
|
|
|
|
bc_free_num(&n2);
|
|
|
|
}
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bcmath_number_compare(zval *op1, zval *op2)
|
|
|
|
{
|
|
|
|
zend_object *obj1 = NULL;
|
|
|
|
zend_string *str1 = NULL;
|
|
|
|
zend_long lval1 = 0;
|
|
|
|
|
|
|
|
zend_object *obj2 = NULL;
|
|
|
|
zend_string *str2 = NULL;
|
|
|
|
zend_long lval2 = 0;
|
|
|
|
|
|
|
|
bc_num n1 = NULL;
|
|
|
|
bc_num n2 = NULL;
|
|
|
|
|
2024-09-23 18:49:33 +08:00
|
|
|
int ret = ZEND_UNCOMPARABLE;
|
|
|
|
|
2024-09-04 10:12:51 +08:00
|
|
|
if (UNEXPECTED(bcmath_number_parse_num(op1, &obj1, &str1, &lval1) == FAILURE)) {
|
2024-09-23 18:49:33 +08:00
|
|
|
goto failure;
|
2024-09-04 10:12:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (UNEXPECTED(bcmath_number_parse_num(op2, &obj2, &str2, &lval2) == FAILURE)) {
|
2024-09-23 18:49:33 +08:00
|
|
|
goto failure;
|
2024-09-04 10:12:51 +08:00
|
|
|
}
|
|
|
|
|
2024-09-22 05:59:06 +08:00
|
|
|
size_t n1_full_scale;
|
|
|
|
size_t n2_full_scale;
|
|
|
|
if (UNEXPECTED(bc_num_from_obj_or_str_or_long(&n1, &n1_full_scale, obj1, str1, lval1) == FAILURE ||
|
|
|
|
bc_num_from_obj_or_str_or_long(&n2, &n2_full_scale, obj2, str2, lval2) == FAILURE)) {
|
2024-09-23 18:49:33 +08:00
|
|
|
goto failure;
|
2024-09-22 05:59:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (UNEXPECTED(CHECK_SCALE_OVERFLOW(n1_full_scale) || CHECK_SCALE_OVERFLOW(n2_full_scale))) {
|
|
|
|
zend_value_error("scale must be between 0 and %d", INT_MAX);
|
2024-09-23 18:49:33 +08:00
|
|
|
goto failure;
|
2024-09-04 10:12:51 +08:00
|
|
|
}
|
|
|
|
|
2024-09-23 18:49:33 +08:00
|
|
|
ret = bc_compare(n1, n2, MAX(n1->n_scale, n2->n_scale));
|
2024-09-04 10:12:51 +08:00
|
|
|
|
2024-09-23 18:49:33 +08:00
|
|
|
failure:
|
2024-09-04 10:12:51 +08:00
|
|
|
if (Z_TYPE_P(op1) != IS_OBJECT) {
|
|
|
|
bc_free_num(&n1);
|
|
|
|
}
|
|
|
|
if (Z_TYPE_P(op2) != IS_OBJECT) {
|
|
|
|
bc_free_num(&n2);
|
|
|
|
}
|
|
|
|
|
2024-09-23 18:49:33 +08:00
|
|
|
return ret;
|
2024-09-04 10:12:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#define BCMATH_PARAM_NUMBER_OR_STR_OR_LONG(dest_obj, ce, dest_str, dest_long) \
|
|
|
|
Z_PARAM_PROLOGUE(0, 0); \
|
|
|
|
if (UNEXPECTED(!(zend_parse_arg_obj(_arg, &(dest_obj), ce, 0) || \
|
|
|
|
zend_parse_arg_str_or_long(_arg, &(dest_str), &(dest_long), &_dummy, 0, _i)))) { \
|
|
|
|
zend_argument_type_error(_i, "must be of type int, string, or %s, %s given", \
|
|
|
|
ZSTR_VAL(bcmath_number_ce->name), zend_zval_value_name(_arg)); \
|
|
|
|
_error_code = ZPP_ERROR_FAILURE; \
|
|
|
|
break; \
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_always_inline zend_result bc_num_from_obj_or_str_or_long_with_err(
|
|
|
|
bc_num *num, size_t *scale, zend_object *obj, zend_string *str, zend_long lval, uint32_t arg_num)
|
|
|
|
{
|
2024-09-22 05:59:06 +08:00
|
|
|
size_t full_scale = 0;
|
|
|
|
if (UNEXPECTED(bc_num_from_obj_or_str_or_long(num, &full_scale, obj, str, lval) == FAILURE)) {
|
2024-09-04 10:12:51 +08:00
|
|
|
zend_argument_value_error(arg_num, "is not well-formed");
|
|
|
|
return FAILURE;
|
|
|
|
}
|
2024-09-22 05:59:06 +08:00
|
|
|
if (UNEXPECTED(CHECK_SCALE_OVERFLOW(full_scale))) {
|
|
|
|
zend_argument_value_error(arg_num, "must be between 0 and %d", INT_MAX);
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
if (scale != NULL) {
|
|
|
|
*scale = full_scale;
|
|
|
|
}
|
2024-09-04 10:12:51 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
PHP_METHOD(BcMath_Number, __construct)
|
|
|
|
{
|
|
|
|
zend_string *str = NULL;
|
|
|
|
zend_long lval = 0;
|
|
|
|
|
|
|
|
ZEND_PARSE_PARAMETERS_START(1, 1)
|
|
|
|
Z_PARAM_STR_OR_LONG(str, lval);
|
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
|
|
|
|
|
|
bcmath_number_obj_t *intern = get_bcmath_number_from_zval(ZEND_THIS);
|
|
|
|
if (UNEXPECTED(intern->num != NULL)) {
|
|
|
|
zend_readonly_property_modification_error_ex(ZSTR_VAL(bcmath_number_ce->name), "value");
|
|
|
|
RETURN_THROWS();
|
|
|
|
}
|
|
|
|
|
|
|
|
bc_num num = NULL;
|
2024-09-22 05:59:06 +08:00
|
|
|
size_t scale = 0;
|
2024-09-04 10:12:51 +08:00
|
|
|
if (bc_num_from_obj_or_str_or_long_with_err(&num, &scale, NULL, str, lval, 1) == FAILURE) {
|
|
|
|
bc_free_num(&num);
|
|
|
|
RETURN_THROWS();
|
|
|
|
}
|
|
|
|
|
|
|
|
intern->num = num;
|
|
|
|
intern->scale = scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bcmath_number_calc_method(INTERNAL_FUNCTION_PARAMETERS, uint8_t opcode)
|
|
|
|
{
|
|
|
|
zend_object *num_obj = NULL;
|
|
|
|
zend_string *num_str = NULL;
|
|
|
|
zend_long num_lval = 0;
|
|
|
|
zend_long scale_lval = 0;
|
|
|
|
bool scale_is_null = true;
|
|
|
|
|
|
|
|
ZEND_PARSE_PARAMETERS_START(1, 2)
|
|
|
|
BCMATH_PARAM_NUMBER_OR_STR_OR_LONG(num_obj, bcmath_number_ce, num_str, num_lval);
|
|
|
|
Z_PARAM_OPTIONAL
|
|
|
|
Z_PARAM_LONG_OR_NULL(scale_lval, scale_is_null);
|
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
|
|
|
|
|
|
bc_num num = NULL;
|
2024-09-22 05:59:06 +08:00
|
|
|
size_t num_full_scale = 0;
|
2024-09-04 10:12:51 +08:00
|
|
|
if (bc_num_from_obj_or_str_or_long_with_err(&num, &num_full_scale, num_obj, num_str, num_lval, 1) == FAILURE) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2024-09-07 03:14:01 +08:00
|
|
|
if (bcmath_check_scale(scale_lval, 2) == FAILURE) {
|
2024-09-04 10:12:51 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
bc_num ret = NULL;
|
|
|
|
size_t scale = scale_lval;
|
|
|
|
bcmath_number_obj_t *intern = get_bcmath_number_from_zval(ZEND_THIS);
|
|
|
|
|
|
|
|
switch (opcode) {
|
|
|
|
case ZEND_ADD:
|
|
|
|
bcmath_number_add_internal(intern->num, num, &ret, intern->scale, num_full_scale, &scale, scale_is_null);
|
|
|
|
break;
|
|
|
|
case ZEND_SUB:
|
|
|
|
bcmath_number_sub_internal(intern->num, num, &ret, intern->scale, num_full_scale, &scale, scale_is_null);
|
|
|
|
break;
|
|
|
|
case ZEND_MUL:
|
|
|
|
if (UNEXPECTED(bcmath_number_mul_internal(intern->num, num, &ret, intern->scale, num_full_scale, &scale, scale_is_null) == FAILURE)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ZEND_DIV:
|
|
|
|
if (UNEXPECTED(bcmath_number_div_internal(intern->num, num, &ret, intern->scale, &scale, scale_is_null) == FAILURE)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ZEND_MOD:
|
|
|
|
if (UNEXPECTED(bcmath_number_mod_internal(intern->num, num, &ret, intern->scale, num_full_scale, &scale, scale_is_null) == FAILURE)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ZEND_POW:
|
|
|
|
if (UNEXPECTED(bcmath_number_pow_internal(intern->num, num, &ret, intern->scale, &scale, scale_is_null, false) == FAILURE)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
EMPTY_SWITCH_DEFAULT_CASE();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_obj == NULL) {
|
|
|
|
bc_free_num(&num);
|
|
|
|
}
|
|
|
|
|
|
|
|
bcmath_number_obj_t *new_intern = bcmath_number_new_obj(ret, scale);
|
|
|
|
RETURN_OBJ(&new_intern->std);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (num_obj == NULL) {
|
|
|
|
bc_free_num(&num);
|
|
|
|
}
|
|
|
|
RETURN_THROWS();
|
|
|
|
}
|
|
|
|
|
|
|
|
PHP_METHOD(BcMath_Number, add)
|
|
|
|
{
|
|
|
|
bcmath_number_calc_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ADD);
|
|
|
|
}
|
|
|
|
|
|
|
|
PHP_METHOD(BcMath_Number, sub)
|
|
|
|
{
|
|
|
|
bcmath_number_calc_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_SUB);
|
|
|
|
}
|
|
|
|
|
|
|
|
PHP_METHOD(BcMath_Number, mul)
|
|
|
|
{
|
|
|
|
bcmath_number_calc_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_MUL);
|
|
|
|
}
|
|
|
|
|
|
|
|
PHP_METHOD(BcMath_Number, div)
|
|
|
|
{
|
|
|
|
bcmath_number_calc_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_DIV);
|
|
|
|
}
|
|
|
|
|
|
|
|
PHP_METHOD(BcMath_Number, mod)
|
|
|
|
{
|
|
|
|
bcmath_number_calc_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_MOD);
|
|
|
|
}
|
|
|
|
|
|
|
|
PHP_METHOD(BcMath_Number, pow)
|
|
|
|
{
|
|
|
|
bcmath_number_calc_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_POW);
|
|
|
|
}
|
|
|
|
|
2024-09-23 05:43:11 +08:00
|
|
|
PHP_METHOD(BcMath_Number, divmod)
|
|
|
|
{
|
|
|
|
zend_object *num_obj = NULL;
|
|
|
|
zend_string *num_str = NULL;
|
|
|
|
zend_long num_lval = 0;
|
|
|
|
zend_long scale_lval = 0;
|
|
|
|
bool scale_is_null = true;
|
|
|
|
|
|
|
|
ZEND_PARSE_PARAMETERS_START(1, 2)
|
|
|
|
BCMATH_PARAM_NUMBER_OR_STR_OR_LONG(num_obj, bcmath_number_ce, num_str, num_lval);
|
|
|
|
Z_PARAM_OPTIONAL
|
|
|
|
Z_PARAM_LONG_OR_NULL(scale_lval, scale_is_null);
|
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
|
|
|
|
|
|
bc_num num = NULL;
|
|
|
|
size_t num_full_scale;
|
|
|
|
if (bc_num_from_obj_or_str_or_long_with_err(&num, &num_full_scale, num_obj, num_str, num_lval, 1) == FAILURE) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (bcmath_check_scale(scale_lval, 2) == FAILURE) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
bc_num quot = NULL;
|
|
|
|
bc_num rem = NULL;
|
|
|
|
size_t scale = scale_lval;
|
|
|
|
bcmath_number_obj_t *intern = get_bcmath_number_from_zval(ZEND_THIS);
|
|
|
|
|
|
|
|
if (scale_is_null) {
|
|
|
|
scale = MAX(intern->scale, num_full_scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bc_divmod(intern->num, num, ", &rem, scale)) {
|
|
|
|
zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Division by zero");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
bc_rm_trailing_zeros(quot);
|
|
|
|
bc_rm_trailing_zeros(rem);
|
|
|
|
|
|
|
|
if (num_obj == NULL) {
|
|
|
|
bc_free_num(&num);
|
|
|
|
}
|
|
|
|
|
|
|
|
bcmath_number_obj_t *quot_intern = bcmath_number_new_obj(quot, 0);
|
|
|
|
bcmath_number_obj_t *rem_intern = bcmath_number_new_obj(rem, scale);
|
|
|
|
|
|
|
|
zval z_quot, z_rem;
|
|
|
|
ZVAL_OBJ(&z_quot, "_intern->std);
|
|
|
|
ZVAL_OBJ(&z_rem, &rem_intern->std);
|
|
|
|
|
|
|
|
RETURN_ARR(zend_new_pair(&z_quot, &z_rem));
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (num_obj == NULL) {
|
|
|
|
bc_free_num(&num);
|
|
|
|
}
|
|
|
|
RETURN_THROWS();
|
|
|
|
}
|
|
|
|
|
2024-09-04 10:12:51 +08:00
|
|
|
PHP_METHOD(BcMath_Number, powmod)
|
|
|
|
{
|
|
|
|
zend_object *exponent_obj = NULL;
|
|
|
|
zend_string *exponent_str = NULL;
|
|
|
|
zend_long exponent_lval = 0;
|
|
|
|
|
|
|
|
zend_object *modulus_obj = NULL;
|
|
|
|
zend_string *modulus_str = NULL;
|
|
|
|
zend_long modulus_lval = 0;
|
|
|
|
|
|
|
|
zend_long scale_lval = 0;
|
|
|
|
bool scale_is_null = true;
|
|
|
|
|
|
|
|
ZEND_PARSE_PARAMETERS_START(2, 3)
|
|
|
|
BCMATH_PARAM_NUMBER_OR_STR_OR_LONG(exponent_obj, bcmath_number_ce, exponent_str, exponent_lval);
|
|
|
|
BCMATH_PARAM_NUMBER_OR_STR_OR_LONG(modulus_obj, bcmath_number_ce, modulus_str, modulus_lval);
|
|
|
|
Z_PARAM_OPTIONAL
|
|
|
|
Z_PARAM_LONG_OR_NULL(scale_lval, scale_is_null);
|
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
|
|
|
|
|
|
bc_num exponent_num = NULL;
|
|
|
|
bc_num modulus_num = NULL;
|
|
|
|
if (bc_num_from_obj_or_str_or_long_with_err(&exponent_num, NULL, exponent_obj, exponent_str, exponent_lval, 1) == FAILURE) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (bc_num_from_obj_or_str_or_long_with_err(&modulus_num, NULL, modulus_obj, modulus_str, modulus_lval, 2) == FAILURE) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2024-09-07 03:14:01 +08:00
|
|
|
if (bcmath_check_scale(scale_lval, 3) == FAILURE) {
|
2024-09-04 10:12:51 +08:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
bcmath_number_obj_t *intern = get_bcmath_number_from_zval(ZEND_THIS);
|
|
|
|
bc_num ret = NULL;
|
|
|
|
size_t scale = scale_lval;
|
|
|
|
raise_mod_status status = bc_raisemod(intern->num, exponent_num, modulus_num, &ret, scale);
|
|
|
|
switch (status) {
|
|
|
|
case BASE_HAS_FRACTIONAL:
|
|
|
|
zend_value_error("Base number cannot have a fractional part");
|
|
|
|
goto cleanup;
|
|
|
|
case EXPO_HAS_FRACTIONAL:
|
|
|
|
zend_argument_value_error(1, "cannot have a fractional part");
|
|
|
|
goto cleanup;
|
|
|
|
case EXPO_IS_NEGATIVE:
|
|
|
|
zend_argument_value_error(1, "must be greater than or equal to 0");
|
|
|
|
goto cleanup;
|
|
|
|
case MOD_HAS_FRACTIONAL:
|
|
|
|
zend_argument_value_error(2, "cannot have a fractional part");
|
|
|
|
goto cleanup;
|
|
|
|
case MOD_IS_ZERO:
|
|
|
|
zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero");
|
|
|
|
goto cleanup;
|
|
|
|
case OK:
|
|
|
|
break;
|
|
|
|
EMPTY_SWITCH_DEFAULT_CASE();
|
|
|
|
}
|
|
|
|
|
|
|
|
bc_rm_trailing_zeros(ret);
|
|
|
|
|
|
|
|
if (exponent_obj == NULL) {
|
|
|
|
bc_free_num(&exponent_num);
|
|
|
|
}
|
|
|
|
if (modulus_obj == NULL) {
|
|
|
|
bc_free_num(&modulus_num);
|
|
|
|
}
|
|
|
|
|
|
|
|
bcmath_number_obj_t *new_intern = bcmath_number_new_obj(ret, scale);
|
|
|
|
RETURN_OBJ(&new_intern->std);
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if (exponent_obj == NULL) {
|
|
|
|
bc_free_num(&exponent_num);
|
|
|
|
}
|
|
|
|
if (modulus_obj == NULL) {
|
|
|
|
bc_free_num(&modulus_num);
|
|
|
|
}
|
|
|
|
RETURN_THROWS();
|
|
|
|
}
|
|
|
|
|
|
|
|
PHP_METHOD(BcMath_Number, sqrt)
|
|
|
|
{
|
|
|
|
zend_long scale_lval = 0;
|
|
|
|
bool scale_is_null = true;
|
|
|
|
|
|
|
|
ZEND_PARSE_PARAMETERS_START(0, 1)
|
|
|
|
Z_PARAM_OPTIONAL
|
|
|
|
Z_PARAM_LONG_OR_NULL(scale_lval, scale_is_null);
|
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
|
|
|
2024-09-07 03:14:01 +08:00
|
|
|
if (bcmath_check_scale(scale_lval, 1) == FAILURE) {
|
2024-09-04 10:12:51 +08:00
|
|
|
RETURN_THROWS();
|
|
|
|
}
|
|
|
|
|
|
|
|
bcmath_number_obj_t *intern = get_bcmath_number_from_zval(ZEND_THIS);
|
|
|
|
|
|
|
|
size_t scale;
|
|
|
|
if (scale_is_null) {
|
|
|
|
scale = intern->scale + BC_MATH_NUMBER_EXPAND_SCALE;
|
|
|
|
if (UNEXPECTED(CHECK_RET_SCALE_OVERFLOW(scale, intern->scale))) {
|
|
|
|
zend_value_error("scale of the result is too large");
|
|
|
|
RETURN_THROWS();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
scale = scale_lval;
|
|
|
|
}
|
|
|
|
|
|
|
|
bc_num ret = bc_copy_num(intern->num);
|
|
|
|
if (!bc_sqrt (&ret, scale)) {
|
|
|
|
zend_value_error("Base number must be greater than or equal to 0");
|
|
|
|
bc_free_num(&ret);
|
|
|
|
RETURN_THROWS();
|
|
|
|
}
|
|
|
|
|
|
|
|
ret->n_scale = MIN(scale, ret->n_scale);
|
|
|
|
bc_rm_trailing_zeros(ret);
|
|
|
|
if (scale_is_null) {
|
|
|
|
size_t diff = scale - ret->n_scale;
|
|
|
|
scale -= diff > BC_MATH_NUMBER_EXPAND_SCALE ? BC_MATH_NUMBER_EXPAND_SCALE : diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
bcmath_number_obj_t *new_intern = bcmath_number_new_obj(ret, scale);
|
|
|
|
RETURN_OBJ(&new_intern->std);
|
|
|
|
}
|
|
|
|
|
|
|
|
PHP_METHOD(BcMath_Number, compare)
|
|
|
|
{
|
|
|
|
zend_object *num_obj = NULL;
|
|
|
|
zend_string *num_str = NULL;
|
|
|
|
zend_long num_lval = 0;
|
|
|
|
zend_long scale_lval = 0;
|
|
|
|
bool scale_is_null = true;
|
|
|
|
|
|
|
|
ZEND_PARSE_PARAMETERS_START(1, 2)
|
|
|
|
BCMATH_PARAM_NUMBER_OR_STR_OR_LONG(num_obj, bcmath_number_ce, num_str, num_lval);
|
|
|
|
Z_PARAM_OPTIONAL
|
|
|
|
Z_PARAM_LONG_OR_NULL(scale_lval, scale_is_null);
|
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
|
|
|
|
|
|
bc_num num = NULL;
|
2024-09-22 05:59:06 +08:00
|
|
|
size_t num_full_scale = 0;
|
2024-09-04 10:12:51 +08:00
|
|
|
if (bc_num_from_obj_or_str_or_long_with_err(&num, &num_full_scale, num_obj, num_str, num_lval, 1) == FAILURE) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2024-09-07 03:14:01 +08:00
|
|
|
if (bcmath_check_scale(scale_lval, 2) == FAILURE) {
|
2024-09-04 10:12:51 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t scale;
|
|
|
|
bcmath_number_obj_t *intern = get_bcmath_number_from_zval(ZEND_THIS);
|
|
|
|
if (scale_is_null) {
|
|
|
|
scale = MAX(intern->num->n_scale, num->n_scale);
|
|
|
|
} else {
|
|
|
|
scale = scale_lval;
|
|
|
|
}
|
|
|
|
zend_long ret = bc_compare(intern->num, num, scale);
|
|
|
|
|
|
|
|
if (num_obj == NULL) {
|
|
|
|
bc_free_num(&num);
|
|
|
|
}
|
|
|
|
RETURN_LONG(ret);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (num_obj == NULL) {
|
|
|
|
bc_free_num(&num);
|
|
|
|
}
|
|
|
|
RETURN_THROWS();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bcmath_number_floor_or_ceil(INTERNAL_FUNCTION_PARAMETERS, bool is_floor)
|
|
|
|
{
|
|
|
|
ZEND_PARSE_PARAMETERS_NONE();
|
|
|
|
|
|
|
|
bcmath_number_obj_t *intern = get_bcmath_number_from_zval(ZEND_THIS);
|
|
|
|
|
|
|
|
bc_num ret = bc_floor_or_ceil(intern->num, is_floor);
|
|
|
|
|
|
|
|
bcmath_number_obj_t *new_intern = bcmath_number_new_obj(ret, 0);
|
|
|
|
RETURN_OBJ(&new_intern->std);
|
|
|
|
}
|
|
|
|
|
|
|
|
PHP_METHOD(BcMath_Number, floor)
|
|
|
|
{
|
|
|
|
bcmath_number_floor_or_ceil(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
PHP_METHOD(BcMath_Number, ceil)
|
|
|
|
{
|
|
|
|
bcmath_number_floor_or_ceil(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
PHP_METHOD(BcMath_Number, round)
|
|
|
|
{
|
|
|
|
zend_long precision = 0;
|
|
|
|
zend_long rounding_mode = PHP_ROUND_HALF_UP;
|
|
|
|
zend_object *mode_object = NULL;
|
|
|
|
|
|
|
|
ZEND_PARSE_PARAMETERS_START(0, 2)
|
|
|
|
Z_PARAM_OPTIONAL
|
|
|
|
Z_PARAM_LONG(precision);
|
|
|
|
Z_PARAM_OBJ_OF_CLASS(mode_object, rounding_mode_ce);
|
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
|
|
|
|
|
|
if (mode_object != NULL) {
|
|
|
|
rounding_mode = php_math_round_mode_from_enum(mode_object);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (rounding_mode) {
|
|
|
|
case PHP_ROUND_HALF_UP:
|
|
|
|
case PHP_ROUND_HALF_DOWN:
|
|
|
|
case PHP_ROUND_HALF_EVEN:
|
|
|
|
case PHP_ROUND_HALF_ODD:
|
|
|
|
case PHP_ROUND_CEILING:
|
|
|
|
case PHP_ROUND_FLOOR:
|
|
|
|
case PHP_ROUND_TOWARD_ZERO:
|
|
|
|
case PHP_ROUND_AWAY_FROM_ZERO:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
zend_argument_value_error(2, "is an unsupported rounding mode");
|
|
|
|
RETURN_THROWS();
|
|
|
|
}
|
|
|
|
|
|
|
|
bcmath_number_obj_t *intern = get_bcmath_number_from_zval(ZEND_THIS);
|
|
|
|
|
|
|
|
bc_num ret = NULL;
|
|
|
|
bc_round(intern->num, precision, rounding_mode, &ret);
|
|
|
|
|
|
|
|
bc_rm_trailing_zeros(ret);
|
|
|
|
|
|
|
|
bcmath_number_obj_t *new_intern = bcmath_number_new_obj(ret, ret->n_scale);
|
|
|
|
RETURN_OBJ(&new_intern->std);
|
|
|
|
}
|
|
|
|
|
|
|
|
PHP_METHOD(BcMath_Number, __toString)
|
|
|
|
{
|
|
|
|
ZEND_PARSE_PARAMETERS_NONE();
|
|
|
|
bcmath_number_obj_t *intern = get_bcmath_number_from_zval(ZEND_THIS);
|
|
|
|
RETURN_STR_COPY(bcmath_number_value_to_str(intern));
|
|
|
|
}
|
|
|
|
|
|
|
|
PHP_METHOD(BcMath_Number, __serialize)
|
|
|
|
{
|
|
|
|
ZEND_PARSE_PARAMETERS_NONE();
|
|
|
|
|
|
|
|
bcmath_number_obj_t *intern = get_bcmath_number_from_zval(ZEND_THIS);
|
|
|
|
|
|
|
|
array_init(return_value);
|
|
|
|
HashTable *props = Z_ARRVAL_P(return_value);
|
|
|
|
|
|
|
|
zval zv;
|
|
|
|
ZVAL_STR_COPY(&zv, bcmath_number_value_to_str(intern));
|
|
|
|
zend_hash_update(props, ZSTR_KNOWN(ZEND_STR_VALUE), &zv);
|
|
|
|
}
|
|
|
|
|
|
|
|
PHP_METHOD(BcMath_Number, __unserialize)
|
|
|
|
{
|
|
|
|
HashTable *props;
|
|
|
|
|
|
|
|
ZEND_PARSE_PARAMETERS_START(1, 1)
|
|
|
|
Z_PARAM_ARRAY_HT(props)
|
|
|
|
ZEND_PARSE_PARAMETERS_END();
|
|
|
|
|
|
|
|
zval *zv = zend_hash_find(props, ZSTR_KNOWN(ZEND_STR_VALUE));
|
|
|
|
if (!zv || Z_TYPE_P(zv) != IS_STRING || Z_STRLEN_P(zv) == 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
bcmath_number_obj_t *intern = get_bcmath_number_from_zval(ZEND_THIS);
|
|
|
|
if (UNEXPECTED(intern->num != NULL)) {
|
|
|
|
zend_readonly_property_modification_error_ex(ZSTR_VAL(bcmath_number_ce->name), "value");
|
|
|
|
RETURN_THROWS();
|
|
|
|
}
|
|
|
|
|
|
|
|
bc_num num = NULL;
|
2024-09-22 05:59:06 +08:00
|
|
|
size_t scale = 0;
|
|
|
|
if (php_str2num_ex(&num, Z_STR_P(zv), &scale) == FAILURE || CHECK_SCALE_OVERFLOW(scale)) {
|
2024-09-04 10:12:51 +08:00
|
|
|
bc_free_num(&num);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
intern->num = num;
|
|
|
|
intern->scale = scale;
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(bcmath_number_ce->name));
|
|
|
|
RETURN_THROWS();
|
|
|
|
}
|
|
|
|
|
1999-04-22 07:28:00 +08:00
|
|
|
#endif
|