BCmath extension code reformatting (#11896)

Re-formats the BCmath extension to have consistent formatting.

Mostly, it adds the spaces in calculations to have them more readable.

Also:

   -  removes unused headers
   -  removes few variables which are used only once in the code

Co-authored-by: George Peter Banyard <girgias@php.net>
This commit is contained in:
Jorg Adam Sowa 2023-08-13 17:17:36 +02:00 committed by GitHub
parent bb092ab4c6
commit e56ed6e1ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 265 additions and 286 deletions

View file

@ -42,8 +42,6 @@
void bc_add(bc_num n1, bc_num n2, bc_num *result, size_t scale_min)
{
bc_num sum = NULL;
int cmp_res;
size_t res_scale;
if (n1->n_sign == n2->n_sign) {
sum = _bc_do_add(n1, n2, scale_min);
@ -51,8 +49,7 @@ void bc_add (bc_num n1, bc_num n2, bc_num *result, size_t scale_min)
} else {
/* subtraction must be done. */
/* Compare magnitudes. */
cmp_res = _bc_do_compare(n1, n2, false, false);
switch (cmp_res) {
switch (_bc_do_compare(n1, n2, false, false)) {
case -1:
/* n1 is less than n2, subtract n1 from n2. */
sum = _bc_do_sub(n2, n1, scale_min);
@ -60,9 +57,8 @@ void bc_add (bc_num n1, bc_num n2, bc_num *result, size_t scale_min)
break;
case 0:
/* They are equal! return zero with the correct scale! */
res_scale = MAX (scale_min, MAX(n1->n_scale, n2->n_scale));
sum = bc_new_num (1, res_scale);
memset (sum->n_value, 0, res_scale+1);
sum = bc_new_num (1, MAX(scale_min, MAX(n1->n_scale, n2->n_scale)));
memset(sum->n_value, 0, sum->n_scale + 1);
break;
case 1:
/* n2 is less than n1, subtract n2 from n1. */

View file

@ -61,7 +61,7 @@ typedef struct bc_struct {
#include "../../php_bcmath.h" /* Needed for BCG() macro */
/* The base used in storing the numbers in n_value above.
Currently this MUST be 10. */
Currently, this MUST be 10. */
#define BASE 10

View file

@ -30,9 +30,9 @@
*************************************************************************/
#include <stdbool.h>
#include <stddef.h>
#include "bcmath.h"
#include "private.h"
#include <stddef.h>
/* Compare two bc numbers. Return value is 0 if equal, -1 if N1 is less
@ -46,9 +46,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);
}
}

View file

@ -33,6 +33,7 @@
#include "private.h"
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include "zend_alloc.h"
@ -43,7 +44,7 @@
static void _one_mult(unsigned char *num, size_t size, int digit, unsigned char *result)
{
int carry, value;
size_t carry, value;
unsigned char *nptr, *rptr;
if (digit == 0) {
@ -84,8 +85,8 @@ bool bc_divide(bc_num n1, bc_num n2, bc_num *quot, int scale)
unsigned int len1, len2, scale2, qdigits, extra, count;
unsigned int qdig, qguess, borrow, carry;
unsigned char *mval;
bool zero;
unsigned int norm;
bool zero;
/* Test for divide by zero. */
if (bc_is_zero(n2)) {
@ -93,8 +94,7 @@ bool bc_divide(bc_num n1, bc_num n2, bc_num *quot, int scale)
}
/* Test for divide by 1. If it is we must truncate. */
if (n2->n_scale == 0) {
if (n2->n_len == 1 && *n2->n_value == 1) {
if (n2->n_scale == 0 && n2->n_len == 1 && *n2->n_value == 1) {
qval = bc_new_num (n1->n_len, scale);
qval->n_sign = (n1->n_sign == n2->n_sign ? PLUS : MINUS);
memset(&qval->n_value[n1->n_len], 0, scale);
@ -102,23 +102,20 @@ bool bc_divide(bc_num n1, bc_num n2, bc_num *quot, int scale)
bc_free_num (quot);
*quot = qval;
}
}
/* Set up the divide. Move the decimal point on n1 by n2's scale.
Remember, zeros on the end of num2 are wasted effort for dividing. */
scale2 = n2->n_scale;
n2ptr = (unsigned char *) n2->n_value + n2->n_len + scale2 - 1;
while ((scale2 > 0) && (*n2ptr-- == 0)) {
while ((scale2 > 0) && (*n2ptr == 0)) {
scale2--;
n2ptr--;
}
len1 = n1->n_len + scale2;
scale1 = n1->n_scale - scale2;
if (scale1 < scale) {
extra = scale - scale1;
} else {
extra = 0;
}
extra = MAX(scale - scale1, 0);
num1 = (unsigned char *) safe_emalloc(1, n1->n_len + n1->n_scale, extra + 2);
memset(num1, 0, n1->n_len + n1->n_scale + extra + 2);
memcpy(num1 + 1, n1->n_value, n1->n_len + n1->n_scale);
@ -181,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--;
}
}

View file

@ -132,10 +132,11 @@ bc_num _bc_do_add(bc_num n1, bc_num n2, size_t scale_min)
bc_num _bc_do_sub(bc_num n1, bc_num n2, size_t scale_min)
{
bc_num diff;
int diff_scale, diff_len;
size_t diff_scale, diff_len;
size_t min_scale, min_len;
size_t borrow, count;
int val;
char *n1ptr, *n2ptr, *diffptr;
int borrow, count, val;
/* Allocate temporary storage. */
diff_len = MAX(n1->n_len, n2->n_len);
@ -192,7 +193,7 @@ bc_num _bc_do_sub(bc_num n1, bc_num n2, size_t scale_min)
*diffptr-- = val;
}
/* If n1 has more digits then n2, we now do that subtract. */
/* If n1 has more digits than n2, we now do that subtract. */
if (diff_len != min_len) {
for (count = diff_len - min_len; count > 0; count--) {
val = *n1ptr-- - borrow;

View file

@ -32,14 +32,14 @@
#include "bcmath.h"
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include "zend_alloc.h"
/* 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)
{
bc_num temp;
/* PHP Change: malloc() -> pemalloc(), removed free_list code */
temp = (bc_num) safe_pemalloc (1, sizeof(bc_struct)+length, scale, persistent);
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;

View file

@ -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. */

View file

@ -30,8 +30,8 @@
*************************************************************************/
#include <stdbool.h>
#include <stddef.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.
@ -53,9 +53,5 @@ bool bc_is_near_zero(bc_num num, size_t scale)
count--;
}
if (count != 0 && (count != 1 || *--nptr != 1)) {
return false;
} else {
return true;
}
return count == 0 || (count == 1 && *--nptr == 1);
}

View file

@ -39,12 +39,9 @@
long bc_num2long(bc_num num)
{
long val;
char *nptr;
/* Extract the int value, ignore the fraction. */
val = 0;
nptr = num->n_value;
long val = 0;
const char *nptr = num->n_value;
for (size_t index = num->n_len; index > 0; index--) {
char n = *nptr++;

View file

@ -38,8 +38,8 @@ zend_string *bc_num2str_ex(bc_num num, size_t scale)
{
zend_string *str;
char *sptr;
char *nptr;
int index, signch;
size_t index;
bool signch;
/* Number of sign chars. */
signch = num->n_sign != PLUS && !bc_is_zero_for_scale(num, MIN(num->n_scale, scale));
@ -55,7 +55,7 @@ zend_string *bc_num2str_ex(bc_num num, size_t scale)
if (signch) *sptr++ = '-';
/* Load the whole number. */
nptr = num->n_value;
const char *nptr = num->n_value;
for (index = num->n_len; index > 0; index--) {
*sptr++ = BCD_CHAR(*nptr++);
}

View file

@ -31,6 +31,7 @@
#include "bcmath.h"
#include <stdbool.h>
#include <string.h>
#include "zend_alloc.h"

View file

@ -50,9 +50,7 @@ int mul_base_digits = MUL_BASE_DIGITS;
static bc_num new_sub_num(size_t length, size_t scale, char *value)
{
bc_num temp;
temp = (bc_num) emalloc (sizeof(bc_struct));
bc_num temp = (bc_num) emalloc(sizeof(bc_struct));
temp->n_sign = PLUS;
temp->n_len = length;
@ -67,21 +65,20 @@ static void _bc_simp_mul(bc_num n1, size_t n1len, bc_num n2, int n2len, bc_num *
{
char *n1ptr, *n2ptr, *pvptr;
char *n1end, *n2end; /* To the end of n1 and n2. */
int indx, sum, prodlen;
int sum = 0;
prodlen = n1len+n2len+1;
int prodlen = n1len + n2len + 1;
*prod = bc_new_num (prodlen, 0);
n1end = (char *) (n1->n_value + n1len - 1);
n2end = (char *) (n2->n_value + n2len - 1);
pvptr = (char *) ((*prod)->n_value + prodlen - 1);
sum = 0;
/* Here is the loop... */
for (indx = 0; indx < prodlen-1; indx++) {
n1ptr = (char *) (n1end - MAX(0, indx-n2len+1));
n2ptr = (char *) (n2end - MIN(indx, n2len-1));
for (int index = 0; index < prodlen - 1; index++) {
n1ptr = (char *) (n1end - MAX(0, index - n2len + 1));
n2ptr = (char *) (n2end - MIN(index, n2len - 1));
while ((n1ptr >= n1->n_value) && (n2ptr <= n2end)) {
sum += *n1ptr * *n2ptr;
n1ptr--;
@ -168,8 +165,8 @@ static void _bc_rec_mul(bc_num u, size_t ulen, bc_num v, size_t vlen, bc_num *pr
{
bc_num u0, u1, v0, v1;
bc_num m1, m2, m3, d1, d2;
int n, prodlen, m1zero;
int d1len, d2len;
size_t n;
bool m1zero;
/* Base case? */
if ((ulen + vlen) < mul_base_digits
@ -210,9 +207,7 @@ static void _bc_rec_mul(bc_num u, size_t ulen, bc_num v, size_t vlen, bc_num *pr
bc_init_num(&d1);
bc_init_num(&d2);
bc_sub(u1, u0, &d1, 0);
d1len = d1->n_len;
bc_sub(v0, v1, &d2, 0);
d2len = d2->n_len;
/* Do recursive multiplies and shifted adds. */
@ -225,7 +220,7 @@ static void _bc_rec_mul(bc_num u, size_t ulen, bc_num v, size_t vlen, bc_num *pr
if (bc_is_zero(d1) || bc_is_zero(d2)) {
m2 = bc_copy_num(BCG(_zero_));
} else {
_bc_rec_mul (d1, d1len, d2, d2len, &m2);
_bc_rec_mul(d1, d1->n_len, d2, d2->n_len, &m2);
}
if (bc_is_zero(u0) || bc_is_zero(v0)) {
@ -235,15 +230,14 @@ static void _bc_rec_mul(bc_num u, size_t ulen, bc_num v, size_t vlen, bc_num *pr
}
/* Initialize product */
prodlen = ulen+vlen+1;
*prod = bc_new_num(prodlen, 0);
*prod = bc_new_num(ulen + vlen + 1, 0);
if (!m1zero) {
_bc_shift_addsub (*prod, m1, 2*n, 0);
_bc_shift_addsub (*prod, m1, n, 0);
_bc_shift_addsub(*prod, m1, 2 * n, false);
_bc_shift_addsub(*prod, m1, n, false);
}
_bc_shift_addsub (*prod, m3, n, 0);
_bc_shift_addsub (*prod, m3, 0, 0);
_bc_shift_addsub(*prod, m3, n, false);
_bc_shift_addsub(*prod, m3, 0, false);
_bc_shift_addsub(*prod, m2, n, d1->n_sign != d2->n_sign);
/* Now clean up! */

View file

@ -38,7 +38,7 @@
void _bc_rm_leading_zeros(bc_num num)
{
/* We can move n_value to point to the first non zero digit! */
/* We can move n_value to point to the first non-zero digit! */
while (*num->n_value == 0 && num->n_len > 1) {
num->n_value++;
num->n_len--;

View file

@ -30,8 +30,8 @@
*************************************************************************/
#include "bcmath.h"
#include <stddef.h>
#include <stdbool.h>
#include <stddef.h>
/* Take the square root NUM and return it in NUM with SCALE digits
after the decimal place. */

View file

@ -37,18 +37,17 @@
bool bc_str2num(bc_num *num, char *str, size_t scale)
{
size_t digits, strscale;
size_t digits = 0;
size_t strscale = 0;
char *ptr, *nptr;
bool zero_int = false;
size_t trailing_zeros = 0;
bool zero_int = false;
/* Prepare num. */
bc_free_num (num);
/* Check for valid number and count digits. */
ptr = str;
digits = 0;
strscale = 0;
if ((*ptr == '+') || (*ptr == '-')) {
/* Skip Sign */
@ -79,7 +78,7 @@ bool bc_str2num (bc_num *num, char *str, size_t scale)
}
if (trailing_zeros > 0) {
/* Trailining zeros should not take part in the computation of the overall scale, as it is pointless. */
/* Trailing zeros should not take part in the computation of the overall scale, as it is pointless. */
strscale = strscale - trailing_zeros;
}
if ((*ptr != '\0') || (digits + strscale == 0)) {

View file

@ -33,6 +33,7 @@
#include "private.h"
#include <stddef.h>
#include <stdbool.h>
#include <string.h>
/* Here is the full subtract routine that takes care of negative numbers.
N2 is subtracted from N1 and the result placed in RESULT. SCALE_MIN
@ -41,7 +42,6 @@
void bc_sub(bc_num n1, bc_num n2, bc_num *result, size_t scale_min)
{
bc_num diff = NULL;
int cmp_res;
if (n1->n_sign != n2->n_sign) {
diff = _bc_do_add(n1, n2, scale_min);
@ -49,8 +49,7 @@ void bc_sub(bc_num n1, bc_num n2, bc_num *result, size_t scale_min)
} else {
/* subtraction must be done. */
/* Compare magnitudes. */
cmp_res = _bc_do_compare(n1, n2, false, false);
switch (cmp_res) {
switch (_bc_do_compare(n1, n2, false, false)) {
case -1:
/* n1 is less than n2, subtract n1 from n2. */
diff = _bc_do_sub(n2, n1, scale_min);

View file

@ -30,8 +30,8 @@
*************************************************************************/
#include "bcmath.h"
#include <stddef.h>
#include <stdbool.h>
#include <stddef.h>
/* In some places we need to check if the number NUM is zero. */