Skip to content

Commit 13a98ae

Browse files
committed
Use quot only if use_quot == true, in other funcs
1 parent 5e4b175 commit 13a98ae

File tree

1 file changed

+31
-25
lines changed
  • ext/bcmath/libbcmath/src

1 file changed

+31
-25
lines changed

ext/bcmath/libbcmath/src/div.c

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@ static inline void bc_standard_div(
255255
static void bc_do_div(
256256
const char *numerator, size_t numerator_size, size_t numerator_readable_size,
257257
const char *divisor, size_t divisor_size,
258-
bc_num *quot, size_t quot_size
258+
bc_num *quot, size_t quot_size,
259+
bool use_quot
259260
) {
260261
size_t numerator_arr_size = BC_ARR_SIZE_FROM_LEN(numerator_size);
261262
size_t divisor_arr_size = BC_ARR_SIZE_FROM_LEN(divisor_size);
@@ -292,10 +293,11 @@ static void bc_do_div(
292293
}
293294

294295
/* Convert to bc_num */
295-
char *qptr = (*quot)->n_value;
296-
char *qend = qptr + (*quot)->n_len + (*quot)->n_scale - 1;
297-
298-
bc_convert_vector_to_char(quot_vectors, qptr, qend, quot_real_arr_size);
296+
if (use_quot) {
297+
char *qptr = (*quot)->n_value;
298+
char *qend = qptr + (*quot)->n_len + (*quot)->n_scale - 1;
299+
bc_convert_vector_to_char(quot_vectors, qptr, qend, quot_real_arr_size);
300+
}
299301

300302
if (allocation_arr_size > BC_STACK_VECTOR_SIZE) {
301303
efree(numerator_vectors);
@@ -309,33 +311,37 @@ static inline void bc_divide_copy_numerator(bc_num numerator, bc_num *num, size_
309311
memcpy((*num)->n_value, numerator->n_value, numerator->n_len + scale);
310312
}
311313

312-
static inline void bc_divide_by_one(bc_num numerator, bc_num divisor, bc_num *quot, size_t quot_scale)
314+
static inline void bc_divide_by_one(bc_num numerator, bc_num divisor, bc_num *quot, size_t quot_scale, bool use_quot)
313315
{
314-
bc_divide_copy_numerator(numerator, quot, quot_scale);
315-
(*quot)->n_sign = numerator->n_sign == divisor->n_sign ? PLUS : MINUS;
316+
if (use_quot) {
317+
bc_divide_copy_numerator(numerator, quot, quot_scale);
318+
(*quot)->n_sign = numerator->n_sign == divisor->n_sign ? PLUS : MINUS;
319+
}
316320
}
317321

318322
static inline void bc_divide_by_pow_10(
319-
const char *numeratorptr, size_t numerator_readable_size, bc_num *quot, size_t quot_size, size_t quot_scale)
323+
const char *numeratorptr, size_t numerator_readable_size, bc_num *quot, size_t quot_size, size_t quot_scale, bool use_quot)
320324
{
321-
char *qptr = (*quot)->n_value;
322-
for (size_t i = quot_size; i <= quot_scale; i++) {
323-
*qptr++ = 0;
324-
}
325+
if (use_quot) {
326+
char *qptr = (*quot)->n_value;
327+
for (size_t i = quot_size; i <= quot_scale; i++) {
328+
*qptr++ = 0;
329+
}
325330

326-
size_t numerator_use_size = quot_size > numerator_readable_size ? numerator_readable_size : quot_size;
327-
memcpy(qptr, numeratorptr, numerator_use_size);
328-
qptr += numerator_use_size;
331+
size_t numerator_use_size = quot_size > numerator_readable_size ? numerator_readable_size : quot_size;
332+
memcpy(qptr, numeratorptr, numerator_use_size);
333+
qptr += numerator_use_size;
329334

330-
if (numerator_use_size < (*quot)->n_len) {
331-
/* e.g. 12.3 / 0.01 <=> 1230 */
332-
for (size_t i = numerator_use_size; i < (*quot)->n_len; i++) {
333-
*qptr++ = 0;
335+
if (numerator_use_size < (*quot)->n_len) {
336+
/* e.g. 12.3 / 0.01 <=> 1230 */
337+
for (size_t i = numerator_use_size; i < (*quot)->n_len; i++) {
338+
*qptr++ = 0;
339+
}
340+
(*quot)->n_scale = 0;
341+
} else {
342+
char *qend = (*quot)->n_value + (*quot)->n_len + (*quot)->n_scale;
343+
(*quot)->n_scale -= qend - qptr;
334344
}
335-
(*quot)->n_scale = 0;
336-
} else {
337-
char *qend = (*quot)->n_value + (*quot)->n_len + (*quot)->n_scale;
338-
(*quot)->n_scale -= qend - qptr;
339345
}
340346
}
341347

@@ -358,7 +364,7 @@ bool bc_divide_ex(bc_num numerator, bc_num divisor, bc_num *quot, bc_num *rem, s
358364

359365
/* If divisor is 1 / -1, the quotient's n_value is equal to numerator's n_value. */
360366
if (_bc_do_compare(divisor, BCG(_one_), divisor->n_scale, false) == BCMATH_EQUAL) {
361-
bc_divide_by_one(numerator, divisor, quot, quot_scale);
367+
bc_divide_by_one(numerator, divisor, quot, quot_scale, use_quot);
362368
return true;
363369
}
364370

0 commit comments

Comments
 (0)