Skip to content

Commit 2fd502f

Browse files
author
Stefan Krah
committed
1) Use _mpd_basedivmod() regardless of the length of the dividend. This is
required for a corner case in dec_hash() in the following commit and also usually faster. dec_hash() needs some extra precision above MPD_MAX_PREC, and _mpd_base_ndivmod() is not audited for that. 2) Use _mpd_basemul() if the length of the smaller operand is less than or equal to 256. While this is technically an optimization, it is required for *testing* corner cases in dec_hash() in reasonable time.
1 parent ad54c6d commit 2fd502f

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

Modules/_decimal/libmpdec/mpdecimal.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3513,8 +3513,7 @@ _mpd_qdiv(int action, mpd_t *q, const mpd_t *a, const mpd_t *b,
35133513
if (b->len == 1) {
35143514
rem = _mpd_shortdiv(q->data, a->data, a->len, b->data[0]);
35153515
}
3516-
else if (a->len < 2*MPD_NEWTONDIV_CUTOFF &&
3517-
b->len < MPD_NEWTONDIV_CUTOFF) {
3516+
else if (b->len <= MPD_NEWTONDIV_CUTOFF) {
35183517
int ret = _mpd_basedivmod(q->data, NULL, a->data, b->data,
35193518
a->len, b->len);
35203519
if (ret < 0) {
@@ -3667,8 +3666,7 @@ _mpd_qdivmod(mpd_t *q, mpd_t *r, const mpd_t *a, const mpd_t *b,
36673666
r->data[0] = _mpd_shortdiv(q->data, a->data, a->len, b->data[0]);
36683667
}
36693668
}
3670-
else if (a->len < 2*MPD_NEWTONDIV_CUTOFF &&
3671-
b->len < MPD_NEWTONDIV_CUTOFF) {
3669+
else if (b->len <= MPD_NEWTONDIV_CUTOFF) {
36723670
int ret;
36733671
ret = _mpd_basedivmod(q->data, r->data, a->data, b->data,
36743672
a->len, b->len);
@@ -5544,10 +5542,15 @@ _mpd_qmul(mpd_t *result, const mpd_t *a, const mpd_t *b,
55445542
}
55455543

55465544

5547-
if (small->len == 1) {
5545+
if (small->len <= 256) {
55485546
rdata = mpd_calloc(rsize, sizeof *rdata);
55495547
if (rdata != NULL) {
5550-
_mpd_shortmul(rdata, big->data, big->len, small->data[0]);
5548+
if (small->len == 1) {
5549+
_mpd_shortmul(rdata, big->data, big->len, small->data[0]);
5550+
}
5551+
else {
5552+
_mpd_basemul(rdata, small->data, big->data, small->len, big->len);
5553+
}
55515554
}
55525555
}
55535556
else if (rsize <= 1024) {

0 commit comments

Comments
 (0)