Skip to content

Commit da0916c

Browse files
committed
Made it a little easier to see and avoid unnecessary zero padding in early return cases
1 parent d016743 commit da0916c

File tree

1 file changed

+39
-20
lines changed
  • ext/bcmath/libbcmath/src

1 file changed

+39
-20
lines changed

ext/bcmath/libbcmath/src/div.c

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,42 @@ static void bc_do_div(
305305
efree(numerator_vectors);
306306
}
307307

308+
static inline void bc_divide_by_one(bc_num numerator, bc_num *quot, size_t quot_scale)
309+
{
310+
quot_scale = MIN(numerator->n_scale, quot_scale);
311+
*quot = bc_new_num_nonzeroed(numerator->n_len, quot_scale);
312+
char *qptr = (*quot)->n_value;
313+
memcpy(qptr, numerator->n_value, numerator->n_len + quot_scale);
314+
}
315+
316+
static inline void bc_divide_by_pow_10(
317+
const char *numeratorptr, size_t numerator_readable_size, bc_num *quot, size_t quot_size, size_t quot_scale)
318+
{
319+
char *qptr = (*quot)->n_value;
320+
if (quot_size <= quot_scale) {
321+
/* int is 0 */
322+
*qptr++ = 0;
323+
for (size_t i = quot_size; i < quot_scale; i++) {
324+
*qptr++ = 0;
325+
}
326+
}
327+
328+
size_t numerator_use_size = quot_size > numerator_readable_size ? numerator_readable_size : quot_size;
329+
memcpy(qptr, numeratorptr, numerator_use_size);
330+
qptr += numerator_use_size;
331+
332+
if (numerator_use_size < (*quot)->n_len) {
333+
/* e.g. 12.3 / 0.01 <=> 1230 */
334+
for (size_t i = numerator_use_size; i < (*quot)->n_len; i++) {
335+
*qptr++ = 0;
336+
}
337+
(*quot)->n_scale = 0;
338+
} else {
339+
char *qend = (*quot)->n_value + (*quot)->n_len + (*quot)->n_scale;
340+
(*quot)->n_scale -= qend - qptr;
341+
}
342+
}
343+
308344
bool bc_divide(bc_num numerator, bc_num divisor, bc_num *quot, size_t scale)
309345
{
310346
/* divide by zero */
@@ -322,10 +358,7 @@ bool bc_divide(bc_num numerator, bc_num divisor, bc_num *quot, size_t scale)
322358

323359
/* If divisor is 1 / -1, the quotient's n_value is equal to numerator's n_value. */
324360
if (_bc_do_compare(divisor, BCG(_one_), divisor->n_scale, false) == BCMATH_EQUAL) {
325-
quot_scale = MIN(numerator->n_scale, quot_scale);
326-
*quot = bc_new_num_nonzeroed(numerator->n_len, quot_scale);
327-
char *qptr = (*quot)->n_value;
328-
memcpy(qptr, numerator->n_value, numerator->n_len + quot_scale);
361+
bc_divide_by_one(numerator, quot, quot_scale);
329362
(*quot)->n_sign = numerator->n_sign == divisor->n_sign ? PLUS : MINUS;
330363
return true;
331364
}
@@ -386,22 +419,8 @@ bool bc_divide(bc_num numerator, bc_num divisor, bc_num *quot, size_t scale)
386419

387420
/* If divisor is 1 here, return the result of adjusting the decimal point position of numerator. */
388421
if (divisor_size == 1 && *divisorptr == 1) {
389-
char *qptr = (*quot)->n_value;
390-
if (quot_size <= quot_scale) {
391-
/* int is 0 */
392-
*qptr++ = 0;
393-
for (size_t i = quot_size; i < quot_scale; i++) {
394-
*qptr++ = 0;
395-
}
396-
}
397-
size_t numerator_use_size = quot_size > numerator_readable_size ? numerator_readable_size : quot_size;
398-
memcpy(qptr, numeratorptr, numerator_use_size);
399-
qptr += numerator_use_size;
400-
for (size_t i = 0; i < quot_size - numerator_use_size; i++) {
401-
*qptr++ = 0;
402-
}
403-
bc_rm_trailing_zeros(*quot);
404-
(*quot)->n_sign = numerator->n_sign == divisor->n_sign ? PLUS : MINUS;
422+
bc_divide_by_pow_10(numeratorptr, numerator_readable_size, quot, quot_size, quot_scale);
423+
(*quot)->n_sign = numerator->n_sign == divisor->n_sign ? PLUS : MINUS;;
405424
return true;
406425
}
407426

0 commit comments

Comments
 (0)