Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions ext/bcmath/bcmath.c
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ static void bcmath_number_register_class(void)
}

static zend_always_inline void bcmath_number_add_internal(
bc_num n1, bc_num n2, bc_num *ret,
const bc_num n1, const bc_num n2, bc_num *ret,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that bc_num is defined as follows: typedef struct bc_struct *bc_num;
Unfortunately, const bc_num won't do what you probably think it does: it doesn't make the pointer const, it just makes the binding of the variable const. If it was written like bc_struct *n1 then n1 would be a const pointer if you added const before it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nielsdos

Ahhhh, indeed.....

Do you think it would be redundant to define a new type just for this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels a bit silly

size_t n1_full_scale, size_t n2_full_scale, size_t *scale, bool auto_scale
) {
if (auto_scale) {
Expand All @@ -1031,7 +1031,7 @@ static zend_always_inline void bcmath_number_add_internal(
}

static zend_always_inline void bcmath_number_sub_internal(
bc_num n1, bc_num n2, bc_num *ret,
const bc_num n1, const 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) {
Expand All @@ -1043,7 +1043,7 @@ static zend_always_inline void bcmath_number_sub_internal(
}

static zend_always_inline zend_result bcmath_number_mul_internal(
bc_num n1, bc_num n2, bc_num *ret,
const bc_num n1, const 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) {
Expand All @@ -1060,7 +1060,7 @@ static zend_always_inline zend_result bcmath_number_mul_internal(
}

static zend_always_inline zend_result bcmath_number_div_internal(
bc_num n1, bc_num n2, bc_num *ret,
const bc_num n1, const bc_num n2, bc_num *ret,
size_t n1_full_scale, size_t *scale, bool auto_scale
) {
if (auto_scale) {
Expand All @@ -1083,7 +1083,7 @@ static zend_always_inline zend_result bcmath_number_div_internal(
}

static zend_always_inline zend_result bcmath_number_mod_internal(
bc_num n1, bc_num n2, bc_num *ret,
const bc_num n1, const 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) {
Expand Down Expand Up @@ -1164,7 +1164,7 @@ static zend_always_inline bcmath_number_obj_t *bcmath_number_new_obj(bc_num ret,
return intern;
}

static zend_result bcmath_number_parse_num(zval *zv, zend_object **obj, zend_string **str, zend_long *lval)
static zend_result bcmath_number_parse_num(const 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);
Expand Down Expand Up @@ -1372,7 +1372,7 @@ static int bcmath_number_compare(zval *op1, zval *op2)
}

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)
bc_num *num, size_t *scale, const zend_object *obj, const zend_string *str, zend_long lval, uint32_t arg_num)
{
size_t full_scale = 0;
if (UNEXPECTED(bc_num_from_obj_or_str_or_long(num, &full_scale, obj, str, lval) == FAILURE)) {
Expand Down
2 changes: 1 addition & 1 deletion ext/bcmath/libbcmath/src/add.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
N1 is added to N2 and the result placed into RESULT. SCALE_MIN
is the minimum scale for the result. */

bc_num bc_add(bc_num n1, bc_num n2, size_t scale_min)
bc_num bc_add(const bc_num n1, const bc_num n2, size_t scale_min)
{
bc_num sum = NULL;

Expand Down
10 changes: 4 additions & 6 deletions ext/bcmath/libbcmath/src/compare.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@
than N2 and +1 if N1 is greater than N2. If USE_SIGN is false, just
compare the magnitudes. */

bcmath_compare_result _bc_do_compare(bc_num n1, bc_num n2, size_t scale, bool use_sign)
bcmath_compare_result _bc_do_compare(const bc_num n1, const bc_num n2, size_t scale, bool use_sign)
{
char *n1ptr, *n2ptr;

/* First, compare signs. */
if (use_sign && n1->n_sign != n2->n_sign) {
/*
Expand Down Expand Up @@ -91,8 +89,8 @@ bcmath_compare_result _bc_do_compare(bc_num n1, bc_num n2, size_t scale, bool us
/* If we get here, they have the same number of integer digits.
check the integer part and the equal length part of the fraction. */
size_t count = n1->n_len + MIN (n1_scale, n2_scale);
n1ptr = n1->n_value;
n2ptr = n2->n_value;
const char *n1ptr = n1->n_value;
const char *n2ptr = n2->n_value;

while ((count > 0) && (*n1ptr == *n2ptr)) {
n1ptr++;
Expand Down Expand Up @@ -151,7 +149,7 @@ bcmath_compare_result _bc_do_compare(bc_num n1, bc_num n2, size_t scale, bool us


/* This is the "user callable" routine to compare numbers N1 and N2. */
bcmath_compare_result bc_compare(bc_num n1, bc_num n2, size_t scale)
bcmath_compare_result bc_compare(const bc_num n1, const bc_num n2, size_t scale)
{
return _bc_do_compare(n1, n2, scale, true);
}
10 changes: 5 additions & 5 deletions ext/bcmath/libbcmath/src/div.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static inline void bc_fast_div(
*/
static inline void bc_standard_div(
BC_VECTOR *numerator_vectors, size_t numerator_arr_size,
BC_VECTOR *divisor_vectors, size_t divisor_arr_size, size_t divisor_len,
const BC_VECTOR *divisor_vectors, size_t divisor_arr_size, size_t divisor_len,
BC_VECTOR *quot_vectors, size_t quot_arr_size
) {
size_t numerator_top_index = numerator_arr_size - 1;
Expand Down Expand Up @@ -300,7 +300,7 @@ static void bc_do_div(
}
}

static inline void bc_divide_by_one(bc_num numerator, bc_num *quot, size_t quot_scale)
static inline void bc_divide_by_one(const bc_num numerator, bc_num *quot, size_t quot_scale)
{
quot_scale = MIN(numerator->n_scale, quot_scale);
*quot = bc_new_num_nonzeroed(numerator->n_len, quot_scale);
Expand Down Expand Up @@ -332,7 +332,7 @@ static inline void bc_divide_by_pow_10(
}
}

bool bc_divide(bc_num numerator, bc_num divisor, bc_num *quot, size_t scale)
bool bc_divide(const bc_num numerator, const bc_num divisor, bc_num *quot, size_t scale)
{
/* divide by zero */
if (bc_is_zero(divisor)) {
Expand All @@ -354,10 +354,10 @@ bool bc_divide(bc_num numerator, bc_num divisor, bc_num *quot, size_t scale)
return true;
}

char *numeratorptr = numerator->n_value;
const char *numeratorptr = numerator->n_value;
size_t numerator_size = numerator->n_len + quot_scale + divisor->n_scale;

char *divisorptr = divisor->n_value;
const char *divisorptr = divisor->n_value;
size_t divisor_size = divisor->n_len + divisor->n_scale;

/* check and remove numerator leading zeros */
Expand Down
4 changes: 2 additions & 2 deletions ext/bcmath/libbcmath/src/divmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
true otherwise for success.
*/

bool bc_divmod(bc_num num1, bc_num num2, bc_num *quot, bc_num *rem, size_t scale)
bool bc_divmod(const bc_num num1, const bc_num num2, bc_num *quot, bc_num *rem, size_t scale)
{
bc_num quotient = NULL;
bc_num temp;
Expand Down Expand Up @@ -84,7 +84,7 @@ bool bc_divmod(bc_num num1, bc_num num2, bc_num *quot, bc_num *rem, size_t scale
/* Modulo for numbers. This computes NUM1 % NUM2 and puts the
result in RESULT. */

bool bc_modulo(bc_num num1, bc_num num2, bc_num *result, size_t scale)
bool bc_modulo(const bc_num num1, const bc_num num2, bc_num *result, size_t scale)
{
return bc_divmod(num1, num2, NULL, result, scale);
}
16 changes: 8 additions & 8 deletions ext/bcmath/libbcmath/src/doaddsub.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,24 @@
returned. The signs of N1 and N2 are ignored.
SCALE_MIN is to set the minimum scale of the result. */

bc_num _bc_do_add(bc_num n1, bc_num n2)
bc_num _bc_do_add(const bc_num n1, const bc_num n2)
{
bc_num sum;
size_t sum_len = MAX(n1->n_len, n2->n_len) + 1;
size_t sum_scale = MAX(n1->n_scale, n2->n_scale);
size_t min_len = MIN (n1->n_len, n2->n_len);
size_t min_scale = MIN(n1->n_scale, n2->n_scale);
size_t min_bytes = min_len + min_scale;
char *n1ptr, *n2ptr, *sumptr;
char *sumptr;
bool carry = 0;
size_t count;

/* Prepare sum. */
sum = bc_new_num_nonzeroed(sum_len, sum_scale);

/* Start with the fraction part. Initialize the pointers. */
n1ptr = (char *) (n1->n_value + n1->n_len + n1->n_scale - 1);
n2ptr = (char *) (n2->n_value + n2->n_len + n2->n_scale - 1);
const char *n1ptr = (char *) (n1->n_value + n1->n_len + n1->n_scale - 1);
const char *n2ptr = (char *) (n2->n_value + n2->n_len + n2->n_scale - 1);
sumptr = (char *) (sum->n_value + sum_scale + sum_len - 1);

/* Add the fraction part. First copy the longer fraction.*/
Expand Down Expand Up @@ -169,7 +169,7 @@ bc_num _bc_do_add(bc_num n1, bc_num n2)
returned. The signs of N1 and N2 are ignored. Also, N1 is
assumed to be larger than N2. SCALE_MIN is the minimum scale
of the result. */
bc_num _bc_do_sub(bc_num n1, bc_num n2)
bc_num _bc_do_sub(const bc_num n1, const bc_num n2)
{
bc_num diff;
/* The caller is guaranteed that n1 is always large. */
Expand All @@ -182,14 +182,14 @@ bc_num _bc_do_sub(bc_num n1, bc_num n2)
size_t borrow = 0;
size_t count;
int val;
char *n1ptr, *n2ptr, *diffptr;
char *diffptr;

/* Allocate temporary storage. */
diff = bc_new_num_nonzeroed(diff_len, diff_scale);

/* Initialize the subtract. */
n1ptr = (char *) (n1->n_value + n1->n_len + n1->n_scale - 1);
n2ptr = (char *) (n2->n_value + n2->n_len + n2->n_scale - 1);
const char *n1ptr = (char *) (n1->n_value + n1->n_len + n1->n_scale - 1);
const char *n2ptr = (char *) (n2->n_value + n2->n_len + n2->n_scale - 1);
diffptr = (char *) (diff->n_value + diff_len + diff_scale - 1);

/* Take care of the longer scaled number. */
Expand Down
2 changes: 1 addition & 1 deletion ext/bcmath/libbcmath/src/floor_or_ceil.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "private.h"
#include <stddef.h>

bc_num bc_floor_or_ceil(bc_num num, bool is_floor)
bc_num bc_floor_or_ceil(const bc_num num, bool is_floor)
{
/* Initialize result */
bc_num result = bc_new_num(num->n_len, 0);
Expand Down
6 changes: 3 additions & 3 deletions ext/bcmath/libbcmath/src/private.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ static const BC_VECTOR BC_POW_10_LUT[9] = {


/* routines */
bcmath_compare_result _bc_do_compare (bc_num n1, bc_num n2, size_t scale, bool use_sign);
bc_num _bc_do_add (bc_num n1, bc_num n2);
bc_num _bc_do_sub (bc_num n1, bc_num n2);
bcmath_compare_result _bc_do_compare (const bc_num n1, const bc_num n2, size_t scale, bool use_sign);
bc_num _bc_do_add (const bc_num n1, const bc_num n2);
bc_num _bc_do_sub (const bc_num n1, const bc_num n2);
void _bc_rm_leading_zeros (bc_num num);

#endif
8 changes: 4 additions & 4 deletions ext/bcmath/libbcmath/src/recmul.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static inline void bc_mul_carry_calc(BC_VECTOR *prod_vector, size_t prod_arr_siz
* If the n_values of n1 and n2 are both 4 (32-bit) or 8 (64-bit) digits or less,
* the calculation will be performed at high speed without using an array.
*/
static inline void bc_fast_mul(bc_num n1, size_t n1len, bc_num n2, size_t n2len, bc_num *prod)
static inline void bc_fast_mul(const bc_num n1, size_t n1len, const bc_num n2, size_t n2len, bc_num *prod)
{
const char *n1end = n1->n_value + n1len - 1;
const char *n2end = n2->n_value + n2len - 1;
Expand All @@ -76,7 +76,7 @@ static inline void bc_fast_mul(bc_num n1, size_t n1len, bc_num n2, size_t n2len,
* Equivalent of bc_fast_mul for small numbers to perform computations
* without using array.
*/
static inline void bc_fast_square(bc_num n1, size_t n1len, bc_num *prod)
static inline void bc_fast_square(const bc_num n1, size_t n1len, bc_num *prod)
{
const char *n1end = n1->n_value + n1len - 1;

Expand Down Expand Up @@ -119,7 +119,7 @@ static inline void bc_mul_finish_from_vector(BC_VECTOR *prod_vector, size_t prod
* Multiply and add these groups of numbers to perform multiplication fast.
* How much to shift the digits when adding values can be calculated from the index of the array.
*/
static void bc_standard_mul(bc_num n1, size_t n1len, bc_num n2, size_t n2len, bc_num *prod)
static void bc_standard_mul(const bc_num n1, size_t n1len, const bc_num n2, size_t n2len, bc_num *prod)
{
size_t i;
const char *n1end = n1->n_value + n1len - 1;
Expand Down Expand Up @@ -181,7 +181,7 @@ static void bc_standard_mul(bc_num n1, size_t n1len, bc_num n2, size_t n2len, bc
}

/** This is bc_standard_mul implementation for square */
static void bc_standard_square(bc_num n1, size_t n1len, bc_num *prod)
static void bc_standard_square(const bc_num n1, size_t n1len, bc_num *prod)
{
size_t i;
const char *n1end = n1->n_value + n1len - 1;
Expand Down
2 changes: 1 addition & 1 deletion ext/bcmath/libbcmath/src/round.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <stddef.h>

/* Returns the scale of the value after rounding. */
size_t bc_round(bc_num num, zend_long precision, zend_long mode, bc_num *result)
size_t bc_round(const bc_num num, zend_long precision, zend_long mode, bc_num *result)
{
/* clear result */
bc_free_num(result);
Expand Down
2 changes: 1 addition & 1 deletion ext/bcmath/libbcmath/src/sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
N2 is subtracted from N1 and the result placed in RESULT. SCALE_MIN
is the minimum scale for the result. */

bc_num bc_sub(bc_num n1, bc_num n2, size_t scale_min)
bc_num bc_sub(const bc_num n1, const bc_num n2, size_t scale_min)
{
bc_num diff = NULL;

Expand Down
4 changes: 2 additions & 2 deletions ext/bcmath/libbcmath/src/zero.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

/* In some places we need to check if the number NUM is zero. */

bool bc_is_zero_for_scale(bc_num num, size_t scale)
bool bc_is_zero_for_scale(const bc_num num, size_t scale)
{
size_t count;
char *nptr;
Expand All @@ -55,7 +55,7 @@ bool bc_is_zero_for_scale(bc_num num, size_t scale)
return count == 0;
}

bool bc_is_zero(bc_num num)
bool bc_is_zero(const bc_num num)
{
return bc_is_zero_for_scale(num, num->n_scale);
}
Loading