Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
58056b0f
Commit
58056b0f
authored
10 years ago
by
Damien George
Browse files
Options
Downloads
Patches
Plain Diff
py: Fix handling of "0" mpz in some functions.
parent
f5465b9e
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
py/mpz.c
+13
-8
13 additions, 8 deletions
py/mpz.c
with
13 additions
and
8 deletions
py/mpz.c
+
13
−
8
View file @
58056b0f
...
...
@@ -653,6 +653,11 @@ void mpz_set(mpz_t *dest, const mpz_t *src) {
}
void
mpz_set_from_int
(
mpz_t
*
z
,
mp_int_t
val
)
{
if
(
val
==
0
)
{
z
->
len
=
0
;
return
;
}
mpz_need_dig
(
z
,
MPZ_NUM_DIG_FOR_INT
);
mp_uint_t
uval
;
...
...
@@ -709,19 +714,19 @@ typedef uint32_t mp_float_int_t;
z
->
neg
=
u
.
p
.
sgn
;
if
(
u
.
p
.
exp
==
0
)
{
// value == 0 || value < 1
mpz_
init_zero
(
z
);
mpz_
set_from_int
(
z
,
0
);
}
else
if
(
u
.
p
.
exp
==
((
1
<<
EXP_SZ
)
-
1
))
{
// u.p.frc == 0 indicates inf, else NaN
// should be handled by caller
mpz_
init_zero
(
z
);
mpz_
set_from_int
(
z
,
0
);
}
else
{
const
int
adj_exp
=
(
int
)
u
.
p
.
exp
-
((
1
<<
(
EXP_SZ
-
1
))
-
1
);
if
(
adj_exp
<
0
)
{
// value < 1 , truncates to 0
mpz_
init_zero
(
z
);
mpz_
set_from_int
(
z
,
0
);
}
else
if
(
adj_exp
==
0
)
{
// 1 <= value < 2 , so truncates to 1
mpz_
ini
t_from_int
(
z
,
1
);
mpz_
se
t_from_int
(
z
,
1
);
}
else
{
// 2 <= value
const
int
dig_cnt
=
(
adj_exp
+
1
+
(
DIG_SIZE
-
1
))
/
DIG_SIZE
;
...
...
@@ -1353,7 +1358,7 @@ mp_int_t mpz_hash(const mpz_t *z) {
mp_int_t
val
=
0
;
mpz_dig_t
*
d
=
z
->
dig
+
z
->
len
;
while
(
--
d
>
=
z
->
dig
)
{
while
(
d
--
>
z
->
dig
)
{
val
=
(
val
<<
DIG_SIZE
)
|
*
d
;
}
...
...
@@ -1368,7 +1373,7 @@ bool mpz_as_int_checked(const mpz_t *i, mp_int_t *value) {
mp_int_t
val
=
0
;
mpz_dig_t
*
d
=
i
->
dig
+
i
->
len
;
while
(
--
d
>
=
i
->
dig
)
{
while
(
d
--
>
i
->
dig
)
{
if
(
val
>
(
~
(
WORD_MSBIT_HIGH
)
>>
DIG_SIZE
))
{
// will overflow
return
false
;
...
...
@@ -1393,7 +1398,7 @@ bool mpz_as_uint_checked(const mpz_t *i, mp_uint_t *value) {
mp_uint_t
val
=
0
;
mpz_dig_t
*
d
=
i
->
dig
+
i
->
len
;
while
(
--
d
>
=
i
->
dig
)
{
while
(
d
--
>
i
->
dig
)
{
if
(
val
>
(
~
(
WORD_MSBIT_HIGH
)
>>
(
DIG_SIZE
-
1
)))
{
// will overflow
return
false
;
...
...
@@ -1410,7 +1415,7 @@ mp_float_t mpz_as_float(const mpz_t *i) {
mp_float_t
val
=
0
;
mpz_dig_t
*
d
=
i
->
dig
+
i
->
len
;
while
(
--
d
>
=
i
->
dig
)
{
while
(
d
--
>
i
->
dig
)
{
val
=
val
*
DIG_BASE
+
*
d
;
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment