1###################################-
2#
3#  Copyright (C) 2009-2015 Free Software Foundation, Inc.
4#
5#  Contributed by Michael Eager <eager@eagercon.com>.
6#
7#  This file is free software; you can redistribute it and/or modify it
8#  under the terms of the GNU General Public License as published by the
9#  Free Software Foundation; either version 3, or (at your option) any
10#  later version.
11#
12#  GCC is distributed in the hope that it will be useful, but WITHOUT
13#  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14#  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
15#  License for more details.
16#
17#  Under Section 7 of GPL version 3, you are granted additional
18#  permissions described in the GCC Runtime Library Exception, version
19#  3.1, as published by the Free Software Foundation.
20#
21#  You should have received a copy of the GNU General Public License and
22#  a copy of the GCC Runtime Library Exception along with this program;
23#  see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24#  <http://www.gnu.org/licenses/>.
25#
26#  muldi3_hard.S
27#
28#  Multiply operation for 64 bit integers, for devices with hard multiply
29#	Input :	Operand1[H] in Reg r5
30#		Operand1[L] in Reg r6
31#		Operand2[H] in Reg r7
32#		Operand2[L] in Reg r8
33#	Output: Result[H] in Reg r3
34#		Result[L] in Reg r4
35#
36#  Explaination:
37#
38# 	Both the input numbers are divided into 16 bit number as follows
39#		op1 = A B C D
40# 		op2 = E F G H
41#	result =    D * H
42#		 + (C * H + D * G) << 16
43#		 + (B * H + C * G + D * F) << 32
44#		 + (A * H + B * G + C * F + D * E) << 48
45#
46# 	Only 64 bits of the output are considered
47#
48#######################################
49
50	.globl	muldi3_hardproc
51	.ent	muldi3_hardproc
52muldi3_hardproc:
53	addi	r1,r1,-40
54
55#  Save the input operands on the caller's stack
56	swi	r5,r1,44
57	swi	r6,r1,48
58	swi	r7,r1,52
59	swi	r8,r1,56
60
61# Store all the callee saved registers
62	sw	r20,r1,r0
63	swi	r21,r1,4
64	swi	r22,r1,8
65	swi	r23,r1,12
66	swi	r24,r1,16
67	swi	r25,r1,20
68	swi	r26,r1,24
69	swi	r27,r1,28
70
71# Load all the 16 bit values for A through H
72	lhui	r20,r1,44   # A
73	lhui	r21,r1,46   # B
74	lhui	r22,r1,48   # C
75	lhui	r23,r1,50   # D
76	lhui	r24,r1,52   # E
77	lhui	r25,r1,54   # F
78	lhui	r26,r1,56   # G
79	lhui	r27,r1,58   # H
80
81# D * H ==> LSB of the result on stack ==> Store1
82	mul	r9,r23,r27
83	swi	r9,r1,36    # Pos2 and Pos3
84
85# Hi (Store1) + C * H + D * G ==> Store2 ==> Pos1 and Pos2
86# Store the carry generated in position 2 for Pos 3
87	lhui	r11,r1,36   # Pos2
88	mul	r9,r22,r27   # C * H
89	mul	r10,r23,r26  # D * G
90	add	r9,r9,r10
91	addc	r12,r0,r0
92	add	r9,r9,r11
93	addc	r12,r12,r0    # Store the Carry
94	shi	r9,r1,36    # Store Pos2
95	swi	r9,r1,32
96	lhui	r11,r1,32
97	shi	r11,r1,34   # Store Pos1
98
99# Hi (Store2) + B * H + C * G + D * F ==> Store3 ==> Pos0 and Pos1
100	mul	r9,r21,r27  # B * H
101	mul	r10,r22,r26 # C * G
102	mul	r7,r23,r25 # D * F
103	add	r9,r9,r11
104	add	r9,r9,r10
105	add	r9,r9,r7
106	swi	r9,r1,32   # Pos0 and Pos1
107
108# Hi (Store3) + A * H + B * G + C * F + D * E ==> Store3 ==> Pos0
109	lhui	r11,r1,32  # Pos0
110	mul	r9,r20,r27  # A * H
111	mul	r10,r21,r26 # B * G
112	mul	r7,r22,r25 # C * F
113	mul	r8,r23,r24 # D * E
114	add	r9,r9,r11
115	add 	r9,r9,r10
116	add	r9,r9,r7
117	add	r9,r9,r8
118	sext16	r9,r9       # Sign extend the MSB
119	shi	r9,r1,32
120
121# Move results to r3 and r4
122	lhui	r3,r1,32
123	add	r3,r3,r12
124	shi	r3,r1,32
125	lwi	r3,r1,32  # Hi Part
126	lwi	r4,r1,36  # Lo Part
127
128# Restore Callee saved registers
129	lw	r20,r1,r0
130	lwi	r21,r1,4
131	lwi	r22,r1,8
132	lwi	r23,r1,12
133	lwi	r24,r1,16
134	lwi	r25,r1,20
135	lwi	r26,r1,24
136	lwi	r27,r1,28
137
138# Restore Frame and return
139	rtsd	r15,8
140	addi	r1,r1,40
141
142.end muldi3_hardproc
143
144
145