I just passed this function to the job. This is done (hence no home label). But I would like to see how this can be improved.
Essentially, the function sums the squares of all integers between 1 and a given number using the following formula:
n(n+1)(2n+1)/6
Where nis the maximum number.
The function below is designed to catch any overflow and return 0 in case of occurrence.
UInt32 sumSquares(const UInt32 number)
{
int result = 0;
__asm
{
mov eax, number
mov edx, 2
mul edx
jo end
add eax, 1
jo end
mov ecx, eax
mov ebx, number
add ebx, 1
jo end
mov eax, number
mul ebx
jo end
mul ecx
jo end
mov ebx, 6
div ebx
mov result, eax
end:
}
return result;
}
Basically, I want to know what I can improve there. In terms of best practices basically. One thing is clear: a smart overflow check (with one check on what maximum input will cause an overflow).
source
share