1dnl  RISC-V/64 mpn_add_n and mpn_sub_n.
2
3dnl  Copyright 2016 Free Software Foundation, Inc.
4
5dnl  This file is part of the GNU MP Library.
6dnl
7dnl  The GNU MP Library is free software; you can redistribute it and/or modify
8dnl  it under the terms of either:
9dnl
10dnl    * the GNU Lesser General Public License as published by the Free
11dnl      Software Foundation; either version 3 of the License, or (at your
12dnl      option) any later version.
13dnl
14dnl  or
15dnl
16dnl    * the GNU General Public License as published by the Free Software
17dnl      Foundation; either version 2 of the License, or (at your option) any
18dnl      later version.
19dnl
20dnl  or both in parallel, as here.
21dnl
22dnl  The GNU MP Library is distributed in the hope that it will be useful, but
23dnl  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24dnl  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25dnl  for more details.
26dnl
27dnl  You should have received copies of the GNU General Public License and the
28dnl  GNU Lesser General Public License along with the GNU MP Library.  If not,
29dnl  see https://www.gnu.org/licenses/.
30
31include(`../config.m4')
32
33C  INPUT PARAMETERS
34define(`rp',	`a0')
35define(`up',	`a1')
36define(`vp',	`a2')
37define(`n',	`a3')
38
39ifdef(`OPERATION_add_n',`
40    define(`ADDSUB',	`add')
41    define(`CMPCY',	`sltu	$1, $2, $3')
42    define(`func',	`mpn_add_n')
43')
44ifdef(`OPERATION_sub_n',`
45    define(`ADDSUB',	`sub')
46    define(`CMPCY',	`sltu	$1, $3, $2')
47    define(`func',	`mpn_sub_n')
48')
49
50MULFUNC_PROLOGUE(mpn_add_n mpn_sub_n)
51
52ASM_START()
53PROLOGUE(func)
54	li	t6, 0
55
56	andi	t0, n, 1
57	beq	t0, x0, L(top)
58	addi	up, up, 8
59	addi	vp, vp, -8
60	addi	rp, rp, -8
61	addi	n, n, -1
62	j	L(mid)
63
64L(top):	ld	a4, 0(up)
65	ld	a6, 0(vp)
66	addi	n, n, -2	C bookkeeping
67	addi	up, up, 16	C bookkeeping
68	ADDSUB	t0, a4, a6
69	CMPCY(	t2, t0, a4)
70	ADDSUB	t4, t0, t6	C cycle 3, 9, ...
71	CMPCY(	t3, t4, t0)	C cycle 4, 10, ...
72	sd	t4, 0(rp)
73	add	t6, t2, t3	C cycle 5, 11, ...
74L(mid):	ld	a5, -8(up)
75	ld	a7, 8(vp)
76	addi	vp, vp, 16	C bookkeeping
77	addi	rp, rp, 16	C bookkeeping
78	ADDSUB	t1, a5, a7
79	CMPCY(	t2, t1, a5)
80	ADDSUB	t4, t1, t6	C cycle 0, 6, ...
81	CMPCY(	t3, t4, t1)	C cycle 1, 7, ...
82	sd	t4, -8(rp)
83	add	t6, t2, t3	C cycle 2, 8, ...
84	bne	n, x0, L(top)	C bookkeeping
85
86L(end):	mv	a0, t6
87	ret
88EPILOGUE()
89ASM_END()
90