char[] to hex string exercise

前端 未结 16 864
感情败类
感情败类 2021-01-12 19:14

Below is my current char* to hex string function. I wrote it as an exercise in bit manipulation. It takes ~7ms on a AMD Athlon MP 2800+ to hexify a 10 million byte array. Is

16条回答
  •  死守一世寂寞
    2021-01-12 19:53

    If you're rather obsessive about speed here, you can do the following:

    Each character is one byte, representing two hex values. Thus, each character is really two four-bit values.

    So, you can do the following:

    1. Unpack the four-bit values to 8-bit values using a multiplication or similar instruction.
    2. Use pshufb, the SSSE3 instruction (Core2-only though). It takes an array of 16 8-bit input values and shuffles them based on the 16 8-bit indices in a second vector. Since you have only 16 possible characters, this fits perfectly; the input array is a vector of 0 through F characters, and the index array is your unpacked array of 4-bit values.

    Thus, in a single instruction, you will have performed 16 table lookups in fewer clocks than it normally takes to do just one (pshufb is 1 clock latency on Penryn).

    So, in computational steps:

    1. A B C D E F G H I J K L M N O P (64-bit vector of input values, "Vector A") -> 0A 0B 0C 0D 0E 0F 0G 0H 0I 0J 0K 0L 0M 0N 0O 0P (128-bit vector of indices, "Vector B"). The easiest way is probably two 64-bit multiplies.
    2. pshub [0123456789ABCDEF], Vector B

提交回复
热议问题