-
Notifications
You must be signed in to change notification settings - Fork 35
/
ASMVecDistx64.pas
169 lines (131 loc) · 4.42 KB
/
ASMVecDistx64.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
unit ASMVecDistx64;
// ###################################################################
// #### This file is part of the mathematics library project, and is
// #### offered under the licence agreement described on
// #### http://www.mrsoft.org/
// ####
// #### Copyright:(c) 2024, 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.
// ###################################################################
interface
{$I 'mrMath_CPU.inc'}
{$IFDEF x64}
procedure ASMMtxDistanceSqr(dist : PDouble; LineWidthDist : NativeInt; X, Y : PDouble; {$ifdef UNIX}unixxLen{$ELSE}xLen {$ENDIF}, {$ifdef UNIX}unixyLen{$ELSE}yLen{$ENDIF} : NativeInt); {$IFDEF FPC} assembler; {$ENDIF}
procedure ASMMtxDistanceAbs(dist : PDouble; LineWidthDist : NativeInt; X, Y : PDouble; {$ifdef UNIX}unixxLen{$ELSE}xLen {$ENDIF}, {$ifdef UNIX}unixyLen{$ELSE}yLen{$ENDIF} : NativeInt); {$IFDEF FPC} assembler; {$ENDIF}
{$ENDIF}
implementation
{$IFDEF x64}
procedure ASMMtxDistanceSqr(dist : PDouble; LineWidthDist : NativeInt; X, Y : PDouble; {$ifdef UNIX}unixxLen{$ELSE}
xLen {$ENDIF}, {$ifdef UNIX}unixyLen{$ELSE}yLen{$ENDIF} : NativeInt); {$IFDEF FPC} assembler; {$ENDIF}
{$ifdef UNIX}
var xLen, yLen : NativeInt
{$ENDIF}
// rcx: dest, rdx: destlinewidth; r8: x; r9 : y
asm
{$IFDEF UNIX}
// Linux uses a diffrent ABI -> copy over the registers so they meet with winABI
// (note that the 5th and 6th parameter are are on the stack)
// The parameters are passed in the following order:
// RDI, RSI, RDX, RCX -> mov to RCX, RDX, R8, R9
mov xLen, r8;
mov yLen, r9;
mov r8, rdx;
mov r9, rcx;
mov rcx, rdi;
mov rdx, rsi;
{$ENDIF}
mov r11, xLen;
mov rax, yLen;
imul rax, -8;
sub rcx, rax;
sub r9, rax;
@@foriloop:
// load x[i] -> double
movddup xmm0, [r8];
mov r10, rax;
add r10, 16;
// todo unroll loop...
jg @@lastelem;
@@forjloop:
// dist[i, j] = sqr( x[i] - y[j])
movupd xmm1, [r9 + r10 - 16];
subpd xmm1, xmm0;
mulpd xmm1, xmm1;
movupd [rcx + r10 - 16], xmm1;
add r10, 16;
jle @@forjloop;
@@lastelem:
sub r10, 8;
jg @@linend;
// last element
movsd xmm1, [r9 - 8];
subsd xmm1, xmm0;
mulsd xmm1, xmm1;
movsd [rcx - 8], xmm1;
@@linend:
add r8, 8; // sizeof double
add rcx, rdx; // next line
dec r11;
jnz @@foriloop;
end;
const cLocSignBits : Array[0..1] of int64 = ($7FFFFFFFFFFFFFFF, $7FFFFFFFFFFFFFFF);
procedure ASMMtxDistanceAbs(dist : PDouble; LineWidthDist : NativeInt; X, Y : PDouble; xLen, yLen : NativeInt); {$IFDEF FPC} assembler; {$ELSE} register; {$ENDIF}
{$ifdef UNIX}
var xLen, yLen : NativeInt
{$ENDIF}
// rcx: dest, rdx: destlinewidth; r8: x; r9 : y
asm
{$IFDEF UNIX}
// Linux uses a diffrent ABI -> copy over the registers so they meet with winABI
// (note that the 5th and 6th parameter are are on the stack)
// The parameters are passed in the following order:
// RDI, RSI, RDX, RCX -> mov to RCX, RDX, R8, R9
mov xLen, r8;
mov yLen, r9;
mov r8, rdx;
mov r9, rcx;
mov rcx, rdi;
mov rdx, rsi;
{$ENDIF}
mov r11, xLen;
mov rax, yLen;
imul rax, -8;
sub rcx, rax;
movupd xmm2, [rip + cLocSignBits];
sub r9, rax;
@@foriloop:
// load x[i] -> double
movddup xmm0, [r8];
mov r10, rax;
add r10, 16;
// todo unroll loop...
jg @@lastelem;
@@forjloop:
// dist[i, j] = sqr( x[i] - y[j])
movupd xmm1, [r9 + r10 - 16];
subpd xmm1, xmm0;
andpd xmm1, xmm2;
movupd [rcx + r10 - 16], xmm1;
add r10, 16;
jle @@forjloop;
@@lastelem:
sub r10, 8;
jg @@linend;
// last element
movsd xmm1, [r9 - 8];
subsd xmm1, xmm0;
andpd xmm1, xmm2;
movsd [rcx - 8], xmm1;
@@linend:
add r8, 8; // sizeof double
add rcx, rdx; // next line
dec r11;
jnz @@foriloop;
end;
{$ENDIF}
end.