此矩阵乘法代码的正确ARMv7至ARMv8 NEON端口是什么?

时间:2019-03-08 16:39:25

标签: assembly simd neon armv8

    // http://infocenter.arm.com/help/topic/com.arm.doc.dai0425/DAI0425_migrating_an_application_from_ARMv5_to_ARMv7_AR.pdf
// p. 4-21

.macro mul_col_f32 res_q, col0_d, col1_d
vmul.f32 \res_q, q8, \col0_d[0] @ multiply col element 0 by matrix col 0
vmla.f32 \res_q, q9, \col0_d[1] @ multiply-acc col element 1 by matrix col 1
vmla.f32 \res_q, q10, \col1_d[0] @ multiply-acc col element 2 by matrix col 2
vmla.f32 \res_q, q11, \col1_d[1] @ multiply-acc col element 3 by matrix col 3
.endm

// http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.100748_0606_00_en/lmi1470147220260.html
// http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0203j/Cacjfjei.html

.globl  mat44mulneon
.p2align 2 // what's this ?
.type mat44mulneon,%function
mat44mulneon:
.fnstart // not recognized by eclipse syntax coloring?
// ---------
vld1.32 {d16-d19}, [r1]! @ load first eight elements of matrix 0
vld1.32 {d20-d23}, [r1]! @ load second eight elements of matrix 0
vld1.32 {d0-d3}, [r2]! @ load first eight elements of matrix 1.
vld1.32 {d4-d7}, [r2]! @ load second eight elements of matrix 1.
mul_col_f32 q12, d0, d1 @ matrix 0 * matrix 1 col 0
mul_col_f32 q13, d2, d3 @ matrix 0 * matrix 1 col 1
mul_col_f32 q14, d4, d5 @ matrix 0 * matrix 1 col 2
mul_col_f32 q15, d6, d7 @ matrix 0 * matrix 1 col 3
vst1.32 {d24-d27}, [r0]! @ store first eight elements of result.
vst1.32 {d28-d31}, [r0]! @ store second eight elements of result.
// ---------
bx lr // Return by branching to the address in the link register.
.fnend

我在ARM站点上找到的上面的代码(请参见注释中的链接)可在我的ARM Cortex A9机器(即ARMv7机器)上使用。

我现在正在尝试使其在ARMv8 / aarch64 CPU上运行。 我找到了这张幻灯片: porting to ARM64

最后,它显示了矩阵乘法代码。但是它使用循环,我猜(如果我没看错,请纠正我)如果移植到新的ARMv8助记符,我发布的代码会更快。 链接的文档还显示了v7-> v8的一些更改,例如我将vmul.32之类的内容更改为fmul等。示例中给出的寄存器名称与上面发布的代码中的名称不匹配。由于我不太熟练使用任何ARM组件,因此我不知道它的等效语言。 例如。当我构建项目时,出现类似以下错误:

operand 1 must be a SIMD vector register list -- `st1 {d24-d27},[r0]

不过,我不确定这是否是唯一的问题,所以我宁愿问: 要在aarch64机器上运行的代码需要做哪些更改?

1 个答案:

答案 0 :(得分:1)

这是该例程的粗略AArch64版本:

.macro mul_col_f32 res, col
    fmul \res, v16.4s, \col[0] // multiply col element 0 by matrix col 0
    fmla \res, v17.4s, \col[1] // multiply-acc col element 1 by matrix col 1
    fmla \res, v18.4s, \col[2] // multiply-acc col element 2 by matrix col 2
    fmla \res, v19.4s, \col[3] // multiply-acc col element 3 by matrix col 3
.endm

.globl  mat44mulneon
mat44mulneon:
    ld1 {v16.4s, v17.4s, v18.4s, v19.4s}, [x1] 
    ld1 {v0.4s,  v1.4s,  v2.4s,  v3.4s},  [x2] 
    mul_col_f32 v24.4s, v0.s // matrix 0 * matrix 1 col 0
    mul_col_f32 v25.4s, v1.s // matrix 0 * matrix 1 col 1
    mul_col_f32 v26.4s, v2.s // matrix 0 * matrix 1 col 2
    mul_col_f32 v27.4s, v3.s // matrix 0 * matrix 1 col 3
    st1 {v24.4s, v25.4s, v26.4s, v27.4s}, [x0] 
ret

关于转换的一些非综合性注释,以及链接的演示文稿中提到的一般性内容:

  • 使用一条ld1指令最多可以加载64个字节,而AArch32中使用vld1可以加载32个字节。这样避免了增加r0 / r1 / r2或x0 / x1 / x2指针的需要
  • 我遗漏了特定于操作系统/二进制格式的.fnstart.fnend.type,如果需要,可以在原始版本的相同位置读取它们
  • 对于AArch64程序集,@不再是注释字符
  • col的{​​{1}}参数的形式为mul_col_f32,与v0.s相反。选择特定的车道时,将其与宏中的v0.4s后缀串联后,应省略车道数,例如要选择[0]寄存器的第一个通道,应将其写为v0.4s。 GNU汇编器确实允许v0.s[0],但是其他汇编器(包括Clang / LLVM内置汇编器和Microsoft的armasm64)仅允许使用前一种语法。