From 6c0fab81ff8f559a10e648b2ae7f4d8ab76d3847 Mon Sep 17 00:00:00 2001 From: Tom Herman Date: Mon, 9 Dec 2024 19:03:30 +0200 Subject: [PATCH] support bit arithmetic shift right --- flipjump/stl/bit/shifts.fj | 8 ++++++ programs/simple_math_checks/shra.fj | 29 +++++++++++++++++++++ tests/inout/simple_math_checks/25equals.out | 25 ++++++++++++++++++ tests/tests_tables/test_compile_slow.csv | 2 ++ tests/tests_tables/test_run_slow.csv | 2 ++ 5 files changed, 66 insertions(+) create mode 100644 programs/simple_math_checks/shra.fj create mode 100644 tests/inout/simple_math_checks/25equals.out diff --git a/flipjump/stl/bit/shifts.fj b/flipjump/stl/bit/shifts.fj index 5bd3c63..5c4270f 100644 --- a/flipjump/stl/bit/shifts.fj +++ b/flipjump/stl/bit/shifts.fj @@ -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 diff --git a/programs/simple_math_checks/shra.fj b/programs/simple_math_checks/shra.fj new file mode 100644 index 0000000..4a50922 --- /dev/null +++ b/programs/simple_math_checks/shra.fj @@ -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<>shift)&((1<