最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

學(xué)習(xí)在kernel態(tài)下使用NEON對(duì)算法進(jìn)行加速的方法

 更新時(shí)間:2017年11月17日 15:02:06   作者:commando_lwp  
這篇文章主要介紹了學(xué)習(xí)在kernel態(tài)下使用NEON對(duì)算法進(jìn)行加速的方法,一起來(lái)學(xué)習(xí)下,大大提高數(shù)據(jù)運(yùn)算的效率。

本文跟著小編一起來(lái)學(xué)習(xí)在linux kernel態(tài)下如何使用NEON對(duì)算法進(jìn)行加速的技巧,內(nèi)容通過(guò)圖文實(shí)例給大家做了詳細(xì)分析,一起來(lái)看下。

ARM處理器從cortex系列開始集成NEON處理單元,該單元可以簡(jiǎn)單理解為協(xié)處理器,專門為矩陣運(yùn)算等算法設(shè)計(jì),特別適用于圖像、視頻、音頻處理等場(chǎng)景,應(yīng)用也很廣泛。

本文先對(duì)NEON處理單元進(jìn)行簡(jiǎn)要介紹,然后介紹如何在內(nèi)核態(tài)下使用NEON,最后列舉實(shí)例說(shuō)明。

一.NEON簡(jiǎn)介

其實(shí)最好的資料就是官方文檔,Cortex™-A Series Programmer's Guide ,以下描述摘自該文檔

1.1 SIMD

NEON采用SIMD架構(gòu),single instruction multy data,一條指令處理多個(gè)數(shù)據(jù),NEON中這多個(gè)數(shù)據(jù)可以很多,而且配置靈活(8bit、16bit、32bit為單位,可多個(gè)單位數(shù)據(jù)),這是優(yōu)勢(shì)所在。

如下圖,APU需要至少四條指令完成加操作,而NEON只需要1條,考慮到ld和st,節(jié)省的指令更多。

 

上述特性,使NEON特別適合處理塊數(shù)據(jù)、圖像、視頻、音頻等。

 1.2 NEON architecture overview

NEON也是load/store架構(gòu),寄存器為64bit/128bit,可形成向量化數(shù)據(jù),配合若干便于向量操作的指令。

1.2.1 commonality with VFP         1.2.2 data type

 指令中的數(shù)據(jù)類型表示,例如VMLAL.S8:

1.2.3 registers 

32個(gè)64bit寄存器,D0~D31;同時(shí)可組成16個(gè)128 bit寄存器,Q0~Q15。與VFP公用。

寄存器內(nèi)部的數(shù)據(jù)單位為8bit、16bit、32bit,可以根據(jù)需要靈活配置。

NEON的指令有Normal,Long,Wide,Narrow和Saturating variants等幾種后綴,是根據(jù)操作的源src和dst寄存器的類型確定的。

   

1.2.4 instruction set

                     

1.3 NEON 指令分類概述

指令比較多, 詳細(xì)可參考Cortex™-A Series Programmer's Guide??纱篌w分為:

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

簡(jiǎn)單羅列一下各指令

                

                  

                

  

無(wú)循環(huán)左移,負(fù)數(shù)左移按右移處理。

load和store指令不太好理解,說(shuō)明一下。

  

1.4 NEON 使用方式      

1.4.1 NEON使用方式

NEON有若干種使用方式:

C語(yǔ)言被編譯器自動(dòng)向量化,需要增加編譯選項(xiàng),且C語(yǔ)言編碼時(shí)有若干注意事項(xiàng)。這種方式不確定性太大,沒(méi)啥實(shí)用價(jià)值   NEON匯編,可行,匯編稍微復(fù)雜一點(diǎn),但是核心算法還是值得的   intrinsics,gcc和armcc等編譯器提供了若干與NEON對(duì)應(yīng)的inline函數(shù),可直接在C語(yǔ)言里調(diào)用,這些函數(shù)反匯編時(shí)會(huì)直接編程響應(yīng)的NEON指令。這種方式比較實(shí)用與C語(yǔ)言環(huán)境,且相對(duì)簡(jiǎn)單。本文后續(xù)使用這種方式進(jìn)行詳細(xì)說(shuō)明。          1.4.2  C語(yǔ)言NEON數(shù)據(jù)類型

