Skip to content

Commit 8bb7d95

Browse files
committed
py: Factor duplicated function to calculate size of formatted int.
1 parent df3e5d2 commit 8bb7d95

5 files changed

Lines changed: 10 additions & 31 deletions

File tree

py/mpz.c

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -645,18 +645,6 @@ STATIC void mpn_div(mpz_dig_t *num_dig, mp_uint_t *num_len, const mpz_dig_t *den
645645

646646
#define MIN_ALLOC (2)
647647

648-
STATIC const uint8_t log_base2_floor[] = {
649-
0,
650-
0, 1, 1, 2,
651-
2, 2, 2, 3,
652-
3, 3, 3, 3,
653-
3, 3, 3, 4,
654-
4, 4, 4, 4,
655-
4, 4, 4, 4,
656-
4, 4, 4, 4,
657-
4, 4, 4, 5
658-
};
659-
660648
void mpz_init_zero(mpz_t *z) {
661649
z->neg = 0;
662650
z->fixed_dig = 0;
@@ -1652,18 +1640,6 @@ mp_float_t mpz_as_float(const mpz_t *i) {
16521640
}
16531641
#endif
16541642

1655-
mp_uint_t mpz_as_str_size(const mpz_t *i, mp_uint_t base, const char *prefix, char comma) {
1656-
if (base < 2 || base > 32) {
1657-
return 0;
1658-
}
1659-
1660-
mp_uint_t num_digits = i->len * DIG_SIZE / log_base2_floor[base] + 1;
1661-
mp_uint_t num_commas = comma ? num_digits / 3: 0;
1662-
mp_uint_t prefix_len = prefix ? strlen(prefix) : 0;
1663-
1664-
return num_digits + num_commas + prefix_len + 2; // +1 for sign, +1 for null byte
1665-
}
1666-
16671643
#if 0
16681644
this function is unused
16691645
char *mpz_as_str(const mpz_t *i, mp_uint_t base) {
@@ -1673,7 +1649,7 @@ char *mpz_as_str(const mpz_t *i, mp_uint_t base) {
16731649
}
16741650
#endif
16751651

1676-
// assumes enough space as calculated by mpz_as_str_size
1652+
// assumes enough space as calculated by mp_int_format_size
16771653
// returns length of string, not including null byte
16781654
mp_uint_t mpz_as_str_inpl(const mpz_t *i, mp_uint_t base, const char *prefix, char base_char, char comma, char *str) {
16791655
if (str == NULL || base < 2 || base > 32) {

py/mpz.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ void mpz_or_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
127127
void mpz_xor_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
128128
void mpz_divmod_inpl(mpz_t *dest_quo, mpz_t *dest_rem, const mpz_t *lhs, const mpz_t *rhs);
129129

130+
static inline size_t mpz_max_num_bits(const mpz_t *z) { return z->len * MPZ_DIG_SIZE; }
130131
mp_int_t mpz_hash(const mpz_t *z);
131132
bool mpz_as_int_checked(const mpz_t *z, mp_int_t *value);
132133
bool mpz_as_uint_checked(const mpz_t *z, mp_uint_t *value);

py/objint.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,14 @@ STATIC const uint8_t log_base2_floor[] = {
162162
4, 4, 4, 5
163163
};
164164

165-
STATIC uint int_as_str_size_formatted(uint base, const char *prefix, char comma) {
165+
size_t mp_int_format_size(size_t num_bits, int base, const char *prefix, char comma) {
166166
if (base < 2 || base > 32) {
167167
return 0;
168168
}
169169

170-
uint num_digits = sizeof(fmt_int_t) * 8 / log_base2_floor[base] + 1;
171-
uint num_commas = comma ? num_digits / 3: 0;
172-
uint prefix_len = prefix ? strlen(prefix) : 0;
170+
size_t num_digits = num_bits / log_base2_floor[base] + 1;
171+
size_t num_commas = comma ? num_digits / 3 : 0;
172+
size_t prefix_len = prefix ? strlen(prefix) : 0;
173173
return num_digits + num_commas + prefix_len + 2; // +1 for sign, +1 for null byte
174174
}
175175

@@ -211,7 +211,7 @@ char *mp_obj_int_formatted(char **buf, mp_uint_t *buf_size, mp_uint_t *fmt_size,
211211
sign = '-';
212212
}
213213

214-
uint needed_size = int_as_str_size_formatted(base, prefix, comma);
214+
uint needed_size = mp_int_format_size(sizeof(fmt_int_t) * 8, base, prefix, comma);
215215
if (needed_size > *buf_size) {
216216
*buf = m_new(char, needed_size);
217217
*buf_size = needed_size;

py/objint.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ typedef enum {
5050
mp_fp_as_int_class_t mp_classify_fp_as_int(mp_float_t val);
5151
#endif // MICROPY_PY_BUILTINS_FLOAT
5252

53+
size_t mp_int_format_size(size_t num_bits, int base, const char *prefix, char comma);
54+
5355
void mp_obj_int_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind);
5456
char *mp_obj_int_formatted(char **buf, mp_uint_t *buf_size, mp_uint_t *fmt_size, mp_const_obj_t self_in,
5557
int base, const char *prefix, char base_char, char comma);

py/objint_mpz.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ char *mp_obj_int_formatted_impl(char **buf, mp_uint_t *buf_size, mp_uint_t *fmt_
9595
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_int));
9696
const mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in);
9797

98-
mp_uint_t needed_size = mpz_as_str_size(&self->mpz, base, prefix, comma);
98+
mp_uint_t needed_size = mp_int_format_size(mpz_max_num_bits(&self->mpz), base, prefix, comma);
9999
if (needed_size > *buf_size) {
100100
*buf = m_new(char, needed_size);
101101
*buf_size = needed_size;

0 commit comments

Comments
 (0)