Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
micropython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
xpstem
micropython
Commits
8bb7d958
Commit
8bb7d958
authored
Oct 11, 2016
by
Damien George
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
py: Factor duplicated function to calculate size of formatted int.
parent
df3e5d2b
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
10 additions
and
31 deletions
+10
-31
py/mpz.c
py/mpz.c
+1
-25
py/mpz.h
py/mpz.h
+1
-0
py/objint.c
py/objint.c
+5
-5
py/objint.h
py/objint.h
+2
-0
py/objint_mpz.c
py/objint_mpz.c
+1
-1
No files found.
py/mpz.c
View file @
8bb7d958
...
...
@@ -645,18 +645,6 @@ STATIC void mpn_div(mpz_dig_t *num_dig, mp_uint_t *num_len, const mpz_dig_t *den
#define MIN_ALLOC (2)
STATIC
const
uint8_t
log_base2_floor
[]
=
{
0
,
0
,
1
,
1
,
2
,
2
,
2
,
2
,
3
,
3
,
3
,
3
,
3
,
3
,
3
,
3
,
4
,
4
,
4
,
4
,
4
,
4
,
4
,
4
,
4
,
4
,
4
,
4
,
4
,
4
,
4
,
4
,
5
};
void
mpz_init_zero
(
mpz_t
*
z
)
{
z
->
neg
=
0
;
z
->
fixed_dig
=
0
;
...
...
@@ -1652,18 +1640,6 @@ mp_float_t mpz_as_float(const mpz_t *i) {
}
#endif
mp_uint_t
mpz_as_str_size
(
const
mpz_t
*
i
,
mp_uint_t
base
,
const
char
*
prefix
,
char
comma
)
{
if
(
base
<
2
||
base
>
32
)
{
return
0
;
}
mp_uint_t
num_digits
=
i
->
len
*
DIG_SIZE
/
log_base2_floor
[
base
]
+
1
;
mp_uint_t
num_commas
=
comma
?
num_digits
/
3
:
0
;
mp_uint_t
prefix_len
=
prefix
?
strlen
(
prefix
)
:
0
;
return
num_digits
+
num_commas
+
prefix_len
+
2
;
// +1 for sign, +1 for null byte
}
#if 0
this function is unused
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) {
}
#endif
// assumes enough space as calculated by mp
z_as_str
_size
// assumes enough space as calculated by mp
_int_format
_size
// returns length of string, not including null byte
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
)
{
if
(
str
==
NULL
||
base
<
2
||
base
>
32
)
{
...
...
py/mpz.h
View file @
8bb7d958
...
...
@@ -127,6 +127,7 @@ void mpz_or_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
void
mpz_xor_inpl
(
mpz_t
*
dest
,
const
mpz_t
*
lhs
,
const
mpz_t
*
rhs
);
void
mpz_divmod_inpl
(
mpz_t
*
dest_quo
,
mpz_t
*
dest_rem
,
const
mpz_t
*
lhs
,
const
mpz_t
*
rhs
);
static
inline
size_t
mpz_max_num_bits
(
const
mpz_t
*
z
)
{
return
z
->
len
*
MPZ_DIG_SIZE
;
}
mp_int_t
mpz_hash
(
const
mpz_t
*
z
);
bool
mpz_as_int_checked
(
const
mpz_t
*
z
,
mp_int_t
*
value
);
bool
mpz_as_uint_checked
(
const
mpz_t
*
z
,
mp_uint_t
*
value
);
...
...
py/objint.c
View file @
8bb7d958
...
...
@@ -162,14 +162,14 @@ STATIC const uint8_t log_base2_floor[] = {
4
,
4
,
4
,
5
};
STATIC
uint
int_as_str_size_formatted
(
u
int
base
,
const
char
*
prefix
,
char
comma
)
{
size_t
mp_int_format_size
(
size_t
num_bits
,
int
base
,
const
char
*
prefix
,
char
comma
)
{
if
(
base
<
2
||
base
>
32
)
{
return
0
;
}
uint
num_digits
=
sizeof
(
fmt_int_t
)
*
8
/
log_base2_floor
[
base
]
+
1
;
uint
num_commas
=
comma
?
num_digits
/
3
:
0
;
uin
t
prefix_len
=
prefix
?
strlen
(
prefix
)
:
0
;
size_t
num_digits
=
num_bits
/
log_base2_floor
[
base
]
+
1
;
size_t
num_commas
=
comma
?
num_digits
/
3
:
0
;
size_
t
prefix_len
=
prefix
?
strlen
(
prefix
)
:
0
;
return
num_digits
+
num_commas
+
prefix_len
+
2
;
// +1 for sign, +1 for null byte
}
...
...
@@ -211,7 +211,7 @@ char *mp_obj_int_formatted(char **buf, mp_uint_t *buf_size, mp_uint_t *fmt_size,
sign
=
'-'
;
}
uint
needed_size
=
int_as_str_size_formatted
(
base
,
prefix
,
comma
);
uint
needed_size
=
mp_int_format_size
(
sizeof
(
fmt_int_t
)
*
8
,
base
,
prefix
,
comma
);
if
(
needed_size
>
*
buf_size
)
{
*
buf
=
m_new
(
char
,
needed_size
);
*
buf_size
=
needed_size
;
...
...
py/objint.h
View file @
8bb7d958
...
...
@@ -50,6 +50,8 @@ typedef enum {
mp_fp_as_int_class_t
mp_classify_fp_as_int
(
mp_float_t
val
);
#endif // MICROPY_PY_BUILTINS_FLOAT
size_t
mp_int_format_size
(
size_t
num_bits
,
int
base
,
const
char
*
prefix
,
char
comma
);
void
mp_obj_int_print
(
const
mp_print_t
*
print
,
mp_obj_t
self_in
,
mp_print_kind_t
kind
);
char
*
mp_obj_int_formatted
(
char
**
buf
,
mp_uint_t
*
buf_size
,
mp_uint_t
*
fmt_size
,
mp_const_obj_t
self_in
,
int
base
,
const
char
*
prefix
,
char
base_char
,
char
comma
);
...
...
py/objint_mpz.c
View file @
8bb7d958
...
...
@@ -95,7 +95,7 @@ char *mp_obj_int_formatted_impl(char **buf, mp_uint_t *buf_size, mp_uint_t *fmt_
assert
(
MP_OBJ_IS_TYPE
(
self_in
,
&
mp_type_int
));
const
mp_obj_int_t
*
self
=
MP_OBJ_TO_PTR
(
self_in
);
mp_uint_t
needed_size
=
mp
z_as_str_size
(
&
self
->
mpz
,
base
,
prefix
,
comma
);
mp_uint_t
needed_size
=
mp
_int_format_size
(
mpz_max_num_bits
(
&
self
->
mpz
)
,
base
,
prefix
,
comma
);
if
(
needed_size
>
*
buf_size
)
{
*
buf
=
m_new
(
char
,
needed_size
);
*
buf_size
=
needed_size
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment