Announce SDCC 4.5.0 RC1.
[sdcc.git] / sdcc / device / lib / z80 / __mulsint2slong.s
blob140faceddc89ad5a2f518039d95dcb94d8cd4538
1 ;--------------------------------------------------------------------------
2 ; __mulsint2slong.s
4 ; Copyright (c) 2021, Philipp Klaus Krause
6 ; This library is free software; you can redistribute it and/or modify it
7 ; under the terms of the GNU General Public License as published by the
8 ; Free Software Foundation; either version 2, or (at your option) any
9 ; later version.
11 ; This library is distributed in the hope that it will be useful,
12 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ; GNU General Public License for more details.
16 ; You should have received a copy of the GNU General Public License
17 ; along with this library; see the file COPYING. If not, write to the
18 ; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
19 ; MA 02110-1301, USA.
21 ; As a special exception, if you link this library with other files,
22 ; some of which are compiled with SDCC, to produce an executable,
23 ; this library does not by itself cause the resulting executable to
24 ; be covered by the GNU General Public License. This exception does
25 ; not however invalidate any other reasons why the executable file
26 ; might be covered by the GNU General Public License.
27 ;--------------------------------------------------------------------------
29 .module __mulsint2slong
30 .optsdcc -mz80 sdcccall(1)
32 .globl ___muluint2ulong
33 .globl ___mulsint2slong
35 ; uint32_t _u_16_16_mul(uint16_t l, uint16_t r);
37 .area _CODE
39 ___mulsint2slong:
40 ; Use lowest bit of c to remember if result needs to be negated. Use b to cache #0.
41 ld bc, #0
43 bit #7, l
44 jr z, hl_nonneg
45 ld a, b
46 sub a, l
47 ld l, a
48 ld a, b
49 sbc a, h
50 ld h, a
51 inc c
52 hl_nonneg:
54 bit #7, e
55 jr z, de_nonneg
56 ld a, b
57 sub a, e
58 ld e, a
59 ld a, b
60 sbc a, d
61 ld d, a
62 inc c
63 de_nonneg:
65 push bc
66 call ___muluint2ulong
67 pop bc
69 bit #0, c
70 ret z
72 ; Negate result.
73 ld a, b
74 sub a, e
75 ld e, a
76 ld a, b
77 sbc a, d
78 ld d, a
79 ld a, b
80 sbc a, l
81 ld l, a
82 ld a, b
83 sbc a, h
84 ld h, a
85 ret
87 ; 16x16->32 multiplication
88 ___muluint2ulong:
89 ld iy, #0
90 ld b, #16
91 loop:
92 add iy, iy
93 adc hl, hl
94 jr NC, skip
95 add iy, de
96 jr NC, skip
97 inc hl
98 skip:
99 djnz loop
100 push iy
101 pop de