void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **ptr) { byte *p = *ptr; mp_uint_t align; size_t size = mp_binary_get_size(struct_type, val_type, &align); if (struct_type == '@') { // Make pointer aligned p = (byte*)(((mp_uint_t)p + align - 1) & ~((mp_uint_t)align - 1)); if (MP_ENDIANNESS_LITTLE) { struct_type = '<'; } else { struct_type = '>'; } } *ptr = p + size; mp_uint_t val; switch (val_type) { case 'O': val = (mp_uint_t)val_in; break; #if MICROPY_PY_BUILTINS_FLOAT case 'f': { union { uint32_t i; float f; } fp_sp; fp_sp.f = mp_obj_get_float(val_in); val = fp_sp.i; break; } case 'd': { union { uint64_t i64; uint32_t i32[2]; double f; } fp_dp; fp_dp.f = mp_obj_get_float(val_in); if (BYTES_PER_WORD == 8) { val = fp_dp.i64; } else { int be = struct_type == '>'; mp_binary_set_int(sizeof(uint32_t), be, p, fp_dp.i32[MP_ENDIANNESS_BIG ^ be]); p += sizeof(uint32_t); val = fp_dp.i32[MP_ENDIANNESS_LITTLE ^ be]; } break; } #endif default: #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE if (MP_OBJ_IS_TYPE(val_in, &mp_type_int)) { mp_obj_int_to_bytes_impl(val_in, struct_type == '>', size, p); return; } else #endif { val = mp_obj_get_int(val_in); // sign extend if needed if (BYTES_PER_WORD < 8 && size > sizeof(val) && is_signed(val_type) && (mp_int_t)val < 0) { memset(p + sizeof(val), 0xff, size - sizeof(val)); } } } mp_binary_set_int(MIN((size_t)size, sizeof(val)), struct_type == '>', p, val); }
void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **ptr) { byte *p = *ptr; mp_uint_t align; int size = mp_binary_get_size(struct_type, val_type, &align); if (struct_type == '@') { // Make pointer aligned p = (byte*)(((mp_uint_t)p + align - 1) & ~((mp_uint_t)align - 1)); if (MP_ENDIANNESS_LITTLE) { struct_type = '<'; } else { struct_type = '>'; } } *ptr = p + size; mp_uint_t val; switch (val_type) { case 'O': val = (mp_uint_t)val_in; break; #if MICROPY_PY_BUILTINS_FLOAT case 'f': { union { uint32_t i; float f; } fp_sp; fp_sp.f = mp_obj_get_float(val_in); val = fp_sp.i; break; } case 'd': { union { uint64_t i64; uint32_t i32[2]; double f; } fp_dp; fp_dp.f = mp_obj_get_float(val_in); if (BYTES_PER_WORD == 8) { val = fp_dp.i64; } else { int be = struct_type == '>'; mp_binary_set_int(sizeof(uint32_t), be, p, fp_dp.i32[MP_ENDIANNESS_BIG ^ be]); p += sizeof(uint32_t); val = fp_dp.i32[MP_ENDIANNESS_LITTLE ^ be]; } break; } #endif default: // we handle large ints here by calling the truncated accessor if (MP_OBJ_IS_TYPE(val_in, &mp_type_int)) { val = mp_obj_int_get_truncated(val_in); } else { val = mp_obj_get_int(val_in); } } mp_binary_set_int(MIN((size_t)size, sizeof(val)), struct_type == '>', p, val); }
void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **ptr) { byte *p = *ptr; mp_uint_t align; int size = mp_binary_get_size(struct_type, val_type, &align); if (struct_type == '@') { // Make pointer aligned p = (byte*)(((mp_uint_t)p + align - 1) & ~((mp_uint_t)align - 1)); #if MP_ENDIANNESS_LITTLE struct_type = '<'; #else struct_type = '>'; #endif } *ptr = p + size; #if MP_ENDIANNESS_BIG #error Not implemented #endif mp_int_t val; byte *in = (byte*)&val; switch (val_type) { case 'O': in = (byte*)&val_in; break; default: val = mp_obj_get_int(val_in); } mp_binary_set_int(MIN(size, sizeof(val)), struct_type == '>', p, in); }