summaryrefslogtreecommitdiffstatshomepage
path: root/py/binary.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-04-19 02:18:19 +0100
committerDamien George <damien.p.george@gmail.com>2014-04-19 02:18:19 +0100
commit27dd471098e5d27949826295e821df972d9af083 (patch)
tree05566c9cb594250aafd845512305ba0bcb9cce96 /py/binary.c
parent3d19137442447f2e02db55bcd78b4c83cdc259f7 (diff)
parent206dd2a905fc4bca89c0b1a8beb2c71516f2053f (diff)
downloadmicropython-27dd471098e5d27949826295e821df972d9af083.tar.gz
micropython-27dd471098e5d27949826295e821df972d9af083.zip
Merge branch 'master' of github.com:micropython/micropython
Diffstat (limited to 'py/binary.c')
-rw-r--r--py/binary.c165
1 files changed, 96 insertions, 69 deletions
diff --git a/py/binary.c b/py/binary.c
index d3dd009546..ee95d56e44 100644
--- a/py/binary.c
+++ b/py/binary.c
@@ -1,4 +1,5 @@
#include <stdint.h>
+#include <stdlib.h>
#include <assert.h>
#include "misc.h"
@@ -9,34 +10,52 @@
// Helpers to work with binary-encoded data
-int mp_binary_get_size(char typecode) {
- // This assumes that unsigned and signed types are of the same type,
- // which is invariant for [u]intN_t.
- switch (typecode) {
- case BYTEARRAY_TYPECODE:
- case 'b':
- case 'B':
- return sizeof(int8_t);
- case 'h':
- case 'H':
- return sizeof(int16_t);
- case 'i':
- case 'I':
- return sizeof(int32_t);
- case 'l':
- case 'L':
- return sizeof(int32_t);
- case 'q':
- case 'Q':
- return sizeof(long long);
-#if MICROPY_ENABLE_FLOAT
- case 'f':
- return sizeof(float);
- case 'd':
- return sizeof(double);
-#endif
+int mp_binary_get_size(char struct_type, char val_type, uint *palign) {
+ int size = 0;
+ int align = 1;
+ switch (struct_type) {
+ case '<': case '>':
+ switch (val_type) {
+ case 'b': case 'B':
+ size = 1; break;
+ case 'h': case 'H':
+ size = 2; break;
+ case 'i': case 'I':
+ size = 4; break;
+ case 'l': case 'L':
+ size = 4; break;
+ case 'q': case 'Q':
+ size = 8; break;
+ }
+ break;
+ case '@': {
+ // TODO:
+ // The simplest heuristic for alignment is to align by value
+ // size, but that doesn't work for "bigger than int" types,
+ // for example, long long may very well have long alignment
+ // So, we introduce separate alignment handling, but having
+ // formal support for that is different from actually supporting
+ // particular (or any) ABI.
+ switch (val_type) {
+ case BYTEARRAY_TYPECODE:
+ case 'b': case 'B':
+ align = size = 1; break;
+ case 'h': case 'H':
+ align = size = sizeof(short); break;
+ case 'i': case 'I':
+ align = size = sizeof(int); break;
+ case 'l': case 'L':
+ align = size = sizeof(long); break;
+ case 'q': case 'Q':
+ // TODO: This is for x86
+ align = sizeof(int); size = sizeof(long long); break;
+ }
+ }
}
- return -1;
+ if (palign != NULL) {
+ *palign = align;
+ }
+ return size;
}
mp_obj_t mp_binary_get_val_array(char typecode, void *p, int index) {
@@ -80,48 +99,17 @@ mp_obj_t mp_binary_get_val_array(char typecode, void *p, int index) {
#define is_signed(typecode) (typecode > 'Z')
mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
byte *p = *ptr;
- uint size = 0;
- switch (struct_type) {
- case '<': case '>':
- switch (val_type) {
- case 'b': case 'B':
- size = 1; break;
- case 'h': case 'H':
- size = 2; break;
- case 'i': case 'I':
- size = 4; break;
- case 'l': case 'L':
- size = 4; break;
- }
- break;
- case '@': {
- // TODO:
- // The simplest heuristic for alignment is to align by value
- // size, but that doesn't work for "bigger than int" types,
- // for example, long long may very well have long alignment
- // So, we introduce separate alignment handling, but having
- // formal support for that is different from actually supporting
- // particular (or any) ABI.
- uint align = 0;
- switch (val_type) {
- case 'b': case 'B':
- align = size = 1; break;
- case 'h': case 'H':
- align = size = sizeof(short); break;
- case 'i': case 'I':
- align = size = sizeof(int); break;
- case 'l': case 'L':
- align = size = sizeof(long); break;
- }
- // Make pointer aligned
- p = (byte*)(((machine_uint_t)p + align - 1) & ~(align - 1));
- #if MP_ENDIANNESS_LITTLE
- struct_type = '<';
- #else
- struct_type = '>';
- #endif
- break;
- }
+ uint align;
+
+ int size = mp_binary_get_size(struct_type, val_type, &align);
+ if (struct_type == '@') {
+ // Make pointer aligned
+ p = (byte*)(((machine_uint_t)p + align - 1) & ~(align - 1));
+ #if MP_ENDIANNESS_LITTLE
+ struct_type = '<';
+ #else
+ struct_type = '>';
+ #endif
}
int delta;
@@ -150,6 +138,45 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
}
}
+void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **ptr) {
+ byte *p = *ptr;
+ uint align;
+
+ int size = mp_binary_get_size(struct_type, val_type, &align);
+ if (struct_type == '@') {
+ // Make pointer aligned
+ p = (byte*)(((machine_uint_t)p + align - 1) & ~(align - 1));
+ #if MP_ENDIANNESS_LITTLE
+ struct_type = '<';
+ #else
+ struct_type = '>';
+ #endif
+ }
+
+#if MP_ENDIANNESS_BIG
+#error Not implemented
+#endif
+ machine_int_t val = mp_obj_int_get_checked(val_in);
+ byte *in = (byte*)&val;
+ int in_delta, out_delta;
+ uint val_sz = MIN(size, sizeof(val));
+ if (struct_type == '>') {
+ in_delta = -1;
+ out_delta = 1;
+ in += val_sz - 1;
+ } else {
+ in_delta = out_delta = 1;
+ }
+
+ for (uint i = val_sz; i > 0; i--) {
+ *p = *in;
+ p += out_delta;
+ in += in_delta;
+ }
+
+ *ptr += size;
+}
+
void mp_binary_set_val_array(char typecode, void *p, int index, mp_obj_t val_in) {
switch (typecode) {
#if MICROPY_ENABLE_FLOAT