@@ -457,6 +457,28 @@ mp_obj_t mp_obj_int_binary_op_extra_cases(mp_binary_op_t op, mp_obj_t lhs_in, mp
457
457
return MP_OBJ_NULL ; // op not supported
458
458
}
459
459
460
+ #if MICROPY_CPYTHON_COMPAT
461
+ STATIC mp_obj_t int_bit_length (mp_obj_t self_in ) {
462
+ #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
463
+ if (!MP_OBJ_IS_SMALL_INT (self_in )) {
464
+ return mp_obj_int_bit_length_impl (self_in );
465
+ }
466
+ else
467
+ #endif
468
+ {
469
+ mp_int_t int_val = MP_OBJ_SMALL_INT_VALUE (self_in );
470
+ mp_uint_t value =
471
+ (int_val == 0 ) ? 0 :
472
+ (int_val == MP_SMALL_INT_MIN ) ? 8 * sizeof (mp_int_t ) :
473
+ (int_val < 0 ) ? 8 * sizeof (long ) - __builtin_clzl (- int_val ) :
474
+ 8 * sizeof (long ) - __builtin_clzl (int_val );
475
+ return mp_obj_new_int_from_uint (value );
476
+ }
477
+
478
+ }
479
+ MP_DEFINE_CONST_FUN_OBJ_1 (int_bit_length_obj , int_bit_length );
480
+ #endif
481
+
460
482
// this is a classmethod
461
483
STATIC mp_obj_t int_from_bytes (size_t n_args , const mp_obj_t * args ) {
462
484
// TODO: Support signed param (assumes signed=False at the moment)
@@ -537,6 +559,9 @@ STATIC mp_obj_t int_to_bytes(size_t n_args, const mp_obj_t *pos_args, mp_map_t *
537
559
STATIC MP_DEFINE_CONST_FUN_OBJ_KW (int_to_bytes_obj , 3 , int_to_bytes );
538
560
539
561
STATIC const mp_rom_map_elem_t int_locals_dict_table [] = {
562
+ #if MICROPY_CPYTHON_COMPAT
563
+ { MP_ROM_QSTR (MP_QSTR_bit_length ), MP_ROM_PTR (& int_bit_length_obj ) },
564
+ #endif
540
565
{ MP_ROM_QSTR (MP_QSTR_from_bytes ), MP_ROM_PTR (& int_from_bytes_obj ) },
541
566
{ MP_ROM_QSTR (MP_QSTR_to_bytes ), MP_ROM_PTR (& int_to_bytes_obj ) },
542
567
};
0 commit comments