需包含arm_neon.h頭文件,該頭文件在gcc目錄里。都是向量數(shù)據(jù)。

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函數(shù)

跟NEON指令對(duì)應(yīng),詳見gcc手冊(cè)。

 二.內(nèi)核狀態(tài)下使用NEON的規(guī)則

在linux里,應(yīng)用態(tài)可以比較方便使用NEON instrinsic,增加頭arm_neon.h頭文件后直接使用。但是內(nèi)核態(tài)下使用NEON有較多限制,在linux內(nèi)核文檔  /Documentation/arm/kernel_mode_neon.txt對(duì)此有詳細(xì)說(shuō)明。要點(diǎn)為:

 還有一點(diǎn)特別關(guān)鍵:

 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 <stdint.h>
 沒(méi)有使用-ffreestanding編譯選項(xiàng)時(shí),在內(nèi)核態(tài)下使用出現(xiàn)此編譯錯(cuò)誤。             

 三.實(shí)例

NEON一般在圖像等領(lǐng)域,最小處理單位就是8bit,而不是1bit,這方便的例子非常多,本文就不說(shuō)明了。在實(shí)際項(xiàng)目中,我需要對(duì)液晶的一組數(shù)據(jù)按位操作,變換,形成新的數(shù)據(jù),如果用傳統(tǒng)ARM指令,掩碼、移位、循環(huán),想想效率就非常低。于是決定使用NEON的位相關(guān)指令完成上述任務(wù)。

3.1 任務(wù)說(shuō)明

如下圖,需要對(duì)各個(gè)bit進(jìn)行轉(zhuǎn)換,組成新的數(shù)據(jù)。

 

3.2 算法說(shuō)明

使用vmsk、vshl、vadd等位操作完成。

3.3 kernel配置

必須配置內(nèi)核支持NEON,否則kernel_neon_begin()和kernel_neon_end()等函數(shù)不會(huì)編輯進(jìn)去。

make menuconfig:Floating point emulation,如下圖。

未使能“Support for NEON in kernel mode”時(shí)會(huì)報(bào)錯(cuò):
mmi_module_amp: Unknown symbol kernel_neon_begin (err 0)
mmi_module_amp: Unknown symbol kernel_neon_end (err 0)

3.4 模塊代碼

由于NEON代碼需要單獨(dú)設(shè)置編譯選項(xiàng),所以單獨(dú)建立了一個(gè)內(nèi)核模塊,makefile如下:

CFLAGS_MODULE += -O3 -mfpu=neon -mfloat-abi=softfp -ffreestanding 

核心代碼:

#include <linux/module.h> 
#include <linux/printk.h>
#include <arm_neon.h>  // 來(lái)自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<LCD_8812_PAGE_ROWS;row++ )
      V_src[row] = vld1q_u8( fb_page_x + row*LCD_8812_ROW_BYTES );
       for( bit=0;bit<8;bit++){
        V_msk = vdupq_n_u8(1<<bit);
        for( row=0;row<LCD_8812_PAGE_ROWS;row++){
          V_tmp[row] = vandq_u8(V_src[row],V_msk);  // only process the desire bit
          V_shift = vdupq_n_s8( RSHL_bits[row]-bit );
          V_tmp[row] = vshlq_u8( V_tmp[row],V_shift );
        }  
        V_dst[bit] = vorrq_u8(V_tmp[0],V_tmp[1]);   // all bit_x convert to one row
        V_dst[bit] |= vorrq_u8(V_tmp[2],V_tmp[3]);
        V_dst[bit] |= vorrq_u8(V_tmp[4],V_tmp[5]);
        V_dst[bit] |= vorrq_u8(V_tmp[6],V_tmp[7]);        
      }
      // store to ram
      fb_page_x = dst + page*LCD_PAGE_BYTES;
      for( row=0;row<LCD_8812_PAGE_ROWS;row++ ){
         vst1q_u8(fb_page_x,V_dst[row]);
        fb_page_x += LCD_8812_ROW_BYTES;
      }
  }
  return 0;
}

EXPORT_SYMBOL_GPL(fs8812_cvt_buf);

調(diào)用模塊,務(wù)必沒(méi)有“-mfpu=neon -mfloat-abi=softfp ”選項(xiàng)

  // convert the frame_buf for fs8812
  kernel_neon_begin();
  fs8812_cvt_buf( g_tmp_buf, frame_buf );
  kernel_neon_end();

 以上就是本篇文章的全部?jī)?nèi)容,大家有不懂的可以在下面留言區(qū)討論。

