Skip to content

Commit c0000cd

Browse files
committed
Set the sign of quot efficiently
1 parent b2c1b3f commit c0000cd

File tree

1 file changed

+4
-7
lines changed
  • ext/bcmath/libbcmath/src

1 file changed

+4
-7
lines changed

ext/bcmath/libbcmath/src/div.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,10 @@ static inline void bc_divide_copy_numerator(bc_num numerator, bc_num *num, size_
309309
memcpy((*num)->n_value, numerator->n_value, numerator->n_len + scale);
310310
}
311311

312-
static inline void bc_divide_by_one(bc_num numerator, bc_num *quot, size_t quot_scale)
312+
static inline void bc_divide_by_one(bc_num numerator, bc_num divisor, bc_num *quot, size_t quot_scale)
313313
{
314314
bc_divide_copy_numerator(numerator, quot, quot_scale);
315+
(*quot)->n_sign = numerator->n_sign == divisor->n_sign ? PLUS : MINUS;
315316
}
316317

317318
static inline void bc_divide_by_pow_10(
@@ -355,8 +356,7 @@ bool bc_divide_ex(bc_num numerator, bc_num divisor, bc_num *quot, bc_num *rem, s
355356

356357
/* If divisor is 1 / -1, the quotient's n_value is equal to numerator's n_value. */
357358
if (_bc_do_compare(divisor, BCG(_one_), divisor->n_scale, false) == BCMATH_EQUAL) {
358-
bc_divide_by_one(numerator, quot, quot_scale);
359-
(*quot)->n_sign = numerator->n_sign == divisor->n_sign ? PLUS : MINUS;
359+
bc_divide_by_one(numerator, divisor, quot, quot_scale);
360360
return true;
361361
}
362362

@@ -404,14 +404,14 @@ bool bc_divide_ex(bc_num numerator, bc_num divisor, bc_num *quot, bc_num *rem, s
404404
} else {
405405
*quot = bc_new_num_nonzeroed(1, quot_scale); /* 1 is for 0 */
406406
}
407+
(*quot)->n_sign = numerator->n_sign == divisor->n_sign ? PLUS : MINUS;
407408

408409
/* Size that can be read from numeratorptr */
409410
size_t numerator_readable_size = numerator->n_len + numerator->n_scale - numerator_leading_zeros;
410411

411412
/* If divisor is 1 here, return the result of adjusting the decimal point position of numerator. */
412413
if (divisor_size == 1 && *divisorptr == 1) {
413414
bc_divide_by_pow_10(numeratorptr, numerator_readable_size, quot, quot_size, quot_scale);
414-
(*quot)->n_sign = numerator->n_sign == divisor->n_sign ? PLUS : MINUS;
415415
return true;
416416
}
417417

@@ -425,9 +425,6 @@ bool bc_divide_ex(bc_num numerator, bc_num divisor, bc_num *quot, bc_num *rem, s
425425
_bc_rm_leading_zeros(*quot);
426426
if (bc_is_zero(*quot)) {
427427
(*quot)->n_sign = PLUS;
428-
(*quot)->n_scale = 0;
429-
} else {
430-
(*quot)->n_sign = numerator->n_sign == divisor->n_sign ? PLUS : MINUS;
431428
}
432429
return true;
433430

0 commit comments

Comments
 (0)