Skip to content

Commit

Permalink
add tests skeleton for idiv cases
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhea committed Dec 31, 2024
1 parent 3d67724 commit a04ede6
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 5 deletions.
13 changes: 10 additions & 3 deletions flipjump/stl/hex/div.fj
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,20 @@ ns hex {
// Space Complexity: n(8.5@+132) + nb(21.5@+309) so if nb==n: n(30@+441)
// if b==0: goto div0
// q = a/b (signed division)
// r = a%b (signed modulo - sign(r)==sign(b))
// @NOTE: a,b are SIGNED numbers. If you want a division with signed ints, use the idiv macro.
// r = a%b (signed modulo - signed according to rem_opt:)
// if rem_opt is 0: sign(r)==sign(b) // Most programming languages implement this option.
// if rem_opt is 1: sign(r)==sign(a)
// if rem_opt is 0: sign(r) is always positive
// Anyway, a == q*b + r.
// @NOTE: a,b are SIGNED numbers. If you want a division with unsigned ints, use the div macro.
//
//
// q,a are hex[:n], while r,b are hex[:nb]. div0 is the bit-address this function will jump to in-case b is zero.
// @requires hex.sub.init & hex.cmp.init (or hex.init)
def idiv n, nb, q, r, a, b, div0 @ set_negative_a, test_b, set_negative_b,\
def idiv n, nb, q, r, a, b, div0, rem_opt @ set_negative_a, test_b, set_negative_b,\
negative_a, negative_b, one_negative, do_div, neg_b_2, neg_ans, end {
stl.comp_if1 rem_opt == 444, end // TODO remove when the 3 rem_opts are implemented

.if0 nb, b, div0

bit.zero negative_a
Expand Down
4 changes: 2 additions & 2 deletions programs/hexlib_tests/div/test_idiv.fj
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ stl.loop


idiv4_2:
hex.idiv 4, 2, res, mod, ah, bh, div0
hex.idiv 4, 2, res, mod, ah, bh, div0, 0
stl.fret ret


idiv4_4:
hex.idiv 4, 4, res, mod, ah, bh, div0
hex.idiv 4, 4, res, mod, ah, bh, div0, 0
stl.fret ret
56 changes: 56 additions & 0 deletions programs/hexlib_tests/div/test_idiv_cases.fj
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
test_idiv_opt 20, 12, 1, 8, idiv_opt0

test_idiv_opt 20, 12, 1, 8, idiv_opt1

test_idiv_opt 20, 12, 1, 8, idiv_opt2


stl.loop


idiv_opt0:
hex.idiv 4, 4, res, mod, ah, bh, div0, 0
stl.fret ret

idiv_opt1:
hex.idiv 4, 4, res, mod, ah, bh, div0, 1
stl.fret ret

idiv_opt2:
hex.idiv 4, 4, res, mod, ah, bh, div0, 2
stl.fret ret


def test_idiv_opt a, b, expected_res, expected_mod, idiv_label\
@ res_bad, res_good, mod_bad, mod_good, check_mod, expected_res_var, expected_mod_var, end\
< ah, bh, ret, res, mod {
hex.set 4, ah, a
hex.set 4, bh, b
stl.fcall idiv_label, ret

hex.cmp 4, res, expected_res_var, res_bad, res_good, res_bad
res_good:
stl.output '='
;check_mod
res_bad:
stl.output '!'
;check_mod

check_mod:
hex.cmp 4, mod, expected_mod_var, mod_bad, mod_good, mod_bad

mod_good:
stl.output '='
;end
mod_bad:
stl.output '!'
;end

expected_res_var:
hex.vec 4, expected_res
expected_mod_var:
hex.vec 4, expected_mod

end:
stl.output '\n'
}
3 changes: 3 additions & 0 deletions tests/inout/hexlib_tests/div/test_idiv_cases.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
==
==
==
1 change: 1 addition & 0 deletions tests/tests_tables/test_compile_hexlib.csv
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ hexlib-div-8_2, programs/hexlib_tests/div/hexlib_div.fj | programs/hexlib_tests/
hexlib-div-8_4, programs/hexlib_tests/div/hexlib_div.fj | programs/hexlib_tests/div/test8_4.fj, tests/compiled/hexlib_tests/div/test8_4.fjm, 64,3,0, True,True
hexlib-div-8_8, programs/hexlib_tests/div/hexlib_div.fj | programs/hexlib_tests/div/test8_8.fj, tests/compiled/hexlib_tests/div/test8_8.fjm, 64,3,0, True,True
hexlib-idiv, programs/hexlib_tests/div/hexlib_div.fj | programs/hexlib_tests/div/test_idiv.fj, tests/compiled/hexlib_tests/div/test_idiv.fjm, 64,3,0, True,True
hexlib-idiv-cases, programs/hexlib_tests/div/hexlib_div.fj | programs/hexlib_tests/div/test_idiv_cases.fj, tests/compiled/hexlib_tests/div/test_idiv_cases.fjm, 64,3,0, True,True
1 change: 1 addition & 0 deletions tests/tests_tables/test_run_hexlib.csv
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ hexlib-div-8_2, tests/compiled/hexlib_tests/div/test8_2.fjm, ,tests/inout/hexlib
hexlib-div-8_4, tests/compiled/hexlib_tests/div/test8_4.fjm, ,tests/inout/hexlib_tests/div/test8_4.out, False,False
hexlib-div-8_8, tests/compiled/hexlib_tests/div/test8_8.fjm, ,tests/inout/hexlib_tests/div/test8_8.out, False,False
hexlib-idiv, tests/compiled/hexlib_tests/div/test_idiv.fjm, ,tests/inout/hexlib_tests/div/test_idiv.out, False,False
hexlib-idiv-cases, tests/compiled/hexlib_tests/div/test_idiv_cases.fjm, ,tests/inout/hexlib_tests/div/test_idiv_cases.out, False,False

0 comments on commit a04ede6

Please sign in to comment.