Skip to content

Commit be4b10e

Browse files
Added scale to bc_compare argument (#14802)
In the original specification, the scale of bc_num was directly changed and compared. This becomes a problem when objects are supported, so we will modify it to compare without changing bc_num.
1 parent 32bf50a commit be4b10e

File tree

8 files changed

+18
-15
lines changed

8 files changed

+18
-15
lines changed

ext/bcmath/bcmath.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ PHP_FUNCTION(bccomp)
638638
goto cleanup;
639639
}
640640

641-
RETVAL_LONG(bc_compare(first, second));
641+
RETVAL_LONG(bc_compare(first, second, scale));
642642

643643
cleanup: {
644644
bc_free_num(&first);

ext/bcmath/libbcmath/src/add.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ bc_num bc_add(bc_num n1, bc_num n2, size_t scale_min)
4949
} else {
5050
/* subtraction must be done. */
5151
/* Compare magnitudes. */
52-
switch (_bc_do_compare(n1, n2, false)) {
52+
switch (_bc_do_compare(n1, n2, scale_min, false)) {
5353
case BCMATH_RIGHT_GREATER:
5454
/* n1 is less than n2, subtract n1 from n2. */
5555
sum = _bc_do_sub(n2, n1);

ext/bcmath/libbcmath/src/bcmath.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ typedef enum {
112112
BCMATH_RIGHT_GREATER = -1
113113
} bcmath_compare_result;
114114

115-
bcmath_compare_result bc_compare(bc_num n1, bc_num n2);
115+
bcmath_compare_result bc_compare(bc_num n1, bc_num n2, size_t scale);
116116

117117
bool bc_is_zero(bc_num num);
118118

ext/bcmath/libbcmath/src/compare.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
than N2 and +1 if N1 is greater than N2. If USE_SIGN is false, just
4040
compare the magnitudes. */
4141

42-
bcmath_compare_result _bc_do_compare(bc_num n1, bc_num n2, bool use_sign)
42+
bcmath_compare_result _bc_do_compare(bc_num n1, bc_num n2, size_t scale, bool use_sign)
4343
{
4444
char *n1ptr, *n2ptr;
4545

@@ -73,9 +73,12 @@ bcmath_compare_result _bc_do_compare(bc_num n1, bc_num n2, bool use_sign)
7373
}
7474
}
7575

76+
size_t n1_scale = MIN(n1->n_scale, scale);
77+
size_t n2_scale = MIN(n2->n_scale, scale);
78+
7679
/* If we get here, they have the same number of integer digits.
7780
check the integer part and the equal length part of the fraction. */
78-
size_t count = n1->n_len + MIN (n1->n_scale, n2->n_scale);
81+
size_t count = n1->n_len + MIN (n1_scale, n2_scale);
7982
n1ptr = n1->n_value;
8083
n2ptr = n2->n_value;
8184

@@ -104,9 +107,9 @@ bcmath_compare_result _bc_do_compare(bc_num n1, bc_num n2, bool use_sign)
104107
}
105108

106109
/* They are equal up to the last part of the equal part of the fraction. */
107-
if (n1->n_scale != n2->n_scale) {
108-
if (n1->n_scale > n2->n_scale) {
109-
for (count = n1->n_scale - n2->n_scale; count > 0; count--) {
110+
if (n1_scale != n2_scale) {
111+
if (n1_scale > n2_scale) {
112+
for (count = n1_scale - n2_scale; count > 0; count--) {
110113
if (*n1ptr++ != 0) {
111114
/* Magnitude of n1 > n2. */
112115
if (!use_sign || n1->n_sign == PLUS) {
@@ -117,7 +120,7 @@ bcmath_compare_result _bc_do_compare(bc_num n1, bc_num n2, bool use_sign)
117120
}
118121
}
119122
} else {
120-
for (count = n2->n_scale - n1->n_scale; count > 0; count--) {
123+
for (count = n2_scale - n1_scale; count > 0; count--) {
121124
if (*n2ptr++ != 0) {
122125
/* Magnitude of n1 < n2. */
123126
if (!use_sign || n1->n_sign == PLUS) {
@@ -136,7 +139,7 @@ bcmath_compare_result _bc_do_compare(bc_num n1, bc_num n2, bool use_sign)
136139

137140

138141
/* This is the "user callable" routine to compare numbers N1 and N2. */
139-
bcmath_compare_result bc_compare(bc_num n1, bc_num n2)
142+
bcmath_compare_result bc_compare(bc_num n1, bc_num n2, size_t scale)
140143
{
141-
return _bc_do_compare(n1, n2, true);
144+
return _bc_do_compare(n1, n2, scale, true);
142145
}

ext/bcmath/libbcmath/src/private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ static inline uint64_t BC_BSWAP64(uint64_t u)
9797

9898

9999
/* routines */
100-
bcmath_compare_result _bc_do_compare (bc_num n1, bc_num n2, bool use_sign);
100+
bcmath_compare_result _bc_do_compare (bc_num n1, bc_num n2, size_t scale, bool use_sign);
101101
bc_num _bc_do_add (bc_num n1, bc_num n2);
102102
bc_num _bc_do_sub (bc_num n1, bc_num n2);
103103
void _bc_rm_leading_zeros (bc_num num);

ext/bcmath/libbcmath/src/raisemod.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ raise_mod_status bc_raisemod(bc_num base, bc_num expo, bc_num mod, bc_num *resul
5959
}
6060

6161
/* Any integer number mod 1 (or -1) must be equal to 0 */
62-
if (_bc_do_compare(mod, BCG(_one_), false) == BCMATH_EQUAL) {
62+
if (_bc_do_compare(mod, BCG(_one_), mod->n_scale, false) == BCMATH_EQUAL) {
6363
bc_free_num (result);
6464
*result = bc_new_num(1, scale);
6565
return OK;

ext/bcmath/libbcmath/src/sqrt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ bool bc_sqrt(bc_num *num, size_t scale)
5151
return true;
5252
}
5353

54-
bcmath_compare_result num_cmp_one = bc_compare(local_num, BCG(_one_));
54+
bcmath_compare_result num_cmp_one = bc_compare(local_num, BCG(_one_), local_num->n_scale);
5555
/* Square root of 1 is 1 */
5656
if (num_cmp_one == BCMATH_EQUAL) {
5757
bc_free_num (num);

ext/bcmath/libbcmath/src/sub.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ bc_num bc_sub(bc_num n1, bc_num n2, size_t scale_min)
4949
} else {
5050
/* subtraction must be done. */
5151
/* Compare magnitudes. */
52-
switch (_bc_do_compare(n1, n2, false)) {
52+
switch (_bc_do_compare(n1, n2, scale_min, false)) {
5353
case BCMATH_RIGHT_GREATER:
5454
/* n1 is less than n2, subtract n1 from n2. */
5555
diff = _bc_do_sub(n2, n1);

0 commit comments

Comments
 (0)