Linux内核态NEON加速实战:从原理到位操作实例
本文将深入探讨在Linux内核环境中利用NEON技术加速算法的具体技巧,并通过图文实例进行详细解析。
自Cortex系列起,ARM处理器便集成了NEON处理单元。该单元可视为一种协处理器,专门针对矩阵运算等复杂算法优化,尤其在图像、视频及音频处理领域应用广泛且效果显著。
文章首先简要概述NEON处理单元的基本特性,随后重点阐述在内核态下调用NEON的方法,最后通过具体实例加以说明。
一.NEON简介
最权威的资料莫过于官方文档《Cortex™-A Series Programmer's Guide》,以下描述均摘录自该文档。
1.1 SIMD
NEON基于SIMD(单指令多数据)架构,即一条指令可同时处理多个数据。其优势在于数据量大且配置灵活,支持以8bit、16bit或32bit为单位进行组合处理。
如图所示,若使用APU完成相同的加法操作至少需要四条指令,而NEON仅需一条。若计入加载和存储指令,NEON节省的指令数量更为可观。

上述特性使得NEON在处理块数据、图像、视频及音频等场景时具有天然优势。
1.2 NEON architecture overview
NEON同样采用Load/Store架构,拥有64bit或128bit的寄存器,可形成向量化数据,并配合一系列便于向量操作的专用指令。
1.2.1 commonality with VFP 1.2.2 data type

指令中的数据类型表示方式示例如下,例如VMLAL.S8:

1.2.3 registers
NEON包含32个64bit寄存器(D0~D31),同时可组合成16个128bit寄存器(Q0~Q15),这些寄存器与VFP共用。

寄存器内部的数据单位支持8bit、16bit、32bit,可根据实际需求灵活配置。

NEON指令根据源src和目标dst寄存器的类型,分为Normal、Long、Wide、Narrow以及Saturating variants等多种后缀。


1.2.4 instruction set


1.3 NEON 指令分类概述
NEON指令繁多,详细信息可参考《Cortex™-A Series Programmer's Guide》。大体可分为以下几类:
NEON general data processing instructions NEON shift instructions NEON logical and compare operations NEON arithmetic instructions NEON multiply instructions NEON load and store element and structure instructions B.8 NEON and VFP pseudo-instructions
此处简单罗列部分各指令的功能。





无循环左移,负数左移按右移处理。
load和store指令较为复杂,在此稍作说明。

