[ruby/bigdecimal] Rename BDIGIT to DECDIG

https://github.com/ruby/bigdecimal/commit/686487d942
This commit is contained in:
Kenta Murata 2021-01-06 11:54:17 +09:00
parent 7da06c04b2
commit fa8295ccb9
No known key found for this signature in database
GPG Key ID: CEFE8AFB6081B062
2 changed files with 136 additions and 143 deletions

View File

@ -115,7 +115,7 @@ static ID id_half;
*/ */
static unsigned short VpGetException(void); static unsigned short VpGetException(void);
static void VpSetException(unsigned short f); static void VpSetException(unsigned short f);
static void VpInternalRound(Real *c, size_t ixDigit, BDIGIT vPrev, BDIGIT v); static void VpInternalRound(Real *c, size_t ixDigit, DECDIG vPrev, DECDIG v);
static int VpLimitRound(Real *c, size_t ixDigit); static int VpLimitRound(Real *c, size_t ixDigit);
static Real *VpCopy(Real *pv, Real const* const x); static Real *VpCopy(Real *pv, Real const* const x);
@ -137,7 +137,7 @@ static size_t
BigDecimal_memsize(const void *ptr) BigDecimal_memsize(const void *ptr)
{ {
const Real *pv = ptr; const Real *pv = ptr;
return (sizeof(*pv) + pv->MaxPrec * sizeof(BDIGIT)); return (sizeof(*pv) + pv->MaxPrec * sizeof(DECDIG));
} }
#ifndef HAVE_RB_EXT_RACTOR_SAFE #ifndef HAVE_RB_EXT_RACTOR_SAFE
@ -381,7 +381,7 @@ BigDecimal_precision(VALUE self)
ex = 0; ex = 0;
} }
else if (p->Prec > 0) { else if (p->Prec > 0) {
BDIGIT x = p->frac[0]; DECDIG x = p->frac[0];
for (precision = 0; x > 0; x /= 10) { for (precision = 0; x > 0; x /= 10) {
++precision; ++precision;
} }
@ -397,7 +397,7 @@ BigDecimal_precision(VALUE self)
precision += n * BASE_FIG; precision += n * BASE_FIG;
if (ex < (ssize_t)p->Prec) { if (ex < (ssize_t)p->Prec) {
BDIGIT x = p->frac[n]; DECDIG x = p->frac[n];
for (; x > 0 && x % 10 == 0; x /= 10) { for (; x > 0 && x % 10 == 0; x /= 10) {
--precision; --precision;
} }
@ -423,7 +423,7 @@ BigDecimal_n_significant_digits(VALUE self)
int nlz, ntz; int nlz, ntz;
BDIGIT x = p->frac[0]; DECDIG x = p->frac[0];
for (nlz = BASE_FIG; x > 0; x /= 10) --nlz; for (nlz = BASE_FIG; x > 0; x /= 10) --nlz;
x = p->frac[n-1]; x = p->frac[n-1];
@ -452,8 +452,8 @@ BigDecimal_hash(VALUE self)
hash = (st_index_t)p->sign; hash = (st_index_t)p->sign;
/* hash!=2: the case for 0(1),NaN(0) or +-Infinity(3) is sign itself */ /* hash!=2: the case for 0(1),NaN(0) or +-Infinity(3) is sign itself */
if(hash == 2 || hash == (st_index_t)-2) { if(hash == 2 || hash == (st_index_t)-2) {
hash ^= rb_memhash(p->frac, sizeof(BDIGIT)*p->Prec); hash ^= rb_memhash(p->frac, sizeof(DECDIG)*p->Prec);
hash += p->exponent; hash += p->exponent;
} }
return ST2FIX(hash); return ST2FIX(hash);
} }
@ -760,8 +760,8 @@ VpCreateRbObject(size_t mx, const char *str, bool raise_exception)
return VpNewRbClass(mx, str, rb_cBigDecimal, true, raise_exception); return VpNewRbClass(mx, str, rb_cBigDecimal, true, raise_exception);
} }
#define VpAllocReal(prec) (Real *)VpMemAlloc(offsetof(Real, frac) + (prec) * sizeof(BDIGIT)) #define VpAllocReal(prec) (Real *)VpMemAlloc(offsetof(Real, frac) + (prec) * sizeof(DECDIG))
#define VpReallocReal(ptr, prec) (Real *)VpMemRealloc((ptr), offsetof(Real, frac) + (prec) * sizeof(BDIGIT)) #define VpReallocReal(ptr, prec) (Real *)VpMemRealloc((ptr), offsetof(Real, frac) + (prec) * sizeof(DECDIG))
static Real * static Real *
VpCopy(Real *pv, Real const* const x) VpCopy(Real *pv, Real const* const x)
@ -774,7 +774,7 @@ VpCopy(Real *pv, Real const* const x)
pv->exponent = x->exponent; pv->exponent = x->exponent;
pv->sign = x->sign; pv->sign = x->sign;
pv->flag = x->flag; pv->flag = x->flag;
MEMCPY(pv->frac, x->frac, BDIGIT, pv->MaxPrec); MEMCPY(pv->frac, x->frac, DECDIG, pv->MaxPrec);
return pv; return pv;
} }
@ -836,7 +836,7 @@ BigDecimal_to_i(VALUE self)
if (e <= 0) return INT2FIX(0); if (e <= 0) return INT2FIX(0);
nf = VpBaseFig(); nf = VpBaseFig();
if (e <= nf) { if (e <= nf) {
return LONG2NUM((long)(VpGetSign(p) * (BDIGIT_DBL_SIGNED)p->frac[0])); return LONG2NUM((long)(VpGetSign(p) * (DECDIG_DBL_SIGNED)p->frac[0]));
} }
else { else {
VALUE a = BigDecimal_split(self); VALUE a = BigDecimal_split(self);
@ -1424,7 +1424,7 @@ BigDecimal_div(VALUE self, VALUE r)
*/ */
/* Round */ /* Round */
if (VpHasVal(div)) { /* frac[0] must be zero for NaN,INF,Zero */ if (VpHasVal(div)) { /* frac[0] must be zero for NaN,INF,Zero */
VpInternalRound(c, 0, c->frac[c->Prec-1], (BDIGIT)(VpBaseVal() * (BDIGIT_DBL)res->frac[0] / div->frac[0])); VpInternalRound(c, 0, c->frac[c->Prec-1], (DECDIG)(VpBaseVal() * (DECDIG_DBL)res->frac[0] / div->frac[0]));
} }
return VpCheckGetValue(c); return VpCheckGetValue(c);
} }
@ -2724,7 +2724,7 @@ rb_uint64_convert_to_BigDecimal(uint64_t uval, RB_UNUSED_VAR(size_t digs), int r
vp->Prec = 1; vp->Prec = 1;
vp->exponent = 1; vp->exponent = 1;
VpSetSign(vp, 1); VpSetSign(vp, 1);
vp->frac[0] = (BDIGIT)uval; vp->frac[0] = (DECDIG)uval;
} }
else { else {
const size_t len10 = ceil(LOG10_2 * bit_length(uval)); const size_t len10 = ceil(LOG10_2 * bit_length(uval));
@ -2738,7 +2738,7 @@ rb_uint64_convert_to_BigDecimal(uint64_t uval, RB_UNUSED_VAR(size_t digs), int r
size_t i; size_t i;
for (i = 0; i < len; ++i) { for (i = 0; i < len; ++i) {
BDIGIT r = uval % BASE; DECDIG r = uval % BASE;
vp->frac[len - i - 1] = r; vp->frac[len - i - 1] = r;
uval /= BASE; uval /= BASE;
} }
@ -3809,9 +3809,9 @@ enum op_sw {
static int VpIsDefOP(Real *c, Real *a, Real *b, enum op_sw sw); static int VpIsDefOP(Real *c, Real *a, Real *b, enum op_sw sw);
static int AddExponent(Real *a, SIGNED_VALUE n); static int AddExponent(Real *a, SIGNED_VALUE n);
static BDIGIT VpAddAbs(Real *a,Real *b,Real *c); static DECDIG VpAddAbs(Real *a,Real *b,Real *c);
static BDIGIT VpSubAbs(Real *a,Real *b,Real *c); static DECDIG VpSubAbs(Real *a,Real *b,Real *c);
static size_t VpSetPTR(Real *a, Real *b, Real *c, size_t *a_pos, size_t *b_pos, size_t *c_pos, BDIGIT *av, BDIGIT *bv); static size_t VpSetPTR(Real *a, Real *b, Real *c, size_t *a_pos, size_t *b_pos, size_t *c_pos, DECDIG *av, DECDIG *bv);
static int VpNmlz(Real *a); static int VpNmlz(Real *a);
static void VpFormatSt(char *psz, size_t fFmt); static void VpFormatSt(char *psz, size_t fFmt);
static int VpRdup(Real *m, size_t ind_m); static int VpRdup(Real *m, size_t ind_m);
@ -4215,13 +4215,13 @@ VpNumOfChars(Real *vp,const char *pszFmt)
* that BASE is as large as possible satisfying the * that BASE is as large as possible satisfying the
* relation MaxVal <= BASE*(BASE+1). Where the value * relation MaxVal <= BASE*(BASE+1). Where the value
* MaxVal is the largest value which can be represented * MaxVal is the largest value which can be represented
* by one BDIGIT word in the computer used. * by one DECDIG word in the computer used.
* *
* [Returns] * [Returns]
* DBLE_FIG ... OK * DBLE_FIG ... OK
*/ */
VP_EXPORT size_t VP_EXPORT size_t
VpInit(BDIGIT BaseVal) VpInit(DECDIG BaseVal)
{ {
/* Setup +/- Inf NaN -0 */ /* Setup +/- Inf NaN -0 */
VpGetDoubleNegZero(); VpGetDoubleNegZero();
@ -4236,12 +4236,12 @@ VpInit(BDIGIT BaseVal)
#ifdef BIGDECIMAL_DEBUG #ifdef BIGDECIMAL_DEBUG
if (gfDebug) { if (gfDebug) {
printf("VpInit: BaseVal = %"PRIuBDIGIT"\n", BaseVal); printf("VpInit: BaseVal = %"PRIuDECDIG"\n", BaseVal);
printf("\tBASE = %"PRIuBDIGIT"\n", BASE); printf("\tBASE = %"PRIuDECDIG"\n", BASE);
printf("\tHALF_BASE = %"PRIuBDIGIT"\n", HALF_BASE); printf("\tHALF_BASE = %"PRIuDECDIG"\n", HALF_BASE);
printf("\tBASE1 = %"PRIuBDIGIT"\n", BASE1); printf("\tBASE1 = %"PRIuDECDIG"\n", BASE1);
printf("\tBASE_FIG = %u\n", BASE_FIG); printf("\tBASE_FIG = %u\n", BASE_FIG);
printf("\tDBLE_FIG = %d\n", DBLE_FIG); printf("\tDBLE_FIG = %d\n", DBLE_FIG);
} }
#endif /* BIGDECIMAL_DEBUG */ #endif /* BIGDECIMAL_DEBUG */
@ -4601,7 +4601,7 @@ VpAsgn(Real *c, Real *a, int isw)
VpSetSign(c, isw * VpGetSign(a)); /* set sign */ VpSetSign(c, isw * VpGetSign(a)); /* set sign */
n = (a->Prec < c->MaxPrec) ? (a->Prec) : (c->MaxPrec); n = (a->Prec < c->MaxPrec) ? (a->Prec) : (c->MaxPrec);
c->Prec = n; c->Prec = n;
memcpy(c->frac, a->frac, n * sizeof(BDIGIT)); memcpy(c->frac, a->frac, n * sizeof(DECDIG));
/* Needs round ? */ /* Needs round ? */
if (isw != 10) { if (isw != 10) {
/* Not in ActiveRound */ /* Not in ActiveRound */
@ -4632,7 +4632,7 @@ VpAddSub(Real *c, Real *a, Real *b, int operation)
short sw, isw; short sw, isw;
Real *a_ptr, *b_ptr; Real *a_ptr, *b_ptr;
size_t n, na, nb, i; size_t n, na, nb, i;
BDIGIT mrv; DECDIG mrv;
#ifdef BIGDECIMAL_DEBUG #ifdef BIGDECIMAL_DEBUG
if (gfDebug) { if (gfDebug) {
@ -4760,7 +4760,7 @@ end_if:
* a and b assuming abs(a)>abs(b). * a and b assuming abs(a)>abs(b).
* c = abs(a) + abs(b) ; where |a|>=|b| * c = abs(a) + abs(b) ; where |a|>=|b|
*/ */
static BDIGIT static DECDIG
VpAddAbs(Real *a, Real *b, Real *c) VpAddAbs(Real *a, Real *b, Real *c)
{ {
size_t word_shift; size_t word_shift;
@ -4770,7 +4770,7 @@ VpAddAbs(Real *a, Real *b, Real *c)
size_t a_pos; size_t a_pos;
size_t b_pos, b_pos_with_word_shift; size_t b_pos, b_pos_with_word_shift;
size_t c_pos; size_t c_pos;
BDIGIT av, bv, carry, mrv; DECDIG av, bv, carry, mrv;
#ifdef BIGDECIMAL_DEBUG #ifdef BIGDECIMAL_DEBUG
if (gfDebug) { if (gfDebug) {
@ -4855,7 +4855,7 @@ Exit:
/* /*
* c = abs(a) - abs(b) * c = abs(a) - abs(b)
*/ */
static BDIGIT static DECDIG
VpSubAbs(Real *a, Real *b, Real *c) VpSubAbs(Real *a, Real *b, Real *c)
{ {
size_t word_shift; size_t word_shift;
@ -4865,7 +4865,7 @@ VpSubAbs(Real *a, Real *b, Real *c)
size_t a_pos; size_t a_pos;
size_t b_pos, b_pos_with_word_shift; size_t b_pos, b_pos_with_word_shift;
size_t c_pos; size_t c_pos;
BDIGIT av, bv, borrow, mrv; DECDIG av, bv, borrow, mrv;
#ifdef BIGDECIMAL_DEBUG #ifdef BIGDECIMAL_DEBUG
if (gfDebug) { if (gfDebug) {
@ -4972,7 +4972,7 @@ Exit:
* c_pos = | * c_pos = |
*/ */
static size_t static size_t
VpSetPTR(Real *a, Real *b, Real *c, size_t *a_pos, size_t *b_pos, size_t *c_pos, BDIGIT *av, BDIGIT *bv) VpSetPTR(Real *a, Real *b, Real *c, size_t *a_pos, size_t *b_pos, size_t *c_pos, DECDIG *av, DECDIG *bv)
{ {
size_t left_word, right_word, word_shift; size_t left_word, right_word, word_shift;
@ -5087,8 +5087,8 @@ VpMult(Real *c, Real *a, Real *b)
size_t MxIndA, MxIndB, MxIndAB, MxIndC; size_t MxIndA, MxIndB, MxIndAB, MxIndC;
size_t ind_c, i, ii, nc; size_t ind_c, i, ii, nc;
size_t ind_as, ind_ae, ind_bs; size_t ind_as, ind_ae, ind_bs;
BDIGIT carry; DECDIG carry;
BDIGIT_DBL s; DECDIG_DBL s;
Real *w; Real *w;
#ifdef BIGDECIMAL_DEBUG #ifdef BIGDECIMAL_DEBUG
@ -5142,7 +5142,7 @@ VpMult(Real *c, Real *a, Real *b)
VpSetSign(c, VpGetSign(a) * VpGetSign(b)); /* set sign */ VpSetSign(c, VpGetSign(a) * VpGetSign(b)); /* set sign */
carry = 0; carry = 0;
nc = ind_c = MxIndAB; nc = ind_c = MxIndAB;
memset(c->frac, 0, (nc + 1) * sizeof(BDIGIT)); /* Initialize c */ memset(c->frac, 0, (nc + 1) * sizeof(DECDIG)); /* Initialize c */
c->Prec = nc + 1; /* set precision */ c->Prec = nc + 1; /* set precision */
for (nc = 0; nc < MxIndAB; ++nc, --ind_c) { for (nc = 0; nc < MxIndAB; ++nc, --ind_c) {
if (nc < MxIndB) { /* The left triangle of the Fig. */ if (nc < MxIndB) { /* The left triangle of the Fig. */
@ -5162,15 +5162,15 @@ VpMult(Real *c, Real *a, Real *b)
} }
for (i = ind_as; i <= ind_ae; ++i) { for (i = ind_as; i <= ind_ae; ++i) {
s = (BDIGIT_DBL)a->frac[i] * b->frac[ind_bs--]; s = (DECDIG_DBL)a->frac[i] * b->frac[ind_bs--];
carry = (BDIGIT)(s / BASE); carry = (DECDIG)(s / BASE);
s -= (BDIGIT_DBL)carry * BASE; s -= (DECDIG_DBL)carry * BASE;
c->frac[ind_c] += (BDIGIT)s; c->frac[ind_c] += (DECDIG)s;
if (c->frac[ind_c] >= BASE) { if (c->frac[ind_c] >= BASE) {
s = c->frac[ind_c] / BASE; s = c->frac[ind_c] / BASE;
carry += (BDIGIT)s; carry += (DECDIG)s;
c->frac[ind_c] -= (BDIGIT)(s * BASE); c->frac[ind_c] -= (DECDIG)(s * BASE);
} }
if (carry) { if (carry) {
ii = ind_c; ii = ind_c;
while (ii-- > 0) { while (ii-- > 0) {
@ -5216,9 +5216,9 @@ VpDivd(Real *c, Real *r, Real *a, Real *b)
size_t word_a, word_b, word_c, word_r; size_t word_a, word_b, word_c, word_r;
size_t i, n, ind_a, ind_b, ind_c, ind_r; size_t i, n, ind_a, ind_b, ind_c, ind_r;
size_t nLoop; size_t nLoop;
BDIGIT_DBL q, b1, b1p1, b1b2, b1b2p1, r1r2; DECDIG_DBL q, b1, b1p1, b1b2, b1b2p1, r1r2;
BDIGIT borrow, borrow1, borrow2; DECDIG borrow, borrow1, borrow2;
BDIGIT_DBL qb; DECDIG_DBL qb;
#ifdef BIGDECIMAL_DEBUG #ifdef BIGDECIMAL_DEBUG
if (gfDebug) { if (gfDebug) {
@ -5290,7 +5290,7 @@ VpDivd(Real *c, Real *r, Real *a, Real *b)
++ind_c; ++ind_c;
continue; continue;
} }
r1r2 = (BDIGIT_DBL)r->frac[ind_c] * BASE + r->frac[ind_c + 1]; r1r2 = (DECDIG_DBL)r->frac[ind_c] * BASE + r->frac[ind_c + 1];
if (r1r2 == b1b2) { if (r1r2 == b1b2) {
/* The first two word digits is the same */ /* The first two word digits is the same */
ind_b = 2; ind_b = 2;
@ -5327,17 +5327,17 @@ VpDivd(Real *c, Real *r, Real *a, Real *b)
/* The first two word digits is not the same, */ /* The first two word digits is not the same, */
/* then compare magnitude, and divide actually. */ /* then compare magnitude, and divide actually. */
if (r1r2 >= b1b2p1) { if (r1r2 >= b1b2p1) {
q = r1r2 / b1b2p1; /* q == (BDIGIT)q */ q = r1r2 / b1b2p1; /* q == (DECDIG)q */
c->frac[ind_c] += (BDIGIT)q; c->frac[ind_c] += (DECDIG)q;
ind_r = b->Prec + ind_c - 1; ind_r = b->Prec + ind_c - 1;
goto sub_mult; goto sub_mult;
} }
div_b1p1: div_b1p1:
if (ind_c + 1 >= word_c) goto out_side; if (ind_c + 1 >= word_c) goto out_side;
q = r1r2 / b1p1; /* q == (BDIGIT)q */ q = r1r2 / b1p1; /* q == (DECDIG)q */
c->frac[ind_c + 1] += (BDIGIT)q; c->frac[ind_c + 1] += (DECDIG)q;
ind_r = b->Prec + ind_c; ind_r = b->Prec + ind_c;
sub_mult: sub_mult:
borrow1 = borrow2 = 0; borrow1 = borrow2 = 0;
@ -5349,16 +5349,16 @@ sub_mult:
qb = q * b->frac[ind_b]; qb = q * b->frac[ind_b];
if (qb < BASE) borrow1 = 0; if (qb < BASE) borrow1 = 0;
else { else {
borrow1 = (BDIGIT)(qb / BASE); borrow1 = (DECDIG)(qb / BASE);
qb -= (BDIGIT_DBL)borrow1 * BASE; /* get qb < BASE */ qb -= (DECDIG_DBL)borrow1 * BASE; /* get qb < BASE */
} }
if(r->frac[ind_r] < qb) { if(r->frac[ind_r] < qb) {
r->frac[ind_r] += (BDIGIT)(BASE - qb); r->frac[ind_r] += (DECDIG)(BASE - qb);
borrow2 = borrow2 + borrow1 + 1; borrow2 = borrow2 + borrow1 + 1;
} }
else { else {
r->frac[ind_r] -= (BDIGIT)qb; r->frac[ind_r] -= (DECDIG)qb;
borrow2 += borrow1; borrow2 += borrow1;
} }
if (borrow2) { if (borrow2) {
if(r->frac[ind_r - 1] < borrow2) { if(r->frac[ind_r - 1] < borrow2) {
@ -5440,9 +5440,9 @@ VpNmlz(Real *a)
i = 0; i = 0;
while (a->frac[i] == 0) ++i; /* skip the first few zeros */ while (a->frac[i] == 0) ++i; /* skip the first few zeros */
if (i) { if (i) {
a->Prec -= i; a->Prec -= i;
if (!AddExponent(a, -(SIGNED_VALUE)i)) return 0; if (!AddExponent(a, -(SIGNED_VALUE)i)) return 0;
memmove(&a->frac[0], &a->frac[i], a->Prec*sizeof(BDIGIT)); memmove(&a->frac[0], &a->frac[i], a->Prec*sizeof(DECDIG));
} }
return 1; return 1;
} }
@ -5565,7 +5565,7 @@ static int
VPrint(FILE *fp, const char *cntl_chr, Real *a) VPrint(FILE *fp, const char *cntl_chr, Real *a)
{ {
size_t i, j, nc, nd, ZeroSup, sep = 10; size_t i, j, nc, nd, ZeroSup, sep = 10;
BDIGIT m, e, nn; DECDIG m, e, nn;
j = 0; j = 0;
nd = nc = 0; /* nd : number of digits in fraction part(every 10 digits, */ nd = nc = 0; /* nd : number of digits in fraction part(every 10 digits, */
@ -5709,7 +5709,7 @@ VP_EXPORT void
VpSzMantissa(Real *a,char *psz) VpSzMantissa(Real *a,char *psz)
{ {
size_t i, n, ZeroSup; size_t i, n, ZeroSup;
BDIGIT_DBL m, e, nn; DECDIG_DBL m, e, nn;
if (VpIsNaN(a)) { if (VpIsNaN(a)) {
sprintf(psz, SZ_NaN); sprintf(psz, SZ_NaN);
@ -5792,7 +5792,7 @@ VpToString(Real *a, char *psz, size_t fFmt, int fPlus)
/* fPlus = 0: default, 1: set ' ' before digits, 2: set '+' before digits. */ /* fPlus = 0: default, 1: set ' ' before digits, 2: set '+' before digits. */
{ {
size_t i, n, ZeroSup; size_t i, n, ZeroSup;
BDIGIT shift, m, e, nn; DECDIG shift, m, e, nn;
char *pszSav = psz; char *pszSav = psz;
ssize_t ex; ssize_t ex;
@ -5840,7 +5840,7 @@ VpToFString(Real *a, char *psz, size_t fFmt, int fPlus)
/* fPlus = 0: default, 1: set ' ' before digits, 2: set '+' before digits. */ /* fPlus = 0: default, 1: set ' ' before digits, 2: set '+' before digits. */
{ {
size_t i, n; size_t i, n;
BDIGIT m, e, nn; DECDIG m, e, nn;
char *pszSav = psz; char *pszSav = psz;
ssize_t ex; ssize_t ex;
@ -5915,7 +5915,7 @@ VpCtoV(Real *a, const char *int_chr, size_t ni, const char *frac, size_t nf, con
me = ne; me = ne;
signe = 1; signe = 1;
exponent_overflow = 0; exponent_overflow = 0;
memset(a->frac, 0, ma * sizeof(BDIGIT)); memset(a->frac, 0, ma * sizeof(DECDIG));
if (ne > 0) { if (ne > 0) {
i = 0; i = 0;
if (exp_chr[0] == '-') { if (exp_chr[0] == '-') {
@ -6130,7 +6130,7 @@ VpDtoV(Real *m, double d)
{ {
size_t ind_m, mm; size_t ind_m, mm;
SIGNED_VALUE ne; SIGNED_VALUE ne;
BDIGIT i; DECDIG i;
double val, val2; double val, val2;
if (isnan(d)) { if (isnan(d)) {
@ -6165,12 +6165,12 @@ VpDtoV(Real *m, double d)
/* Now val = 0.xxxxx*BASE**ne */ /* Now val = 0.xxxxx*BASE**ne */
mm = m->MaxPrec; mm = m->MaxPrec;
memset(m->frac, 0, mm * sizeof(BDIGIT)); memset(m->frac, 0, mm * sizeof(DECDIG));
for (ind_m = 0; val > 0.0 && ind_m < mm; ind_m++) { for (ind_m = 0; val > 0.0 && ind_m < mm; ind_m++) {
val *= (double)BASE; val *= (double)BASE;
i = (BDIGIT)val; i = (DECDIG)val;
val -= (double)i; val -= (double)i;
m->frac[ind_m] = i; m->frac[ind_m] = i;
} }
if (ind_m >= mm) ind_m = mm - 1; if (ind_m >= mm) ind_m = mm - 1;
VpSetSign(m, (d > 0.0) ? 1 : -1); VpSetSign(m, (d > 0.0) ? 1 : -1);
@ -6178,7 +6178,7 @@ VpDtoV(Real *m, double d)
m->exponent = ne; m->exponent = ne;
VpInternalRound(m, 0, (m->Prec > 0) ? m->frac[m->Prec-1] : 0, VpInternalRound(m, 0, (m->Prec > 0) ? m->frac[m->Prec-1] : 0,
(BDIGIT)(val*(double)BASE)); (DECDIG)(val*(double)BASE));
Exit: Exit:
#ifdef BIGDECIMAL_DEBUG #ifdef BIGDECIMAL_DEBUG
@ -6374,8 +6374,8 @@ VpMidRound(Real *y, unsigned short f, ssize_t nf)
/* exptoadd: number of digits needed to compensate negative nf */ /* exptoadd: number of digits needed to compensate negative nf */
int fracf, fracf_1further; int fracf, fracf_1further;
ssize_t n,i,ix,ioffset, exptoadd; ssize_t n,i,ix,ioffset, exptoadd;
BDIGIT v, shifter; DECDIG v, shifter;
BDIGIT div; DECDIG div;
nf += y->exponent * (ssize_t)BASE_FIG; nf += y->exponent * (ssize_t)BASE_FIG;
exptoadd=0; exptoadd=0;
@ -6397,8 +6397,8 @@ VpMidRound(Real *y, unsigned short f, ssize_t nf)
n = (ssize_t)BASE_FIG - ioffset - 1; n = (ssize_t)BASE_FIG - ioffset - 1;
for (shifter = 1, i = 0; i < n; ++i) shifter *= 10; for (shifter = 1, i = 0; i < n; ++i) shifter *= 10;
/* so the representation used (in y->frac) is an array of BDIGIT, where /* so the representation used (in y->frac) is an array of DECDIG, where
each BDIGIT contains a value between 0 and BASE-1, consisting of BASE_FIG each DECDIG contains a value between 0 and BASE-1, consisting of BASE_FIG
decimal places. decimal places.
(that numbers of decimal places are typed as ssize_t is somewhat confusing) (that numbers of decimal places are typed as ssize_t is somewhat confusing)
@ -6406,10 +6406,10 @@ VpMidRound(Real *y, unsigned short f, ssize_t nf)
nf is now position (in decimal places) of the digit from the start of nf is now position (in decimal places) of the digit from the start of
the array. the array.
ix is the position (in BDIGITS) of the BDIGIT containing the decimal digit, ix is the position (in DECDIGs) of the DECDIG containing the decimal digit,
from the start of the array. from the start of the array.
v is the value of this BDIGIT v is the value of this DECDIG
ioffset is the number of extra decimal places along of this decimal digit ioffset is the number of extra decimal places along of this decimal digit
within v. within v.
@ -6435,7 +6435,7 @@ VpMidRound(Real *y, unsigned short f, ssize_t nf)
now fracf_1further is whether any of the remaining digits within v are non-zero now fracf_1further is whether any of the remaining digits within v are non-zero
*/ */
/* now check all the remaining BDIGITS for zero-ness a whole BDIGIT at a time. /* now check all the remaining DECDIGs for zero-ness a whole DECDIG at a time.
if we spot any non-zeroness, that means that we found a positive digit under if we spot any non-zeroness, that means that we found a positive digit under
rounding position, and we also found a positive digit under one further than rounding position, and we also found a positive digit under one further than
the rounding position, so both searches (to see if any such non-zero digit exists) the rounding position, so both searches (to see if any such non-zero digit exists)
@ -6454,7 +6454,7 @@ VpMidRound(Real *y, unsigned short f, ssize_t nf)
now v = the first digit under the rounding position */ now v = the first digit under the rounding position */
/* drop digits after pointed digit */ /* drop digits after pointed digit */
memset(y->frac + ix + 1, 0, (y->Prec - (ix + 1)) * sizeof(BDIGIT)); memset(y->frac + ix + 1, 0, (y->Prec - (ix + 1)) * sizeof(DECDIG));
switch (f) { switch (f) {
case VP_ROUND_DOWN: /* Truncate */ case VP_ROUND_DOWN: /* Truncate */
@ -6482,11 +6482,11 @@ VpMidRound(Real *y, unsigned short f, ssize_t nf)
} }
else { else {
if (ioffset == 0) { if (ioffset == 0) {
/* v is the first decimal digit of its BDIGIT; /* v is the first decimal digit of its DECDIG;
need to grab the previous BDIGIT if present need to grab the previous DECDIG if present
to check for evenness of the previous decimal to check for evenness of the previous decimal
digit (which is same as that of the BDIGIT since digit (which is same as that of the DECDIG since
base 10 has a factor of 2) */ base 10 has a factor of 2) */
if (ix && (y->frac[ix-1] % 2)) ++div; if (ix && (y->frac[ix-1] % 2)) ++div;
} }
else { else {
@ -6534,7 +6534,7 @@ VpLeftRound(Real *y, unsigned short f, ssize_t nf)
* Round from the left hand side of the digits. * Round from the left hand side of the digits.
*/ */
{ {
BDIGIT v; DECDIG v;
if (!VpHasVal(y)) return 0; /* Unable to round */ if (!VpHasVal(y)) return 0; /* Unable to round */
v = y->frac[0]; v = y->frac[0];
nf -= VpExponent(y) * (ssize_t)BASE_FIG; nf -= VpExponent(y) * (ssize_t)BASE_FIG;
@ -6565,7 +6565,7 @@ VpLimitRound(Real *c, size_t ixDigit)
/* If I understand correctly, this is only ever used to round off the final decimal /* If I understand correctly, this is only ever used to round off the final decimal
digit of precision */ digit of precision */
static void static void
VpInternalRound(Real *c, size_t ixDigit, BDIGIT vPrev, BDIGIT v) VpInternalRound(Real *c, size_t ixDigit, DECDIG vPrev, DECDIG v)
{ {
int f = 0; int f = 0;
@ -6615,7 +6615,7 @@ VpInternalRound(Real *c, size_t ixDigit, BDIGIT vPrev, BDIGIT v)
static int static int
VpRdup(Real *m, size_t ind_m) VpRdup(Real *m, size_t ind_m)
{ {
BDIGIT carry; DECDIG carry;
if (!ind_m) ind_m = m->Prec; if (!ind_m) ind_m = m->Prec;
@ -6810,12 +6810,12 @@ VpVarCheck(Real * v)
} }
for (i = 0; i < v->Prec; ++i) { for (i = 0; i < v->Prec; ++i) {
if (v->frac[i] >= BASE) { if (v->frac[i] >= BASE) {
printf("ERROR(VpVarCheck): Illegal fraction\n"); printf("ERROR(VpVarCheck): Illegal fraction\n");
printf(" Frac[%"PRIuSIZE"]=%"PRIuBDIGIT"\n", i, v->frac[i]); printf(" Frac[%"PRIuSIZE"]=%"PRIuDECDIG"\n", i, v->frac[i]);
printf(" Prec. =%"PRIuSIZE"\n", v->Prec); printf(" Prec. =%"PRIuSIZE"\n", v->Prec);
printf(" Exp. =%"PRIdVALUE"\n", v->exponent); printf(" Exp. =%"PRIdVALUE"\n", v->exponent);
printf(" BASE =%"PRIuBDIGIT"\n", BASE); printf(" BASE =%"PRIuDECDIG"\n", BASE);
return 3; return 3;
} }
} }
return 0; return 0;

View File

@ -17,46 +17,39 @@
# include <float.h> # include <float.h>
#endif #endif
#undef BDIGIT
#undef SIZEOF_BDIGITS
#undef BDIGIT_DBL
#undef BDIGIT_DBL_SIGNED
#undef PRI_BDIGIT_PREFIX
#undef PRI_BDIGIT_DBL_PREFIX
#ifdef HAVE_INT64_T #ifdef HAVE_INT64_T
# define BDIGIT uint32_t # define DECDIG uint32_t
# define BDIGIT_DBL uint64_t # define DECDIG_DBL uint64_t
# define BDIGIT_DBL_SIGNED int64_t # define DECDIG_DBL_SIGNED int64_t
# define SIZEOF_BDIGITS 4 # define SIZEOF_DECDIG 4
# define PRI_BDIGIT_PREFIX "" # define PRI_DECDIG_PREFIX ""
# ifdef PRI_LL_PREFIX # ifdef PRI_LL_PREFIX
# define PRI_BDIGIT_DBL_PREFIX PRI_LL_PREFIX # define PRI_DECDIG_DBL_PREFIX PRI_LL_PREFIX
# else # else
# define PRI_BDIGIT_DBL_PREFIX "l" # define PRI_DECDIG_DBL_PREFIX "l"
# endif # endif
#else #else
# define BDIGIT uint16_t # define DECDIG uint16_t
# define BDIGIT_DBL uint32_t # define DECDIG_DBL uint32_t
# define BDIGIT_DBL_SIGNED int32_t # define DECDIG_DBL_SIGNED int32_t
# define SIZEOF_BDIGITS 2 # define SIZEOF_DECDIG 2
# define PRI_BDIGIT_PREFIX "h" # define PRI_DECDIG_PREFIX "h"
# define PRI_BDIGIT_DBL_PREFIX "" # define PRI_DECDIG_DBL_PREFIX ""
#endif #endif
#define PRIdBDIGIT PRI_BDIGIT_PREFIX"d" #define PRIdDECDIG PRI_DECDIG_PREFIX"d"
#define PRIiBDIGIT PRI_BDIGIT_PREFIX"i" #define PRIiDECDIG PRI_DECDIG_PREFIX"i"
#define PRIoBDIGIT PRI_BDIGIT_PREFIX"o" #define PRIoDECDIG PRI_DECDIG_PREFIX"o"
#define PRIuBDIGIT PRI_BDIGIT_PREFIX"u" #define PRIuDECDIG PRI_DECDIG_PREFIX"u"
#define PRIxBDIGIT PRI_BDIGIT_PREFIX"x" #define PRIxDECDIG PRI_DECDIG_PREFIX"x"
#define PRIXBDIGIT PRI_BDIGIT_PREFIX"X" #define PRIXDECDIG PRI_DECDIG_PREFIX"X"
#define PRIdBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"d" #define PRIdDECDIG_DBL PRI_DECDIG_DBL_PREFIX"d"
#define PRIiBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"i" #define PRIiDECDIG_DBL PRI_DECDIG_DBL_PREFIX"i"
#define PRIoBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"o" #define PRIoDECDIG_DBL PRI_DECDIG_DBL_PREFIX"o"
#define PRIuBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"u" #define PRIuDECDIG_DBL PRI_DECDIG_DBL_PREFIX"u"
#define PRIxBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"x" #define PRIxDECDIG_DBL PRI_DECDIG_DBL_PREFIX"x"
#define PRIXBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"X" #define PRIXDECDIG_DBL PRI_DECDIG_DBL_PREFIX"X"
#if defined(__cplusplus) #if defined(__cplusplus)
extern "C" { extern "C" {
@ -67,21 +60,21 @@ extern "C" {
extern VALUE rb_cBigDecimal; extern VALUE rb_cBigDecimal;
#if 0 || SIZEOF_BDIGITS >= 16 #if 0 || SIZEOF_DECDIG >= 16
# define RMPD_COMPONENT_FIGURES 38 # define RMPD_COMPONENT_FIGURES 38
# define RMPD_BASE ((BDIGIT)100000000000000000000000000000000000000U) # define RMPD_BASE ((DECDIG)100000000000000000000000000000000000000U)
#elif SIZEOF_BDIGITS >= 8 #elif SIZEOF_DECDIG >= 8
# define RMPD_COMPONENT_FIGURES 19 # define RMPD_COMPONENT_FIGURES 19
# define RMPD_BASE ((BDIGIT)10000000000000000000U) # define RMPD_BASE ((DECDIG)10000000000000000000U)
#elif SIZEOF_BDIGITS >= 4 #elif SIZEOF_DECDIG >= 4
# define RMPD_COMPONENT_FIGURES 9 # define RMPD_COMPONENT_FIGURES 9
# define RMPD_BASE ((BDIGIT)1000000000U) # define RMPD_BASE ((DECDIG)1000000000U)
#elif SIZEOF_BDIGITS >= 2 #elif SIZEOF_DECDIG >= 2
# define RMPD_COMPONENT_FIGURES 4 # define RMPD_COMPONENT_FIGURES 4
# define RMPD_BASE ((BDIGIT)10000U) # define RMPD_BASE ((DECDIG)10000U)
#else #else
# define RMPD_COMPONENT_FIGURES 2 # define RMPD_COMPONENT_FIGURES 2
# define RMPD_BASE ((BDIGIT)100U) # define RMPD_BASE ((DECDIG)100U)
#endif #endif
@ -164,7 +157,7 @@ typedef struct {
* -3 : Negative infinite number * -3 : Negative infinite number
*/ */
short flag; /* Not used in vp_routines,space for user. */ short flag; /* Not used in vp_routines,space for user. */
BDIGIT frac[FLEXIBLE_ARRAY_SIZE]; /* Array of fraction part. */ DECDIG frac[FLEXIBLE_ARRAY_SIZE]; /* Array of fraction part. */
} Real; } Real;
/* /*
@ -177,7 +170,7 @@ VP_EXPORT Real *VpNewRbClass(size_t mx, char const *str, VALUE klass, bool stric
VP_EXPORT Real *VpCreateRbObject(size_t mx, const char *str, bool raise_exception); VP_EXPORT Real *VpCreateRbObject(size_t mx, const char *str, bool raise_exception);
static inline BDIGIT static inline DECDIG
rmpd_base_value(void) { return RMPD_BASE; } rmpd_base_value(void) { return RMPD_BASE; }
static inline size_t static inline size_t
rmpd_component_figures(void) { return RMPD_COMPONENT_FIGURES; } rmpd_component_figures(void) { return RMPD_COMPONENT_FIGURES; }
@ -208,7 +201,7 @@ VP_EXPORT int VpException(unsigned short f,const char *str,int always);
VP_EXPORT int VpIsNegDoubleZero(double v); VP_EXPORT int VpIsNegDoubleZero(double v);
#endif #endif
VP_EXPORT size_t VpNumOfChars(Real *vp,const char *pszFmt); VP_EXPORT size_t VpNumOfChars(Real *vp,const char *pszFmt);
VP_EXPORT size_t VpInit(BDIGIT BaseVal); VP_EXPORT size_t VpInit(DECDIG BaseVal);
VP_EXPORT void *VpMemAlloc(size_t mb); VP_EXPORT void *VpMemAlloc(size_t mb);
VP_EXPORT void *VpMemRealloc(void *ptr, size_t mb); VP_EXPORT void *VpMemRealloc(void *ptr, size_t mb);
VP_EXPORT void VpFree(Real *pv); VP_EXPORT void VpFree(Real *pv);