Skip to content

Commit a27f5e8

Browse files
committed
Compress the LUT
1 parent ed41210 commit a27f5e8

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

ext/bcmath/libbcmath/src/recmul.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,17 +159,17 @@ static inline void bc_fast_mul(bc_num n1, size_t n1len, bc_num n2, size_t n2len,
159159
}
160160

161161
#if BC_LITTLE_ENDIAN
162-
# define BC_ENCODE_LUT(A, B) ((A) | (B) << 8)
162+
# define BC_ENCODE_LUT(A, B) ((A) | (B) << 4)
163163
#else
164-
# define BC_ENCODE_LUT(A, B) ((B) | (A) << 8)
164+
# define BC_ENCODE_LUT(A, B) ((B) | (A) << 4)
165165
#endif
166166

167167
#define LUT_ITERATE(_, A) \
168168
_(A, 0), _(A, 1), _(A, 2), _(A, 3), _(A, 4), _(A, 5), _(A, 6), _(A, 7), _(A, 8), _(A, 9)
169169

170170
/* This LUT encodes the decimal representation of numbers 0-100
171171
* such that we can avoid taking modulos and divisions which would be slow. */
172-
static const unsigned short LUT[100] = {
172+
static const unsigned char LUT[100] = {
173173
LUT_ITERATE(BC_ENCODE_LUT, 0),
174174
LUT_ITERATE(BC_ENCODE_LUT, 1),
175175
LUT_ITERATE(BC_ENCODE_LUT, 2),
@@ -182,6 +182,11 @@ static const unsigned short LUT[100] = {
182182
LUT_ITERATE(BC_ENCODE_LUT, 9),
183183
};
184184

185+
static inline unsigned short bc_expand_lut(unsigned char c)
186+
{
187+
return (c & 0x0f) | (c & 0xf0) << 4;
188+
}
189+
185190
/* Writes the character representation of the number encoded in value.
186191
* E.g. if value = 1234, then the string "1234" will be written to str. */
187192
static void bc_write_bcd_representation(uint32_t value, char *str)
@@ -191,10 +196,10 @@ static void bc_write_bcd_representation(uint32_t value, char *str)
191196

192197
#if BC_LITTLE_ENDIAN
193198
/* Note: little endian, so `lower` comes before `upper`! */
194-
uint32_t digits = LUT[lower] << 16 | LUT[upper];
199+
uint32_t digits = bc_expand_lut(LUT[lower]) << 16 | bc_expand_lut(LUT[upper]);
195200
#else
196201
/* Note: big endian, so `upper` comes before `lower`! */
197-
uint32_t digits = LUT[upper] << 16 | LUT[lower];
202+
uint32_t digits = bc_expand_lut(LUT[upper]) << 16 | bc_expand_lut(LUT[lower]);
198203
#endif
199204
memcpy(str, &digits, sizeof(digits));
200205
}

0 commit comments

Comments
 (0)