154359Sroberto;   Copyright (C) 2011-2020 Free Software Foundation, Inc.
254359Sroberto;   Contributed by Red Hat.
354359Sroberto;
454359Sroberto; This file is free software; you can redistribute it and/or modify it
554359Sroberto; under the terms of the GNU General Public License as published by the
654359Sroberto; Free Software Foundation; either version 3, or (at your option) any
754359Sroberto; later version.
854359Sroberto;
954359Sroberto; This file is distributed in the hope that it will be useful, but
1054359Sroberto; WITHOUT ANY WARRANTY; without even the implied warranty of
1154359Sroberto; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1254359Sroberto; General Public License for more details.
1354359Sroberto;
1454359Sroberto; Under Section 7 of GPL version 3, you are granted additional
1554359Sroberto; permissions described in the GCC Runtime Library Exception, version
1654359Sroberto; 3.1, as published by the Free Software Foundation.
1754359Sroberto;
1854359Sroberto; You should have received a copy of the GNU General Public License and
1954359Sroberto; a copy of the GCC Runtime Library Exception along with this program;
2054359Sroberto; see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
2154359Sroberto; <http://www.gnu.org/licenses/>.
2254359Sroberto
2354359Sroberto;; 32x32=32 multiply
2454359Sroberto
2554359Sroberto#include "vregs.h"
2654359Sroberto
2754359Sroberto;----------------------------------------------------------------------
2854359Sroberto
2954359Sroberto; Register use:
3054359Sroberto;	RB0	RB1	RB2
3154359Sroberto; AX	op2L	res32L	res32H
3254359Sroberto; BC	op2H	(resH)	op1
3354359Sroberto; DE	count	(resL-tmp)
3454359Sroberto; HL	[sp+4]
3554359Sroberto
3654359Sroberto; Register use (G10):
3754359Sroberto;
3854359Sroberto; AX	    op2L
3954359Sroberto; BC	    op2H
4054359Sroberto; DE	    count
4154359Sroberto; HL	    [sp+4]
4254359Sroberto; r8/r9	    res32L
4354359Sroberto; r10/r11   (resH)
4454359Sroberto; r12/r13   (resL-tmp)
4554359Sroberto; r16/r17   res32H
4654359Sroberto; r18/r19   op1
4754359Sroberto
4854359SrobertoSTART_FUNC ___mulsi3
4954359Sroberto	;; A is at [sp+4]
5054359Sroberto	;; B is at [sp+8]
5154359Sroberto	;; result is in R8..R11
5254359Sroberto
5354359Sroberto#ifdef __RL78_G10__
5454359Sroberto	movw	ax, r16
5554359Sroberto	push	ax
5654359Sroberto	movw	ax, r18
5754359Sroberto	push	ax
5854359Sroberto#else
5954359Sroberto	sel	rb2
6054359Sroberto	push	ax
6154359Sroberto	push	bc
6254359Sroberto	sel	rb0
6354359Sroberto#endif
6454359Sroberto	clrw	ax
6554359Sroberto	movw	r8, ax
6654359Sroberto	movw	r16, ax
6754359Sroberto
6854359Sroberto	movw	ax, [sp+14]
6954359Sroberto	cmpw	ax, #0
7054359Sroberto	bz	$1f
7154359Sroberto	cmpw	ax, #0xffff
7254359Sroberto	bnz	$2f
7354359Sroberto	movw	ax, [sp+8]
7454359Sroberto#ifdef __RL78_G10__
7554359Sroberto	push    bc
7654359Sroberto	movw    bc, r8
7754359Sroberto	xchw	ax, bc
7854359Sroberto	subw    ax, bc
7954359Sroberto	movw	r8, ax
8054359Sroberto	movw    ax, bc
8154359Sroberto	pop     bc
8254359Sroberto#else
8354359Sroberto	sel	rb1
8454359Sroberto	subw	ax, r_0
8554359Sroberto	sel	rb0
8654359Sroberto#endif
8754359Sroberto	br	$1f
8854359Sroberto2:
8954359Sroberto	movw	bc, ax
9054359Sroberto	movw	ax, [sp+8]
9154359Sroberto	cmpw	ax, #0
9254359Sroberto	skz
9354359Sroberto	call	!.Lmul_hi
9454359Sroberto1:
9554359Sroberto
9654359Sroberto	movw	ax, [sp+10]
9754359Sroberto	cmpw	ax, #0
9854359Sroberto	bz	$1f
9954359Sroberto	cmpw	ax, #0xffff
10054359Sroberto	bnz	$2f
10154359Sroberto	movw	ax, [sp+12]
10254359Sroberto#ifdef __RL78_G10__
10354359Sroberto	push    bc
10454359Sroberto	movw    bc, r8
10554359Sroberto	xchw	ax, bc
10654359Sroberto	subw    ax, bc
10754359Sroberto	movw	r8, ax
10854359Sroberto	movw    ax, bc
10954359Sroberto	pop     bc
11054359Sroberto#else
11154359Sroberto	sel	rb1
11254359Sroberto	subw	ax, r_0
11354359Sroberto	sel	rb0
11454359Sroberto#endif
11554359Sroberto	br	$1f
11654359Sroberto2:
11754359Sroberto	movw	bc, ax
11854359Sroberto	movw	ax, [sp+12]
11954359Sroberto	cmpw	ax, #0
12054359Sroberto	skz
12154359Sroberto	call	!.Lmul_hi
12254359Sroberto1:
12354359Sroberto
12454359Sroberto	movw	ax, r8
12554359Sroberto	movw	r16, ax
12654359Sroberto	clrw	ax
12754359Sroberto	movw	r8, ax
12854359Sroberto
12954359Sroberto	;; now do R16:R8 += op1L * op2L
13054359Sroberto
13154359Sroberto	;; op1 is in AX.0 (needs to shrw)
13254359Sroberto	;; op2 is in BC.2 and BC.1 (bc can shlw/rolcw)
13354359Sroberto	;; res is in AX.2 and AX.1 (needs to addw)
13454359Sroberto
13554359Sroberto	movw	ax, [sp+8]
13654359Sroberto	movw	r10, ax		; BC.1
13754359Sroberto	movw	ax, [sp+12]
13854359Sroberto
13954359Sroberto	cmpw	ax, r10
14054359Sroberto	bc	$.Lmul_hisi_top
14154359Sroberto	movw	bc, r10
14254359Sroberto	movw	r10, ax
14354359Sroberto	movw	ax, bc
14454359Sroberto
14554359Sroberto.Lmul_hisi_top:
14654359Sroberto	movw	bc, #0
14754359Sroberto
14854359Sroberto.Lmul_hisi_loop:
14954359Sroberto	shrw	ax, 1
15054359Sroberto#ifdef __RL78_G10__
15154359Sroberto	push	ax
15254359Sroberto	bnc	$.Lmul_hisi_no_add_g10
15354359Sroberto	movw	ax, r8
15454359Sroberto	addw	ax, r10
15554359Sroberto	movw	r8, ax
15654359Sroberto	sknc
15754359Sroberto	incw	r16
15854359Sroberto	movw	ax, r16
15954359Sroberto	addw	ax, r_2
16054359Sroberto	movw	r16, ax
16154359Sroberto.Lmul_hisi_no_add_g10:
16254359Sroberto	movw	ax, r10
16354359Sroberto	shlw	ax, 1
16454359Sroberto	movw	r10, ax
16554359Sroberto	pop	ax
16654359Sroberto#else
16754359Sroberto	bnc	$.Lmul_hisi_no_add
16854359Sroberto	sel	rb1
16954359Sroberto	addw	ax, bc
17054359Sroberto	sel	rb2
17154359Sroberto	sknc
17254359Sroberto	incw	ax
17354359Sroberto	addw	ax, r_2
17454359Sroberto.Lmul_hisi_no_add:
17554359Sroberto	sel	rb1
17654359Sroberto	shlw	bc, 1
17754359Sroberto	sel	rb0
17854359Sroberto#endif
17954359Sroberto	rolwc	bc, 1
18054359Sroberto	cmpw	ax, #0
18154359Sroberto	bz	$.Lmul_hisi_done
18254359Sroberto
18354359Sroberto	shrw	ax, 1
18454359Sroberto#ifdef __RL78_G10__
18554359Sroberto	push	ax
18654359Sroberto	bnc	$.Lmul_hisi_no_add2_g10
18754359Sroberto	movw	ax, r8
18854359Sroberto	addw	ax, r10
18954359Sroberto	movw	r8, ax
19054359Sroberto	movw	ax, r16
19154359Sroberto	sknc
19254359Sroberto	incw	ax
19354359Sroberto	addw	ax, r_2
19454359Sroberto	movw	r16, ax
19554359Sroberto.Lmul_hisi_no_add2_g10:
19654359Sroberto	movw	ax, r10
19754359Sroberto	shlw	ax, 1
19854359Sroberto	movw	r10, ax
19954359Sroberto	pop	ax
20054359Sroberto#else
20154359Sroberto	bnc	$.Lmul_hisi_no_add2
20254359Sroberto	sel	rb1
20354359Sroberto	addw	ax, bc
20454359Sroberto	sel	rb2
20554359Sroberto	sknc
20654359Sroberto	incw	ax
20754359Sroberto	addw	ax, r_2
20854359Sroberto.Lmul_hisi_no_add2:
20954359Sroberto	sel	rb1
21054359Sroberto	shlw	bc, 1
21154359Sroberto	sel	rb0
21254359Sroberto#endif
21354359Sroberto	rolwc	bc, 1
21454359Sroberto	cmpw	ax, #0
21554359Sroberto	bnz	$.Lmul_hisi_loop
21654359Sroberto
21754359Sroberto.Lmul_hisi_done:
21854359Sroberto
21954359Sroberto	movw	ax, r16
22054359Sroberto	movw	r10, ax
22154359Sroberto
22254359Sroberto#ifdef __RL78_G10__
22354359Sroberto	pop	ax
22454359Sroberto	movw	r18, ax
22554359Sroberto	pop	ax
22654359Sroberto	movw	r16, ax
22754359Sroberto#else
22854359Sroberto	sel	rb2
22954359Sroberto	pop	bc
23054359Sroberto	pop	ax
23154359Sroberto	sel	rb0
23254359Sroberto#endif
23354359Sroberto
23454359Sroberto	ret
23554359SrobertoEND_FUNC ___mulsi3
23654359Sroberto
23754359Sroberto;----------------------------------------------------------------------
23854359Sroberto
23954359SrobertoSTART_FUNC ___mulhi3
24054359Sroberto	movw	r8, #0
24154359Sroberto	movw	ax, [sp+6]
24254359Sroberto	movw	bc, ax
24354359Sroberto	movw	ax, [sp+4]
24454359Sroberto
24554359Sroberto	;; R8 += AX * BC
24654359Sroberto.Lmul_hi:
24754359Sroberto	cmpw	ax, bc
24854359Sroberto	skc
24954359Sroberto	xchw	ax, bc
25054359Sroberto	br	$.Lmul_hi_loop
25154359Sroberto
25254359Sroberto.Lmul_hi_top:
25354359Sroberto#ifdef __RL78_G10__
25454359Sroberto	push	ax
25554359Sroberto	movw	ax, r8
25654359Sroberto	addw	ax, r_2
25754359Sroberto	movw	r8, ax
25854359Sroberto	pop	ax
25954359Sroberto#else
26054359Sroberto	sel	rb1
26154359Sroberto	addw	ax, r_2
26254359Sroberto	sel	rb0
26354359Sroberto#endif
26454359Sroberto
26554359Sroberto.Lmul_hi_no_add:
26654359Sroberto	shlw	bc, 1
26754359Sroberto.Lmul_hi_loop:
26854359Sroberto	shrw	ax, 1
26954359Sroberto	bc	$.Lmul_hi_top
27054359Sroberto	cmpw	ax, #0
27154359Sroberto	bz	$.Lmul_hi_done
27254359Sroberto
27354359Sroberto	shlw	bc, 1
27454359Sroberto	shrw	ax, 1
27554359Sroberto	bc	$.Lmul_hi_top
27654359Sroberto	cmpw	ax, #0
27754359Sroberto	bnz	$.Lmul_hi_no_add
27854359Sroberto
27954359Sroberto.Lmul_hi_done:
28054359Sroberto	ret
28154359SrobertoEND_FUNC ___mulhi3
28254359Sroberto
28354359Sroberto;;; --------------------------------------
28454359Sroberto#ifdef __RL78_G10__
28554359Sroberto	START_FUNC ___mulqi3
28654359Sroberto
28754359Sroberto       	mov	a, [sp+4]
28854359Sroberto       	mov	r9, a
28954359Sroberto       	mov	a, [sp+6]
29054359Sroberto       	mov	r10, a
29154359Sroberto       	mov	a, #9
29254359Sroberto       	mov	r11, a
29354359Sroberto       	clrb	a
29454359Sroberto       	mov	r8, a
29554359Sroberto.L2:
29654359Sroberto    	cmp0	r10
29754359Sroberto    	skz
29854359Sroberto    	dec	r11
29954359Sroberto    	sknz
30054359Sroberto    	ret
30154359Sroberto    	mov	a, r10
30254359Sroberto    	and	a, #1
30354359Sroberto    	mov	r12, a
30454359Sroberto    	cmp0	r12
30554359Sroberto    	sknz
30654359Sroberto    	br	!!.L3
30754359Sroberto    	mov	a, r9
30854359Sroberto    	mov	l, a
30954359Sroberto    	mov	a, r8
31054359Sroberto    	add	a, l
31154359Sroberto    	mov	r8, a
31254359Sroberto.L3:
31354359Sroberto       	mov	a, r9
31454359Sroberto       	add	a, a
31554359Sroberto       	mov	r9, a
31654359Sroberto       	mov	a, r10
31754359Sroberto       	shr	a, 1
31854359Sroberto       	mov	r10, a
31954359Sroberto       	br	!!.L2
32054359Sroberto
32154359Sroberto	END_FUNC   ___mulqi3
32254359Sroberto#endif
32354359Sroberto
32454359Sroberto