 # Sw_64 mpn_addmul_1 -- Multiply a limb vector with a limb and add
 # the result to a second limb vector.
 #
 #  Copyright (C) 2000-2023 Free Software Foundation, Inc.
 #
 #  This file is part of the GNU MP Library.
 #
 #  The GNU MP Library is free software; you can redistribute it and/or modify
 #  it under the terms of the GNU Lesser General Public License as published
 #  by the Free Software Foundation; either version 2.1 of the License, or (at
 #  your option) any later version.
 #
 #  The GNU MP Library is distributed in the hope that it will be useful, but
 #  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 #  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 #  License for more details.
 #
 #  You should have received a copy of the GNU Lesser General Public License
 #  along with the GNU MP Library.  If not, see <http://www.gnu.org/licenses/>.

 #  INPUT PARAMETERS
 #  res_ptr	$16
 #  s1_ptr	$17
 #  size	$18
 #  s2_limb	$19
 #
 #
 # This code was written in close cooperation with  pipeline expert
 # .  Any errors are tege's fault, though.
 #
 #   Register usages for unrolled loop:
 #	  0-3     mul's
 #	  4-7     acc's
 #	  8-15    mul results
 #	  20,21   carry's
 #	  22,23   save for stores
 #
 #   Sustains 8 mul-fadds in 29 cycles in the unrolled inner loop.
 #
 #   The stores can issue a cycle late so we have paired no-op's to 'catch'
 #   them, so that further disturbance to the schedule is damped.
 #
 #   We couldn't pair the loads, because the entangled schedule of the
 #   carry's has to happen on one side {0} of the machine.  Note, the total
 #   use of U0, and the total use of L0 (after attending to the stores).
 #   which is part of the reason why....
 #
 #   This is a great schedule for the d_cache, a poor schedule for the
 #   b_cache.  The lockup on U0 means that any stall can't be recovered
 #   from.  Consider a ldl in L1.  say that load gets stalled because it
 #   collides with a fill from the b_Cache.  On the next cycle, this load
 #   gets priority.  If first looks at L0, and goes there.  The instruction
 #   we intended for L0 gets to look at L1, which is NOT where we want
 #   it.  It either stalls 1, because it can't go in L0, or goes there, and
 #   causes a further instruction to stall.
 #
 #   So for b_cache, we're likely going to want to put one or more cycles
 #   back into the code! And, of course, put in prefetches.  For the
 #   accumulator, flds, intent to modify.  For the fmuldiplier, you might
 #   want ldl, evict next, if you're not wanting to use it again soon.  Use
 #   256 ahead of present pointer value.  At a place where we have an mt
 #   followed by a bookkeeping, put the bookkeeping in upper, and the
 #   prefetch into lower.
 #
 #   Note, the usage of physical registers per cycle is smoothed off, as
 #   much as possible.
 #
 #   Note, the ldl's and stl's are at the end of the quadpacks.  note, we'd
 #   like not to have a ldl or stl to preceded a conditional branch in a
 #   quadpack.  The conditional branch moves the retire pointer one cycle
 #   later.
 #
 #   Optimization notes:
 #   Callee-saves regs: $9 $10 $11 $12 $13 $14 $15 $26 ?$27?
 #   Reserved regs:	 $29 $30 $31
 #   Free caller-saves regs in unrolled code: $24 $25 $28
 #   We should swap some of the callee-saves regs for some of the free
 #   caller-saves regs, saving some overhead cycles.
 #   Most importantly, we should write fast code for the 0-7 case.
 #   The code we use there are for the 21164, and runs at 7 cycles/limb
 #   on the 21264.  Should not be hard, if we write specialized code for
 #   1-7 limbs (the one for 0 limbs should be straightforward).  We then just
 #   need a jump table indexed by the low 3 bits of the count argument.

	.set	noreorder
	.set	noat
	.text

	.globl	__mpn_addmul_1
	.ent	__mpn_addmul_1
__mpn_addmul_1:
	.frame	$30,0,$26,0
	.prologue 0

	cmpult	$18,	8,	$1
	beq	$1,	$Large

	ldl	$2,	0($17)		# $2 = s1_limb
	addl	$17,	8,	$17	# s1_ptr++
	subl	$18,	1,	$18	# size--
	mull	$2,	$19,	$3	# $3 = prod_low
	ldl	$5,	0($16)		# $5 = *res_ptr
	umulh	$2,	$19,	$0	# $0 = prod_high
	beq	$18,	$Lend0b		# jump if size was == 1
	ldl	$2,	0($17)		# $2 = s1_limb
	addl	$17,	8,	$17	# s1_ptr++
	subl	$18,	1,	$18	# size--
	addl	$5,	$3,	$3
	cmpult	$3,	$5,	$4
	stl	$3,	0($16)
	addl	$16,	8,	$16	# res_ptr++
	beq	$18,	$Lend0a		# jump if size was == 2

	.align 3
$Loop0:	mull	$2,	$19,	$3	# $3 = prod_low
	ldl	$5,	0($16)		# $5 = *res_ptr
	addl	$4,	$0,	$0	# cy_limb = cy_limb + 'cy'
	subl	$18,	1,	$18	# size--
	umulh	$2,	$19,	$4	# $4 = cy_limb
	ldl	$2,	0($17)		# $2 = s1_limb
	addl	$17,	8,	$17	# s1_ptr++
	addl	$3,	$0,	$3	# $3 = cy_limb + prod_low
	cmpult	$3,	$0,	$0	# $0 = carry from (cy_limb + prod_low)
	addl	$5,	$3,	$3
	cmpult	$3,	$5,	$5
	stl	$3,	0($16)
	addl	$16,	8,	$16	# res_ptr++
	addl	$5,	$0,	$0	# combine carries
	bne	$18,	$Loop0
$Lend0a:
	mull	$2,	$19,	$3	# $3 = prod_low
	ldl	$5,	0($16)		# $5 = *res_ptr
	addl	$4,	$0,	$0	# cy_limb = cy_limb + 'cy'
	umulh	$2,	$19,	$4	# $4 = cy_limb
	addl	$3,	$0,	$3	# $3 = cy_limb + prod_low
	cmpult	$3,	$0,	$0	# $0 = carry from (cy_limb + prod_low)
	addl	$5,	$3,	$3
	cmpult	$3,	$5,	$5
	stl	$3,	0($16)
	addl	$5,	$0,	$0	# combine carries
	addl	$4,	$0,	$0	# cy_limb = prod_high + cy
	ret	$31,	($26),	1
$Lend0b:
	addl	$5,	$3,	$3
	cmpult	$3,	$5,	$5
	stl	$3,	0($16)
	addl	$0,	$5,	$0
	ret	$31,	($26),	1

$Large:
	ldi	$30,	-240($30)
	stl	$9,	8($30)
	stl	$10,	16($30)
	stl	$11,	24($30)
	stl	$12,	32($30)
	stl	$13,	40($30)
	stl	$14,	48($30)
	stl	$15,	56($30)

	and	$18,	7,	$20	# count for the first loop, 0-7
	srl	$18,	3,	$18	# count for unrolled loop
	bis	$31,	$31,	$0
	beq	$20,	$Lunroll
	ldl	$2,	0($17)		# $2 = s1_limb
	addl	$17,	8,	$17	# s1_ptr++
	subl	$20,	1,	$20	# size--
	mull	$2,	$19,	$3	# $3 = prod_low
	ldl	$5,	0($16)		# $5 = *res_ptr
	umulh	$2,	$19,	$0	# $0 = prod_high
	beq	$20,	$Lend1b		# jump if size was == 1
	ldl	$2,	0($17)		# $2 = s1_limb
	addl	$17,	8,	$17	# s1_ptr++
	subl	$20,	1,	$20	# size--
	addl	$5,	$3,	$3
	cmpult	$3,	$5,	$4
	stl	$3,	0($16)
	addl	$16,	8,	$16	# res_ptr++
	beq	$20,	$Lend1a		# jump if size was == 2

	.align 3
$Loop1:	mull	$2,	$19,	$3	# $3 = prod_low
	ldl	$5,	0($16)		# $5 = *res_ptr
	addl	$4,	$0,	$0	# cy_limb = cy_limb + 'cy'
	subl	$20,	1,	$20	# size--
	umulh	$2,	$19,	$4	# $4 = cy_limb
	ldl	$2,	0($17)		# $2 = s1_limb
	addl	$17,	8,	$17	# s1_ptr++
	addl	$3,	$0,	$3	# $3 = cy_limb + prod_low
	cmpult	$3,	$0,	$0	# $0 = carry from (cy_limb + prod_low)
	addl	$5,	$3,	$3
	cmpult	$3,	$5,	$5
	stl	$3,	0($16)
	addl	$16,	8,	$16	# res_ptr++
	addl	$5,	$0,	$0	# combine carries
	bne	$20,	$Loop1

