Skip to content

Commit 25579a8

Browse files
ext/bcmath: Renamed macros and variables (#14507)
Made the macro BC_UINT_T a typedef and renamed it BC_VECTOR. Additionally, several macros have been renamed to be consistent with BC_VECTOR.
1 parent f109795 commit 25579a8

File tree

3 files changed

+84
-78
lines changed

3 files changed

+84
-78
lines changed

ext/bcmath/libbcmath/src/doaddsub.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,17 @@ bc_num _bc_do_add(bc_num n1, bc_num n2)
7474
/* Now add the remaining fraction part and equal size integer parts. */
7575
count = 0;
7676
/* Uses SIMD to perform calculations at high speed. */
77-
if (min_bytes >= sizeof(BC_UINT_T)) {
77+
if (min_bytes >= sizeof(BC_VECTOR)) {
7878
sumptr++;
7979
n1ptr++;
8080
n2ptr++;
81-
while (count + sizeof(BC_UINT_T) <= min_bytes) {
82-
sumptr -= sizeof(BC_UINT_T);
83-
n1ptr -= sizeof(BC_UINT_T);
84-
n2ptr -= sizeof(BC_UINT_T);
81+
while (count + sizeof(BC_VECTOR) <= min_bytes) {
82+
sumptr -= sizeof(BC_VECTOR);
83+
n1ptr -= sizeof(BC_VECTOR);
84+
n2ptr -= sizeof(BC_VECTOR);
8585

86-
BC_UINT_T n1bytes;
87-
BC_UINT_T n2bytes;
86+
BC_VECTOR n1bytes;
87+
BC_VECTOR n2bytes;
8888
memcpy(&n1bytes, n1ptr, sizeof(n1bytes));
8989
memcpy(&n2bytes, n2ptr, sizeof(n2bytes));
9090

@@ -103,15 +103,15 @@ bc_num _bc_do_add(bc_num n1, bc_num n2)
103103
*/
104104
n1bytes += SWAR_REPEAT(0xF6) + n2bytes + carry;
105105
/* If the most significant bit is 0, a carry has occurred. */
106-
carry = !(n1bytes & ((BC_UINT_T) 1 << (8 * sizeof(BC_UINT_T) - 1)));
106+
carry = !(n1bytes & ((BC_VECTOR) 1 << (8 * sizeof(BC_VECTOR) - 1)));
107107

108108
/*
109109
* The calculation result is a mixture of bytes that have been carried and bytes that have not.
110110
* The most significant bit of each byte is 0 if it is carried forward, and 1 if it is not.
111111
* Using this, subtract the 0xF6 added for adjustment from the byte that has not been carried
112112
* over to return it to the correct value as a decimal number.
113113
*/
114-
BC_UINT_T sum_mask = ((n1bytes & SWAR_REPEAT(0x80)) >> 7) * 0xF6;
114+
BC_VECTOR sum_mask = ((n1bytes & SWAR_REPEAT(0x80)) >> 7) * 0xF6;
115115
n1bytes -= sum_mask;
116116

117117
#if BC_LITTLE_ENDIAN
@@ -121,7 +121,7 @@ bc_num _bc_do_add(bc_num n1, bc_num n2)
121121

122122
memcpy(sumptr, &n1bytes, sizeof(n1bytes));
123123

124-
count += sizeof(BC_UINT_T);
124+
count += sizeof(BC_VECTOR);
125125
}
126126
sumptr--;
127127
n1ptr--;
@@ -215,17 +215,17 @@ bc_num _bc_do_sub(bc_num n1, bc_num n2)
215215
/* Now do the equal length scale and integer parts. */
216216
count = 0;
217217
/* Uses SIMD to perform calculations at high speed. */
218-
if (min_bytes >= sizeof(BC_UINT_T)) {
218+
if (min_bytes >= sizeof(BC_VECTOR)) {
219219
diffptr++;
220220
n1ptr++;
221221
n2ptr++;
222-
while (count + sizeof(BC_UINT_T) <= min_bytes) {
223-
diffptr -= sizeof(BC_UINT_T);
224-
n1ptr -= sizeof(BC_UINT_T);
225-
n2ptr -= sizeof(BC_UINT_T);
222+
while (count + sizeof(BC_VECTOR) <= min_bytes) {
223+
diffptr -= sizeof(BC_VECTOR);
224+
n1ptr -= sizeof(BC_VECTOR);
225+
n2ptr -= sizeof(BC_VECTOR);
226226

227-
BC_UINT_T n1bytes;
228-
BC_UINT_T n2bytes;
227+
BC_VECTOR n1bytes;
228+
BC_VECTOR n2bytes;
229229
memcpy(&n1bytes, n1ptr, sizeof(n1bytes));
230230
memcpy(&n2bytes, n2ptr, sizeof(n2bytes));
231231

@@ -237,7 +237,7 @@ bc_num _bc_do_sub(bc_num n1, bc_num n2)
237237

238238
n1bytes -= n2bytes + borrow;
239239
/* If the most significant bit is 1, a carry down has occurred. */
240-
bool tmp_borrow = n1bytes & ((BC_UINT_T) 1 << (8 * sizeof(BC_UINT_T) - 1));
240+
bool tmp_borrow = n1bytes & ((BC_VECTOR) 1 << (8 * sizeof(BC_VECTOR) - 1));
241241

242242
/*
243243
* Check the most significant bit of each of the bytes, and if it is 1, a carry down has
@@ -246,7 +246,7 @@ bc_num _bc_do_sub(bc_num n1, bc_num n2)
246246
* Therefore, for a byte that has been carried down, set all the upper 4 bits to 0 and subtract
247247
* 6 from the lower 4 bits to adjust it to the correct value as a decimal number.
248248
*/
249-
BC_UINT_T borrow_mask = ((n1bytes & SWAR_REPEAT(0x80)) >> 7) * 0x06;
249+
BC_VECTOR borrow_mask = ((n1bytes & SWAR_REPEAT(0x80)) >> 7) * 0x06;
250250
n1bytes = (n1bytes & SWAR_REPEAT(0x0F)) - borrow_mask;
251251

252252
#if BC_LITTLE_ENDIAN
@@ -257,14 +257,14 @@ bc_num _bc_do_sub(bc_num n1, bc_num n2)
257257
memcpy(diffptr, &n1bytes, sizeof(n1bytes));
258258

259259
borrow = tmp_borrow;
260-
count += sizeof(BC_UINT_T);
260+
count += sizeof(BC_VECTOR);
261261
}
262262
diffptr--;
263263
n1ptr--;
264264
n2ptr--;
265265
}
266266

267-
/* Calculate the remaining bytes that are less than the size of BC_UINT_T using a normal loop. */
267+
/* Calculate the remaining bytes that are less than the size of BC_VECTOR using a normal loop. */
268268
for (; count < min_bytes; count++) {
269269
val = *n1ptr-- - *n2ptr-- - borrow;
270270
if (val < 0) {

ext/bcmath/libbcmath/src/private.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ static inline uint64_t BC_BSWAP64(uint64_t u)
8383

8484
#if SIZEOF_SIZE_T >= 8
8585
# define BC_BSWAP(u) BC_BSWAP64(u)
86-
# define BC_UINT_T uint64_t
86+
typedef uint64_t BC_VECTOR;
8787
#else
8888
# define BC_BSWAP(u) BC_BSWAP32(u)
89-
# define BC_UINT_T uint32_t
89+
typedef uint32_t BC_VECTOR;
9090
#endif
9191

9292
#ifdef WORDS_BIGENDIAN

ext/bcmath/libbcmath/src/recmul.c

Lines changed: 61 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,34 @@
3333
#include <stddef.h>
3434
#include <assert.h>
3535
#include <stdbool.h>
36-
#include "private.h" /* For _bc_rm_leading_zeros() */
36+
#include "private.h"
3737
#include "zend_alloc.h"
3838

3939

4040
#if SIZEOF_SIZE_T >= 8
41-
# define BC_MUL_UINT_DIGITS 8
42-
# define BC_MUL_UINT_OVERFLOW (BC_UINT_T) 100000000
41+
# define BC_VECTOR_SIZE 8
42+
/* The boundary number is computed from BASE ** BC_VECTOR_SIZE */
43+
# define BC_VECTOR_BOUNDARY_NUM (BC_VECTOR) 100000000
4344
#else
44-
# define BC_MUL_UINT_DIGITS 4
45-
# define BC_MUL_UINT_OVERFLOW (BC_UINT_T) 10000
45+
# define BC_VECTOR_SIZE 4
46+
/* The boundary number is computed from BASE ** BC_VECTOR_SIZE */
47+
# define BC_VECTOR_BOUNDARY_NUM (BC_VECTOR) 10000
4648
#endif
4749

48-
#define BC_MUL_MAX_ADD_COUNT (~((BC_UINT_T) 0) / (BC_MUL_UINT_OVERFLOW * BC_MUL_UINT_OVERFLOW))
50+
/*
51+
* Adding more than this many times may cause uint32_t/uint64_t to overflow.
52+
* Typically this is 1844 for 64bit and 42 for 32bit.
53+
*/
54+
#define BC_VECTOR_NO_OVERFLOW_ADD_COUNT (~((BC_VECTOR) 0) / (BC_VECTOR_BOUNDARY_NUM * BC_VECTOR_BOUNDARY_NUM))
4955

5056

5157
/* Multiply utility routines */
5258

53-
static inline void bc_digits_adjustment(BC_UINT_T *prod_uint, size_t prod_arr_size)
59+
static inline void bc_digits_adjustment(BC_VECTOR *prod_vector, size_t prod_arr_size)
5460
{
5561
for (size_t i = 0; i < prod_arr_size - 1; i++) {
56-
prod_uint[i + 1] += prod_uint[i] / BC_MUL_UINT_OVERFLOW;
57-
prod_uint[i] %= BC_MUL_UINT_OVERFLOW;
62+
prod_vector[i + 1] += prod_vector[i] / BC_VECTOR_BOUNDARY_NUM;
63+
prod_vector[i] %= BC_VECTOR_BOUNDARY_NUM;
5864
}
5965
}
6066

@@ -66,16 +72,16 @@ static inline void bc_digits_adjustment(BC_UINT_T *prod_uint, size_t prod_arr_si
6672
* due to its divide-and-conquer nature.
6773
*/
6874
#if SIZEOF_SIZE_T == 4
69-
static uint32_t bc_parse_chunk_chars(const char *str)
75+
static BC_VECTOR bc_parse_chunk_chars(const char *str)
7076
{
71-
uint32_t tmp;
77+
BC_VECTOR tmp;
7278
memcpy(&tmp, str, sizeof(tmp));
7379
#if !BC_LITTLE_ENDIAN
7480
tmp = BC_BSWAP(tmp);
7581
#endif
7682

77-
uint32_t lower_digits = (tmp & 0x0f000f00) >> 8;
78-
uint32_t upper_digits = (tmp & 0x000f000f) * 10;
83+
BC_VECTOR lower_digits = (tmp & 0x0f000f00) >> 8;
84+
BC_VECTOR upper_digits = (tmp & 0x000f000f) * 10;
7985

8086
tmp = lower_digits + upper_digits;
8187

@@ -85,16 +91,16 @@ static uint32_t bc_parse_chunk_chars(const char *str)
8591
return lower_digits + upper_digits;
8692
}
8793
#elif SIZEOF_SIZE_T == 8
88-
static uint64_t bc_parse_chunk_chars(const char *str)
94+
static BC_VECTOR bc_parse_chunk_chars(const char *str)
8995
{
90-
uint64_t tmp;
96+
BC_VECTOR tmp;
9197
memcpy(&tmp, str, sizeof(tmp));
9298
#if !BC_LITTLE_ENDIAN
9399
tmp = BC_BSWAP(tmp);
94100
#endif
95101

96-
uint64_t lower_digits = (tmp & 0x0f000f000f000f00) >> 8;
97-
uint64_t upper_digits = (tmp & 0x000f000f000f000f) * 10;
102+
BC_VECTOR lower_digits = (tmp & 0x0f000f000f000f00) >> 8;
103+
BC_VECTOR upper_digits = (tmp & 0x000f000f000f000f) * 10;
98104

99105
tmp = lower_digits + upper_digits;
100106

@@ -111,17 +117,17 @@ static uint64_t bc_parse_chunk_chars(const char *str)
111117
#endif
112118

113119
/*
114-
* Converts BCD to uint, going backwards from pointer n by the number of
120+
* Converts bc_num to BC_VECTOR, going backwards from pointer n by the number of
115121
* characters specified by len.
116122
*/
117-
static inline BC_UINT_T bc_partial_convert_to_uint(const char *n, size_t len)
123+
static inline BC_VECTOR bc_partial_convert_to_vector(const char *n, size_t len)
118124
{
119-
if (len == BC_MUL_UINT_DIGITS) {
120-
return bc_parse_chunk_chars(n - BC_MUL_UINT_DIGITS + 1);
125+
if (len == BC_VECTOR_SIZE) {
126+
return bc_parse_chunk_chars(n - BC_VECTOR_SIZE + 1);
121127
}
122128

123-
BC_UINT_T num = 0;
124-
BC_UINT_T base = 1;
129+
BC_VECTOR num = 0;
130+
BC_VECTOR base = 1;
125131

126132
for (size_t i = 0; i < len; i++) {
127133
num += *n * base;
@@ -132,12 +138,12 @@ static inline BC_UINT_T bc_partial_convert_to_uint(const char *n, size_t len)
132138
return num;
133139
}
134140

135-
static inline void bc_convert_to_uint(BC_UINT_T *n_uint, const char *nend, size_t nlen)
141+
static inline void bc_convert_to_vector(BC_VECTOR *n_vector, const char *nend, size_t nlen)
136142
{
137143
size_t i = 0;
138144
while (nlen > 0) {
139-
size_t len = MIN(BC_MUL_UINT_DIGITS, nlen);
140-
n_uint[i] = bc_partial_convert_to_uint(nend, len);
145+
size_t len = MIN(BC_VECTOR_SIZE, nlen);
146+
n_vector[i] = bc_partial_convert_to_vector(nend, len);
141147
nend -= len;
142148
nlen -= len;
143149
i++;
@@ -153,18 +159,18 @@ static inline void bc_fast_mul(bc_num n1, size_t n1len, bc_num n2, size_t n2len,
153159
const char *n1end = n1->n_value + n1len - 1;
154160
const char *n2end = n2->n_value + n2len - 1;
155161

156-
BC_UINT_T n1_uint = bc_partial_convert_to_uint(n1end, n1len);
157-
BC_UINT_T n2_uint = bc_partial_convert_to_uint(n2end, n2len);
158-
BC_UINT_T prod_uint = n1_uint * n2_uint;
162+
BC_VECTOR n1_vector = bc_partial_convert_to_vector(n1end, n1len);
163+
BC_VECTOR n2_vector = bc_partial_convert_to_vector(n2end, n2len);
164+
BC_VECTOR prod_vector = n1_vector * n2_vector;
159165

160166
size_t prodlen = n1len + n2len;
161167
*prod = bc_new_num_nonzeroed(prodlen, 0);
162168
char *pptr = (*prod)->n_value;
163169
char *pend = pptr + prodlen - 1;
164170

165171
while (pend >= pptr) {
166-
*pend-- = prod_uint % BASE;
167-
prod_uint /= BASE;
172+
*pend-- = prod_vector % BASE;
173+
prod_vector /= BASE;
168174
}
169175
}
170176

@@ -215,7 +221,7 @@ static void bc_write_bcd_representation(uint32_t value, char *str)
215221
}
216222

217223
/*
218-
* Converts the BCD of bc_num by 4 (32 bits) or 8 (64 bits) digits to an array of BC_UINT_Ts.
224+
* Converts the BCD of bc_num by 4 (32 bits) or 8 (64 bits) digits to an array of BC_VECTOR.
219225
* The array is generated starting with the smaller digits.
220226
* e.g. 12345678901234567890 => {34567890, 56789012, 1234}
221227
*
@@ -229,28 +235,28 @@ static void bc_standard_mul(bc_num n1, size_t n1len, bc_num n2, size_t n2len, bc
229235
const char *n2end = n2->n_value + n2len - 1;
230236
size_t prodlen = n1len + n2len;
231237

232-
size_t n1_arr_size = (n1len + BC_MUL_UINT_DIGITS - 1) / BC_MUL_UINT_DIGITS;
233-
size_t n2_arr_size = (n2len + BC_MUL_UINT_DIGITS - 1) / BC_MUL_UINT_DIGITS;
238+
size_t n1_arr_size = (n1len + BC_VECTOR_SIZE - 1) / BC_VECTOR_SIZE;
239+
size_t n2_arr_size = (n2len + BC_VECTOR_SIZE - 1) / BC_VECTOR_SIZE;
234240
size_t prod_arr_size = n1_arr_size + n2_arr_size - 1;
235241

236242
/*
237-
* let's say that N is the max of n1len and n2len (and a multiple of BC_MUL_UINT_DIGITS for simplicity),
238-
* then this sum is <= N/BC_MUL_UINT_DIGITS + N/BC_MUL_UINT_DIGITS + N/BC_MUL_UINT_DIGITS + N/BC_MUL_UINT_DIGITS - 1
239-
* which is equal to N - 1 if BC_MUL_UINT_DIGITS is 4, and N/2 - 1 if BC_MUL_UINT_DIGITS is 8.
243+
* let's say that N is the max of n1len and n2len (and a multiple of BC_VECTOR_SIZE for simplicity),
244+
* then this sum is <= N/BC_VECTOR_SIZE + N/BC_VECTOR_SIZE + N/BC_VECTOR_SIZE + N/BC_VECTOR_SIZE - 1
245+
* which is equal to N - 1 if BC_VECTOR_SIZE is 4, and N/2 - 1 if BC_VECTOR_SIZE is 8.
240246
*/
241-
BC_UINT_T *buf = safe_emalloc(n1_arr_size + n2_arr_size + prod_arr_size, sizeof(BC_UINT_T), 0);
247+
BC_VECTOR *buf = safe_emalloc(n1_arr_size + n2_arr_size + prod_arr_size, sizeof(BC_VECTOR), 0);
242248

243-
BC_UINT_T *n1_uint = buf;
244-
BC_UINT_T *n2_uint = buf + n1_arr_size;
245-
BC_UINT_T *prod_uint = n2_uint + n2_arr_size;
249+
BC_VECTOR *n1_vector = buf;
250+
BC_VECTOR *n2_vector = buf + n1_arr_size;
251+
BC_VECTOR *prod_vector = n2_vector + n2_arr_size;
246252

247253
for (i = 0; i < prod_arr_size; i++) {
248-
prod_uint[i] = 0;
254+
prod_vector[i] = 0;
249255
}
250256

251257
/* Convert to uint[] */
252-
bc_convert_to_uint(n1_uint, n1end, n1len);
253-
bc_convert_to_uint(n2_uint, n2end, n2len);
258+
bc_convert_to_vector(n1_vector, n1end, n1len);
259+
bc_convert_to_vector(n2_vector, n2end, n2len);
254260

255261
/* Multiplication and addition */
256262
size_t count = 0;
@@ -260,34 +266,34 @@ static void bc_standard_mul(bc_num n1, size_t n1len, bc_num n2, size_t n2len, bc
260266
* When multiplying large numbers of digits, there is a possibility of
261267
* overflow, so digit adjustment is performed beforehand.
262268
*/
263-
if (UNEXPECTED(count >= BC_MUL_MAX_ADD_COUNT)) {
264-
bc_digits_adjustment(prod_uint, prod_arr_size);
269+
if (UNEXPECTED(count >= BC_VECTOR_NO_OVERFLOW_ADD_COUNT)) {
270+
bc_digits_adjustment(prod_vector, prod_arr_size);
265271
count = 0;
266272
}
267273
count++;
268274
for (size_t j = 0; j < n2_arr_size; j++) {
269-
prod_uint[i + j] += n1_uint[i] * n2_uint[j];
275+
prod_vector[i + j] += n1_vector[i] * n2_vector[j];
270276
}
271277
}
272278

273279
/*
274280
* Move a value exceeding 4/8 digits by carrying to the next digit.
275281
* However, the last digit does nothing.
276282
*/
277-
bc_digits_adjustment(prod_uint, prod_arr_size);
283+
bc_digits_adjustment(prod_vector, prod_arr_size);
278284

279285
/* Convert to bc_num */
280286
*prod = bc_new_num_nonzeroed(prodlen, 0);
281287
char *pptr = (*prod)->n_value;
282288
char *pend = pptr + prodlen - 1;
283289
i = 0;
284290
while (i < prod_arr_size - 1) {
285-
#if BC_MUL_UINT_DIGITS == 4
286-
bc_write_bcd_representation(prod_uint[i], pend - 3);
291+
#if BC_VECTOR_SIZE == 4
292+
bc_write_bcd_representation(prod_vector[i], pend - 3);
287293
pend -= 4;
288294
#else
289-
bc_write_bcd_representation(prod_uint[i] / 10000, pend - 7);
290-
bc_write_bcd_representation(prod_uint[i] % 10000, pend - 3);
295+
bc_write_bcd_representation(prod_vector[i] / 10000, pend - 7);
296+
bc_write_bcd_representation(prod_vector[i] % 10000, pend - 3);
291297
pend -= 8;
292298
#endif
293299
i++;
@@ -298,8 +304,8 @@ static void bc_standard_mul(bc_num n1, size_t n1len, bc_num n2, size_t n2len, bc
298304
* Also need to fill it to the end with zeros, so loop until the end of the string.
299305
*/
300306
while (pend >= pptr) {
301-
*pend-- = prod_uint[i] % BASE;
302-
prod_uint[i] /= BASE;
307+
*pend-- = prod_vector[i] % BASE;
308+
prod_vector[i] /= BASE;
303309
}
304310

305311
efree(buf);
@@ -320,7 +326,7 @@ bc_num bc_multiply(bc_num n1, bc_num n2, size_t scale)
320326
size_t prod_scale = MIN(full_scale, MAX(scale, MAX(n1->n_scale, n2->n_scale)));
321327

322328
/* Do the multiply */
323-
if (len1 <= BC_MUL_UINT_DIGITS && len2 <= BC_MUL_UINT_DIGITS) {
329+
if (len1 <= BC_VECTOR_SIZE && len2 <= BC_VECTOR_SIZE) {
324330
bc_fast_mul(n1, len1, n2, len2, &prod);
325331
} else {
326332
bc_standard_mul(n1, len1, n2, len2, &prod);

0 commit comments

Comments
 (0)