What's the fastest way to convert hex to integer in C++?

后端 未结 6 591
甜味超标
甜味超标 2021-02-01 08:52

I\'m trying to convert a hex char to integer as fast as possible.

This is only one line: int x = atoi(hex.c_str);

Is there a faster way

6条回答
  •  孤城傲影
    2021-02-01 09:37

    In case you (or someone else) are actually converting an array of values, I made an AVX2 SIMD encoder and decoder that benchmarks ~12x faster than the fastest scalar implementation: https://github.com/zbjornson/fast-hex

    The 16 hex values conveniently fit (twice) into a YMM register, so you can use PSHUFB to do a parallelized lookup. Decoding is a bit harder and based on bit-wise ops.

提交回复
热议问题