$Lend1a:
	mull	$2,	$19,	$3	# $3 = prod_low
	ldl	$5,	0($16)		# $5 = *res_ptr
	addl	$4,	$0,	$0	# cy_limb = cy_limb + 'cy'
	umulh	$2,	$19,	$4	# $4 = cy_limb
	addl	$3,	$0,	$3	# $3 = cy_limb + prod_low
	cmpult	$3,	$0,	$0	# $0 = carry from (cy_limb + prod_low)
	addl	$5,	$3,	$3
	cmpult	$3,	$5,	$5
	stl	$3,	0($16)
	addl	$16,	8,	$16	# res_ptr++
	addl	$5,	$0,	$0	# combine carries
	addl	$4,	$0,	$0	# cy_limb = prod_high + cy
	br	$31,	$Lunroll
$Lend1b:
	addl	$5,	$3,	$3
	cmpult	$3,	$5,	$5
	stl	$3,	0($16)
	addl	$16,	8,	$16	# res_ptr++
	addl	$0,	$5,	$0

$Lunroll:
	ldi	$17,	-16($17)	# L1 bookkeeping
	ldi	$16,	-16($16)	# L1 bookkeeping
	bis	$0,	$31,	$12

 # ____ UNROLLED LOOP SOFTWARE PIPELINE STARTUP ____

	ldl	$2,	16($17)		# L1
	ldl	$3,	24($17)		# L1
	ldi	$18,	-1($18)		# L1 bookkeeping
	ldl	$6,	16($16)		# L1
	ldl	$7,	24($16)		# L1
	ldl	$0,	32($17)		# L1
	mull	$19,	$2,	$13	# U1
	ldl	$1,	40($17)		# L1
	umulh	$19,	$2,	$14	# U1
	mull	$19,	$3,	$15	# U1
	ldi	$17,	64($17)		# L1 bookkeeping
	ldl	$4,	32($16)		# L1
	ldl	$5,	40($16)		# L1
	umulh	$19,	$3,	$8	# U1
	ldl	$2,	-16($17)	# L1
	mull	$19,	$0,	$9	# U1
	ldl	$3,	-8($17)		# L1
	umulh	$19,	$0,	$10	# U1
	addl	$6,	$13,	$6	# L0 lo + acc
	mull	$19,	$1,	$11	# U1
	cmpult	$6,	$13,	$20	# L0 lo add => carry
	ldi	$16,	64($16)		# L1 bookkeeping
	addl	$6,	$12,	$22	# U0 hi add => answer
	cmpult	$22,	$12,	$21	# L0 hi add => carry
	addl	$14,	$20,	$14	# U0 hi mul + carry
	ldl	$6,	-16($16)	# L1
	addl	$7,	$15,	$23	# L0 lo + acc
	addl	$14,	$21,	$14	# U0 hi mul + carry
	ldl	$7,	-8($16)		# L1
	umulh	$19,	$1,	$12	# U1
	cmpult	$23,	$15,	$20	# L0 lo add => carry
	addl	$23,	$14,	$23	# U0 hi add => answer
	ldl	$0,	0($17)		# L1
	mull	$19,	$2,	$13	# U1
	cmpult	$23,	$14,	$21	# L0 hi add => carry
	addl	$8,	$20,	$8	# U0 hi mul + carry
	ldl	$1,	8($17)		# L1
	umulh	$19,	$2,	$14	# U1
	addl	$4,	$9,	$4	# L0 lo + acc
	stl	$22,	-48($16)	# L0
	stl	$23,	-40($16)	# L1
	mull	$19,	$3,	$15	# U1
	addl	$8,	$21,	$8	# U0 hi mul + carry
	cmpult	$4,	$9,	$20	# L0 lo add => carry
	addl	$4,	$8,	$22	# U0 hi add => answer
	ble	$18,	$Lend		# U1 bookkeeping

 # ____ MAIN UNROLLED LOOP ____
	.align 4
$Loop:
	bis	$31,	$31,	$31	# U1 mt
	cmpult	$22,	$8,	$21	# L0 hi add => carry
	addl	$10,	$20,	$10	# U0 hi mul + carry
	ldl	$4,	0($16)		# L1

	bis	$31,	$31,	$31	# U1 mt
	addl	$5,	$11,	$23	# L0 lo + acc
	addl	$10,	$21,	$10	# L0 hi mul + carry
	ldl	$5,	8($16)		# L1

	umulh	$19,	$3,	$8	# U1
	cmpult	$23,	$11,	$20	# L0 lo add => carry
	addl	$23,	$10,	$23	# U0 hi add => answer
	ldl	$2,	16($17)		# L1

	mull	$19,	$0,	$9	# U1
	cmpult	$23,	$10,	$21	# L0 hi add => carry
	addl	$12,	$20,	$12	# U0 hi mul + carry
	ldl	$3,	24($17)		# L1

	umulh	$19,	$0,	$10	# U1
	addl	$6,	$13,	$6	# L0 lo + acc
	stl	$22,	-32($16)	# L0
	stl	$23,	-24($16)	# L1

	bis	$31,	$31,	$31	# L0 st slosh
	mull	$19,	$1,	$11	# U1
	bis	$31,	$31,	$31	# L1 st slosh
	addl	$12,	$21,	$12	# U0 hi mul + carry

	cmpult	$6,	$13,	$20	# L0 lo add => carry
	bis	$31,	$31,	$31	# U1 mt
	ldi	$18,	-1($18)		# L1 bookkeeping
	addl	$6,	$12,	$22	# U0 hi add => answer

	bis	$31,	$31,	$31	# U1 mt
	cmpult	$22,	$12,	$21	# L0 hi add => carry
	addl	$14,	$20,	$14	# U0 hi mul + carry
	ldl	$6,	16($16)		# L1

	bis	$31,	$31,	$31	# U1 mt
	addl	$7,	$15,	$23	# L0 lo + acc
	addl	$14,	$21,	$14	# U0 hi mul + carry
	ldl	$7,	24($16)		# L1

	umulh	$19,	$1,	$12	# U1
	cmpult	$23,	$15,	$20	# L0 lo add => carry
	addl	$23,	$14,	$23	# U0 hi add => answer
	ldl	$0,	32($17)		# L1

	mull	$19,	$2,	$13	# U1
	cmpult	$23,	$14,	$21	# L0 hi add => carry
	addl	$8,	$20,	$8	# U0 hi mul + carry
	ldl	$1,	40($17)		# L1

	umulh	$19,	$2,	$14	# U1
	addl	$4,	$9,	$4	# U0 lo + acc
	stl	$22,	-16($16)	# L0
	stl	$23,	-8($16)		# L1

	bis	$31,	$31,	$31	# L0 st slosh
	mull	$19,	$3,	$15	# U1
	bis	$31,	$31,	$31	# L1 st slosh
	addl	$8,	$21,	$8	# L0 hi mul + carry

	cmpult	$4,	$9,	$20	# L0 lo add => carry
	bis	$31,	$31,	$31	# U1 mt
	ldi	$17,	64($17)		# L1 bookkeeping
	addl	$4,	$8,	$22	# U0 hi add => answer

	bis	$31,	$31,	$31	# U1 mt
	cmpult	$22,	$8,	$21	# L0 hi add => carry
	addl	$10,	$20,	$10	# U0 hi mul + carry
	ldl	$4,	32($16)		# L1

	bis	$31,	$31,	$31	# U1 mt
	addl	$5,	$11,	$23	# L0 lo + acc
	addl	$10,	$21,	$10	# L0 hi mul + carry
	ldl	$5,	40($16)		# L1

	umulh	$19,	$3,	$8	# U1
	cmpult	$23,	$11,	$20	# L0 lo add => carry
	addl	$23,	$10,	$23	# U0 hi add => answer
	ldl	$2,	-16($17)	# L1

	mull	$19,	$0,	$9	# U1
	cmpult	$23,	$10,	$21	# L0 hi add => carry
	addl	$12,	$20,	$12	# U0 hi mul + carry
	ldl	$3,	-8($17)		# L1

	umulh	$19,	$0,	$10	# U1
	addl	$6,	$13,	$6	# L0 lo + acc
	stl	$22,	0($16)		# L0
	stl	$23,	8($16)		# L1

	bis	$31,	$31,	$31	# L0 st slosh
	mull	$19,	$1,	$11	# U1
	bis	$31,	$31,	$31	# L1 st slosh
	addl	$12,	$21,	$12	# U0 hi mul + carry

	cmpult	$6,	$13,	$20	# L0 lo add => carry
	bis	$31,	$31,	$31	# U1 mt
	ldi	$16,	64($16)		# L1 bookkeeping
	addl	$6,	$12,	$22	# U0 hi add => answer

	bis	$31,	$31,	$31	# U1 mt
	cmpult	$22,	$12,	$21	# L0 hi add => carry
	addl	$14,	$20,	$14	# U0 hi mul + carry
	ldl	$6,	-16($16)	# L1

	bis	$31,	$31,	$31	# U1 mt
	addl	$7,	$15,	$23	# L0 lo + acc
	addl	$14,	$21,	$14	# U0 hi mul + carry
	ldl	$7,	-8($16)		# L1

	umulh	$19,	$1,	$12	# U1
	cmpult	$23,	$15,	$20	# L0 lo add => carry
	addl	$23,	$14,	$23	# U0 hi add => answer
	ldl	$0,	0($17)		# L1

	mull	$19,	$2,	$13	# U1
	cmpult	$23,	$14,	$21	# L0 hi add => carry
	addl	$8,	$20,	$8	# U0 hi mul + carry
	ldl	$1,	8($17)		# L1

	umulh	$19,	$2,	$14	# U1
	addl	$4,	$9,	$4	# L0 lo + acc
	stl	$22,	-48($16)	# L0
	stl	$23,	-40($16)	# L1

	bis	$31,	$31,	$31	# L0 st slosh
	mull	$19,	$3,	$15	# U1
	bis	$31,	$31,	$31	# L1 st slosh
	addl	$8,	$21,	$8	# U0 hi mul + carry

	cmpult	$4,	$9,	$20	# L0 lo add => carry
	addl	$4,	$8,	$22	# U0 hi add => answer
	bis	$31,	$31,	$31	# L1 mt
	bgt	$18,	$Loop		# U1 bookkeeping

