- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 ARM NEON 汇编程序优化 4D(128 位)矩阵 vector 乘法。
如果我将矩阵和 vector 加载到 NEON 寄存器并对其进行转换,我将不会获得很大的性能提升,因为切换到 NEON 寄存器需要 20 个周期。此外,我为每个乘法重新加载矩阵,尽管它没有改变。
有足够的寄存器空间来一次对更多 vector 执行转换。这正在提高性能。
但是..
我想知道如果我在汇编程序中对所有顶点(增加指针)进行循环,这个操作会有多快。但是我刚开始使用 Neon 汇编程序,但不知道该怎么做。有人可以帮助我吗?
我想要实现的目标:
循环的现有 C 版本:
void TransformVertices(ESMatrix* m, GLfloat* vertices, GLfloat* normals, int count)
{
GLfloat* pVertex = vertices;
int i;
// iterate trough vertices only one at a time
for (i = 0; i < count ; i ++)
{
Matrix4Vector4Mul( (float *)m, (float *)pVertex, (float *)pVertex);
pVertex += 4;
}
//LoadMatrix( (const float*) m);
//// two at a time
//for (i = 0; i < count ; i += 2)
//{
// Matrix4Vector4Mul2( (float *)m, (float *)pVertex, (float *)(pVertex + 4));
// pVertex += 8;
//}
}
以下 NEON 版本的代码仅进行一次转换:
void Matrix4Vector4Mul (const float* m, const float* vIn, float* vOut)
{
asm volatile
(
"vldmia %1, {q1-q4 } \n\t"
"vldmia %2, {q5} \n\t"
"vmul.f32 q0, q1, d10[0] \n\t"
"vmla.f32 q0, q2, d10[1] \n\t"
"vmla.f32 q0, q3, d11[0] \n\t"
"vmla.f32 q0, q4, d11[1] \n\t"
"vstmia %0, {q0}"
: // no output
: "r" (vOut), "r" (m), "r" (vIn)
: "memory", "q0", "q1", "q2", "q3", "q4", "q5"
);
}
C 版本的转换:
void Matrix4Vector4Mul (const float* m, const float* vIn, float* vOut)
{
Vertex4D* v1 = (Vertex4D*)vIn;
Vertex4D vOut1;
Vertex4D* l0;
Vertex4D* l1;
Vertex4D* l2;
Vertex4D* l3;
// 4x4 Matrix with members m00 - m33
ESMatrix* m1 = (ESMatrix*)m;
l0 = (Vertex4D*)&m1->m00;
vOut1.x = l0->x * v1->x;
vOut1.y = l0->y * v1->x;
vOut1.z = l0->z * v1->x;
vOut1.w = l0->w * v1->x;
l1 = (Vertex4D*)&m1->m10;
vOut1.x += l1->x * v1->y;
vOut1.y += l1->y * v1->y;
vOut1.z += l1->z * v1->y;
vOut1.w += l1->w * v1->y;
l2 = (Vertex4D*)&m1->m20;
vOut1.x += l2->x * v1->z;
vOut1.y += l2->y * v1->z;
vOut1.z += l2->z * v1->z;
vOut1.w += l2->w * v1->z;
l3 = (Vertex4D*)&m1->m30;
vOut1.x += l3->x * v1->w;
vOut1.y += l3->y * v1->w;
vOut1.z += l3->z * v1->w;
vOut1.w += l3->w * v1->w;
*(vOut) = vOut1.x;
*(vOut + 1) = vOut1.y;
*(vOut + 2) = vOut1.z;
*(vOut + 3) = vOut1.w;
}
性能:(转换 > 90 000 个顶点 | Android 4.0.4 SGS II)
C-Version: 190 FPS
NEON-Version: 162 FPS ( .. slower -.- )
--- LOAD Matrix only ONCE (seperate ASM) and then perform two V's at a time ---
NEON-Version: 217 FPS ( + 33 % NEON | + 14 % C-Code )
最佳答案
您是否尝试过使用编译器标志?
-mcpu=cortex-a9 -mtune=cortex-a9 -mfloat-abi=softfp -mfpu=neon -O3
在这种情况下对我来说做得很好(gcc 4.4.3,随 Android NDK 8b 一起分发)。尝试通过定义内部函数静态和内联以及将矩阵(m[X][0] 东西)移动到静态全局变量或只是将 Matrix4Vector4Mul 合并到循环中并创建矩阵局部变量而不是在函数中传递它来尝试拥有紧凑的源代码- gcc 在那里并不聪明。
当我这样做时,我得到主循环的下方。
a4: ed567a03 vldr s15, [r6, #-12]
a8: ee276aa0 vmul.f32 s12, s15, s1
ac: ee676aa8 vmul.f32 s13, s15, s17
b0: ed564a04 vldr s9, [r6, #-16]
b4: ee277a88 vmul.f32 s14, s15, s16
b8: ed165a02 vldr s10, [r6, #-8]
bc: ee677a80 vmul.f32 s15, s15, s0
c0: ed565a01 vldr s11, [r6, #-4]
c4: e2833001 add r3, r3, #1
c8: ee046a89 vmla.f32 s12, s9, s18
cc: e1530004 cmp r3, r4
d0: ee446aaa vmla.f32 s13, s9, s21
d4: ee047a8a vmla.f32 s14, s9, s20
d8: ee447aa9 vmla.f32 s15, s9, s19
dc: ee056a22 vmla.f32 s12, s10, s5
e0: ee456a01 vmla.f32 s13, s10, s2
e4: ee057a21 vmla.f32 s14, s10, s3
e8: ee457a02 vmla.f32 s15, s10, s4
ec: ee056a8b vmla.f32 s12, s11, s22
f0: ee456a83 vmla.f32 s13, s11, s6
f4: ee057aa3 vmla.f32 s14, s11, s7
f8: ee457a84 vmla.f32 s15, s11, s8
fc: ed066a01 vstr s12, [r6, #-4]
100: ed466a04 vstr s13, [r6, #-16]
104: ed067a03 vstr s14, [r6, #-12]
108: ed467a02 vstr s15, [r6, #-8]
10c: e2866010 add r6, r6, #16
110: 1affffe3 bne a4 <TransformVertices+0xa4>
有 4 个加载、4 个乘法、12 个乘法和累加以及 4 个存储,这与您在 Matrix4Vector4Mul 中所做的相匹配。
如果您对编译器生成的代码仍然不满意,请传递编译器“-S”以获取程序集输出并将其用作进一步改进的起点,而不是从头开始。
您还应该检查 vertices
缓存行大小是否对齐(Cortex-A9 为 32 字节)以获得良好的数据流。
对于矢量化,有 gcc 选项,例如 -ftree-vectorizer-verbose=9
来打印矢量化的信息。还可以在 gcc 文档 this one 中搜索以了解如何引导 gcc 或需要修改哪些内容才能使乘法矢量化。这听起来可能需要深入研究很多,但从长远来看,它比“手动矢量化”更有成效。
关于android - 如何使用 ARM NEON 优化循环 4D 矩阵 vector 乘法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12976539/
将 ARM 处理器模式与 x86 操作模式(ring0 到 ring 3)进行比较,用户模式看起来就像 ring3,用户空间程序在其中运行。 但是,我无法将 ring0 与系统模式或主管模式联系起来。
为什么我们在 ARM 架构中有暂存寄存器?处理器如何使用它,我的意思是这个寄存器的用途是什么? 最佳答案 来自 Procedure Call Standard for the Arm Architec
我了解弱内存模型和强内存模型的基本区别。但是没有确切的弱定义,它取决于体系结构(这里是 ARM)。 我已经阅读了有关 ARM 信息中心的文档,但仍有很多内容不清楚。有人可以列出 - ARM 保证哪些内
我想在 arm 9 上分析我的代码,是否有任何分析器可以给我函数调用时间和每个函数占用的总周期?我更喜欢任何免费的分析器。我喜欢在 Linux 中使用 kcachegrind。 最佳答案 我不知道有什
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 7 年前。 Improve this qu
众所周知,对于X86架构:按下电源按钮后,机器开始执行0xFFFFFFF0处的代码,然后开始执行BIOS中的代码以进行硬件初始化。 BIOS 执行后,它使用引导加载程序将操作系统镜像加载到内存中。最后
我有 rootfs 和 klibc 文件系统。我正在创建 make 规则,而一些开发人员的编译器较旧,但没有联网。note1 我正在尝试验证所有文件都是使用 arm 仅当检测到某个版本的编译器时。我已
在部署实际应用程序之前,我们使用 ARM 模板部署 Azure 资源,作为构建过程的一部分。 到目前为止,我们所有的应用程序资源都自包含在资源组中。例如需要 SQL Server 和存储帐户的 Web
为什么 ARM Controller 在发生异常时要从 THUMB 状态返回到 ARM 状态? 最佳答案 一种解释可能是 ARM 模式是 CPU 的“ native ”操作模式,与有限的 Thumb
我正在尝试反转 128 位向量 (uint16x8) 的顺序。 例如,如果我有 a b c d e f g h 我想获得 h g f e d c b a 有没有一种简单的方法可以使用 NEON 内在函
有很多关于内存屏障的信息。大多数信息是指多核或多处理器架构。 Stackoverflow 上的某个地方还指出,单核处理器不需要内存屏障。 到目前为止,我找不到任何明确的解释,为什么单核 CPU 上不需
我想在 ARM Cortex A8 处理器上移植一小段代码。 L1 缓存和 L2 缓存都非常有限。我的程序中有 3 个数组。其中两个是顺序访问的(大小> 数组 A:6MB 和数组 B:3MB),第三个
我无法弄清楚这个 ARM 指令是做什么的: strd.w r0, r1, [r2] 我知道这是一个存储指令,它在 *r2 中存储了一些东西。但我不完全确定是什么。为什么有两个源寄存器
我很好奇为什么有些 ARM 指令(如 MUL 和 ADD)不使用桶形移位器。我想知道极限背后的理性。谢谢! 最佳答案 并不是没有使用桶形移位器;这是您无法指定它在非常具体的指令(数据处理和加载/存储)
我需要计算与 SSE 相同的操作: __m128i result1=_mm_avg_epu8 (upper, lower); 使用 NEON,我执行以下操作: uint8x16_t result1=v
我正在尝试使用 PLD 指令。我面临的问题如下: int32_t addr[10]; asm ("PLD [addr,#5]"); 我收到以下错误: Error: ARM register expec
根据 ARM 手册,应该可以访问特定 CPU 模式的存储寄存器,例如“r13_svc”。当我尝试执行此操作时,gcc 对我大喊大叫,并显示以下错误: 立即表达式需要 # 前缀 -- `mov r2,s
我正在使用 mbxxx 目标开发 Contiki 2.7。在构建我的代码时,链接器提示 .ARM.exidx 和 .data 部分的重叠 .在修改了链接器脚本 contiki-2.7/cpu/stm3
如何确定给定 ARM 处理器上是否存在 NEON 引擎?可以为此目的查询任何状态/标志寄存器吗? 最佳答案 我相信unixsmurf's answer如果使用具有特权内核的操作系统,这将与您获得的一样
如何在设备上分析我的 ARM 代码。 这是涉及 USB 和 SDH 处理的裸机代码,我看到了这个 Code Profiler for ARM但似乎很 slim ,我很熟悉DS5但如果您使用基于 lin
我是一名优秀的程序员,十分优秀!