|
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;
|
@@ -387,6 +390,7 @@ mp_obj_t mp_obj_int_binary_op_extra_cases(mp_binary_op_t op, mp_obj_t lhs_in, mp
|
387 | 390 | return MP_OBJ_NULL; // op not supported
|
388 | 391 | }
|
389 | 392 |
|
| 393 | +/* |
390 | 394 | // this is a classmethod
|
391 | 395 | static mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) {
|
392 | 396 | // TODO: Support signed param (assumes signed=False at the moment)
|
@@ -416,6 +420,102 @@ static mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) {
|
416 | 420 | }
|
417 | 421 | return mp_obj_new_int_from_uint(value);
|
418 | 422 | }
|
| 423 | +*/ |
| 424 | + |
| 425 | +int calc_size(mp_longint_impl_t x) { |
| 426 | + int size = 0; |
| 427 | + if (x < 0) { |
| 428 | + x = -x; |
| 429 | + } |
| 430 | + do { |
| 431 | + x >>= 8; |
| 432 | + size++; |
| 433 | + } while (x); |
| 434 | + return size; |
| 435 | +} |
| 436 | + |
| 437 | +void *reverce_memcpy(void *dest, const void *src, size_t len) { |
| 438 | + char *d = (char *)dest + len - 1; |
| 439 | + const char *s = src; |
| 440 | + while (len--) { |
| 441 | + *d-- = *s++; |
| 442 | + } |
| 443 | + return dest; |
| 444 | +} |
| 445 | + |
| 446 | +// this is a classmethod |
| 447 | +// result = int.int_from_bytes(bytearray(), order='big', signed=False) |
| 448 | +static mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) { |
| 449 | + mp_buffer_info_t bufinfo; |
| 450 | + mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ); |
| 451 | + bool big_endian = (n_args < 3) || (args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little)); |
| 452 | + bool signd = (n_args > 3) && mp_obj_is_true(args[3]); |
| 453 | + |
| 454 | + // debug_printf("mp_obj_is_bool(args[3])=%d", mp_obj_is_bool(args[3])); |
| 455 | + // debug_printf("n_args=%d, MP_SMALL_INT_MAX=%d, MP_SMALL_INT_MIN=%d", n_args, MP_SMALL_INT_MAX, MP_SMALL_INT_MIN); |
| 456 | + #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE |
| 457 | + if ((1ULL << (bufinfo.len * 8 - 2)) > (MP_SMALL_INT_MAX + 1)) { |
| 458 | + debug_printf("//1 Result will overflow a small-int so construct a big-int"); |
| 459 | + debug_printf("big_endian:%d signed:%d bufinfo.len:%d", big_endian, signd, bufinfo.len); |
| 460 | + return mp_obj_int_from_bytes_impl(big_endian, signd, bufinfo.len, bufinfo.buf); |
| 461 | + } |
| 462 | + #endif |
| 463 | + |
| 464 | + union { |
| 465 | + mp_int_t ival; |
| 466 | + mp_uint_t uval; |
| 467 | + byte buf[sizeof(mp_uint_t)]; |
| 468 | + } result = {0}; |
| 469 | + |
| 470 | + if (bufinfo.len > sizeof(result)) { |
| 471 | + mp_raise_msg(&mp_type_OverflowError, MP_ERROR_TEXT("small-int too large")); |
| 472 | + bufinfo.len = sizeof(result); |
| 473 | + } |
| 474 | + |
| 475 | + if (big_endian) { |
| 476 | + reverce_memcpy(&result.uval, bufinfo.buf, bufinfo.len); |
| 477 | + } else { // little endian |
| 478 | + memcpy(&result.uval, bufinfo.buf, bufinfo.len); |
| 479 | + } |
| 480 | + if (signd) { |
| 481 | + if ((sizeof(result) > bufinfo.len) && (result.buf[bufinfo.len - 1] & 0x80)) { |
| 482 | + // Sign propagation |
| 483 | + // x = 2 |
| 484 | + // x.to_bytes(1, 'little', True) -> b'\x02' |
| 485 | + // x.to_bytes(4, 'little', True) -> b'\x02\x00\x00\x00' |
| 486 | + // x = -2 |
| 487 | + // x.to_bytes(1, 'little', True) -> b'\xFE' |
| 488 | + // x.to_bytes(4, 'little', True) -> b'\xFE\xFF\xFF\xFF' |
| 489 | + debug_printf("result=0x%08X", result.uval); |
| 490 | + for (int i = 0; i < sizeof(result); i++) { |
| 491 | + _debug_printf("\\%02X", result.buf[i]); |
| 492 | + } |
| 493 | + memset(result.buf + bufinfo.len, 0xFF, sizeof(result) - bufinfo.len); |
| 494 | + debug_printf("\nresult=0x%08X", result.uval); |
| 495 | + for (int i = 0; i < sizeof(result); i++) { |
| 496 | + _debug_printf("\\%02X", result.buf[i]); |
| 497 | + } |
| 498 | + debug_printf(""); |
| 499 | + } |
| 500 | + #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE |
| 501 | + // debug_printf("big_endian:%d signed:%d bufinfo.len:%d sizeof(result.ival):%d result.ival:%ld", big_endian, signd, bufinfo.len, sizeof(result.ival), result.ival); |
| 502 | + if (!MP_SMALL_INT_FITS(result.ival)) { |
| 503 | + debug_printf("//2 Result will overflow a small-int so construct a big-int"); |
| 504 | + return mp_obj_int_from_bytes_impl(big_endian, signd, bufinfo.len, bufinfo.buf); |
| 505 | + } |
| 506 | + #endif |
| 507 | + return mp_obj_new_int(result.ival); |
| 508 | + } else { |
| 509 | + #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE |
| 510 | + // debug_printf("big_endian:%d signed:%d bufinfo.len:%d sizeof(result.uval):%d result.uval:%ld", big_endian, signd, bufinfo.len, sizeof(result.uval), result.uval); |
| 511 | + if (!MP_SMALL_UINT_FITS(result.uval)) { |
| 512 | + debug_printf("//3 Result will overflow a small-int so construct a big-int"); |
| 513 | + return mp_obj_int_from_bytes_impl(big_endian, signd, bufinfo.len, bufinfo.buf); |
| 514 | + } |
| 515 | + #endif |
| 516 | + return mp_obj_new_int_from_uint(result.uval); |
| 517 | + } |
| 518 | +} |
419 | 519 |
|
420 | 520 | static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_fun_obj, 2, 4, int_from_bytes);
|
421 | 521 | static MP_DEFINE_CONST_CLASSMETHOD_OBJ(int_from_bytes_obj, MP_ROM_PTR(&int_from_bytes_fun_obj));
|
|
0 commit comments