|
39 | 39 | #include <math.h>
|
40 | 40 | #endif
|
41 | 41 |
|
| 42 | +#define debug_printf(...) // mp_printf(&mp_plat_print, __VA_ARGS__); mp_printf(&mp_plat_print, "\n"); // mp_printf(&mp_plat_print, " | func:%s line:%d at %s\n", __FUNCTION__, __LINE__, __FILE__); |
| 43 | +#define _debug_printf(...) // mp_printf(&mp_plat_print, __VA_ARGS__); |
| 44 | + |
42 | 45 | // This dispatcher function is expected to be independent of the implementation of long int
|
43 | 46 | static mp_obj_t mp_obj_int_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
44 | 47 | (void)type_in;
|
@@ -386,7 +389,7 @@ mp_obj_t mp_obj_int_binary_op_extra_cases(mp_binary_op_t op, mp_obj_t lhs_in, mp
|
386 | 389 | }
|
387 | 390 | return MP_OBJ_NULL; // op not supported
|
388 | 391 | }
|
389 |
| - |
| 392 | +/* |
390 | 393 | // this is a classmethod
|
391 | 394 | static mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) {
|
392 | 395 | // TODO: Support signed param (assumes signed=False at the moment)
|
@@ -416,6 +419,88 @@ static mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) {
|
416 | 419 | }
|
417 | 420 | return mp_obj_new_int_from_uint(value);
|
418 | 421 | }
|
| 422 | +*/ |
| 423 | + |
| 424 | +void *reverce_memcpy(void *dest, const void *src, size_t len) { |
| 425 | + char *d = (char *)dest + len - 1; |
| 426 | + const char *s = src; |
| 427 | + while (len--) { |
| 428 | + *d-- = *s++; |
| 429 | + } |
| 430 | + return dest; |
| 431 | +} |
| 432 | + |
| 433 | +mp_obj_t mp_obj_integer_from_bytes_impl(bool big_endian, bool signd, size_t len, const byte *buf) { |
| 434 | + if (len > sizeof(mp_int_t)) { |
| 435 | + #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE |
| 436 | + // Result will overflow a small-int size so construct a big-int |
| 437 | + return mp_obj_int_from_bytes_impl(big_endian, signd, len, buf); |
| 438 | + #else |
| 439 | + mp_raise_msg(&mp_type_OverflowError, MP_ERROR_TEXT("small-int overflow")); |
| 440 | + #endif |
| 441 | + } |
| 442 | + union { |
| 443 | + mp_int_t value; |
| 444 | + mp_uint_t uvalue; |
| 445 | + byte buf[sizeof(mp_int_t)]; |
| 446 | + } result = {0}; |
| 447 | + // #if sizeof(mp_int_t) != sizeof(mp_uint_t) |
| 448 | + // #error "sizeof(mp_int_t) != sizeof(mp_uint_t)" |
| 449 | + // #endif |
| 450 | + |
| 451 | + if (big_endian) { |
| 452 | + reverce_memcpy(&result, buf, len); |
| 453 | + } else { // little-endian |
| 454 | + memcpy(&result, buf, len); |
| 455 | + } |
| 456 | + |
| 457 | + if ((signd) && (sizeof(result) > len) && (result.buf[len - 1] & 0x80)) { |
| 458 | + // Sign propagation in little-endian |
| 459 | + // x = 2 |
| 460 | + // x.to_bytes(1, 'little', True) -> b'\x02' |
| 461 | + // x.to_bytes(4, 'little', True) -> b'\x02\x00\x00\x00' |
| 462 | + // x = -2 |
| 463 | + // x.to_bytes(1, 'little', True) -> b'\xFE' |
| 464 | + // x.to_bytes(4, 'little', True) -> b'\xFE\xFF\xFF\xFF' |
| 465 | + _debug_printf(" 1result=0x%08X=", result.uvalue); |
| 466 | + for (unsigned int i = 0; i < sizeof(result); i++) { |
| 467 | + _debug_printf("\\%02X", result.buf[i]); |
| 468 | + } |
| 469 | + debug_printf(""); |
| 470 | + |
| 471 | + memset(result.buf + len, 0xFF, sizeof(result) - len); |
| 472 | + |
| 473 | + _debug_printf("\n 2result=0x%08X=", result.uvalue); |
| 474 | + for (unsigned int i = 0; i < sizeof(result); i++) { |
| 475 | + _debug_printf("\\%02X", result.buf[i]); |
| 476 | + } |
| 477 | + debug_printf(""); |
| 478 | + } |
| 479 | + // debug_printf("big_endian:%d signed:%d len:%d sizeof(result):%d result.value:%ld=0x%X", big_endian, signd, len, sizeof(result), result.value, result.value); |
| 480 | + debug_printf("MP_SMALL_INT_MAX=%d=0x%X, MP_SMALL_INT_MIN=%d=0x%X, MP_SMALL_INT_POSITIVE_MASK=%d=0x%X", MP_SMALL_INT_MAX, MP_SMALL_INT_MAX, MP_SMALL_INT_MIN, MP_SMALL_INT_MIN, MP_SMALL_INT_POSITIVE_MASK, MP_SMALL_INT_POSITIVE_MASK); |
| 481 | + // debug_printf("(MP_SMALL_INT_MAX << 1) + 1=%d=0x%X", (MP_SMALL_INT_MAX << 1) + 1, (MP_SMALL_INT_MAX << 1) + 1); |
| 482 | + if (((!signd) && (result.uvalue > MP_SMALL_INT_MAX)) || (signd && ((result.value < MP_SMALL_INT_MIN) || (result.value > MP_SMALL_INT_MAX)))) { |
| 483 | + // Result will overflow a small-int so construct a big-int |
| 484 | + #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE |
| 485 | + return mp_obj_int_from_bytes_impl(big_endian, signd, len, buf); |
| 486 | + #else |
| 487 | + mp_raise_msg(&mp_type_OverflowError, MP_ERROR_TEXT("small-int overflow")); |
| 488 | + #endif |
| 489 | + } |
| 490 | + return mp_obj_new_int(result.value); |
| 491 | +} |
| 492 | + |
| 493 | +// this is a classmethod |
| 494 | +// result = int.from_bytes(bytearray(), order='big', signed=False) |
| 495 | +static mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) { |
| 496 | + // get the buffer info |
| 497 | + mp_buffer_info_t bufinfo; |
| 498 | + mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ); |
| 499 | + bool big_endian = n_args < 3 || args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little); |
| 500 | + bool signd = (n_args > 3) && mp_obj_is_true(args[3]); |
| 501 | + |
| 502 | + return mp_obj_integer_from_bytes_impl(big_endian, signd, bufinfo.len, bufinfo.buf); |
| 503 | +} |
419 | 504 |
|
420 | 505 | static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_fun_obj, 2, 4, int_from_bytes);
|
421 | 506 | static MP_DEFINE_CONST_CLASSMETHOD_OBJ(int_from_bytes_obj, MP_ROM_PTR(&int_from_bytes_fun_obj));
|
|
0 commit comments