Skip to content

Commit

Permalink
Berry bit-shift operators to 'int64' (#22709)
Browse files Browse the repository at this point in the history
  • Loading branch information
s-hadinger authored Dec 24, 2024
1 parent fc3ad45 commit 2024bf8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 18 additions & 0 deletions lib/libesp32/berry_int64/src/be_int64_class.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
46 changes: 46 additions & 0 deletions lib/libesp32/berry_int64/tests/int64.be
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 2024bf8

Please sign in to comment.