xref: /plan9/sys/src/libmp/power/mpvecadd.s (revision 7dd7cddf99dd7472612f1413b4da293630e6b1bc)
1#define	BDNZ	BC	16,0,
2#define	BDNE	BC	0,2,
3
4/*
5 *	mpvecadd(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *sum)
6 *
7 *		sum[0:alen] = a[0:alen-1] + b[0:blen-1]
8 *
9 *	prereq: alen >= blen, sum has room for alen+1 digits
10 *
11 *		R3 == a	(first arg passed in R3)
12 *		R4 == alen
13 *		R5 == b
14 *		R6 == blen
15 *		R7 == sum
16 *		R8 == temporary
17 *		R9 == temporary
18 */
19TEXT	mpvecadd(SB),$-4
20
21	MOVW	alen+4(FP), R4
22	MOVW	b+8(FP), R5
23	MOVW	blen+12(FP), R6
24	MOVW	sum+16(FP), R7
25	SUB	R6, R4		/* calculate counter for second loop (alen > blen) */
26	SUB	$4, R3		/* pre decrement for MOVWU's */
27	SUB	$4, R5		/* pre decrement for MOVWU's */
28	SUB	$4, R7		/* pre decrement for MOVWU's */
29	MOVW	R0, XER		/* zero carry going in */
30
31	/* if blen == 0, don't need to add it in */
32	CMP	R0, R6
33	BEQ	_add1
34
35	/* sum[0:blen-1],carry = a[0:blen-1] + b[0:blen-1] */
36	MOVW	R6, CTR
37_addloop1:
38	MOVWU	4(R3), R8
39	MOVWU	4(R5), R9
40	ADDE	R8, R9
41	MOVWU	R9, 4(R7)
42	BDNZ	_addloop1
43
44_add1:
45	/* if alen == blen, we're done */
46	CMP	R0, R4
47	BEQ	_addend
48
49	/* sum[blen:alen-1],carry = a[blen:alen-1] + 0 + carry */
50	MOVW	R4, CTR
51_addloop2:
52	MOVWU	4(R3), R8
53	ADDE	R0, R8
54	MOVWU	R8, 4(R7)
55	BDNZ	_addloop2
56
57	/* sum[alen] = carry */
58_addend:
59	ADDE	R0, R0, R8
60	MOVW	R8, 4(R7)
61	RETURN
62