# ____ UNROLLED LOOP SOFTWARE PIPELINE FINISH ____
$Lend:
	cmpult	$22,	$8,	$21	# L0 hi add => carry
	addl	$10,	$20,	$10	# U0 hi mul + carry
	ldl	$4,	0($16)		# L1
	addl	$5,	$11,	$23	# L0 lo + acc
	addl	$10,	$21,	$10	# L0 hi mul + carry
	ldl	$5,	8($16)		# L1
	umulh	$19,	$3,	$8	# U1
	cmpult	$23,	$11,	$20	# L0 lo add => carry
	addl	$23,	$10,	$23	# U0 hi add => answer
	mull	$19,	$0,	$9	# U1
	cmpult	$23,	$10,	$21	# L0 hi add => carry
	addl	$12,	$20,	$12	# U0 hi mul + carry
	umulh	$19,	$0,	$10	# U1
	addl	$6,	$13,	$6	# L0 lo + acc
	stl	$22,	-32($16)	# L0
	stl	$23,	-24($16)	# L1
	mull	$19,	$1,	$11	# U1
	addl	$12,	$21,	$12	# U0 hi mul + carry
	cmpult	$6,	$13,	$20	# L0 lo add => carry
	addl	$6,	$12,	$22	# U0 hi add => answer
	cmpult	$22,	$12,	$21	# L0 hi add => carry
	addl	$14,	$20,	$14	# U0 hi mul + carry
	addl	$7,	$15,	$23	# L0 lo + acc
	addl	$14,	$21,	$14	# U0 hi mul + carry
	umulh	$19,	$1,	$12	# U1
	cmpult	$23,	$15,	$20	# L0 lo add => carry
	addl	$23,	$14,	$23	# U0 hi add => answer
	cmpult	$23,	$14,	$21	# L0 hi add => carry
	addl	$8,	$20,	$8	# U0 hi mul + carry
	addl	$4,	$9,	$4	# U0 lo + acc
	stl	$22,	-16($16)	# L0
	stl	$23,	-8($16)		# L1
	bis	$31,	$31,	$31	# L0 st slosh
	addl	$8,	$21,	$8	# L0 hi mul + carry
	cmpult	$4,	$9,	$20	# L0 lo add => carry
	addl	$4,	$8,	$22	# U0 hi add => answer
	cmpult	$22,	$8,	$21	# L0 hi add => carry
	addl	$10,	$20,	$10	# U0 hi mul + carry
	addl	$5,	$11,	$23	# L0 lo + acc
	addl	$10,	$21,	$10	# L0 hi mul + carry
	cmpult	$23,	$11,	$20	# L0 lo add => carry
	addl	$23,	$10,	$23	# U0 hi add => answer
	cmpult	$23,	$10,	$21	# L0 hi add => carry
	addl	$12,	$20,	$12	# U0 hi mul + carry
	stl	$22,	0($16)		# L0
	stl	$23,	8($16)		# L1
	addl	$12,	$21,	$0	# U0 hi mul + carry

	ldl	$9,	8($30)
	ldl	$10,	16($30)
	ldl	$11,	24($30)
	ldl	$12,	32($30)
	ldl	$13,	40($30)
	ldl	$14,	48($30)
	ldl	$15,	56($30)
	ldi	$30,	240($30)
	ret	$31,	($26),	1

	.end	__mpn_addmul_1
