【问题标题】:uint8 to float using SIMD Neon intrinsicsuint8 使用 SIMD Neon 内在函数浮动
【发布时间】:2020-12-12 07:15:41
【问题描述】:

我正在尝试优化将灰度图像转换为在 Neon A64/v8 上运行的浮动图像的代码。

使用 OpenCV 的 convertTo()(为 android 编译)当前的实现相当快,但这仍然是我们的瓶颈。

所以我想出了以下代码,并希望了解可能的改进。

如果可以的话,图像的高度和宽度是 16 倍。

我正在运行for 循环:

static void u8_2_f(unsigned char* in, float* out)
{
    //1 u8x8->u16x8
    uint8x8_t u8x8src = vld1_u8(in);
    uint16x8_t u16x8src = vmovl_u8(u8x8src);

    //2 u16x8 -> u32x4high, u32x4low
    uint32x4_t u32x4srch = vmovl_u16(vget_high_u16(u16x8src));
    uint32x4_t u32x4srcl = vmovl_u16(vget_low_u16(u16x8src));

    //3 u32x4high, u32x4low -> f32x4high, f32x4low
    vst1q_f32(out, vcvtq_f32_u32(u32x4srch));
    vst1q_f32(out+4, vcvtq_f32_u32(u32x4srcl));
}

【问题讨论】:

  • 如果内存带宽是一个瓶颈,作为内存瓶颈图像的其他传递的一部分,它可能值得在运行中执行此操作。或者作为 ALU 瓶颈的传递的一部分,并保存图像的浮动版本以供以后使用,同时在已经加载时使用它,因此该传递保持内存繁忙以及 ALU。或者可能缓存阻止转换,以便您转换适合 L1d 或 L2 缓存的部分,然后在以后的传递中循环。
  • 编写汇编代码的最大原因之一是vget_。编译器一看到它们就会生成 FUBAR 机器代码。

标签: c++ c simd intrinsics neon


【解决方案1】:

为了可能的改进,尝试用这个函数替换vcvtq_f32_u32。它是 2 条指令而不是 1 条,但在某些 CPU 上它们可能更快。

// Convert bytes to float, assuming the input is within [ 0 .. 0xFF ] interval
inline float32x4_t byteToFloat( uint32x4_t u32 )
{
    // Floats have 23 bits of mantissa.
    // We want least significant 8 bits to be shifted to [ 0 .. 255 ], therefore need to add 2^23
    // See this page for details: https://www.h-schmidt.net/FloatConverter/IEEE754.html
    // If you want output floats in [ 0 .. 255.0 / 256.0 ] interval, change into 2^15 = 0x47000000
    constexpr uint32_t offsetValue = 0x4b000000;
    // Check disassembly & verify your compiler has moved this initialization outside the loop
    const uint32x4_t offsetInt = vdupq_n_u32( offsetValue );
    // Bitwise is probably slightly faster than addition, delivers same results for our input
    u32 = vorrq_u32( u32, offsetInt );
    // The only FP operation required is subtraction, hopefully faster than UCVTF
    return vsubq_f32( vreinterpretq_f32_u32( u32 ), vreinterpretq_f32_u32( offsetInt ) );
}

【讨论】:

    猜你喜欢
    • 2014-09-18
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 2017-10-17
    • 1970-01-01
    • 2016-05-20
    • 2013-09-18
    相关资源
    最近更新 更多