@@ -159,17 +159,17 @@ static inline void bc_fast_mul(bc_num n1, size_t n1len, bc_num n2, size_t n2len,
159
159
}
160
160
161
161
#if BC_LITTLE_ENDIAN
162
- # define BC_ENCODE_LUT (A , B ) ((A) | (B) << 8 )
162
+ # define BC_ENCODE_LUT (A , B ) ((A) | (B) << 4 )
163
163
#else
164
- # define BC_ENCODE_LUT (A , B ) ((B) | (A) << 8 )
164
+ # define BC_ENCODE_LUT (A , B ) ((B) | (A) << 4 )
165
165
#endif
166
166
167
167
#define LUT_ITERATE (_ , A ) \
168
168
_(A, 0), _(A, 1), _(A, 2), _(A, 3), _(A, 4), _(A, 5), _(A, 6), _(A, 7), _(A, 8), _(A, 9)
169
169
170
170
/* This LUT encodes the decimal representation of numbers 0-100
171
171
* 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 ] = {
173
173
LUT_ITERATE (BC_ENCODE_LUT , 0 ),
174
174
LUT_ITERATE (BC_ENCODE_LUT , 1 ),
175
175
LUT_ITERATE (BC_ENCODE_LUT , 2 ),
@@ -182,6 +182,11 @@ static const unsigned short LUT[100] = {
182
182
LUT_ITERATE (BC_ENCODE_LUT , 9 ),
183
183
};
184
184
185
+ static inline unsigned short bc_expand_lut (unsigned char c )
186
+ {
187
+ return (c & 0x0f ) | (c & 0xf0 ) << 4 ;
188
+ }
189
+
185
190
/* Writes the character representation of the number encoded in value.
186
191
* E.g. if value = 1234, then the string "1234" will be written to str. */
187
192
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)
191
196
192
197
#if BC_LITTLE_ENDIAN
193
198
/* 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 ]) ;
195
200
#else
196
201
/* 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 ]) ;
198
203
#endif
199
204
memcpy (str , & digits , sizeof (digits ));
200
205
}
0 commit comments