30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
@implementation OFMatrix4x4
#if defined(OF_AMD64) || defined(OF_X86)
# ifndef __clang__
# pragma GCC push_options
# pragma GCC target("3dnow")
# endif
static void
multiplyWithMatrix_3DNow(OFMatrix4x4 *self, SEL _cmd, OFMatrix4x4 *matrix)
{
float result[4][4];
for (uint_fast8_t i = 0; i < 4; i++) {
for (uint_fast8_t j = 0; j < 4; j++) {
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
|
@implementation OFMatrix4x4
#if defined(OF_AMD64) || defined(OF_X86)
# ifndef __clang__
# pragma GCC push_options
# pragma GCC target("3dnow")
# endif
static void
multiplyWithMatrix_enhanced3DNow(OFMatrix4x4 *self, SEL _cmd,
OFMatrix4x4 *matrix)
{
float result[4][4];
for (uint_fast8_t i = 0; i < 4; i++) {
for (uint_fast8_t j = 0; j < 4; j++) {
__asm__ (
"movd (%2), %%mm0\n\t"
"punpckldq 16(%2), %%mm0\n\t"
"pfmul (%1), %%mm0\n\t"
"movd 32(%2), %%mm1\n\t"
"punpckldq 48(%2), %%mm1\n\t"
"pfmul 8(%1), %%mm1\n\t"
"pfadd %%mm1, %%mm0\n\t"
"pswapd %%mm0, %%mm1\n\t"
"pfadd %%mm1, %%mm0\n\t"
"movd %%mm0, %0"
:: "m"(result[i][j]), "r"(&matrix->_values[i][0]),
"r"(&self->_values[0][j])
: "mm0", "mm1", "memory"
);
}
}
__asm__ ("femms");
memcpy(self->_values, result, sizeof(result));
}
static void
multiplyWithMatrix_3DNow(OFMatrix4x4 *self, SEL _cmd, OFMatrix4x4 *matrix)
{
float result[4][4];
for (uint_fast8_t i = 0; i < 4; i++) {
for (uint_fast8_t j = 0; j < 4; j++) {
|
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
}
}
__asm__ ("femms");
memcpy(self->_values, result, sizeof(result));
}
static OFVector4D
transformedVector_3DNow(OFMatrix4x4 *self, SEL _cmd, OFVector4D vector)
{
OFVector4D result;
__asm__ (
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
|
}
}
__asm__ ("femms");
memcpy(self->_values, result, sizeof(result));
}
static OFVector4D
transformedVector_enhanced3DNow(OFMatrix4x4 *self, SEL _cmd, OFVector4D vector)
{
OFVector4D result;
__asm__ (
"movq (%2), %%mm0\n\t"
"movq 8(%2), %%mm1\n"
"\n\t"
"movq %%mm0, %%mm2\n\t"
"movq %%mm1, %%mm3\n\t"
"pfmul (%1), %%mm2\n\t"
"pfmul 8(%1), %%mm3\n\t"
"pfadd %%mm3, %%mm2\n\t"
"pswapd %%mm2, %%mm3\n\t"
"pfadd %%mm3, %%mm2\n"
"\n\t"
"movq %%mm0, %%mm3\n\t"
"movq %%mm1, %%mm4\n\t"
"pfmul 16(%1), %%mm3\n\t"
"pfmul 24(%1), %%mm4\n\t"
"pfadd %%mm4, %%mm3\n\t"
"pswapd %%mm3, %%mm4\n\t"
"pfadd %%mm4, %%mm3\n"
"\n\t"
"punpckldq %%mm3, %%mm2\n\t"
"movq %%mm2, (%0)\n"
"\n\t"
"movq %%mm0, %%mm2\n\t"
"movq %%mm1, %%mm3\n\t"
"pfmul 32(%1), %%mm2\n\t"
"pfmul 40(%1), %%mm3\n\t"
"pfadd %%mm3, %%mm2\n\t"
"pswapd %%mm2, %%mm3\n\t"
"pfadd %%mm3, %%mm2\n"
"\n\t"
"pfmul 48(%1), %%mm0\n\t"
"pfmul 56(%1), %%mm1\n\t"
"pfadd %%mm1, %%mm0\n\t"
"pswapd %%mm0, %%mm1\n\t"
"pfadd %%mm1, %%mm0\n"
"\n\t"
"punpckldq %%mm0, %%mm2\n\t"
"movq %%mm2, 8(%0)\n"
"\n\t"
"femms"
:: "r"(&result), "r"(&self->_values), "r"(&vector)
: "mm0", "mm1", "mm2", "mm3", "mm4", "memory"
);
return result;
}
static OFVector4D
transformedVector_3DNow(OFMatrix4x4 *self, SEL _cmd, OFVector4D vector)
{
OFVector4D result;
__asm__ (
|
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
|
}
# ifndef __clang__
# pragma GCC pop_options
# endif
+ (void)initialize
{
if (self != [OFMatrix4x4 class])
return;
if ([OFSystemInfo supports3DNow]) {
SEL selector;
const char *typeEncoding;
selector = @selector(multiplyWithMatrix:);
typeEncoding = method_getTypeEncoding(
class_getInstanceMethod(self, selector));
class_replaceMethod(self, selector,
(IMP)multiplyWithMatrix_3DNow, typeEncoding);
selector = @selector(transformedVector:);
typeEncoding = method_getTypeEncoding(
class_getInstanceMethod(self, selector));
class_replaceMethod(self, selector,
(IMP)transformedVector_3DNow, typeEncoding);
}
}
#endif
+ (OFMatrix4x4 *)identityMatrix
{
return [[[OFMatrix4x4 alloc]
initWithValues: identityValues] autorelease];
|
>
>
<
<
<
|
<
|
|
|
<
>
>
>
|
<
|
>
|
>
|
>
>
|
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
|
}
# ifndef __clang__
# pragma GCC pop_options
# endif
+ (void)initialize
{
const char *typeEncoding;
if (self != [OFMatrix4x4 class])
return;
# define REPLACE(selector, func) \
typeEncoding = method_getTypeEncoding( \
class_getInstanceMethod(self, selector)); \
class_replaceMethod(self, selector, (IMP)func, typeEncoding);
if ([OFSystemInfo supportsEnhanced3DNow]) {
REPLACE(@selector(multiplyWithMatrix:),
multiplyWithMatrix_enhanced3DNow)
REPLACE(@selector(transformedVector:),
transformedVector_enhanced3DNow)
} else if ([OFSystemInfo supports3DNow]) {
REPLACE(@selector(multiplyWithMatrix:),
multiplyWithMatrix_3DNow)
REPLACE(@selector(transformedVector:), transformedVector_3DNow)
}
# undef REPLACE
}
#endif
+ (OFMatrix4x4 *)identityMatrix
{
return [[[OFMatrix4x4 alloc]
initWithValues: identityValues] autorelease];
|