Skip to content

Commit 663fe1a

Browse files
committed
Added bc_convert_vector_to_char() and bc_convert_vector_to_char_with_skip()
1 parent 6e3c533 commit 663fe1a

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed

ext/bcmath/libbcmath/src/convert.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,50 @@ static inline void bc_convert_to_vector_with_zero_pad(BC_VECTOR *n_vector, const
8585
bc_convert_to_vector(n_vector, nend, nlen);
8686
}
8787

88+
static inline void bc_convert_vector_to_char(char *nptr, char *nend, BC_VECTOR *vectors, size_t array_size)
89+
{
90+
size_t i;
91+
for (i = 0; i < array_size - 1; i++) {
92+
#if BC_VECTOR_SIZE == 4
93+
bc_write_bcd_representation(vectors[i], nend - 3);
94+
nend -= 4;
95+
#else
96+
bc_write_bcd_representation(vectors[i] / 10000, nend - 7);
97+
bc_write_bcd_representation(vectors[i] % 10000, nend - 3);
98+
nend -= 8;
99+
#endif
100+
}
101+
102+
while (nend >= nptr) {
103+
*nend-- = vectors[i] % BASE;
104+
vectors[i] /= BASE;
105+
}
106+
}
107+
108+
static inline void bc_convert_vector_to_char_with_skip(char *nptr, char *nend, BC_VECTOR *vectors, size_t array_size, size_t skip)
109+
{
110+
/* bulk skip */
111+
size_t array_skip = skip / BC_VECTOR_SIZE;
112+
array_size -= array_skip;
113+
vectors += array_skip;
114+
115+
/* skip */
116+
skip %= BC_VECTOR_SIZE;
117+
if (skip > 0) {
118+
BC_VECTOR current_vector = *vectors;
119+
current_vector /= BC_POW_10_LUT[skip];
120+
size_t write_size = MIN(nend - nptr + 1, BC_VECTOR_SIZE - skip);
121+
for (size_t i = 0; i < write_size; i++) {
122+
*nend-- = current_vector % BASE;
123+
current_vector /= BASE;
124+
}
125+
vectors++;
126+
array_size--;
127+
}
128+
129+
if (array_size > 0) {
130+
bc_convert_vector_to_char(nptr, nend, vectors, array_size);
131+
}
132+
}
133+
88134
#endif

ext/bcmath/libbcmath/src/div.c

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -286,23 +286,7 @@ static void bc_do_div(
286286
/* Convert to bc_num */
287287
char *qptr = (*quot)->n_value;
288288
char *qend = qptr + (*quot)->n_len + (*quot)->n_scale - 1;
289-
290-
size_t i;
291-
for (i = 0; i < quot_real_arr_size - 1; i++) {
292-
#if BC_VECTOR_SIZE == 4
293-
bc_write_bcd_representation(quot_vectors[i], qend - 3);
294-
qend -= 4;
295-
#else
296-
bc_write_bcd_representation(quot_vectors[i] / 10000, qend - 7);
297-
bc_write_bcd_representation(quot_vectors[i] % 10000, qend - 3);
298-
qend -= 8;
299-
#endif
300-
}
301-
302-
while (qend >= qptr) {
303-
*qend-- = quot_vectors[i] % BASE;
304-
quot_vectors[i] /= BASE;
305-
}
289+
bc_convert_vector_to_char(qptr, qend, quot_vectors, quot_real_arr_size);
306290

307291
efree(numerator_vectors);
308292
}

0 commit comments

Comments
 (0)