1.4 NEON 使用方式
1.4.1 NEON使用方式
NEON主要有以下几种使用方式:
C语言被编译器自动向量化,需增加编译选项,但编码时有诸多注意事项,这种方式不确定性较大,实用价值有限。NEON汇编可行,但汇编编写相对复杂,仅核心算法值得投入。Intrinsics是gcc和armcc等编译器提供的与NEON对应的内联函数,可直接在C语言中调用,反汇编后会生成相应的NEON指令。这种方式在C语言环境中既实用又相对简单,本文后续将重点介绍此方法。1.4.2 C语言NEON数据类型
需包含arm_neon.h头文件,该文件位于gcc目录中。所有定义均为向量数据类型。
typedef __builtin_neon_qi int8x8_t __attribute__ ((__vector_size__ (8)));
typedef __builtin_neon_hi int16x4_t __attribute__ ((__vector_size__ (8)));
typedef __builtin_neon_si int32x2_t __attribute__ ((__vector_size__ (8)));
typedef __builtin_neon_di int64x1_t;
typedef __builtin_neon_sf float32x2_t __attribute__ ((__vector_size__ (8)));
typedef __builtin_neon_poly8 poly8x8_t __attribute__ ((__vector_size__ (8)));
typedef __builtin_neon_poly16 poly16x4_t __attribute__ ((__vector_size__ (8)));
typedef __builtin_neon_uqi uint8x8_t __attribute__ ((__vector_size__ (8)));
typedef __builtin_neon_uhi uint16x4_t __attribute__ ((__vector_size__ (8)));
typedef __builtin_neon_usi uint32x2_t __attribute__ ((__vector_size__ (8)));
typedef __builtin_neon_udi uint64x1_t;
typedef __builtin_neon_qi int8x16_t __attribute__ ((__vector_size__ (16)));
typedef __builtin_neon_hi int16x8_t __attribute__ ((__vector_size__ (16)));
typedef __builtin_neon_si int32x4_t __attribute__ ((__vector_size__ (16)));
typedef __builtin_neon_di int64x2_t __attribute__ ((__vector_size__ (16)));
typedef __builtin_neon_sf float32x4_t __attribute__ ((__vector_size__ (16)));
typedef __builtin_neon_poly8 poly8x16_t __attribute__ ((__vector_size__ (16)));
typedef __builtin_neon_poly16 poly16x8_t __attribute__ ((__vector_size__ (16)));
typedef __builtin_neon_uqi uint8x16_t __attribute__ ((__vector_size__ (16)));
typedef __builtin_neon_uhi uint16x8_t __attribute__ ((__vector_size__ (16)));
typedef __builtin_neon_usi uint32x4_t __attribute__ ((__vector_size__ (16)));
typedef __builtin_neon_udi uint64x2_t __attribute__ ((__vector_size__ (16)));
typedef float float32_t;
typedef __builtin_neon_poly8 poly8_t;
typedef __builtin_neon_poly16 poly16_t;
typedef struct int8x8x2_t
{
int8x8_t val[2];
} int8x8x2_t;
typedef struct int8x16x2_t
{
int8x16_t val[2];
} int8x16x2_t;
typedef struct int16x4x2_t
{
int16x4_t val[2];
} int16x4x2_t;
typedef struct int16x8x2_t
{
int16x8_t val[2];
} int16x8x2_t;
typedef struct int32x2x2_t
{
int32x2_t val[2];
} int32x2x2_t;
typedef struct int32x4x2_t
{
int32x4_t val[2];
} int32x4x2_t;
typedef struct int64x1x2_t
{
int64x1_t val[2];
} int64x1x2_t;
typedef struct int64x2x2_t
{
int64x2_t val[2];
} int64x2x2_t;
typedef struct uint8x8x2_t
{
uint8x8_t val[2];
} uint8x8x2_t;
typedef struct uint8x16x2_t
{
uint8x16_t val[2];
} uint8x16x2_t;
typedef struct uint16x4x2_t
{
uint16x4_t val[2];
} uint16x4x2_t;
typedef struct uint16x8x2_t
{
uint16x8_t val[2];
} uint16x8x2_t;
typedef struct uint32x2x2_t
{
uint32x2_t val[2];
} uint32x2x2_t;
typedef struct uint32x4x2_t
{
uint32x4_t val[2];
} uint32x4x2_t;
typedef struct uint64x1x2_t
{
uint64x1_t val[2];
} uint64x1x2_t;
typedef struct uint64x2x2_t
{
uint64x2_t val[2];
} uint64x2x2_t;
typedef struct float32x2x2_t
{
float32x2_t val[2];
} float32x2x2_t;
typedef struct float32x4x2_t
{
float32x4_t val[2];
} float32x4x2_t;
typedef struct poly8x8x2_t
{
poly8x8_t val[2];
} poly8x8x2_t;
typedef struct poly8x16x2_t
{
poly8x16_t val[2];
} poly8x16x2_t;
typedef struct poly16x4x2_t
{
poly16x4_t val[2];
} poly16x4x2_t;
typedef struct poly16x8x2_t
{
poly16x8_t val[2];
} poly16x8x2_t;
typedef struct int8x8x3_t
{
int8x8_t val[3];
} int8x8x3_t;
typedef struct int8x16x3_t
{
int8x16_t val[3];
} int8x16x3_t;
typedef struct int16x4x3_t
{
int16x4_t val[3];
} int16x4x3_t;
typedef struct int16x8x3_t
{
int16x8_t val[3];
} int16x8x3_t;
typedef struct int32x2x3_t
{
int32x2_t val[3];
} int32x2x3_t;
typedef struct int32x4x3_t
{
int32x4_t val[3];
} int32x4x3_t;
typedef struct int64x1x3_t
{
int64x1_t val[3];
} int64x1x3_t;
typedef struct int64x2x3_t
{
int64x2_t val[3];
} int64x2x3_t;
typedef struct uint8x8x3_t
{
uint8x8_t val[3];
} uint8x8x3_t;
typedef struct uint8x16x3_t
{
uint8x16_t val[3];
} uint8x16x3_t;
typedef struct uint16x4x3_t
{
uint16x4_t val[3];
} uint16x4x3_t;
typedef struct uint16x8x3_t
{
uint16x8_t val[3];
} uint16x8x3_t;
typedef struct uint32x2x3_t
{
uint32x2_t val[3];
} uint32x2x3_t;
typedef struct uint32x4x3_t
{
uint32x4_t val[3];
} uint32x4x3_t;
typedef struct uint64x1x3_t
{
uint64x1_t val[3];
} uint64x1x3_t;
typedef struct uint64x2x3_t
{
uint64x2_t val[3];
} uint64x2x3_t;
typedef struct float32x2x3_t
{
float32x2_t val[3];
} float32x2x3_t;
typedef struct float32x4x3_t
{
float32x4_t val[3];
} float32x4x3_t;
typedef struct poly8x8x3_t
{
poly8x8_t val[3];
} poly8x8x3_t;
typedef struct poly8x16x3_t
{
poly8x16_t val[3];
} poly8x16x3_t;
typedef struct poly16x4x3_t
{
poly16x4_t val[3];
} poly16x4x3_t;
typedef struct poly16x8x3_t
{
poly16x8_t val[3];
} poly16x8x3_t;
typedef struct int8x8x4_t
{
int8x8_t val[4];
} int8x8x4_t;
typedef struct int8x16x4_t
{
int8x16_t val[4];
} int8x16x4_t;
typedef struct int16x4x4_t
{
int16x4_t val[4];
} int16x4x4_t;
typedef struct int16x8x4_t
{
int16x8_t val[4];
} int16x8x4_t;
typedef struct int32x2x4_t
{
int32x2_t val[4];
} int32x2x4_t;
typedef struct int32x4x4_t
{
int32x4_t val[4];
} int32x4x4_t;
typedef struct int64x1x4_t
{
int64x1_t val[4];
} int64x1x4_t;
typedef struct int64x2x4_t
{
int64x2_t val[4];
} int64x2x4_t;
typedef struct uint8x8x4_t
{
uint8x8_t val[4];
} uint8x8x4_t;
typedef struct uint8x16x4_t
{
uint8x16_t val[4];
} uint8x16x4_t;
typedef struct uint16x4x4_t
{
uint16x4_t val[4];
} uint16x4x4_t;
typedef struct uint16x8x4_t
{
uint16x8_t val[4];
} uint16x8x4_t;
typedef struct uint32x2x4_t
{
uint32x2_t val[4];
} uint32x2x4_t;
typedef struct uint32x4x4_t
{
uint32x4_t val[4];
} uint32x4x4_t;
typedef struct uint64x1x4_t
{
uint64x1_t val[4];
} uint64x1x4_t;
typedef struct uint64x2x4_t
{
uint64x2_t val[4];
} uint64x2x4_t;
typedef struct float32x2x4_t
{
float32x2_t val[4];
} float32x2x4_t;
typedef struct float32x4x4_t
{
float32x4_t val[4];
} float32x4x4_t;
typedef struct poly8x8x4_t
{
poly8x8_t val[4];
} poly8x8x4_t;
typedef struct poly8x16x4_t
{
poly8x16_t val[4];
} poly8x16x4_t;
typedef struct poly16x4x4_t
{
poly16x4_t val[4];
} poly16x4x4_t;
typedef struct poly16x8x4_t
{
poly16x8_t val[4];
} poly16x8x4_t;
1.4.3 gcc的NEON函数
这些函数与NEON指令一一对应,详见gcc手册。

