Skip to content

Commit eee1c15

Browse files
committed
fixup! intn: add intn_to_integer_bytes and intn_required_unsigned_integer_bytes
1 parent fca326b commit eee1c15

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

src/libAtomVM/intn.c

+8-9
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,9 @@ static size_t count16(const uint16_t *num, size_t num_len)
190190
return count;
191191
}
192192

193-
static inline uint32_t nlz(uint32_t x)
193+
// make sure that x != 0 before calling this function
194+
static inline uint32_t uint32_nlz(uint32_t x)
194195
{
195-
// This function is used only from divmnu, that doesn't allow 32 leading zeros
196196
ASSUME(x != 0);
197197

198198
#ifdef __has_builtin
@@ -272,7 +272,7 @@ static int divmnu16(
272272
// same amount. We may have to append a high-order
273273
// digit on the dividend; we do that unconditionally.
274274

275-
s = nlz(v[n - 1]) - 16; // 0 <= s <= 15.
275+
s = uint32_nlz(v[n - 1]) - 16; // 0 <= s <= 15.
276276
uint16_t vn[INTN_DIVMNU_MAX_IN_LEN * (sizeof(intn_digit_t) / sizeof(uint16_t))];
277277
for (i = n - 1; i > 0; i--)
278278
vn[i] = (v[i] << s) | (v[i - 1] >> (16 - s));
@@ -778,14 +778,13 @@ int intn_to_integer_bytes(const intn_digit_t in[], size_t in_len, intn_integer_s
778778

779779
size_t intn_required_unsigned_integer_bytes(const intn_digit_t in[], size_t in_len)
780780
{
781-
uint8_t *in8 = (uint8_t *) in;
782781
int i;
783-
for (i = in_len * sizeof(intn_digit_t) - 1; i >= 0; i--) {
784-
if (in8[i] != 0) {
785-
break;
782+
for (i = in_len - 1; i >= 0; i--) {
783+
uint32_t in_i = in[i];
784+
if (in_i != 0) {
785+
return (i + 1) * sizeof(uint32_t) - (uint32_nlz(in_i) / 8);
786786
}
787787
}
788-
size_t count = i + 1;
789788

790-
return count;
789+
return 0;
791790
}

0 commit comments

Comments
 (0)