These two functions are doing the same thing. The only difference is the order of parameters of Fma.MultiplyAdd. However, the codegen of the second function is not as good as the first one. Maybe worth looking at this to see if the JIT can optimize it.
static unsafe void foo2(float scale, float[] src, float[] dst, int count)
{
fixed (float* psrc = &src[0])
fixed (float* pdst = &dst[0])
{
float* pSrcCurrent = psrc;
float* pDstCurrent = pdst;
float* pEnd = pdst + count;
Vector256<float> scaleVector256 = Avx.SetAllVector256(scale);
while (pDstCurrent + 8 <= pEnd)
{
Vector256<float> dstVector = Avx.LoadVector256(pDstCurrent);
dstVector = Fma.MultiplyAdd(Avx.LoadVector256(pSrcCurrent), scaleVector256, dstVector);
Avx.Store(pDstCurrent, dstVector);
pSrcCurrent += 8;
pDstCurrent += 8;
}
}
}
static unsafe void foo3(float scale, float[] src, float[] dst, int count)
{
fixed (float* psrc = &src[0])
fixed (float* pdst = &dst[0])
{
float* pSrcCurrent = psrc;
float* pDstCurrent = pdst;
float* pEnd = pdst + count;
Vector256<float> scaleVector256 = Avx.SetAllVector256(scale);
while (pDstCurrent + 8 <= pEnd)
{
Vector256<float> dstVector = Avx.LoadVector256(pDstCurrent);
dstVector = Fma.MultiplyAdd(scaleVector256, Avx.LoadVector256(pSrcCurrent), dstVector);
Avx.Store(pDstCurrent, dstVector);
pSrcCurrent += 8;
pDstCurrent += 8;
}
}
}
These two functions are doing the same thing. The only difference is the order of parameters of
Fma.MultiplyAdd. However, the codegen of the second function is not as good as the first one. Maybe worth looking at this to see if the JIT can optimize it.Codegen of foo2:

Codegen of foo3:

category:cq
theme:register-allocator
skill-level:expert
cost:medium