相關(guān)文章

  • Linux中 CentOS 6.5 手動(dòng)升級(jí)gcc到gcc-6.1.0

    Linux中 CentOS 6.5 手動(dòng)升級(jí)gcc到gcc-6.1.0

    這篇文章主要介紹了Linux中 CentOS 6.5 手動(dòng)升級(jí)gcc到gcc-6.1.0的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-10-10
  • 3分鐘學(xué)會(huì)如何上手supervisor看門狗

    3分鐘學(xué)會(huì)如何上手supervisor看門狗

    這篇文章主要介紹了3分鐘學(xué)會(huì)如何上手supervisor看門狗,文章中操作步驟講解的很清楚,有需要的同學(xué)可以研究下
    2021-02-02
  • linux下/etc/passwd文件的選項(xiàng)說(shuō)明

    linux下/etc/passwd文件的選項(xiàng)說(shuō)明

    /etc/passwd 文件是系統(tǒng)的主要文件之一。該文件中包含了所有用戶登錄名清單;為所有用戶指定了主目錄;在登錄時(shí)使用的 shell 程序名稱等。該文件還保存了用戶口令;給每個(gè)用戶提供系統(tǒng)識(shí)別號(hào)。
    2008-06-06
  • Linux防火墻firewall設(shè)置端口訪問(wèn)限制方式

    Linux防火墻firewall設(shè)置端口訪問(wèn)限制方式

    在Linux系統(tǒng)中,firewall工具可以幫助用戶配置防火墻規(guī)則,實(shí)現(xiàn)對(duì)端口的精確限制,通過(guò)簡(jiǎn)單的命令,用戶可以開放或移除單個(gè)端口、批量處理端口規(guī)則、查看所有開放端口、允許或禁止特定IP或網(wǎng)段訪問(wèn)某端口等操作
    2024-10-10
  • Linux中關(guān)于inode的知識(shí)總結(jié)

    Linux中關(guān)于inode的知識(shí)總結(jié)

    這篇文章主要給大家介紹了在Linux中關(guān)于inode的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-06-06
  • Centos7.5配置java環(huán)境安裝tomcat的講解

    Centos7.5配置java環(huán)境安裝tomcat的講解

    今天小編就為大家分享一篇關(guān)于Centos7.5配置java環(huán)境安裝tomcat的講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-03-03
  • CentOS使用expect批量遠(yuǎn)程執(zhí)行腳本和命令

    CentOS使用expect批量遠(yuǎn)程執(zhí)行腳本和命令

    這篇文章主要介紹了CentOS使用expect批量遠(yuǎn)程執(zhí)行腳本和命令,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • linux下安裝golang的方法

    linux下安裝golang的方法

    這篇文章主要介紹了linux下安裝golang的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09
  • Linux使用scp遠(yuǎn)程傳輸命令進(jìn)行服務(wù)器文件互傳(阿里云服務(wù)器)

    Linux使用scp遠(yuǎn)程傳輸命令進(jìn)行服務(wù)器文件互傳(阿里云服務(wù)器)

    因?yàn)橐慌_(tái)服務(wù)器linux服務(wù)器總是出問(wèn)題,也沒(méi)找到問(wèn)題只好重新配置環(huán)境,那么就需要文件備份操作,既然是linux服務(wù)器可以使用scp命令傳輸文件了
    2016-09-09
  • linux指令man page之rm,mv詳解

    linux指令man page之rm,mv詳解

    這篇文章主要介紹了linux指令man page之rm,mv詳解的相關(guān)資料,需要的朋友可以參考下
    2017-02-02

最新評(píng)論

邢台县| 札达县| 资中县| 扎赉特旗| 美姑县| 盈江县| 久治县| 芒康县| 天津市| 开江县| 获嘉县| 海阳市| 延边| 手游| 洱源县| 青田县| 龙泉市| 城步| 中山市| 偏关县| 玉树县| 乌兰浩特市| 梅河口市| 府谷县| 宁城县| 绩溪县| 大新县| 麻栗坡县| 杭州市| 湖北省| 苗栗县| 青川县| 昭通市| 右玉县| 五原县| 故城县| 武清区| 张家川| 西贡区| 留坝县| 和政县|