Thanks Paolo.
Just for fun I made it in assember 32bits:
bool UIntOverflowAsm(const char *p)
{
__asm
{
xor ebx,ebx;
mov edx, p;
repeat:
movsx eax,byte ptr [edx];
cmp eax,0x30; //'0'
jb success;
cmp eax,0x39; //'9'
ja success;
sub eax,0x30; //*p-'0'
shl ebx,1; //ebx = val*2
jc ovflow; //jump if overflow
add eax, ebx; //eax=val*2+actual digit
jc ovflow; //jump if overflow
shl ebx,1; //ebx=val*4
jc ovflow; //jump if overflow
shl ebx,1; //ebx=val*8
jc ovflow; //jump if overflow
add ebx,eax; //ebx=val*8+val*2+actual digit=val*10+actual digit
jc ovflow; //jump if overflow
inc edx; //p++
jmp repeat;
}
ovflow:
return 0;
success:
return 1;
}
For 64 bits the C short version is already optimized.
As suggested computing the length of the string can shorter the test, but, apart the time to compute length, the string must not include non numeric characters. I.e. testing for "0 foo bar foo" will fail.