1.data	128
2small_ops:
3.i	40 64 80
4large_ops:
5.i	98304 65536 163840
6fmt:
7.c	"%i/%i = %i (expected %i)\n"
8x:
9.c	"%d\n"
10.code
11	jmpi main
12
13#define generate_divider(operand)	\
14	name divider_##operand		\
15divider_##operand:			\
16	prolog				\
17	arg $i				\
18	getarg %r1 $i			\
19	divi %r2 %r1 operand		\
20	retr %r2			\
21	epilog
22generate_divider(8)
23generate_divider(32768)
24
25#define generate_test_divider(divisor)	\
26	name test_divider_##divisor	\
27test_divider_##divisor:			\
28	prolog				\
29	allocai 4 $loc			\
30	arg $p				\
31	arg $c				\
32	getarg %v0 $p			\
33	getarg %v1 $c			\
34	muli %v1 %v1 4			\
35	addr %v1 %v0 %v1		\
36loop_##divisor:				\
37	bger done_##divisor %v0 %v1	\
38	ldr_i %v2 %v0			\
39	prepare				\
40		pushargr %v2		\
41	finishi divider_##divisor	\
42	retval %v2			\
43	ldr_i %r2 %v0			\
44	divi %r0 %r2 divisor		\
45	/* save div result */		\
46	stxi_i $loc %fp %r0		\
47	prepare				\
48		pushargi fmt		\
49		ellipsis		\
50		pushargr %r2		\
51		pushargi divisor	\
52		pushargr %v2		\
53		pushargr %r0		\
54	finishi @printf			\
55	addi %v0 %v0 4			\
56	/* reload div result */		\
57	ldxi_i %r0 %fp $loc		\
58	beqr loop_##divisor %r0 %v2	\
59	/* return if failed */		\
60	reti 1				\
61done_##divisor:				\
62	reti 0				\
63	epilog
64generate_test_divider(8)
65generate_test_divider(32768)
66
67	name main
68main:
69	prolog
70	prepare
71		pushargi small_ops
72		pushargi 3
73	finishi test_divider_8
74	retval %r0
75	bnei fail %r0 0
76	prepare
77		pushargi large_ops
78		pushargi 3
79	finishi test_divider_32768
80	retval %r0
81	bnei fail %r0 0
82	reti 0
83fail:
84	reti 1
85	epilog
86