mirror of
https://github.com/php/php-src.git
synced 2025-08-15 21:48:51 +02:00
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:
parent
bb092ab4c6
commit
e56ed6e1ab
21 changed files with 265 additions and 286 deletions
|
@ -42,8 +42,6 @@
|
||||||
void bc_add(bc_num n1, bc_num n2, bc_num *result, size_t scale_min)
|
void bc_add(bc_num n1, bc_num n2, bc_num *result, size_t scale_min)
|
||||||
{
|
{
|
||||||
bc_num sum = NULL;
|
bc_num sum = NULL;
|
||||||
int cmp_res;
|
|
||||||
size_t res_scale;
|
|
||||||
|
|
||||||
if (n1->n_sign == n2->n_sign) {
|
if (n1->n_sign == n2->n_sign) {
|
||||||
sum = _bc_do_add(n1, n2, scale_min);
|
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 {
|
} else {
|
||||||
/* subtraction must be done. */
|
/* subtraction must be done. */
|
||||||
/* Compare magnitudes. */
|
/* Compare magnitudes. */
|
||||||
cmp_res = _bc_do_compare(n1, n2, false, false);
|
switch (_bc_do_compare(n1, n2, false, false)) {
|
||||||
switch (cmp_res) {
|
|
||||||
case -1:
|
case -1:
|
||||||
/* n1 is less than n2, subtract n1 from n2. */
|
/* n1 is less than n2, subtract n1 from n2. */
|
||||||
sum = _bc_do_sub(n2, n1, scale_min);
|
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;
|
break;
|
||||||
case 0:
|
case 0:
|
||||||
/* They are equal! return zero with the correct scale! */
|
/* 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, MAX(scale_min, MAX(n1->n_scale, n2->n_scale)));
|
||||||
sum = bc_new_num (1, res_scale);
|
memset(sum->n_value, 0, sum->n_scale + 1);
|
||||||
memset (sum->n_value, 0, res_scale+1);
|
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
/* n2 is less than n1, subtract n2 from n1. */
|
/* n2 is less than n1, subtract n2 from n1. */
|
||||||
|
|
|
@ -61,7 +61,7 @@ typedef struct bc_struct {
|
||||||
#include "../../php_bcmath.h" /* Needed for BCG() macro */
|
#include "../../php_bcmath.h" /* Needed for BCG() macro */
|
||||||
|
|
||||||
/* The base used in storing the numbers in n_value above.
|
/* The base used in storing the numbers in n_value above.
|
||||||
Currently this MUST be 10. */
|
Currently, this MUST be 10. */
|
||||||
|
|
||||||
#define BASE 10
|
#define BASE 10
|
||||||
|
|
||||||
|
|
|
@ -30,9 +30,9 @@
|
||||||
*************************************************************************/
|
*************************************************************************/
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
|
||||||
#include "bcmath.h"
|
#include "bcmath.h"
|
||||||
#include "private.h"
|
#include "private.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
|
||||||
/* Compare two bc numbers. Return value is 0 if equal, -1 if N1 is less
|
/* 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. */
|
/* First, compare signs. */
|
||||||
if (use_sign && n1->n_sign != n2->n_sign) {
|
if (use_sign && n1->n_sign != n2->n_sign) {
|
||||||
if (n1->n_sign == PLUS) {
|
if (n1->n_sign == PLUS) {
|
||||||
return (1); /* Positive N1 > Negative N2 */
|
/* Positive N1 > Negative N2 */
|
||||||
|
return (1);
|
||||||
} else {
|
} else {
|
||||||
return (-1); /* Negative N1 < Positive N1 */
|
/* Negative N1 < Positive N1 */
|
||||||
|
return (-1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,7 @@
|
||||||
#include "private.h"
|
#include "private.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <string.h>
|
||||||
#include "zend_alloc.h"
|
#include "zend_alloc.h"
|
||||||
|
|
||||||
|
|
||||||
|
@ -43,7 +44,7 @@
|
||||||
|
|
||||||
static void _one_mult(unsigned char *num, size_t size, int digit, unsigned char *result)
|
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;
|
unsigned char *nptr, *rptr;
|
||||||
|
|
||||||
if (digit == 0) {
|
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 len1, len2, scale2, qdigits, extra, count;
|
||||||
unsigned int qdig, qguess, borrow, carry;
|
unsigned int qdig, qguess, borrow, carry;
|
||||||
unsigned char *mval;
|
unsigned char *mval;
|
||||||
bool zero;
|
|
||||||
unsigned int norm;
|
unsigned int norm;
|
||||||
|
bool zero;
|
||||||
|
|
||||||
/* Test for divide by zero. */
|
/* Test for divide by zero. */
|
||||||
if (bc_is_zero(n2)) {
|
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. */
|
/* Test for divide by 1. If it is we must truncate. */
|
||||||
if (n2->n_scale == 0) {
|
if (n2->n_scale == 0 && n2->n_len == 1 && *n2->n_value == 1) {
|
||||||
if (n2->n_len == 1 && *n2->n_value == 1) {
|
|
||||||
qval = bc_new_num (n1->n_len, scale);
|
qval = bc_new_num (n1->n_len, scale);
|
||||||
qval->n_sign = (n1->n_sign == n2->n_sign ? PLUS : MINUS);
|
qval->n_sign = (n1->n_sign == n2->n_sign ? PLUS : MINUS);
|
||||||
memset(&qval->n_value[n1->n_len], 0, scale);
|
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);
|
bc_free_num (quot);
|
||||||
*quot = qval;
|
*quot = qval;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/* Set up the divide. Move the decimal point on n1 by n2's scale.
|
/* 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. */
|
Remember, zeros on the end of num2 are wasted effort for dividing. */
|
||||||
scale2 = n2->n_scale;
|
scale2 = n2->n_scale;
|
||||||
n2ptr = (unsigned char *) n2->n_value + n2->n_len + scale2 - 1;
|
n2ptr = (unsigned char *) n2->n_value + n2->n_len + scale2 - 1;
|
||||||
while ((scale2 > 0) && (*n2ptr-- == 0)) {
|
while ((scale2 > 0) && (*n2ptr == 0)) {
|
||||||
scale2--;
|
scale2--;
|
||||||
|
n2ptr--;
|
||||||
}
|
}
|
||||||
|
|
||||||
len1 = n1->n_len + scale2;
|
len1 = n1->n_len + scale2;
|
||||||
scale1 = n1->n_scale - scale2;
|
scale1 = n1->n_scale - scale2;
|
||||||
if (scale1 < scale) {
|
extra = MAX(scale - scale1, 0);
|
||||||
extra = scale - scale1;
|
|
||||||
} else {
|
|
||||||
extra = 0;
|
|
||||||
}
|
|
||||||
num1 = (unsigned char *) safe_emalloc(1, n1->n_len + n1->n_scale, extra + 2);
|
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);
|
memset(num1, 0, n1->n_len + n1->n_scale + extra + 2);
|
||||||
memcpy(num1 + 1, n1->n_value, n1->n_len + n1->n_scale);
|
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. */
|
/* Test qguess. */
|
||||||
if (
|
if (n2ptr[1] * qguess > (num1[qdig] * 10 + num1[qdig + 1] - *n2ptr * qguess) * 10 + num1[qdig + 2]) {
|
||||||
n2ptr[1]*qguess > (num1[qdig]*10 + num1[qdig+1] - *n2ptr*qguess)*10 + num1[qdig+2]
|
|
||||||
) {
|
|
||||||
qguess--;
|
qguess--;
|
||||||
/* And again. */
|
/* And again. */
|
||||||
if (
|
if (n2ptr[1] * qguess > (num1[qdig] * 10 + num1[qdig + 1] - *n2ptr * qguess) * 10 + num1[qdig + 2]) {
|
||||||
n2ptr[1]*qguess > (num1[qdig]*10 + num1[qdig+1] - *n2ptr*qguess)*10 + num1[qdig+2]
|
|
||||||
) {
|
|
||||||
qguess--;
|
qguess--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 _bc_do_sub(bc_num n1, bc_num n2, size_t scale_min)
|
||||||
{
|
{
|
||||||
bc_num diff;
|
bc_num diff;
|
||||||
int diff_scale, diff_len;
|
size_t diff_scale, diff_len;
|
||||||
size_t min_scale, min_len;
|
size_t min_scale, min_len;
|
||||||
|
size_t borrow, count;
|
||||||
|
int val;
|
||||||
char *n1ptr, *n2ptr, *diffptr;
|
char *n1ptr, *n2ptr, *diffptr;
|
||||||
int borrow, count, val;
|
|
||||||
|
|
||||||
/* Allocate temporary storage. */
|
/* Allocate temporary storage. */
|
||||||
diff_len = MAX(n1->n_len, n2->n_len);
|
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;
|
*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) {
|
if (diff_len != min_len) {
|
||||||
for (count = diff_len - min_len; count > 0; count--) {
|
for (count = diff_len - min_len; count > 0; count--) {
|
||||||
val = *n1ptr-- - borrow;
|
val = *n1ptr-- - borrow;
|
||||||
|
|
|
@ -32,14 +32,14 @@
|
||||||
#include "bcmath.h"
|
#include "bcmath.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <string.h>
|
||||||
#include "zend_alloc.h"
|
#include "zend_alloc.h"
|
||||||
|
|
||||||
/* new_num allocates a number and sets fields to known values. */
|
/* 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 _bc_new_num_ex(size_t length, size_t scale, bool persistent)
|
||||||
{
|
{
|
||||||
bc_num temp;
|
|
||||||
/* PHP Change: malloc() -> pemalloc(), removed free_list code */
|
/* 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_sign = PLUS;
|
||||||
temp->n_len = length;
|
temp->n_len = length;
|
||||||
temp->n_scale = scale;
|
temp->n_scale = scale;
|
||||||
|
|
|
@ -55,7 +55,8 @@ void bc_int2num(bc_num *num, int val)
|
||||||
while (val != 0) {
|
while (val != 0) {
|
||||||
*bptr++ = val % BASE;
|
*bptr++ = val % BASE;
|
||||||
val = val / BASE;
|
val = val / BASE;
|
||||||
ix++; /* Count the digits. */
|
/* Count the digits. */
|
||||||
|
ix++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Make the number. */
|
/* Make the number. */
|
||||||
|
|
|
@ -30,8 +30,8 @@
|
||||||
*************************************************************************/
|
*************************************************************************/
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
|
||||||
#include "bcmath.h"
|
#include "bcmath.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
/* In some places we need to check if the number NUM is almost zero.
|
/* 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.
|
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--;
|
count--;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count != 0 && (count != 1 || *--nptr != 1)) {
|
return count == 0 || (count == 1 && *--nptr == 1);
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,12 +39,9 @@
|
||||||
|
|
||||||
long bc_num2long(bc_num num)
|
long bc_num2long(bc_num num)
|
||||||
{
|
{
|
||||||
long val;
|
|
||||||
char *nptr;
|
|
||||||
|
|
||||||
/* Extract the int value, ignore the fraction. */
|
/* Extract the int value, ignore the fraction. */
|
||||||
val = 0;
|
long val = 0;
|
||||||
nptr = num->n_value;
|
const char *nptr = num->n_value;
|
||||||
for (size_t index = num->n_len; index > 0; index--) {
|
for (size_t index = num->n_len; index > 0; index--) {
|
||||||
char n = *nptr++;
|
char n = *nptr++;
|
||||||
|
|
||||||
|
|
|
@ -38,8 +38,8 @@ zend_string *bc_num2str_ex(bc_num num, size_t scale)
|
||||||
{
|
{
|
||||||
zend_string *str;
|
zend_string *str;
|
||||||
char *sptr;
|
char *sptr;
|
||||||
char *nptr;
|
size_t index;
|
||||||
int index, signch;
|
bool signch;
|
||||||
|
|
||||||
/* Number of sign chars. */
|
/* Number of sign chars. */
|
||||||
signch = num->n_sign != PLUS && !bc_is_zero_for_scale(num, MIN(num->n_scale, scale));
|
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++ = '-';
|
if (signch) *sptr++ = '-';
|
||||||
|
|
||||||
/* Load the whole number. */
|
/* Load the whole number. */
|
||||||
nptr = num->n_value;
|
const char *nptr = num->n_value;
|
||||||
for (index = num->n_len; index > 0; index--) {
|
for (index = num->n_len; index > 0; index--) {
|
||||||
*sptr++ = BCD_CHAR(*nptr++);
|
*sptr++ = BCD_CHAR(*nptr++);
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,7 @@
|
||||||
|
|
||||||
#include "bcmath.h"
|
#include "bcmath.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <string.h>
|
||||||
#include "zend_alloc.h"
|
#include "zend_alloc.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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)
|
static bc_num new_sub_num(size_t length, size_t scale, char *value)
|
||||||
{
|
{
|
||||||
bc_num temp;
|
bc_num temp = (bc_num) emalloc(sizeof(bc_struct));
|
||||||
|
|
||||||
temp = (bc_num) emalloc (sizeof(bc_struct));
|
|
||||||
|
|
||||||
temp->n_sign = PLUS;
|
temp->n_sign = PLUS;
|
||||||
temp->n_len = length;
|
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 *n1ptr, *n2ptr, *pvptr;
|
||||||
char *n1end, *n2end; /* To the end of n1 and n2. */
|
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);
|
*prod = bc_new_num (prodlen, 0);
|
||||||
|
|
||||||
n1end = (char *) (n1->n_value + n1len - 1);
|
n1end = (char *) (n1->n_value + n1len - 1);
|
||||||
n2end = (char *) (n2->n_value + n2len - 1);
|
n2end = (char *) (n2->n_value + n2len - 1);
|
||||||
pvptr = (char *) ((*prod)->n_value + prodlen - 1);
|
pvptr = (char *) ((*prod)->n_value + prodlen - 1);
|
||||||
sum = 0;
|
|
||||||
|
|
||||||
/* Here is the loop... */
|
/* Here is the loop... */
|
||||||
for (indx = 0; indx < prodlen-1; indx++) {
|
for (int index = 0; index < prodlen - 1; index++) {
|
||||||
n1ptr = (char *) (n1end - MAX(0, indx-n2len+1));
|
n1ptr = (char *) (n1end - MAX(0, index - n2len + 1));
|
||||||
n2ptr = (char *) (n2end - MIN(indx, n2len-1));
|
n2ptr = (char *) (n2end - MIN(index, n2len - 1));
|
||||||
while ((n1ptr >= n1->n_value) && (n2ptr <= n2end)) {
|
while ((n1ptr >= n1->n_value) && (n2ptr <= n2end)) {
|
||||||
sum += *n1ptr * *n2ptr;
|
sum += *n1ptr * *n2ptr;
|
||||||
n1ptr--;
|
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 u0, u1, v0, v1;
|
||||||
bc_num m1, m2, m3, d1, d2;
|
bc_num m1, m2, m3, d1, d2;
|
||||||
int n, prodlen, m1zero;
|
size_t n;
|
||||||
int d1len, d2len;
|
bool m1zero;
|
||||||
|
|
||||||
/* Base case? */
|
/* Base case? */
|
||||||
if ((ulen + vlen) < mul_base_digits
|
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(&d1);
|
||||||
bc_init_num(&d2);
|
bc_init_num(&d2);
|
||||||
bc_sub(u1, u0, &d1, 0);
|
bc_sub(u1, u0, &d1, 0);
|
||||||
d1len = d1->n_len;
|
|
||||||
bc_sub(v0, v1, &d2, 0);
|
bc_sub(v0, v1, &d2, 0);
|
||||||
d2len = d2->n_len;
|
|
||||||
|
|
||||||
|
|
||||||
/* Do recursive multiplies and shifted adds. */
|
/* 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)) {
|
if (bc_is_zero(d1) || bc_is_zero(d2)) {
|
||||||
m2 = bc_copy_num(BCG(_zero_));
|
m2 = bc_copy_num(BCG(_zero_));
|
||||||
} else {
|
} 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)) {
|
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 */
|
/* Initialize product */
|
||||||
prodlen = ulen+vlen+1;
|
*prod = bc_new_num(ulen + vlen + 1, 0);
|
||||||
*prod = bc_new_num(prodlen, 0);
|
|
||||||
|
|
||||||
if (!m1zero) {
|
if (!m1zero) {
|
||||||
_bc_shift_addsub (*prod, m1, 2*n, 0);
|
_bc_shift_addsub(*prod, m1, 2 * n, false);
|
||||||
_bc_shift_addsub (*prod, m1, n, 0);
|
_bc_shift_addsub(*prod, m1, n, false);
|
||||||
}
|
}
|
||||||
_bc_shift_addsub (*prod, m3, n, 0);
|
_bc_shift_addsub(*prod, m3, n, false);
|
||||||
_bc_shift_addsub (*prod, m3, 0, 0);
|
_bc_shift_addsub(*prod, m3, 0, false);
|
||||||
_bc_shift_addsub(*prod, m2, n, d1->n_sign != d2->n_sign);
|
_bc_shift_addsub(*prod, m2, n, d1->n_sign != d2->n_sign);
|
||||||
|
|
||||||
/* Now clean up! */
|
/* Now clean up! */
|
||||||
|
|
|
@ -38,7 +38,7 @@
|
||||||
|
|
||||||
void _bc_rm_leading_zeros(bc_num num)
|
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) {
|
while (*num->n_value == 0 && num->n_len > 1) {
|
||||||
num->n_value++;
|
num->n_value++;
|
||||||
num->n_len--;
|
num->n_len--;
|
||||||
|
|
|
@ -30,8 +30,8 @@
|
||||||
*************************************************************************/
|
*************************************************************************/
|
||||||
|
|
||||||
#include "bcmath.h"
|
#include "bcmath.h"
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
/* Take the square root NUM and return it in NUM with SCALE digits
|
/* Take the square root NUM and return it in NUM with SCALE digits
|
||||||
after the decimal place. */
|
after the decimal place. */
|
||||||
|
|
|
@ -37,18 +37,17 @@
|
||||||
|
|
||||||
bool bc_str2num(bc_num *num, char *str, size_t scale)
|
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;
|
char *ptr, *nptr;
|
||||||
bool zero_int = false;
|
|
||||||
size_t trailing_zeros = 0;
|
size_t trailing_zeros = 0;
|
||||||
|
bool zero_int = false;
|
||||||
|
|
||||||
/* Prepare num. */
|
/* Prepare num. */
|
||||||
bc_free_num (num);
|
bc_free_num (num);
|
||||||
|
|
||||||
/* Check for valid number and count digits. */
|
/* Check for valid number and count digits. */
|
||||||
ptr = str;
|
ptr = str;
|
||||||
digits = 0;
|
|
||||||
strscale = 0;
|
|
||||||
|
|
||||||
if ((*ptr == '+') || (*ptr == '-')) {
|
if ((*ptr == '+') || (*ptr == '-')) {
|
||||||
/* Skip Sign */
|
/* Skip Sign */
|
||||||
|
@ -79,7 +78,7 @@ bool bc_str2num (bc_num *num, char *str, size_t scale)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (trailing_zeros > 0) {
|
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;
|
strscale = strscale - trailing_zeros;
|
||||||
}
|
}
|
||||||
if ((*ptr != '\0') || (digits + strscale == 0)) {
|
if ((*ptr != '\0') || (digits + strscale == 0)) {
|
||||||
|
|
|
@ -33,6 +33,7 @@
|
||||||
#include "private.h"
|
#include "private.h"
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
/* Here is the full subtract routine that takes care of negative numbers.
|
/* 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
|
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)
|
void bc_sub(bc_num n1, bc_num n2, bc_num *result, size_t scale_min)
|
||||||
{
|
{
|
||||||
bc_num diff = NULL;
|
bc_num diff = NULL;
|
||||||
int cmp_res;
|
|
||||||
|
|
||||||
if (n1->n_sign != n2->n_sign) {
|
if (n1->n_sign != n2->n_sign) {
|
||||||
diff = _bc_do_add(n1, n2, scale_min);
|
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 {
|
} else {
|
||||||
/* subtraction must be done. */
|
/* subtraction must be done. */
|
||||||
/* Compare magnitudes. */
|
/* Compare magnitudes. */
|
||||||
cmp_res = _bc_do_compare(n1, n2, false, false);
|
switch (_bc_do_compare(n1, n2, false, false)) {
|
||||||
switch (cmp_res) {
|
|
||||||
case -1:
|
case -1:
|
||||||
/* n1 is less than n2, subtract n1 from n2. */
|
/* n1 is less than n2, subtract n1 from n2. */
|
||||||
diff = _bc_do_sub(n2, n1, scale_min);
|
diff = _bc_do_sub(n2, n1, scale_min);
|
||||||
|
|
|
@ -30,8 +30,8 @@
|
||||||
*************************************************************************/
|
*************************************************************************/
|
||||||
|
|
||||||
#include "bcmath.h"
|
#include "bcmath.h"
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
/* In some places we need to check if the number NUM is zero. */
|
/* In some places we need to check if the number NUM is zero. */
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue