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
|
#ifndef _included_mmx_math_h_
#define _included_mmx_math_h_
#if SUPPORT_MMX
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/*
Calling-convention independent
definitions of inline MMX assembler
functions and declarations for non-
inline MMX assembler functions
*/
/* SPECIFICATION */
/*
Dot Product and Vector Transform functions take
arguments referencing matrices or vectors whose
elements are 32 bit signed integers and arranged as
follows. All integers (including the results) are
in 16.16 fixed point form - ie. The 64-bit results
are shifted down 16 bits (divided by 65536) before
being written back as 32-bit values. Results are
rounded down (towards negative infinity).
the matrix structure looks like this (not ideal!)
[ +00 +0c +18 ]
[ +04 +10 +1c ]
[ +08 +14 +20 ]
and the vector structure looks like this
[ +00 ]
[ +04 ]
[ +08 ]
*/
/* TYPICAL CHARACTERISTICS */
/*
Accuracy
Internal rounding errors may be propogated, and
the results may not be exact. For the Dot Product
result and the Vector Transform results (x,y and z
independently), the error distributions are all
the same, as follows:
Exact: 25%
-1: 50%
-2: 25%
Better accuracy can be obtained by adding 1 to each integer result,
but this will produce poor results in the case of nice simple round
numbers, eg Dot({1.0,0.0,0.0},{0.0,1.0,0.0}) gives 1 not 0!
Speed
The DotProduct Takes 33 cycles (not including call instruction)
The inline DotProduct takes 30+1 cycles (the last instruction is pairable)
All Vector transforms take 63 cycles. These figures assume no
stalls due to cache misses or misaligned data. A matrix multiply
or cross product could be supplied if it is thought they would
be necessary
For optimal performance, it is recommended that vector and
matrix structures should be aligned to EIGHT byte boundaries.
To ensure this in arrays of vectors/matrices, the structure
should contain a dummy padding 32-bit value (recommended).
*/
/* forward reference declared in global scope */
struct vectorch;
struct matrixch;
/***********************/
/* F-U-N-C-T-I-O-N */
/* P-R-O-T-O-T-Y-P-E-S */
/* F-O-R A-L-L */
/* P-U-B-L-I-C */
/* F-U-N-C-T-I-O-N-S */
/***********************/
/* overwrites the input vector with the new vector */
_asmcall void MMX_VectorTransform(struct vectorch * vector, struct matrixch const * matrix);
/* fills a new vector with the result of the input vector transformed by the matrix */
_asmcall void MMX_VectorTransformed(struct vectorch * v_result, struct vectorch const * v_parm, struct matrixch const * matrix);
/* overwrites the input vector with the new vector, then adds another vector */
_asmcall void MMX_VectorTransformAndAdd(struct vectorch * vector, struct matrixch const * matrix, struct vectorch const * v_add);
/* fills a new vector with the result of the input vector transformed by the matrix then added to another vector */
_asmcall void MMX_VectorTransformedAndAdd(struct vectorch * v_result, struct vectorch const * v_parm, struct matrixch const * matrix, struct vectorch const * v_add);
/* compute dot product */
_asmcall signed MMX_VectorDot(struct vectorch const * v1, struct vectorch const * v2);
/* this one assumes all the input vector elements are in the range [-32768,32767] */
_asmcall signed MMX_VectorDot16(struct vectorch const * v1, struct vectorch const * v2);
/* inline versions */
_asminline signed MMXInline_VectorDot(struct vectorch const * v1, struct vectorch const * v2);
_asminline signed MMXInline_VectorDot16(struct vectorch const * v1, struct vectorch const * v2);
/*****************/
/* PRIVATE PARTS */
/*****************/
/* Assembler labels */
extern void MMXAsm_VectorTransform(void);
extern void MMXAsm_VectorTransformed(void);
extern void MMXAsm_VectorTransformAndAdd(void);
extern void MMXAsm_VectorTransformedAndAdd(void);
extern void MMXAsm_VectorDot(void);
extern void MMXAsm_VectorDot16(void);
/* inline calls to MMX functions with correct parameters set */
#ifdef __WATCOMC__
#pragma aux MMX_VectorTransform = "call MMXAsm_VectorTransform" parm [eax] [edx];
#pragma aux MMX_VectorTransformed = "call MMXAsm_VectorTransformed" parm [eax] [edx] [ecx];
#pragma aux MMX_VectorTransformAndAdd = "call MMXAsm_VectorTransformAndAdd" parm [eax] [edx] [ecx];
#pragma aux MMX_VectorTransformedAndAdd = "call MMXAsm_VectorTransformedAndAdd" parm [eax] [edx] [ecx] [ebx];
#pragma aux MMX_VectorDot = "call MMXAsm_VectorDot" parm [eax] [edx] value [eax];
#pragma aux MMX_VectorDot16 = "call MMXAsm_VectorDot16" parm [eax] [edx] value [eax];
#elif defined(_MSC_VER)
_asmcall void MMX_VectorTransform(struct vectorch * vector, struct matrixch const * matrix)
{
_asm
{
mov eax,vector
mov edx,matrix
call MMXAsm_VectorTransform
}
}
_asmcall void MMX_VectorTransformed(struct vectorch * v_result, struct vectorch const * v_parm, struct matrixch const * matrix)
{
_asm
{
mov eax,v_result
mov edx,v_parm
mov ecx,matrix
call MMXAsm_VectorTransformed
}
}
_asmcall void MMX_VectorTransformAndAdd(struct vectorch * vector, struct matrixch const * matrix, struct vectorch const * v_add)
{
_asm
{
mov eax,vector
mov edx,matrix
mov ecx,v_add
call MMXAsm_VectorTransformAndAdd
}
}
_asmcall void MMX_VectorTransformedAndAdd(struct vectorch * v_result, struct vectorch const * v_parm, struct matrixch const * matrix, struct vectorch const * v_add)
{
_asm
{
mov eax,v_result
mov edx,v_parm
mov ecx,matrix
mov ebx,v_add
call MMXAsm_VectorTransformedAndAdd
}
}
_asmcall signed MMX_VectorDot(struct vectorch const * v1, struct vectorch const * v2)
{
signed retval;
_asm
{
mov eax,v1
mov edx,v2
call MMXAsm_VectorDot
mov retval,eax
}
return retval;
}
_asmcall signed MMX_VectorDot16(struct vectorch const * v1, struct vectorch const * v2)
{
signed retval;
_asm
{
mov eax,v1
mov edx,v2
call MMXAsm_VectorDot16
mov retval,eax
}
return retval;
}
#else
/* #error "Unknown compiler" */
void MMX_VectorTransform(struct vectorch * vector, struct matrixch const * matrix);
void MMX_VectorTransformed(struct vectorch * v_result, struct vectorch const * v_parm, struct matrixch const * matrix);
void MMX_VectorTransformAndAdd(struct vectorch * vector, struct matrixch const * matrix, struct vectorch const * v_add);
void MMX_VectorTransformedAndAdd(struct vectorch * v_result, struct vectorch const * v_parm, struct matrixch const * matrix, struct vectorch const * v_add);
int MMX_VectorDot(struct vectorch const * v1, struct vectorch const * v2);
int MMX_VectorDot16(struct vectorch const * v1, struct vectorch const * v2);
#endif
/* Cross product? Mod? MatrixMultiply? */
/* globals */
extern int use_mmx_math;
/* inline functions - no call */
extern __int64 const mmx_sign_mask;
extern __int64 const mmx_one_fixed_h;
#ifdef __WATCOMC__
#pragma aux MMXInline_VectorDot = \
\
" movq mm0,[edx]" \
\
" movd mm2,[edx+08h]" \
" movq mm4,mm0" \
\
" pand mm4,mmx_sign_mask" \
" movq mm6,mm2" \
\
" movq mm1,[eax]" \
" paddd mm4,mm4" \
\
" movd mm3,[eax+08h]" \
" movq mm5,mm1" \
\
" pand mm6,mmx_sign_mask" \
" movq mm7,mm3" \
\
" pand mm5,mmx_sign_mask" \
" paddd mm6,mm6" \
\
" pand mm7,mmx_sign_mask" \
" paddd mm5,mm5" \
\
" paddd mm0,mm4" \
" paddd mm2,mm6" \
\
" paddd mm7,mm7" \
" movq mm4,mm2" \
\
" punpcklwd mm4,mm0" \
" paddd mm1,mm5" \
\
" punpckhwd mm2,mm0" \
" paddd mm3,mm7" \
\
" movq mm5,mm3" \
" punpckhwd mm3,mm1" \
\
" punpcklwd mm5,mm1" \
" movq mm0,mm2" \
\
" movq mm1,mm4" \
" pmaddwd mm0,mm3" \
\
" movq mm6,mm3" \
" psrlq mm3,32" \
\
" movq mm7,mm5" \
" punpckldq mm3,mm6" \
\
" pmaddwd mm1,mm5" \
" psrlq mm5,32" \
\
" punpckldq mm5,mm7" \
" pmaddwd mm2,mm3" \
\
" pmaddwd mm4,mm5" \
" movq mm3,mm0" \
\
" punpckldq mm0,mm1" \
\
" psubd mm0,mmx_one_fixed_h" \
" punpckhdq mm1,mm3" \
\
" psrad mm0,16" \
" paddd mm2,mm4" \
\
" pslld mm1,16" \
" paddd mm2,mm0" \
\
" paddd mm2,mm1" \
\
" movq mm1,mm2" \
" psrlq mm2,32" \
\
" paddd mm1,mm2" \
\
" movd eax,mm1" \
\
" emms" \
\
" inc eax" \
\
parm [eax] [edx] value [eax];
#pragma aux MMXInline_VectorDot16 = \
\
" movd mm0,[edx+08h]" \
\
" packssdw mm0,[edx]" \
\
" movd mm1,[eax+08h]" \
\
" packssdw mm1,[eax]" \
\
" pmaddwd mm0,mm1" \
\
" movq mm1,mm0" \
" psrlq mm0,32" \
\
" paddd mm0,mm1" \
\
" movd eax,mm0" \
\
" emms" \
\
parm [eax] [edx] value [eax];
#elif defined(_MSC_VER)
_asminline signed MMXInline_VectorDot(struct vectorch const * v1, struct vectorch const * v2)
{
signed retval;
_asm
{
mov edx,v1
mov eax,v2
movq mm0,[edx]
movd mm2,[edx+08h]
movq mm4,mm0
pand mm4,mmx_sign_mask
movq mm6,mm2
movq mm1,[eax]
paddd mm4,mm4
movd mm3,[eax+08h]
movq mm5,mm1
pand mm6,mmx_sign_mask
movq mm7,mm3
pand mm5,mmx_sign_mask
paddd mm6,mm6
pand mm7,mmx_sign_mask
paddd mm5,mm5
paddd mm0,mm4
paddd mm2,mm6
paddd mm7,mm7
movq mm4,mm2
punpcklwd mm4,mm0
paddd mm1,mm5
punpckhwd mm2,mm0
paddd mm3,mm7
movq mm5,mm3
punpckhwd mm3,mm1
punpcklwd mm5,mm1
movq mm0,mm2
movq mm1,mm4
pmaddwd mm0,mm3
movq mm6,mm3
psrlq mm3,32
movq mm7,mm5
punpckldq mm3,mm6
pmaddwd mm1,mm5
psrlq mm5,32
punpckldq mm5,mm7
pmaddwd mm2,mm3
pmaddwd mm4,mm5
movq mm3,mm0
punpckldq mm0,mm1
psubd mm0,mmx_one_fixed_h
punpckhdq mm1,mm3
psrad mm0,16
paddd mm2,mm4
pslld mm1,16
paddd mm2,mm0
paddd mm2,mm1
movq mm1,mm2
psrlq mm2,32
paddd mm1,mm2
movd retval,mm1
emms
}
return retval+1;
}
_asminline signed MMXInline_VectorDot16(struct vectorch const * v1, struct vectorch const * v2)
{
signed retval;
_asm
{
mov eax,v1
mov edx,v2
movd mm0,[edx+08h]
packssdw mm0,[edx]
movd mm1,[eax+08h]
packssdw mm1,[eax]
pmaddwd mm0,mm1
movq mm1,mm0
psrlq mm0,32
paddd mm0,mm1
movd retval,mm0
emms
}
return retval;
}
#else
/* #error "Unknown compiler" */
int MMXInline_VectorDot(struct vectorch const * v1, struct vectorch const * v2);
int MMXInline_VectorDot16(struct vectorch const * v1, struct vectorch const * v2);
#endif
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* SUPPORT_MMX */
#endif /* ! _included_mmx_math_h_ */
|