gpt4 book ai didi

x86-64 - 带有 SSE 的 8 位 LERP

转载 作者:行者123 更新时间:2023-12-04 03:53:01 25 4
gpt4 key购买 nike

我一直在尝试找出使用 AMD64 SIMD 指令来实现与大量 u8 值一起使用的 lerp 的最佳方法,但我似乎无法在不需要所有 SIMD 扩展的情况下找出正确的指令。

我现在使用的公式是

u8* a;
u8* b;
u8* result;
size_t count;
u16 total;
u16 progress;

u32 invertedProgress = total - progress;
for(size_t i = 0; i < count; i++){
result[i] = (u8)((b[i] * progress + a[i] * invertedProgress) / total);
}

我认为它看起来像这样:

u8* a;
u8* b;
u8* result;
size_t count;
u16 total;
u16 progress;

__m128i mmxZero;
__m128i mmxProgress;
__m128i mmxInvertedProgress;
__m128i mmxProductA;
__m128i mmxProductB;

mmxZero = _mm_xor_ps(zero, zero); // Is there a clear?

mmxProgress = Fill with progress;

mmxTotal = Fill with total;

mmxInvertedProgress = mmxTotal;
mmxInvertedProgress = _mm_unpacklo_epi8(mmxInvertedProgres, mmxZero);
mmxInvertedProgress = _mm_sub_epi8(mmxTotal, progress);

for(size_t i = 0; i < count; i += 8){
mmxProductA = load A;
// u8 -> u16
mmxProductA = _mm_unpacklo_epi8(mmxProductA, mmxZero);

mmxProductB = load B;
// u8 -> u16
mmxProductB = _mm_unpacklo_epi8(mmxProductB, mmxZero);

// a * (total - progress)
mmxProductA = _mm_mullo_epi16(mmxProductA, mmxInvertedProgress);
// b * progress
mmxProductB = _mm_mullo_epi16(mmxProductB, mmxProgress);

// a * (total - progress) + b * progress
mmxProductA = _mm_add_epi16(mmxProductA, mmxProductB);
// (a * (total - progress) + b * progress) / total
mmxProductA = _mm_div_epi16(mmxProductA, mmxTotal);

mmxProductA = saturated u16 -> u8;
store result = maxProductA;
}

这里有一些我似乎无法挖掘的东西 in the guide ,主要与加载和存储值有关。

我知道有一些更新的指令可以同时执行更多的操作,这个初始实现应该适用于旧芯片。

对于这个例子,我也忽略了对齐和缓冲区溢出的可能性,我认为这有点超出了问题的范围。

最佳答案

好问题。正如您所发现的,SSE 没有整数除法指令,并且(与 ARM NEON 不同)它没有字节乘法或 FMA。

这是我通常做的。下面的代码将向量拆分为偶数/奇数字节,使用 16 位乘法指令分别缩放,然后将它们合并回字节。

// Linear interpolation is based on the following formula: x*(1-s) + y*s which can equivalently be written as x + s(y-x).
class LerpBytes
{
// Multipliers are fixed point numbers in 16-bit lanes of these vectors, in 1.8 format
__m128i mulX, mulY;

public:

LerpBytes( uint16_t progress, uint16_t total )
{
// The source and result are bytes.
// Multipliers only need 1.8 fixed point format, anything above that is wasteful.
assert( total > 0 );
assert( progress >= 0 );
assert( progress <= total );

const uint32_t fp = (uint32_t)progress * 0x100 / total;
mulY = _mm_set1_epi16( (short)fp );
mulX = _mm_set1_epi16( (short)( 0x100 - fp ) );
}

__m128i lerp( __m128i x, __m128i y ) const
{
const __m128i lowMask = _mm_set1_epi16( 0xFF );

// Split both vectors into even/odd bytes in 16-bit lanes
__m128i lowX = _mm_and_si128( x, lowMask );
__m128i highX = _mm_srli_epi16( x, 8 );
__m128i lowY = _mm_and_si128( y, lowMask );
__m128i highY = _mm_srli_epi16( y, 8 );

// That multiply instruction has relatively high latency, 3-5 cycles.
// We're lucky to have 4 vectors to handle.
lowX = _mm_mullo_epi16( lowX, mulX );
lowY = _mm_mullo_epi16( lowY, mulY );
highX = _mm_mullo_epi16( highX, mulX );
highY = _mm_mullo_epi16( highY, mulY );

// Add the products
__m128i low = _mm_adds_epu16( lowX, lowY );
__m128i high = _mm_adds_epu16( highX, highY );

// Pack them back into bytes.
// The multiplier was 1.8 fixed point, trimming the lowest byte off both vectors.
low = _mm_srli_epi16( low, 8 );
high = _mm_andnot_si128( lowMask, high );
return _mm_or_si128( low, high );
}
};

static void print( const char* what, __m128i v )
{
printf( "%s:\t", what );
alignas( 16 ) std::array<uint8_t, 16> arr;
_mm_store_si128( ( __m128i * )arr.data(), v );
for( uint8_t b : arr )
printf( " %02X", (int)b );
printf( "\n" );
}

int main()
{
const __m128i x = _mm_setr_epi32( 0x33221100, 0x77665544, 0xBBAA9988, 0xFFEEDDCC );
const __m128i y = _mm_setr_epi32( 0xCCDDEEFF, 0x8899AABB, 0x44556677, 0x00112233 );
LerpBytes test( 0, 1 );
print( "zero", test.lerp( x, y ) );
test = LerpBytes( 1, 1 );
print( "one", test.lerp( x, y ) );
test = LerpBytes( 1, 2 );
print( "half", test.lerp( x, y ) );
test = LerpBytes( 1, 3 );
print( "1/3", test.lerp( x, y ) );
test = LerpBytes( 1, 4 );
print( "1/4", test.lerp( x, y ) );
return 0;
}

关于x86-64 - 带有 SSE 的 8 位 LERP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64165250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com