Skip to content

Commit 797fa6b

Browse files
committed
Use quot only if use_quot == true, in other funcs
1 parent 663fe1a commit 797fa6b

File tree

1 file changed

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

1 file changed

+31
-24
lines changed

ext/bcmath/libbcmath/src/div.c

Lines changed: 31 additions & 24 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 = (numerator_size + BC_VECTOR_SIZE - 1) / BC_VECTOR_SIZE;
261262
size_t divisor_arr_size = (divisor_size + BC_VECTOR_SIZE - 1) / BC_VECTOR_SIZE;
@@ -284,9 +285,11 @@ static void bc_do_div(
284285
}
285286

286287
/* Convert to bc_num */
287-
char *qptr = (*quot)->n_value;
288-
char *qend = qptr + (*quot)->n_len + (*quot)->n_scale - 1;
289-
bc_convert_vector_to_char(qptr, qend, quot_vectors, quot_real_arr_size);
288+
if (use_quot) {
289+
char *qptr = (*quot)->n_value;
290+
char *qend = qptr + (*quot)->n_len + (*quot)->n_scale - 1;
291+
bc_convert_vector_to_char(qptr, qend, quot_vectors, quot_real_arr_size);
292+
}
290293

291294
efree(numerator_vectors);
292295
}
@@ -298,33 +301,37 @@ static inline void bc_divide_copy_numerator(bc_num numerator, bc_num *num, size_
298301
memcpy((*num)->n_value, numerator->n_value, numerator->n_len + scale);
299302
}
300303

301-
static inline void bc_divide_by_one(bc_num numerator, bc_num divisor, bc_num *quot, size_t quot_scale)
304+
static inline void bc_divide_by_one(bc_num numerator, bc_num divisor, bc_num *quot, size_t quot_scale, bool use_quot)
302305
{
303-
bc_divide_copy_numerator(numerator, quot, quot_scale);
304-
(*quot)->n_sign = numerator->n_sign == divisor->n_sign ? PLUS : MINUS;
306+
if (use_quot) {
307+
bc_divide_copy_numerator(numerator, quot, quot_scale);
308+
(*quot)->n_sign = numerator->n_sign == divisor->n_sign ? PLUS : MINUS;
309+
}
305310
}
306311

307312
static inline void bc_divide_by_pow_10(
308-
const char *numeratorptr, size_t numerator_readable_size, bc_num *quot, size_t quot_size, size_t quot_scale)
313+
const char *numeratorptr, size_t numerator_readable_size, bc_num *quot, size_t quot_size, size_t quot_scale, bool use_quot)
309314
{
310-
char *qptr = (*quot)->n_value;
311-
for (size_t i = quot_size; i <= quot_scale; i++) {
312-
*qptr++ = 0;
313-
}
315+
if (use_quot) {
316+
char *qptr = (*quot)->n_value;
317+
for (size_t i = quot_size; i <= quot_scale; i++) {
318+
*qptr++ = 0;
319+
}
314320

315-
size_t numerator_use_size = quot_size > numerator_readable_size ? numerator_readable_size : quot_size;
316-
memcpy(qptr, numeratorptr, numerator_use_size);
317-
qptr += numerator_use_size;
321+
size_t numerator_use_size = quot_size > numerator_readable_size ? numerator_readable_size : quot_size;
322+
memcpy(qptr, numeratorptr, numerator_use_size);
323+
qptr += numerator_use_size;
318324

319-
if (numerator_use_size < (*quot)->n_len) {
320-
/* e.g. 12.3 / 0.01 <=> 1230 */
321-
for (size_t i = numerator_use_size; i < (*quot)->n_len; i++) {
322-
*qptr++ = 0;
325+
if (numerator_use_size < (*quot)->n_len) {
326+
/* e.g. 12.3 / 0.01 <=> 1230 */
327+
for (size_t i = numerator_use_size; i < (*quot)->n_len; i++) {
328+
*qptr++ = 0;
329+
}
330+
(*quot)->n_scale = 0;
331+
} else {
332+
char *qend = (*quot)->n_value + (*quot)->n_len + (*quot)->n_scale;
333+
(*quot)->n_scale -= qend - qptr;
323334
}
324-
(*quot)->n_scale = 0;
325-
} else {
326-
char *qend = (*quot)->n_value + (*quot)->n_len + (*quot)->n_scale;
327-
(*quot)->n_scale -= qend - qptr;
328335
}
329336
}
330337

@@ -347,7 +354,7 @@ bool bc_divide_ex(bc_num numerator, bc_num divisor, bc_num *quot, bc_num *rem, s
347354

348355
/* If divisor is 1 / -1, the quotient's n_value is equal to numerator's n_value. */
349356
if (_bc_do_compare(divisor, BCG(_one_), divisor->n_scale, false) == BCMATH_EQUAL) {
350-
bc_divide_by_one(numerator, divisor, quot, quot_scale);
357+
bc_divide_by_one(numerator, divisor, quot, quot_scale, use_quot);
351358
return true;
352359
}
353360

0 commit comments

Comments
 (0)