4

Look in the C# Original Code, I see the method of string's EqualsHelper:

    // unroll the loop 
#if AMD64 
    // for AMD64 bit platform we unroll by 12 and
    // check 3 qword at a time. This is less code 
    // than the 32 bit case and is shorter pathlength

    while (length >= 12) 
    {
        if (*(long*)a     != *(long*)b) break; 
        if (*(long*)(a+4) != *(long*)(b+4)) break; 
        if (*(long*)(a+8) != *(long*)(b+8)) break;
        a += 12; b += 12; length -= 12; 
    }
#else
    while (length >= 10)
    { 
        if (*(int*)a != *(int*)b) break;
        if (*(int*)(a+2) != *(int*)(b+2)) break; 
        if (*(int*)(a+4) != *(int*)(b+4)) break; 
        if (*(int*)(a+6) != *(int*)(b+6)) break;
        if (*(int*)(a+8) != *(int*)(b+8)) break; 
        a += 10; b += 10; length -= 10;
    }
#endif

I want to know why the loop unrolls by 10 or 12, if this because of the CPU's optimization: the SIMD(Single Instruction Multiple Data)?

Why don't I write in this way directly:

while (length > 0) 
{
    if (*(int*)a != *(int*)b) break;
    a += 2; b += 2; length -= 2;
}
Jevgeni Geurtsen
  • 3,133
  • 4
  • 17
  • 35
Minotauros
  • 49
  • 3

0 Answers0