math.unsigned: fix lsh() for Uint128, add tests (#24840)

This commit is contained in:
Mike 2025-07-03 12:44:31 +03:00 committed by GitHub
parent 7e35d40661
commit 2a5398aff6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 1 deletions

View file

@ -300,7 +300,16 @@ pub fn (u Uint128) quo_rem(v Uint128) (Uint128, Uint128) {
// lsh returns u << n
pub fn (u Uint128) lsh(n u32) Uint128 {
mut s := Uint128{}
if n > 64 {
if n == 0 {
s.lo = u.lo
s.hi = u.hi
} else if n >= 128 {
s.lo = 0
s.hi = 0
} else if n == 64 {
s.lo = 0
s.hi = u.lo
} else if n > 64 {
s.lo = 0
s.hi = u.lo << (n - 64)
} else {

View file

@ -174,3 +174,12 @@ fn test_separators() {
fn test_new() {
assert unsigned.uint128_new(max_u64, max_u64) == unsigned.uint128_max
}
fn test_lsh() {
a := unsigned.uint128_from_dec_str('123456789012345678901234567890')!
assert a.str() == a.lsh(0).str()
assert '246913578024691357802469135780' == a.lsh(1).str()
assert '259801135457060040952792416454273138688' == a.lsh(64).str()
assert '302984417681386893975453667670529933312' == a.lsh(100).str()
assert unsigned.uint128_zero == a.lsh(200)
}