-
Notifications
You must be signed in to change notification settings - Fork 3
/
simdssevec.pas
490 lines (436 loc) · 16.1 KB
/
simdssevec.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
{*!
* Operator SIMD (https://oprsimd.github.io)
*
* @link https://github.com/oprsimd
* @copyright Copyright (c) 2018 Zamrony P. Juhara
* @license https://github.com/oprsimd/blob/master/LICENSE (MIT)
*}
unit simdssevec;
interface
{$MODE OBJFPC}
{$ALIGN 16}
uses
vectypes;
{------------------------------------------------
operator overloading collections to allow fast
vector operations using Intel SIMD SSE instructions.
@author Zamrony P. Juhara <[email protected]>
-----------------------------------------------}
{-------------------------------------
Copy scalar value to vector component
-------------------------------------
for example:
res.x = scalar
res.y = scalar
res.z = scalar
res.w = scalar
can be written as
res := scalar
--------------------------------------}
operator := (const scalar : single) res : TVector;
{-------------------------------------
Add two vectors using SSE instruction
-------------------------------------
for example:
res.x = v1.x + v2.x
res.y = v1.y + v2.y
res.z = v1.z + v2.z
res.w = v1.w + v2.w
can be written as
res := v1 + v2
--------------------------------------}
operator + (const v1:TVector; const v2:TVector) res : TVector;
{-------------------------------------
Subtract two vectors using SSE instruction
-------------------------------------
for example:
res.x = v1.x - v2.x
res.y = v1.y - v2.y
res.z = v1.z - v2.z
res.w = v1.w - v2.w
can be written as
res := v1 - v2
--------------------------------------}
operator - (const v1 : TVector; const v2 : TVector) res : TVector;
{-------------------------------------
Multiply a vector with scalar using SSE instruction
-------------------------------------
for example:
res.x = v1.x * scalar
res.y = v1.y * scalar
res.z = v1.z * scalar
res.w = v1.w * scalar
can be written as
res := v1 * scalar
--------------------------------------}
operator * (const v1 : TVector; const scalar : single) res : TVector;
{-------------------------------------
Multiply a vector with scalar using SSE instruction
-------------------------------------
for example:
res.x = scalar * v1.x
res.y = scalar * v1.y
res.z = scalar * v1.z
res.w = scalar * v1.w
can be written as
res := scalar * v1
--------------------------------------}
operator * (const scalar : single; const v1 : TVector) res : TVector;
{-------------------------------------
Calculate dot product of two vectors using
SSE instruction
--------------------------------------
var res : single;
v1, v2 :TVector;
following example
res = v1.x * v2.x +
v1.y * v2.y +
v1.z * v2.z
can be written as
res := v1 * v2;
--------------------------------------}
operator * (const v1 : TVector; const v2 : TVector) res : single;
{-------------------------------------
Cross product of two vectors
-------------------------------------
var res, v1, v2 : TVector;
res.x := v1.y * v2.z - v1.z * v2.y;
res.y := v1.z * v2.x - v1.x * v2.z;
res.z := v1.x * v2.y - v1.y * v2.x;
res.w := 0;
can be writen as
res := v1 ** v2;
--------------------------------------}
operator ** (const v1 : TVector; const v2 : TVector) : TVector; assembler;
implementation
{$ASMMODE intel}
{-------------------------------------
Copy scalar value to vector component
-------------------------------------
res.x = scalar
res.y = scalar
res.z = scalar
res.w = scalar
-------------------------------------
input:
For x86-64 architecture
scalar value will be passed
in xmm0 in following order
xmm0 = [ scalar, (not used), (not used), (not used)]
--------------------------------------
output:
result will be stored in xmm0 and xmm1
register with following order
xmm0 = [ res.x, res.y, [not used], [not used]]
xmm1 = [ res.z, res.w, [not used], [not used]]
--------------------------------------}
operator := (const scalar : single) res : TVector; assembler;
asm
//shuffle xmm0 so that
//xmm0 = {scalar, scalar, scalar, scalar}
shufps xmm0, xmm0, 00000000b
//copy high quadword of xmm0 to low quadword of xmm1
//xmm1 = {res.z, res.w, [not used], [not used]}
movhlps xmm1, xmm0
end;
{-------------------------------------
Add two vectors using SSE instruction
-------------------------------------
res.x = v1.x + v2.x
res.y = v1.y + v2.y
res.z = v1.z + v2.z
res.w = v1.w + v2.w
-------------------------------------
input:
For x86-64 architecture
v1 and v2 value will be passed
in xmm0, xmm1, xmm2, xmm3 in following order
xmm0 = [ v1.x, v1.y, (not used), (not used)]
xmm1 = [ v1.z, v1.w, (not used), (not used)]
xmm2 = [ v2.x, v2.y, (not used), (not used)]
xmm3 = [ v2.z, v2.w, (not used), [not used]]
--------------------------------------
output:
result will be stored in xmm0 and xmm1
register with following order
xmm0 = [ res.x, res.y, [not used], [not used]]
xmm1 = [ res.z, res.w, [not used], [not used]]
--------------------------------------}
operator + (const v1:TVector; const v2:TVector) res : TVector; assembler;
asm
//copy low quadword of xmm1 to high quadword of xmm0
//xmm0 = {v1.x, v1.y, v1.z, v1.w}
movlhps xmm0, xmm1
//copy low quadword of xmm3 to high quadword of xmm2
//xmm2 = {v2.x, v2.y, v2.z, v2.w}
movlhps xmm2, xmm3
//add xmm0 and xmm2
//xmm0 = {v1.x + v2.x,
// v1.y + v2.y,
// v1.z + v2.z,
// v1.w + v2.w}
addps xmm0, xmm2
//copy high quadword of xmm0 to low quadword of xmm1
//xmm1 = {res.z, res.w, [not used], [not used]}
movhlps xmm1, xmm0
end;
{-------------------------------------
Substract two vector using SSE instruction
-------------------------------------
result.x = v1.x - v2.x
result.y = v1.y - v2.y
result.z = v1.z - v2.z
result.w = v1.w - v2.w
-------------------------------------
input:
For x86-64 architecture
v1 and v2 value will be passed
in xmm0, xmm1, xmm2, xmm3 in following order
xmm0 = [ v1.x, v1.y, (not used), (not used)]
xmm1 = [ v1.z, v1.w, (not used), (not used)]
xmm2 = [ v2.x, v2.y, (not used), (not used)]
xmm3 = [ v2.z, v2.w, (not used), [not used]]
--------------------------------------
output:
res will be stored in xmm0 and xmm1
register with following order
xmm0 = [ res.x, res.y, [not used], [not used]]
xmm1 = [ res.z, res.w, [not used], [not used]]
--------------------------------------}
operator - (const v1 : TVector; const v2 : TVector) res : TVector; assembler;
asm
//copy low quadword of xmm1 to high quadword of xmm0
//xmm0 = {v1.x, v1.y, v1.z, v1.w}
movlhps xmm0, xmm1
//copy low quadword of xmm3 to high quadword of xmm2
//xmm2 = {v2.x, v2.y, v2.z, v2.w}
movlhps xmm2, xmm3
//subtract xmm0 and xmm2
//xmm0 = {v1.x - v2.x,
// v1.y - v2.y,
// v1.z - v2.z,
// v1.w - v2.w}
subps xmm0, xmm2
//copy high quadword of xmm0 to low quadword of xmm1
//xmm1 = {res.z, res.w, [not used], [not used]}
movhlps xmm1, xmm0
end;
{-------------------------------------
multiply a vector with a scalar using
SSE instruction
--------------------------------------
res.x = v1.x * scalar
res.y = v1.y * scalar
res.z = v1.z * scalar
res.w = v1.w * scalar
-------------------------------------
input:
For x86-64 architecture
v1 and scalar value will be passed
in xmm0, xmm1, xmm2 in following order
xmm0 = [ v1.x, v1.y, (not used), (not used)]
xmm1 = [ v1.z, v1.w, (not used), (not used)]
xmm2 = [ scalar, (not used), (not used), (not used)]
--------------------------------------
output:
result will be stored in xmm0 and xmm1
register with following order
xmm0 = [ res.x, res.y, [not used], [not used]]
xmm1 = [ res.z, res.w, [not used], [not used]]
--------------------------------------}
operator * (const v1 : TVector; const scalar : single) res : TVector; assembler;
asm
//copy low quadword of xmm1 to high quadword of xmm0
//xmm0 = {v1.x, v1.y, v1.z, v1.w}
movlhps xmm0, xmm1
//shuffle xmm2 so that
//xmm2 = {scalar, scalar, scalar, scalar}
shufps xmm2, xmm2, 00000000b
//multiply xmm0 and xmm2
//xmm0 = {v1.x * scalar,
// v1.y * scalar,
// v1.z * scalar,
// v1.w * scalar}
mulps xmm0, xmm2
//copy high quadword of xmm0 to low quadword of xmm1
//xmm1 = {res.z, res.w, [not used], [not used]}
movhlps xmm1, xmm0
end;
{-------------------------------------
multiply a vector with a scalar using
SSE instruction
--------------------------------------
res.x = v1.x * scalar
res.y = v1.y * scalar
res.z = v1.z * scalar
res.w = v1.w * scalar
-------------------------------------
input:
For x86-64 architecture
v1 and scalar value will be passed
in xmm0, xmm1, xmm2 in following order
xmm0 = [ scalar, (not used), (not used), (not used)]
xmm1 = [ v1.x, v1.y, (not used), (not used)]
xmm2 = [ v1.z, v1.w, (not used), (not used)]
--------------------------------------
output:
result will be stored in xmm0 and xmm1
register with following order
xmm0 = [ res.x, res.y, [not used], [not used]]
xmm1 = [ res.z, res.w, [not used], [not used]]
--------------------------------------}
operator * (const scalar : single; const v1 : TVector) res : TVector; assembler;
asm
//copy low quadword of xmm2 to high quadword of xmm1
//xmm1 = {v1.x, v1.y, v1.z, v1.w}
movlhps xmm1, xmm2
//shuffle xmm2 so that
//xmm2 = {scalar, scalar, scalar, scalar}
shufps xmm0, xmm0, 00000000b
//multiply xmm0 and xmm1
//xmm0 = {v1.x * scalar,
// v1.y * scalar,
// v1.z * scalar,
// v1.w * scalar}
mulps xmm0, xmm1
//copy high quadword of xmm0 to low quadword of xmm1
//xmm1 = {res.z, res.w, [not used], [not used]}
movhlps xmm1, xmm0
end;
{-------------------------------------
Dot product of two vectors using SSE instruction
-------------------------------------
res = v1.x * v2.x +
v1.y * v2.y +
v1.z * v2.z
-------------------------------------
input:
For x86-64 architecture
v1 and v2 value will be passed
in xmm0, xmm1, xmm2, xmm3 in following order
xmm0 = [ v1.x, v1.y, (not used), (not used)]
xmm1 = [ v1.z, v1.w, (not used), (not used)]
xmm2 = [ v2.x, v2.y, (not used), (not used)]
xmm3 = [ v2.z, v2.w, (not used), (not used)]
--------------------------------------
output:
result will be stored in xmm0 register with following order
xmm0 = [ dotProd, (not used), (not used), (not used)]
--------------------------------------}
operator * (const v1 : TVector; const v2 : TVector) res : single; assembler;
asm
//this is just to ensure that v1.w = 0.0
//before shuffle
//xmm1 = {v1.z, v1.w, 0, 0}
//after shuffle
//xmm1 = {v1.z, 0, 0, 0}
shufps xmm1, xmm1, 11101000b
//this is just to ensure that v2.w = 0.0
//before shuffle
//xmm3 = {v2.z, v2.w, 0, 0}
//after shuffle
//xmm3 = {v2.z, 0, 0, 0}
shufps xmm3, xmm3, 11101000b
//copy low quadword of xmm1 to high quadword of xmm0
//xmm0 = {v1.x, v1.y, v1.z, 0}
movlhps xmm0, xmm1
//copy low quadword of xmm3 to high quadword of xmm2
//xmm2 = {v2.x, v2.y, v2.z, 0}
movlhps xmm2, xmm3
//multiply xmm0 and xmm2
//xmm0 = {v1.x * v2.x,
// v1.y * v2.y,
// v1.z * v2.z,
// 0}
//xmm0 = {resx, resy, resz, 0}
mulps xmm0, xmm2
//copy high quadword of xmm0 to low quadword of xmm1
//xmm1 = {resz, 0, [not used], [not used]}
movhlps xmm1, xmm0
//xmm0 = {resx, resy, resz, 0}
//xmm1 = {resz, 0, [not used], [not used]}
//add horizontal fields so that
//xmm0 = {resx + resz, resy + 0, [not used], [not used]}
addps xmm0, xmm1
//copy xmm0 to xmm1
//xmm1 = {resx + resz, resy, [not used], [not used]}
movaps xmm1, xmm0
//shuffle so that
//xmm1 = {resy, [not used], [not used], [not used]}
shufps xmm1, xmm0, 0000001b
//xmm0 = {resx + resz, resy, [not used], [not used]}
//xmm1 = {resy, [not used], [not used], [not used]}
//add so that
//xmm0 = {resx + resy + resz, [not used], [not used], [not used]}
addps xmm0, xmm1
end;
{-------------------------------------
Cross product of two vectors
-------------------------------------
res.x := v1.y * v2.z - v1.z * v2.y;
res.y := v1.z * v2.x - v1.x * v2.z;
res.z := v1.x * v2.y - v1.y * v2.x;
res.w := 0;
-------------------------------------
input:
For x86-64 architecture
v1 and v2 value will be passed
in xmm0, xmm1, xmm2, xmm3 in following order
xmm0 = [ v1.x, v1.y, (not used), (not used)]
xmm1 = [ v1.z, v1.w, (not used), (not used)]
xmm2 = [ v2.x, v2.y, (not used), (not used)]
xmm3 = [ v2.z, v2.w, (not used), (not used)]
--------------------------------------
output:
result will be stored in xmm0 register with following order
xmm0 = [ res.x, res.y, res.z, (not used)]
--------------------------------------}
operator ** (const v1 : TVector; const v2 : TVector) : TVector; assembler;
asm
//copy low quadword of xmm1 to high quadword of xmm0
//xmm0 = {v1.x, v1.y, v1.z, v1.w}
movlhps xmm0, xmm1
//copy low quadword of xmm3 to high quadword of xmm2
//xmm2 = {v2.x, v2.y, v2.z, v2.w}
movlhps xmm2, xmm3
//xmm1 = {v1.x, v1.y, v1.z, v1.w}
//xmm4 = {v1.x, v1.y, v1.z, v1.w}
movaps xmm1, xmm0
movaps xmm4, xmm0
//xmm3 = {v2.x, v2.y, v2.z, v2.w}
//xmm5 = {v2.x, v2.y, v2.z, v2.w}
movaps xmm3, xmm2
movaps xmm5, xmm2
//xmm1 = {v1.y, v1.z, v1.x, v1.w}
//xmm4 = {v1.z, v1.x, v1.y, v1.w}
shufps xmm1, xmm0, 11001001b
shufps xmm4, xmm0, 11010010b
//xmm3 = {v2.z, v2.x, v2.y, v2.w}
//xmm5 = {v2.y, v2.z, v2.x, v2.w}
shufps xmm3, xmm2, 11010010b
shufps xmm5, xmm2, 11001001b
//before multiplication
//xmm1 = {v1.y, v1.z, v1.x, v1.w}
//xmm3 = {v2.z, v2.x, v2.y, v2.w}
//after multiplication
//xmm1 = {v1.y * v2.z, v1.z * v2.x, v1.x * v2.y, v1.w * v2.w}
mulps xmm1, xmm3
//before multiplication
//xmm4 = {v1.z, v1.x, v1.y, v1.w}
//xmm5 = {v2.y, v2.z, v2.x, v2.w}
//after multiplication
//xmm4 = {v1.z * v2.y, v1.x * v2.z, v1.y * v2.x, v1.w * v2.w}
mulps xmm4, xmm5
//before subtraction
//xmm1 = {v1.y * v2.z, v1.z * v2.x, v1.x * v2.y, v1.w * v2.w}
//xmm4 = {v1.z * v2.y, v1.x * v2.z, v1.y * v2.x, v1.w * v2.w}
//after subtraction
//xmm1 = {(v1.y * v2.z - v1.z * v2.y) , (v1.z * v2.x - v1.x * v2.z), (v1.x * v2.y - v1.y * v2.x) , 0}
subps xmm1, xmm4
//xmm0 = {(v1.y * v2.z - v1.z * v2.y) , (v1.z * v2.x - v1.x * v2.z), (v1.x * v2.y - v1.y * v2.x) , 0}
movaps xmm0, xmm1
//xmm0 = {(v1.y * v2.z - v1.z * v2.y) , (v1.z * v2.x - v1.x * v2.z), not used, not used}
//xmm1 = {(v1.x * v2.y - v1.y * v2.x) , 0, not used, not used}
movhlps xmm1, xmm0
end;
end.