二.内核状态下使用NEON的规则
在Linux用户态下,只需增加头文件arm_neon.h即可方便地使用NEON intrinsics。但在内核态下使用NEON存在较多限制,Linux内核文档 /Documentation/arm/kernel_mode_neon.txt对此有详细说明。要点如下:

此外还有一点特别关键:

CC [M] /work/platform-zynq/drivers/zynq_fpga_driver/mmi_neon/lcd_hw_fs8812_neon.o
In file included from /home/liuwanpeng/lin/lib/gcc/arm-xilinx-linux-gnueabi/4.8.3/include/arm_neon.h:39:0,
from /work/platform-zynq/drivers/zynq_fpga_driver/mmi_neon/lcd_hw_fs8812_neon.c:8:
/home/liuwanpeng/lin/lib/gcc/arm-xilinx-linux-gnueabi/4.8.3/include/stdint.h:9:26: error: no include path in which to search for stdint.h
# include_next
没有使用-ffreestanding编译选项时,在内核态下使用出现此编译错误。
三.实例
NEON通常应用于图像等领域,最小处理单位为8bit而非1bit,此类例子众多,本文不再赘述。在实际项目中,需对液晶的一组数据进行按位操作和变换以形成新数据。若使用传统ARM指令,涉及掩码、移位和循环,效率极低。因此决定使用NEON的位相关指令来完成此任务。
3.1 任务说明
如下图所示,需要对各个bit进行转换,组成新的数据。

