-
Notifications
You must be signed in to change notification settings - Fork 6
/
smult3.a
209 lines (193 loc) · 3.52 KB
/
smult3.a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
; smult3.a
; from https://codebase64.org/doku.php?id=base:seriously_fast_multiplication
; tweaked slightly for speed and size
;
; 16 bit x 16 bit signed multiplication, 32 bit result
; Average cycles: 277.57
; 2253 bytes
T1 = $02
T2 = $04
PRODUCT = $06
* = $0200
; Align tables to start of page
square1_lo
!for i, 0, 511 {
!byte <((i*i)/4)
}
square1_hi
!for i, 0, 511 {
!byte >((i*i)/4)
}
square2_lo
!for i, 0, 511 {
!byte <(((i - 255)*(i - 255))/4)
}
square2_hi
!for i, 0, 511 {
!byte >(((i - 255)*(i - 255))/4)
}
; Description: Signed 16-bit multiplication with signed 32-bit result.
;
; Input: 16-bit signed value in T1
; 16-bit signed value in T2
; Carry=0: Re-use T1 from previous multiplication (faster)
; Carry=1: Set T1 (slower)
;
; Output: 32-bit signed value in PRODUCT
;
; Clobbered: PRODUCT, X, A, C
multiply_16bit_signed
; Step 1: unsigned multiply
jsr multiply_16bit_unsigned
; Step 2: Apply sign (See C=Hacking16 for details).
bit T1+1
bpl +
sec
; lda PRODUCT+2
sbc T2+0
sta PRODUCT+2
lda PRODUCT+3
sbc T2+1
sta PRODUCT+3
+
bit T2+1
bpl +
sec
lda PRODUCT+2
sbc T1+0
sta PRODUCT+2
lda PRODUCT+3
sbc T1+1
sta PRODUCT+3
+
rts
; See mult31.a
; Description: Unsigned 16-bit multiplication with unsigned 32-bit result.
;
; Input: 16-bit unsigned value in T1
; 16-bit unsigned value in T2
;
; Output: 32-bit unsigned value in PRODUCT
;
; Clobbered: PRODUCT, X, A, C
;
; Allocation setup: T1,T2 and PRODUCT preferably on Zero-page.
; square1_lo, square1_hi, square2_lo, square2_hi must be
; page aligned. Each table are 512 bytes. Total 2kb.
;
; Table generation: I:0..511
; square1_lo = <((I*I)/4)
; square1_hi = >((I*I)/4)
; square2_lo = <(((I-255)*(I-255))/4)
; square2_hi = >(((I-255)*(I-255))/4)
multiply_16bit_unsigned
; <T1 * <T2 = AAaa
; <T1 * >T2 = BBbb
; >T1 * <T2 = CCcc
; >T1 * >T2 = DDdd
;
; AAaa
; BBbb
; CCcc
; + DDdd
; ----------
; PRODUCT!
; Setup T1 if changed
lda T1+0
sta sm1a+1
sta sm3a+1
sta sm5a+1
sta sm7a+1
eor #$ff
sta sm2a+1
sta sm4a+1
sta sm6a+1
sta sm8a+1
lda T1+1
sta sm1b+1
sta sm3b+1
sta sm5b+1
sta sm7b+1
eor #$ff
sta sm2b+1
sta sm4b+1
sta sm6b+1
sta sm8b+1
; Perform <T1 * <T2 = AAaa
ldx T2+0
sec
sm1a
lda square1_lo,x
sm2a
sbc square2_lo,x
sta PRODUCT+0
sm3a
lda square1_hi,x
sm4a
sbc square2_hi,x
sta _AA+1
; Perform >T1_hi * <T2 = CCcc
sec
sm1b
lda square1_lo,x
sm2b
sbc square2_lo,x
sta _cc+1
sm3b
lda square1_hi,x
sm4b
sbc square2_hi,x
sta _CC+1
; Perform <T1 * >T2 = BBbb
ldx T2+1
sec
sm5a
lda square1_lo,x
sm6a
sbc square2_lo,x
sta _bb+1
sm7a
lda square1_hi,x
sm8a
sbc square2_hi,x
sta _BB+1
; Perform >T1 * >T2 = DDdd
sec
sm5b
lda square1_lo,x
sm6b
sbc square2_lo,x
sta _dd+1
sm7b
lda square1_hi,x
sm8b
sbc square2_hi,x
sta PRODUCT+3
; Add the separate multiplications together
clc
_AA
lda #0
_bb
adc #0
sta PRODUCT+1
_BB
lda #0
_CC
adc #0
sta PRODUCT+2
bcc +
inc PRODUCT+3
clc
+
_cc
lda #0
adc PRODUCT+1
sta PRODUCT+1
_dd
lda #0
adc PRODUCT+2
sta PRODUCT+2
bcc +
inc PRODUCT+3
+
rts