diff --git a/CHANGELOG.md b/CHANGELOG.md index dd120a1dabe2..5b2460298d5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file. - Berry `animate.crenel` primitive (#22673) - Berry scroll to Leds_matrix (#22693) - HASPmota support for `tabview` +- Berry bit-shift operators to `int64` ### Breaking Changed diff --git a/lib/libesp32/berry_int64/src/be_int64_class.c b/lib/libesp32/berry_int64/src/be_int64_class.c index 91a0a7f2f6b7..356c096df052 100644 --- a/lib/libesp32/berry_int64/src/be_int64_class.c +++ b/lib/libesp32/berry_int64/src/be_int64_class.c @@ -194,6 +194,22 @@ int64_t* int64_div(bvm *vm, int64_t *i64, int64_t *j64) { } BE_FUNC_CTYPE_DECLARE(int64_div, "int64", "@.(int64)") +int64_t* int64_shiftleft(bvm *vm, int64_t *i64, int32_t j32) { + int64_t* r64 = (int64_t*)be_malloc(vm, sizeof(int64_t)); + // it's possible that arg j64 is nullptr, since class type does allow NULLPTR to come through. + *r64 = *i64 << j32; + return r64; +} +BE_FUNC_CTYPE_DECLARE(int64_shiftleft, "int64", "@(int64)i") + +int64_t* int64_shiftright(bvm *vm, int64_t *i64, int32_t j32) { + int64_t* r64 = (int64_t*)be_malloc(vm, sizeof(int64_t)); + // it's possible that arg j64 is nullptr, since class type does allow NULLPTR to come through. + *r64 = *i64 >> j32; + return r64; +} +BE_FUNC_CTYPE_DECLARE(int64_shiftright, "int64", "@(int64)i") + bbool int64_equals(int64_t *i64, int64_t *j64) { // it's possible that arg j64 is nullptr, since class type does allow NULLPTR to come through. int64_t j = 0; @@ -349,6 +365,8 @@ class be_class_int64 (scope: global, name: int64) { >=, ctype_func(int64_gte) <, ctype_func(int64_lt) <=, ctype_func(int64_lte) + <<, ctype_func(int64_shiftleft) + >>, ctype_func(int64_shiftright) tobytes, ctype_func(int64_tobytes) frombytes, static_ctype_func(int64_frombytes) diff --git a/lib/libesp32/berry_int64/tests/int64.be b/lib/libesp32/berry_int64/tests/int64.be index 69514944502f..bcaa12cf4dfc 100644 --- a/lib/libesp32/berry_int64/tests/int64.be +++ b/lib/libesp32/berry_int64/tests/int64.be @@ -134,3 +134,49 @@ assert(int64.toint64(int64(42)).tostring() == "42") # invalid assert(int64.toint64("").tostring() == "0") assert(int64.toint64(nil) == nil) + +# bitshift +assert(str(int64(15) << 0) == "15") +assert(str(int64(15) << 1) == "30") +assert(str(int64(15) << 2) == "60") +assert(str(int64(15) << 20) == "15728640") +assert((int64(15) << 20).tobytes().reverse().tohex() == "0000000000F00000") +assert((int64(15) << 40).tobytes().reverse().tohex() == "00000F0000000000") +assert((int64(15) << 44).tobytes().reverse().tohex() == "0000F00000000000") +assert((int64(15) << 48).tobytes().reverse().tohex() == "000F000000000000") +assert((int64(15) << 52).tobytes().reverse().tohex() == "00F0000000000000") +assert((int64(15) << 56).tobytes().reverse().tohex() == "0F00000000000000") +assert((int64(15) << 60).tobytes().reverse().tohex() == "F000000000000000") +assert((int64(15) << 61).tobytes().reverse().tohex() == "E000000000000000") +assert((int64(15) << 62).tobytes().reverse().tohex() == "C000000000000000") +assert((int64(15) << 63).tobytes().reverse().tohex() == "8000000000000000") +assert((int64(15) << -1).tobytes().reverse().tohex() == "8000000000000000") + +assert(str(int64(-15) << 0) == "-15") +assert(str(int64(-15) << 1) == "-30") +assert(str(int64(-15) << 2) == "-60") +assert(str(int64(-15) << 20) == "-15728640") +assert((int64(-15) << 20).tobytes().reverse().tohex() == "FFFFFFFFFF100000") +assert((int64(-15) << 40).tobytes().reverse().tohex() == "FFFFF10000000000") +assert((int64(-15) << 56).tobytes().reverse().tohex() == "F100000000000000") +assert((int64(-15) << 60).tobytes().reverse().tohex() == "1000000000000000") +assert((int64(-15) << 61).tobytes().reverse().tohex() == "2000000000000000") +assert((int64(-15) << 62).tobytes().reverse().tohex() == "4000000000000000") +assert((int64(-15) << 63).tobytes().reverse().tohex() == "8000000000000000") +assert((int64(-15) << -1).tobytes().reverse().tohex() == "8000000000000000") + +assert(str(int64(15) >> 0) == "15") +assert(str(int64(15) >> 1) == "7") +assert(str(int64(15) >> 2) == "3") +assert(str(int64(15) >> 3) == "1") +assert(str(int64(15) >> 4) == "0") +assert(str(int64(15) >> 5) == "0") +assert(str(int64(15) >> -1) == "0") + +assert(str(int64(-15) >> 0) == "-15") +assert(str(int64(-15) >> 1) == "-8") +assert(str(int64(-15) >> 2) == "-4") +assert(str(int64(-15) >> 3) == "-2") +assert(str(int64(-15) >> 4) == "-1") +assert(str(int64(-15) >> 5) == "-1") +assert(str(int64(-15) >> -1) == "-1")