forked from mikerabat/mrmath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASMMatrixVectorMultOperations.pas
294 lines (217 loc) · 8.81 KB
/
ASMMatrixVectorMultOperations.pas
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// ###################################################################
// #### This file is part of the mathematics library project, and is
// #### offered under the licence agreement described on
// #### http://www.mrsoft.org/
// ####
// #### Copyright:(c) 2011, Michael R. . All rights reserved.
// ####
// #### Unless required by applicable law or agreed to in writing, software
// #### distributed under the License is distributed on an "AS IS" BASIS,
// #### WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// #### See the License for the specific language governing permissions and
// #### limitations under the License.
// ###################################################################
unit ASMMatrixVectorMultOperations;
// #######################################################
// #### special routines for matrix vector multiplications.
// #######################################################
interface
{$IFDEF CPUX64}
{$DEFINE x64}
{$ENDIF}
{$IFDEF cpux86_64}
{$DEFINE x64}
{$ENDIF}
{$IFNDEF x64}
uses MatrixConst;
procedure ASMMatrixVectorMultAlignedEvenW1(dest : PDouble; const destLineWidth : TaSMNativeInt; mt1, mt2 : PDouble; width1 : TaSMNativeInt; height1 : TaSMNativeInt; height2 : TaSMNativeInt; const LineWidth1 : TaSMNativeInt);
procedure ASMMatrixVectorMultUnAlignedEvenW1(dest : PDouble; const destLineWidth : TaSMNativeInt; mt1, mt2 : PDouble; width1 : TaSMNativeInt; height1 : TaSMNativeInt; height2 : TaSMNativeInt; const LineWidth1 : TaSMNativeInt);
procedure ASMMatrixVectorMultAlignedOddW1(dest : PDouble; const destLineWidth : TaSMNativeInt; mt1, mt2 : PDouble; width1 : TaSMNativeInt; height1 : TaSMNativeInt; height2 : TaSMNativeInt; const LineWidth1 : TaSMNativeInt);
procedure ASMMatrixVectorMultUnAlignedOddW1(dest : PDouble; const destLineWidth : TaSMNativeInt; mt1, mt2 : PDouble; width1 : TaSMNativeInt; height1 : TaSMNativeInt; height2 : TaSMNativeInt; const LineWidth1 : TaSMNativeInt);
{$ENDIF}
implementation
{$IFNDEF x64}
{$IFDEF FPC} {$ASMMODE intel} {$ENDIF}
procedure ASMMatrixVectorMultAlignedOddW1(dest : PDouble; const destLineWidth : TaSMNativeInt; mt1, mt2 : PDouble; width1 : TaSMNativeInt; height1 : TaSMNativeInt; height2 : TaSMNativeInt; const LineWidth1 : TaSMNativeInt);
var iter : TaSMNativeInt;
begin
assert(((Cardinal(dest) and $0000000F) = 0) and ((Cardinal(mt1) and $0000000F) = 0) and ((Cardinal(mt2) and $0000000F) = 0), 'Error aligned operations cannot be performed');
assert(((Cardinal(LineWidth1) and $0000000F) = 0), 'Error cannot perform aligned operations');
assert((width1 and $00000001) = 1, 'Error odd width 1 expected');
assert(width1 = height2, 'Dimension error');
assert(height1 > 0, 'Dimension error');
iter := -(width1 - 1)*sizeof(double);
asm
push ebx;
push edi;
xorpd xmm7, xmm7;
mov edi, dest;
// prepare for "Reverse loop indexing"
mov ebx, mt2;
sub ebx, iter;
mov eax, mt1;
sub eax, iter;
// init for y := 0 to height1 - 1:
mov edx, height1;
@@foryloop:
// init values:
xorpd xmm0, xmm0; // dest^ := 0;
mov ecx, iter;
@@forxloop:
movapd xmm1, [eax + ecx];
mulpd xmm1, [ebx + ecx];
addpd xmm0, xmm1;
add ecx, 16;
jnz @@forxloop;
// special treatment for the last value:
movlpd xmm1, [eax];
movlpd xmm2, [ebx];
mulsd xmm1, xmm2;
addsd xmm0, xmm1;
// write back result (final addition and compactation)
haddpd xmm0, xmm7;
movlpd [edi], xmm0;
// next results:
add edi, destLineWidth;
add eax, LineWidth1;
dec edx;
jnz @@foryloop;
pop edi;
pop ebx;
end;
end;
procedure ASMMatrixVectorMultUnAlignedOddW1(dest : PDouble; const destLineWidth : TaSMNativeInt; mt1, mt2 : PDouble; width1 : TaSMNativeInt; height1 : TaSMNativeInt; height2 : TaSMNativeInt; const LineWidth1 : TaSMNativeInt);
var iter : TaSMNativeInt;
begin
assert((width1 and $00000001) = 1, 'Error odd width 1 expected');
assert(width1 = height2, 'Dimension error');
assert(height1 > 0, 'Dimension error');
iter := -(width1 - 1)*sizeof(double);
asm
push ebx;
push edi;
xorpd xmm7, xmm7;
mov edi, dest;
// prepare for "Reverse loop indexing"
mov ebx, mt2;
sub ebx, iter;
mov eax, mt1;
sub eax, iter;
mov mt1, eax;
// init for y := 0 to height1 - 1:
mov edx, height1;
@@foryloop:
// init values:
xorpd xmm0, xmm0; // dest^ := 0;
mov ecx, iter;
@@forxloop:
movupd xmm1, [eax + ecx];
movupd xmm2, [ebx + ecx];
mulpd xmm1, xmm2;
addpd xmm0, xmm1;
add ecx, 16;
jnz @@forxloop;
// special treatment for the last value:
movlpd xmm1, [eax];
movlpd xmm2, [ebx];
mulsd xmm1, xmm2;
addsd xmm0, xmm1;
// write back result (final addition and compactation)
haddpd xmm0, xmm7;
movlpd [edi], xmm0;
// next results:
add edi, destLineWidth;
add eax, LineWidth1;
dec edx;
jnz @@foryloop;
pop edi;
pop ebx;
end;
end;
procedure ASMMatrixVectorMultAlignedEvenW1(dest : PDouble; const destLineWidth : TaSMNativeInt; mt1, mt2 : PDouble; width1 : TaSMNativeInt; height1 : TaSMNativeInt; height2 : TaSMNativeInt; const LineWidth1 : TaSMNativeInt);
var iter : TaSMNativeInt;
begin
assert(((Cardinal(dest) and $0000000F) = 0) and ((Cardinal(mt1) and $0000000F) = 0) and ((Cardinal(mt2) and $0000000F) = 0), 'Error aligned operations cannot be performed');
assert(((Cardinal(LineWidth1) and $0000000F) = 0), 'Error aligned operations cannot be performed');
assert((width1 and $00000001) = 0, 'Error even width 1 expected');
assert(width1 = height2, 'Dimension error');
assert(height1 > 0, 'Dimension error');
iter := -width1*sizeof(double);
asm
push ebx;
push edi;
xorpd xmm7, xmm7;
mov edi, dest;
// prepare for reverse loop indexing
mov ebx, mt2;
sub ebx, iter;
mov eax, mt1;
sub eax, iter;
// init for y := 0 to height1 - 1:
mov edx, height1;
@@foryloop:
// init values:
xorpd xmm0, xmm0; // dest^ := 0;
mov ecx, iter;
@@forxloop:
movapd xmm1, [eax + ecx];
mulpd xmm1, [ebx + ecx];
addpd xmm0, xmm1;
add ecx, 16;
jnz @@forxloop;
// write back result (final addition and compactation)
haddpd xmm0, xmm7;
movlpd [edi], xmm0;
// next results:
add edi, destLineWidth;
add eax, LineWidth1;
dec edx;
jnz @@foryloop;
pop edi;
pop ebx;
end;
end;
procedure ASMMatrixVectorMultUnAlignedEvenW1(dest : PDouble; const destLineWidth : TaSMNativeInt; mt1, mt2 : PDouble; width1 : TaSMNativeInt; height1 : TaSMNativeInt; height2 : TaSMNativeInt; const LineWidth1 : TaSMNativeInt);
var iter : TaSMNativeInt;
begin
assert((width1 and $00000001) = 0, 'Error even width 1 expected');
assert(width1 = height2, 'Dimension error');
assert(height1 > 0, 'Dimension error');
iter := -width1*sizeof(double);
asm
push ebx;
push edi;
xorpd xmm7, xmm7;
mov edi, dest;
// prepare for reverse loop indexing
mov ebx, mt2;
sub ebx, iter;
mov eax, mt1;
sub eax, iter;
// init for y := 0 to height1 - 1:
mov edx, height1;
@@foryloop:
// init values:
xorpd xmm0, xmm0; // dest^ := 0;
mov ecx, iter;
@@forxloop:
movupd xmm1, [eax + ecx];
movupd xmm2, [ebx + ecx];
mulpd xmm1, xmm2;
addpd xmm0, xmm1;
add ecx, 16;
jnz @@forxloop;
// write back result (final addition and compactation)
haddpd xmm0, xmm7;
movlpd [edi], xmm0;
// next results:
add edi, destLineWidth;
add eax, LineWidth1;
dec edx;
jnz @@foryloop;
pop edi;
pop ebx;
end;
end;
{$ENDIF}
end.