forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add big int format to msgpack.pack #7811
Draft
Neradoc
wants to merge
2
commits into
adafruit:main
Choose a base branch
from
Neradoc:msgpack-pack-more
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+39
−15
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ | |
#include "py/obj.h" | ||
#include "py/binary.h" | ||
#include "py/objarray.h" | ||
#include "py/objint.h" | ||
#include "py/objlist.h" | ||
#include "py/objstringio.h" | ||
#include "py/parsenum.h" | ||
|
@@ -195,19 +196,43 @@ STATIC mp_map_elem_t *dict_iter_next(mp_obj_dict_t *dict, size_t *cur) { | |
return NULL; | ||
} | ||
|
||
STATIC void pack_int(msgpack_stream_t *s, int32_t x) { | ||
if (x > -32 && x < 128) { | ||
write1(s, x); | ||
} else if ((int8_t)x == x) { | ||
write1(s, 0xd0); | ||
write1(s, x); | ||
} else if ((int16_t)x == x) { | ||
write1(s, 0xd1); | ||
write2(s, x); | ||
STATIC void pack_int(msgpack_stream_t *s, mp_obj_t obj) { | ||
byte buffer[9]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This module is already setup with |
||
byte *buf = (buffer + 1); | ||
byte *type = buffer; | ||
size_t len = 0; | ||
// encode signed or unsigned | ||
bool _signed = mp_obj_int_sign(obj) < 0; | ||
if (mp_obj_is_small_int(obj)) { | ||
int32_t x = MP_OBJ_SMALL_INT_VALUE(obj); | ||
if (x > -32 && x < 128) { | ||
write1(s, x); | ||
return; | ||
} else if (-0x80 <= x && x <= 0xff) { | ||
*type = _signed ? 0xd0 : 0xcc; | ||
len = 1; | ||
} else if (-0x8000 <= x && x <= 0xffff) { | ||
*type = _signed ? 0xd1 : 0xcd; | ||
len = 2; | ||
} else { | ||
*type = _signed ? 0xd2 : 0xce; | ||
len = 4; | ||
} | ||
mp_binary_set_int(len, true, buf, x); | ||
} else { | ||
write1(s, 0xd2); | ||
write4(s, x); | ||
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE | ||
// raise if long int overflows | ||
mp_obj_int_buffer_overflow_check(obj, 8, _signed); | ||
// todo: encode remaining 32 bit values as 0xd2/0xce ? | ||
*type = _signed ? 0xd3 : 0xcf; | ||
len = 8; | ||
mp_obj_int_to_bytes_impl(obj, true, len, buf); | ||
#else | ||
// never reached because you can't have mp_obj_is_small_int false | ||
// if there is no LONGINT implemented ! | ||
#endif | ||
} | ||
write(s, buffer, len + 1); | ||
} | ||
|
||
STATIC void pack_bin(msgpack_stream_t *s, const uint8_t *data, size_t len) { | ||
|
@@ -275,10 +300,9 @@ STATIC void pack_dict(msgpack_stream_t *s, size_t len) { | |
} | ||
|
||
STATIC void pack(mp_obj_t obj, msgpack_stream_t *s, mp_obj_t default_handler) { | ||
if (mp_obj_is_small_int(obj)) { | ||
// int | ||
int32_t x = MP_OBJ_SMALL_INT_VALUE(obj); | ||
pack_int(s, x); | ||
if (mp_obj_is_int(obj)) { | ||
// all ints | ||
pack_int(s, obj); | ||
} else if (mp_obj_is_str(obj)) { | ||
// string | ||
size_t len; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this function works as is for
pack_small_int
. I wouldn't bother trying to optimize for 1 byte of packing for some int values.