Skip to content

BCmath extension code reformatting #11896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Aug 13, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
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
6 changes: 4 additions & 2 deletions ext/bcmath/libbcmath/src/compare.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ int _bc_do_compare(bc_num n1, bc_num n2, bool use_sign, bool ignore_last)
/* First, compare signs. */
if (use_sign && n1->n_sign != n2->n_sign) {
if (n1->n_sign == PLUS) {
return (1); /* Positive N1 > Negative N2 */
/* Positive N1 > Negative N2 */
return (1);
} else {
return (-1); /* Negative N1 < Positive N1 */
/* Negative N1 < Positive N1 */
return (-1);
}
}

Expand Down
1 change: 1 addition & 0 deletions ext/bcmath/libbcmath/src/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <stdio.h>
#include "bcmath.h"
#include "private.h"
#include <stddef.h>

/* pn prints the number NUM in base 10. */

Expand Down
22 changes: 13 additions & 9 deletions ext/bcmath/libbcmath/src/div.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ bool bc_divide(bc_num n1, bc_num n2, bc_num *quot, int scale)
unsigned int qdig, qguess, borrow, carry;
unsigned char *mval;
unsigned int norm;
bool zero;

/* Test for divide by zero. */
if (bc_is_zero(n2)) {
Expand Down Expand Up @@ -130,10 +131,17 @@ bool bc_divide(bc_num n1, bc_num n2, bc_num *quot, int scale)
}

/* Calculate the number of quotient digits. */
if (len2 > len1 + scale || len2 > len1) {
if (len2 > len1 + scale) {
qdigits = scale + 1;
zero = true;
} else {
qdigits = len1 - len2 + scale + 1;
zero = false;
if (len2 > len1) {
/* One for the zero integer part. */
qdigits = scale + 1;
} else {
qdigits = len1 - len2 + scale + 1;
}
}

/* Allocate and zero the storage for the quotient. */
Expand All @@ -144,7 +152,7 @@ bool bc_divide(bc_num n1, bc_num n2, bc_num *quot, int scale)
mval = (unsigned char *) safe_emalloc(1, len2, 1);

/* Now for the full divide algorithm. */
if (len2 <= len1 + scale) {
if (!zero) {
/* Normalize */
norm = 10 / ((int) *n2ptr + 1);
if (norm != 1) {
Expand All @@ -170,14 +178,10 @@ bool bc_divide(bc_num n1, bc_num n2, bc_num *quot, int scale)
}

/* Test qguess. */
if (
n2ptr[1] * qguess > (num1[qdig] * 10 + num1[qdig + 1] - *n2ptr * qguess) * 10 + num1[qdig + 2]
) {
if (n2ptr[1] * qguess > (num1[qdig] * 10 + num1[qdig + 1] - *n2ptr * qguess) * 10 + num1[qdig + 2]) {
qguess--;
/* And again. */
if (
n2ptr[1] * qguess > (num1[qdig] * 10 + num1[qdig + 1] - *n2ptr * qguess) * 10 + num1[qdig + 2]
) {
if (n2ptr[1] * qguess > (num1[qdig] * 10 + num1[qdig + 1] - *n2ptr * qguess) * 10 + num1[qdig + 2]) {
qguess--;
}
}
Expand Down
1 change: 1 addition & 0 deletions ext/bcmath/libbcmath/src/doaddsub.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "bcmath.h"
#include "private.h"
#include <stddef.h>


/* Perform addition: N1 is added to N2 and the value is
Expand Down
2 changes: 2 additions & 0 deletions ext/bcmath/libbcmath/src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@
/* new_num allocates a number and sets fields to known values. */
bc_num _bc_new_num_ex(size_t length, size_t scale, bool persistent)
{
/* PHP Change: malloc() -> pemalloc(), removed free_list code */
bc_num temp = (bc_num) safe_pemalloc(1, sizeof(bc_struct) + length, scale, persistent);
temp->n_sign = PLUS;
temp->n_len = length;
temp->n_scale = scale;
temp->n_refs = 1;
/* PHP Change: malloc() -> pemalloc() */
temp->n_ptr = (char *) safe_pemalloc(1, length, scale, persistent);
temp->n_value = temp->n_ptr;
memset(temp->n_ptr, 0, length + scale);
Expand Down
3 changes: 2 additions & 1 deletion ext/bcmath/libbcmath/src/int2num.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ void bc_int2num(bc_num *num, int val)
while (val != 0) {
*bptr++ = val % BASE;
val = val / BASE;
ix++; /* Count the digits. */
/* Count the digits. */
ix++;
}

/* Make the number. */
Expand Down
7 changes: 2 additions & 5 deletions ext/bcmath/libbcmath/src/nearzero.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include <stdbool.h>
#include "bcmath.h"
#include <stddef.h>

/* In some places we need to check if the number NUM is almost zero.
Specifically, all but the last digit is 0 and the last digit is 1.
Expand All @@ -52,9 +53,5 @@ bool bc_is_near_zero(bc_num num, size_t scale)
count--;
}

if (count == 0 || (count == 1 && *--nptr == 1)) {
return true;
} else {
return false;
}
return count == 0 || (count == 1 && *--nptr == 1);
}
3 changes: 2 additions & 1 deletion ext/bcmath/libbcmath/src/num2long.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*************************************************************************/

#include "bcmath.h"
#include <stddef.h>

/* Convert a number NUM to a long. The function returns only the integer
part of the number. For numbers that are too large to represent as
Expand All @@ -40,7 +41,7 @@ long bc_num2long(bc_num num)
{
/* Extract the int value, ignore the fraction. */
long val = 0;
char *nptr = num->n_value;
const char *nptr = num->n_value;
for (size_t index = num->n_len; index > 0; index--) {
char n = *nptr++;

Expand Down
10 changes: 5 additions & 5 deletions ext/bcmath/libbcmath/src/num2str.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ zend_string *bc_num2str_ex(bc_num num, size_t scale)
{
zend_string *str;
char *sptr;
char *nptr;
int index, signch;
const char *nptr = num->n_value;;
size_t index;
int signch;

/* Number of sign chars. */
signch = num->n_sign != PLUS && !bc_is_zero_for_scale(num, MIN(num->n_scale, scale));
Expand All @@ -55,7 +56,6 @@ zend_string *bc_num2str_ex(bc_num num, size_t scale)
if (signch) *sptr++ = '-';

/* Load the whole number. */
nptr = num->n_value;
for (index = num->n_len; index > 0; index--) {
*sptr++ = BCD_CHAR(*nptr++);
}
Expand All @@ -73,6 +73,6 @@ zend_string *bc_num2str_ex(bc_num num, size_t scale)

/* Terminate the string and return it! */
*sptr = '\0';
ZSTR_LEN(str) = sptr - (char *) ZSTR_VAL(str);
return str;
ZSTR_LEN(str) = sptr - ZSTR_VAL(str);
return (zend_string*)str;
}
5 changes: 4 additions & 1 deletion ext/bcmath/libbcmath/src/raise.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "bcmath.h"
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>


/* Raise NUM1 to the NUM2 power. The result is placed in RESULT.
Expand All @@ -41,7 +42,9 @@
void bc_raise(bc_num num1, long exponent, bc_num *result, size_t scale)
{
bc_num temp, power;
size_t rscale, pwrscale, calcscale;
size_t rscale;
size_t pwrscale;
size_t calcscale;
bool is_neg;

/* Special case if exponent is a zero. */
Expand Down
1 change: 1 addition & 0 deletions ext/bcmath/libbcmath/src/raisemod.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*************************************************************************/

#include "bcmath.h"
#include <stddef.h>

/* Raise BASE to the EXPO power, reduced modulo MOD. The result is placed in RESULT. */
raise_mod_status bc_raisemod(bc_num base, bc_num expo, bc_num mod, bc_num *result, size_t scale)
Expand Down
3 changes: 1 addition & 2 deletions ext/bcmath/libbcmath/src/recmul.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ static void _bc_rec_mul(bc_num u, size_t ulen, bc_num v, size_t vlen, bc_num *pr
/* Base case? */
if ((ulen + vlen) < mul_base_digits
|| ulen < MUL_SMALL_DIGITS
|| vlen < MUL_SMALL_DIGITS
) {
|| vlen < MUL_SMALL_DIGITS) {
_bc_simp_mul(u, ulen, v, vlen, prod);
return;
}
Expand Down
1 change: 1 addition & 0 deletions ext/bcmath/libbcmath/src/sqrt.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "bcmath.h"
#include <stdbool.h>
#include <stddef.h>

/* Take the square root NUM and return it in NUM with SCALE digits
after the decimal place. */
Expand Down
1 change: 1 addition & 0 deletions ext/bcmath/libbcmath/src/str2num.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "bcmath.h"
#include <stdbool.h>
#include <stddef.h>

/* Convert strings to bc numbers. Base 10 only.*/

Expand Down
1 change: 1 addition & 0 deletions ext/bcmath/libbcmath/src/zero.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "bcmath.h"
#include <stdbool.h>
#include <stddef.h>

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

Expand Down