Skip to content

Commit

Permalink
Merge pull request #280 from tomhea/hotfix/small-improvements
Browse files Browse the repository at this point in the history
support bit arithmetic shift right
  • Loading branch information
tomhea authored Dec 9, 2024
2 parents 7cc021a + 6c0fab8 commit 53edb39
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
8 changes: 8 additions & 0 deletions flipjump/stl/bit/shifts.fj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ ns bit {
.zero times, x+(n-times)*dw
}

// Complexity: n(2@-1)
// x[:n] >>= times (arithmetic shift right)
// times is a constant.
def shra n, times, x {
rep(n-times, i) .mov x+i*dw, x+(i+times)*dw
rep(times-1, i) .mov x+(n-1-(i+1))*dw, x+(n-1)*dw
}


// Complexity: n(2@-1)
// x[:n] <<= 1
Expand Down
29 changes: 29 additions & 0 deletions programs/simple_math_checks/shra.fj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
stl.startup

SHRA_BIT_NUM = 0xe71e3e4a716549fb31fc565d0dbb2f96c846c32ee4018325d1cd59436959489e67e9c48d3c4861da188988ead4f9fefab18e0c51c22c7241405f456ff95023b9cec204245f9bc9285cdead281e5aef9458296a4036cdb03ffcece333cb5c2d05fd66ddf091b82de8e16bc08b648ab018a3f05a1d812f94223419680ea44e0b11121da44b5bdf7f55a1346ea0d69a244177005a266bfb233372c7e12a8b6f76a39c1364624820a19d395f224d19d91fb6f535c8d485420d4c6520065687092d730d5257944ce12a5e
rep(25, i) test_shra 32, (SHRA_BIT_NUM>>(37*i))&((1<<32)-1), (SHRA_BIT_NUM>>(37*i + 32))&((1<<5)-1)

stl.loop


hex.init

def test_shra n, x, shift @ lt, eq, gt, xh, ch, end {
bit.shra n, shift, xh
bit.cmp n, xh, ch, lt, eq, gt

lt:
stl.output '<'
;end
eq:
stl.output '='
;end
gt:
stl.output '>'
;end

xh: bit.vec n, x
ch: bit.vec n, x&(1<<(n-1)) ? ((x>>shift)&((1<<n)-1)) | (((1<<shift)-1) << (n-shift)) : (x>>shift)&((1<<n)-1)
end:
stl.output '\n'
}
25 changes: 25 additions & 0 deletions tests/inout/simple_math_checks/25equals.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
2 changes: 2 additions & 0 deletions tests/tests_tables/test_compile_slow.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
hex_func7, programs/func_tests/func7.fj,tests/compiled/func_tests/func7.fjm, 64,3,0, True,True

shra, programs/simple_math_checks/shra.fj,tests/compiled/simple_math_checks/shra.fjm, 64,3,0, True,True

calc, programs/calc.fj,tests/compiled/calc.fjm, 64,3,0, True,True

hex_func1, programs/func_tests/func1.fj,tests/compiled/func_tests/func1.fjm, 64,3,0, True,True
Expand Down
2 changes: 2 additions & 0 deletions tests/tests_tables/test_run_slow.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
hex_func7, tests/compiled/func_tests/func7.fjm, ,tests/inout/func_tests/func7.out, False,False

shra, tests/compiled/simple_math_checks/shra.fjm, ,tests/inout/simple_math_checks/25equals.out, False,False

calc1, tests/compiled/calc.fjm, tests/inout/calc_tests/calc1.in,tests/inout/calc_tests/calc1.out, False,False
calc2, tests/compiled/calc.fjm, tests/inout/calc_tests/calc2.in,tests/inout/calc_tests/calc2.out, False,False
calc3, tests/compiled/calc.fjm, tests/inout/calc_tests/calc3.in,tests/inout/calc_tests/calc3.out, False,False
Expand Down

0 comments on commit 53edb39

Please sign in to comment.