问题描述
我试图在 ARM 中击败 Gcc 编译器,我花了很多时间,但似乎我是否编译了用编写的代码- o3 标志比在我的代码中使用 NEON内在更快。 实际上,此代码是具有c ++代码的 gaussianBlur 过滤器:
inline void GaussianBlur_5x5_row(const float __restrict_arr *in,float __restrict_arr *out,const unsigned int cols)
{
//Left columns
out[0] = (in[0]+in[2])*0.054488685f + (in[0]+in[1])*0.24420135f + in[0]*0.40261996f;
out[1] = (in[0]+in[3])*0.054488685f + (in[0]+in[2])*0.24420135f + in[1]*0.40261996f;
//Middle columns (Tiled)
for (unsigned int j=2; j<cols-2; j+=2)
{
out[j ] = (in[j-2]+in[j+2])*0.054488685f + (in[j-1]+in[j+1])*0.24420135f + in[j ]*0.40261996f;
out[j+1] = (in[j-1]+in[j+3])*0.054488685f + (in[j ]+in[j+2])*0.24420135f + in[j+1]*0.40261996f;
}
//Right columns
out[cols-2] = (in[cols-4]+in[cols-1])*0.054488685f + (in[cols-3]+in[cols-1])*0.24420135f + in[cols-2]*0.40261996f;
out[cols-1] = (in[cols-3]+in[cols-1])*0.054488685f + (in[cols-2]+in[cols-1])*0.24420135f + in[cols-1]*0.40261996f;
}
void GaussianBlur_5x5(const Matrixf &input,Matrixf &output)
{
//sigma is 1.0f
//kernels prevIoUsly calculated from MATLAB code
//border type is replicate
const unsigned int rows = input.rows;
const unsigned int cols = input.cols;
output.resize(rows,cols);
float buffer[cols] __attribute__((aligned(16)));
//Top rows
#pragma omp parallel for private(buffer)
for (unsigned int i=0; i<2; i++)
{
for (unsigned int j=0; j<cols; j++)
buffer[j] = (input.val[0][j]+input.val[i+2][j])*0.054488685f + (input.val[0][j]+input.val[i+1][j])*0.24420135f + input.val[i][j]*0.40261996f;
GaussianBlur_5x5_row(buffer,output.val[i],cols);
}
//Middle rows
#pragma omp parallel for private(buffer) schedule(runtime)
for (unsigned int i=2; i<rows-2; i++)
{
for (unsigned int j=0; j<cols; j++)
buffer[j] = (input.val[i-2][j]+input.val[i+2][j])*0.054488685f + (input.val[i-1][j]+input.val[i+1][j])*0.24420135f + input.val[i][j]*0.40261996f;
GaussianBlur_5x5_row(buffer,cols);
}
//Bottom rows
#pragma omp parallel for private(buffer)
for (unsigned int i=rows-2; i<rows; i++)
{
for (unsigned int j=0; j<cols; j++)
buffer[j] = (input.val[i-2][j]+input.val[rows-1][j])*0.054488685f + (input.val[i-1][j]+input.val[rows-1][j])*0.24420135f + input.val[i][j]*0.40261996f;
GaussianBlur_5x5_row(buffer,cols);
}
}
#include <arm_neon.h>
inline void GaussianBlur5x5_row_rc( const float __restrict_arr *in,const unsigned int cols)
{
//Left columns
out[0] = (in[0]+in[2])*0.054488685f + (in[0]+in[1])*0.24420135f + in[0]*0.40261996f;
out[1] = (in[0]+in[3])*0.054488685f + (in[0]+in[2])*0.24420135f + in[1]*0.40261996f;
const float32x4_t coef_1 = { 0.054488685f,0.122100675f,0.20130998f,0.122100675f };
const float32x4_t coef_2 = { 0.122100675f,0.054488685f };
//Middle columns
for (unsigned int j=2; j<cols-2; j+=2)
{
out[j ] = (in[j-2]+in[j+2])*0.054488685f + (in[j-1]+in[j+1])*0.24420135f + in[j ]*0.40261996f;
out[j+1] = (in[j-1]+in[j+3])*0.054488685f + (in[j ]+in[j+2])*0.24420135f + in[j+1]*0.40261996f
}
//Right columns
out[cols-2] = (in[cols-4]+in[cols-1])*0.054488685f + (in[cols-3]+in[cols-1])*0.24420135f + in[cols-2]*0.40261996f;
out[cols-1] = (in[cols-3]+in[cols-1])*0.054488685f + (in[cols-2]+in[cols-1])*0.24420135f + in[cols-1]*0.40261996f;
}
void GaussianBlur5x5_NEON_rc( const Matrixf &input,cols);
float buffer[cols] __attribute__((aligned(16)));
//Top rows
#pragma omp parallel for private(buffer)
for (unsigned int i=0; i<2; i++)
{
for (unsigned int j=0; j<cols; j++)
buffer[j] = (input.val[0][j]+input.val[i+2][j])*0.054488685f + (input.val[0][j]+input.val[i+1][j])*0.24420135f + input.val[i][j]*0.40261996f;
GaussianBlur5x5_row_rc(buffer,cols);
}
const float32x4_t a0 = {0.054488685f,0.054488685f,0.054488685f };
const float32x4_t a1 = {0.24420135f,0.24420135f,0.24420135f };
const float32x4_t a2 = {0.40261996f,0.40261996f,0.40261996f };
//Middle rows
//#pragma omp parallel for private(buffer) schedule(runtime)
for (unsigned int i=2; i<rows-2; i++)
{
for (unsigned int j=0; j<cols; j+=4)
{
float32x4_t row_0 = vld1q_f32( &input.val[i-2][j] );
float32x4_t row_1 = vld1q_f32( &input.val[i-1][j] );
float32x4_t row_2 = vld1q_f32( &input.val[i ][j] );
float32x4_t row_3 = vld1q_f32( &input.val[i+1][j] );
float32x4_t row_4 = vld1q_f32( &input.val[i+2][j] );
float32x4_t h_0 = vaddq_f32( row_4,row_0 ); ///// vmlaq
float32x4_t h_1 = vaddq_f32( row_3,row_1 );
float32x4_t h_2 = vmulq_f32( h_0,a0 );
h_0 = vmlaq_f32( h_2,h_1,a1 );
h_1 = vmlaq_f32( h_0,row_2,a2 );
vst1q_f32( &buffer[j],h_1 );
}
GaussianBlur5x5_row_rc(buffer,cols);
}
//Bottom rows
#pragma omp parallel for private(buffer)
for (unsigned int i=rows-2; i<rows; i++)
{
for (unsigned int j=0; j<cols; j++)
buffer[j] = (input.val[i-2][j]+input.val[rows-1][j])*0.054488685f + (input.val[i-1][j]+input.val[rows-1][j])*0.24420135f + input.val[i][j]*0.40261996f;
GaussianBlur5x5_row_rc(buffer,cols);
}
}
inline void GaussianBlur_5x5_row_asm(const float __restrict_arr *in,const unsigned int cols)
{
//Left columns
out[0] = (in[0]+in[2])*0.054488685f + (in[0]+in[1])*0.24420135f + in[0]*0.40261996f;
out[1] = (in[0]+in[3])*0.054488685f + (in[0]+in[2])*0.24420135f + in[1]*0.40261996f;
const float c1[4] = { 0.054488685f,0.122100675f };
const float c2[4] = { 0.122100675f,0.054488685f };
//Middle columns (Tiled)
for (unsigned int j=2; j<cols-2; j+=2)
{
asm volatile(
"mov r0,%[co1];"
"mov r1,%[co2];"
"mov r2,%[in_j_n2];"
"mov r3,%[in_j_n1];"
"mov r4,%[in_j];"
"vld1.32 {d0,d1},[r0];" // q0 = c1
"vld1.32 {d2,d3},[r1];" // q1 = c2
"vld1.32 {d4,d5},[r2];" // q2 = in[j-2]
"vld1.32 {d6,d7},[r3];" // q3 = in[j-1]
"vld1.32 {d8,d9},[r4];" // q4 = in[j]
"vmul.f32 q5,q2,q0;"
"vmul.f32 q6,q3,q1;"
"vadd.f32 q7,q6,q5;"
"vadd.f32 d16,d15,d14;"
"vpadd.f32 d17,d16,d16;" // out[j]
"vmul.f32 q5,q4,q5;"
"vadd.f32 d18,d14;"
"vpadd.f32 d19,d18,d18;" // out[j+1]
"mov r5,%[out_j];"
"mov r6,%[out_j_p1];"
"vst1.32 d17[0],[r5];"
"vst1.32 d19[0],[r6];"
:
:[co1] "r" (c1),[co2] "r" (c2),[in_j_n2] "r" (&in[j-2]),[in_j_n1] "r" (&in[j-1]),[in_j] "r" (&in[j]),[out_j] "r" (&out[j]),[out_j_p1] "r" (&out[j+1])
:"r0","r1","r2","r3"//,"q0","q1","q2","q3","q4","q5","q6","q7","d16","d17","d18","d19","r5","r6"
);
}
//Right columns
out[cols-2] = (in[cols-4]+in[cols-1])*0.054488685f + (in[cols-3]+in[cols-1])*0.24420135f + in[cols-2]*0.40261996f;
out[cols-1] = (in[cols-3]+in[cols-1])*0.054488685f + (in[cols-2]+in[cols-1])*0.24420135f + in[cols-1]*0.40261996f;
}
void GaussianBlur_5x5_asm(const Matrixf &input,cols);
float buffer[cols] __attribute__((aligned(16)));
//Top rows
#pragma omp parallel for private(buffer)
for (unsigned int i=0; i<2; i++)
{
for (unsigned int j=0; j<cols; j++)
{
buffer[j] = (input.val[0][j]+input.val[i+2][j])*0.054488685f + (input.val[0][j]+input.val[i+1][j])*0.24420135f + input.val[i][j]*0.40261996f;
}
GaussianBlur_5x5_row_asm(buffer,cols);
}
const float a0[4] = {0.054488685f,0.054488685f};
const float a1[4] = {0.24420135f,0.24420135f };
const float a2[4] = {0.40261996f,0.40261996f };
//Middle rows
//#pragma omp parallel for private(buffer) schedule(runtime)
for (unsigned int i=2; i<rows-2; i++)
{
for (unsigned int j=0; j<cols; j+=4)
{
// buffer[j] = (input.val[i-2][j]+input.val[i+2][j])*0.054488685f + (input.val[i-1][j]+input.val[i+1][j])*0.24420135f + input.val[i][j]*0.40261996f;
asm volatile(
"mov r0,%[c0];"
"mov r1,%[c1];"
"mov r2,%[c2];"
"mov r3,%[in_0];"
"mov r4,%[in_1];"
"mov r5,%[in_2];"
"mov r6,%[in_3];"
"mov r7,%[in_4];"
"vld1.32 {d0,[r0];" // q0 = a0
"vld1.32 {d2,[r1];" // q1 = a1
"vld1.32 {d4,[r2];" // q2 = a2
"vld1.32 {d6,[r3];" // q3 = input.val[i-2]
"vld1.32 {d8,[r4];" // q4 = input.val[i-1]
"vld1.32 {d10,d11},[r5];" // q5 = input.val[i ]
"vld1.32 {d12,d13},[r6];" // q6 = input.val[i+1]
"vld1.32 {d14,d15},[r7];" // q7 = input.val[i+2]
"vmul.f32 q8,q0;"
"vmul.f32 q9,q1;"
"vmul.f32 q10,q5,q2;"
"vmul.f32 q11,q1;"
"vmul.f32 q12,q7,q0;"
"vadd.f32 q0,q9,q8 ;"
"vadd.f32 q1,q10,q11;"
"vadd.f32 q2,q1,q0 ;"
"vadd.f32 q3,q12,q2 ;" // final
"mov r8,%[out_buffer];"
"vst1.32 {d6,[r8];"
:
:[c0]"r"(a0),[c1]"r"(a1),[c2]"r"(a2),[in_0]"r"(&input.val[i-2][j]),[in_1]"r"(&input.val[i-1][j]),[in_2]"r"(&input.val[i][j]),[in_3]"r"(&input.val[i+1][j]),[in_4]"r"(&input.val[i+2][j]),[out_buffer]"r"(&buffer[j])
:"r0","r3"
);
}
GaussianBlur_5x5_row_asm(buffer,cols);
}
//Bottom rows
//#pragma omp parallel for private(buffer)
for (unsigned int i=rows-2; i<rows; i++)
{
for (unsigned int j=0; j<cols; j++)
buffer[j] = (input.val[i-2][j]+input.val[rows-1][j])*0.054488685f + (input.val[i-1][j]+input.val[rows-1][j])*0.24420135f + input.val[i][j]*0.40261996f;
GaussianBlur_5x5_row_asm(buffer,cols);
}
}
如果使用-o3标志,是否有可能击败GCC编译器?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)