3.2 算法说明
使用vmsk、vshl、vadd等位操作指令完成。
3.3 kernel配置
必须配置内核支持NEON,否则kernel_neon_begin()和kernel_neon_end()等函数不会被编译进内核。
执行make menuconfig:Floating point emulation,如下图所示。

未使能“Support for NEON in kernel mode”时会报错:
mmi_module_amp: Unknown symbol kernel_neon_begin (err 0)
mmi_module_amp: Unknown symbol kernel_neon_end (err 0)
3.4 模块代码
由于NEON代码需要单独设置编译选项,因此单独建立了一个内核模块,Makefile如下:
CFLAGS_MODULE += -O3 -mfpu=neon -mfloat-abi=softfp -ffreestanding
核心代码:
#include
#include
#include // 来自GCC的头文件,必须用-ffreestanding编译选徐昂
#define LCD_8812_ROW_BYTES 16 #define LCD_8812_PAGE_ROWS 8 #define LCD_PAGE_BYTES (LCD_8812_ROW_BYTES*LCD_8812_PAGE_ROWS)
int fs8812_cvt_buf( uint8 * dst, uint8 * src )
{
uint8x16_t V_src[8];
uint8x16_t V_tmp[8];
uint8x16_t V_dst[8];
uint8x16_t V_msk;
int8x16_t V_shift;
int8 RSHL_bits[8] = {0,1,2,3,4,5,6,7};
int8 row,bit;
uint8 page;
uint8 * fb_page_x = NULL;
// convert the frame_buf for fs8812
for( page=0;page<4;page++ ){
fb_page_x = src + page*LCD_PAGE_BYTES;
for( row=0;row
调用模块时,务必确保没有“-mfpu=neon -mfloat-abi=softfp ”选项。
// convert the frame_buf for fs8812
kernel_neon_begin();
fs8812_cvt_buf( g_tmp_buf, frame_buf );
kernel_neon_end();
以上就是本篇文章的全部内容,大家有不懂的可以在下面留言区讨论。