字符串转整数,哪位高手来下挑战极限速度

字符串转整数,谁来下挑战极限速度!
本帖最后由 zjcqoo 于 2012-09-19 16:02:07 编辑
需求
  字符分析无需太复杂,只要能识别负数就行。遇到非数字则结束。使用单线程计算。
  "12345" => 12345
  "-1357" => -1357
  "135.7" => 135
  "abcde" => 0
评分
  相对C语言自带的atoi的性能倍数。比率值越大越好。
  
  
  我先提供一个。(本机测试,比C自带的 atoi 快3.8倍)

int atoi_v1(const char* s)
{
    int sign = 0;

    if(s[0] == '-')
    {
        sign = 1;
        s++;
    }

    __asm
    {
        mov ecx, s              ; pos = s

        xor eax, eax            ; 总数
        xor edx, edx            ; 个位数

        ;------------------------------
        ;     二进制      Ascii
        ;   0011-0000     '0'
        ;   0011-0001     '1'
        ;   ...
        ;   0011-1001     '9'
        ;------------------------------
next_char:
        mov dl, byte ptr [ecx]  ; dl = *pos

        cmp dl, '0'             ; 判断字符是否为数字
        jb done
        cmp dl, '9'
        ja done

        and dl, 00001111b       ; 低4位,对应十进制数字

        shl eax, 1              ; sum = sum * 10
        lea eax, [eax * 4 + eax]

        add eax, edx            ; sum = sum + dig

        inc ecx                 ; pos++
        jmp next_char
done:
        cmp sign, 1             ; 判断是否有负号
        jne no_sign
        neg eax                 ; sum = -sum
no_sign:
    }
}