optimized itoa function

后端 未结 8 1485
不知归路
不知归路 2021-02-04 06:48

I am thinking on how to implement the conversion of an integer (4byte, unsigned) to string with SSE instructions. The usual routine is to divide the number and store it in a loc

8条回答
  •  孤街浪徒
    2021-02-04 07:19

    For unsigned 0 to 9,999,999 with terminating null. (99,999,999 without)

    void itoa(uint64_t u, char *out) // up to 9,999,999 with terminating zero
    {
        *out = 0;
        do { 
            uint64_t n0 = u;
            *((uint64_t *)out) = (*((uint64_t *)out) << 8) | (n0 + '0' - (u /= 10) * 10);
        } while (u);
    }
    

提交回复
热议问题