Thread: What is the faster way to add byte before each byte in char *

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    5

    What is the faster way to add byte before each byte in char *

    If I have a char* for example :
    Code:
    {0x01,0x02,0x03,0x04,0x05}
    and I want to add 0x00 before each byte .

    the result will be :
    Code:
    {0x00,0x01,0x00,0x02,0x00,0x03,0x00,0x04,0x00,0x05}
    What is the faster way to do that ?

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Probably picking up more than one character at a time, which would be processor dependent. In assembly on X86 with xmm registers, you could load 16 bytes at a time, copy to another xmm register, then use pshufb on both xmm registers to insert the zeroes, then store 16 bytes at a time (twice).
    Last edited by rcgldr; 01-03-2019 at 01:13 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > What is the faster way to do that ?
    Faster than what?
    You haven't shown us what you're doing at the moment.

    Unless you have some woefully underpowered micro-controller, or needing to do 100's of MB/sec, this looks like a premature optimisation.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2016
    Posts
    104
    Quote Originally Posted by rcgldr View Post
    Probably picking up more than one character at a time, which would be processor dependent. In assembly on X86 with xmm registers, you could load 16 bytes at a time, copy to another xmm register, then use pshufb on both xmm registers to insert the zeroes, then store 16 bytes at a time (twice).
    For those of us not familiar with assembly, couldn't the same be achieved by memcpy'ing into a uint16_t pointer? Would probably need to account for endianness though.





    EDIT: offtopic, BTW, do u guys know if the forums were under ddos attack? It was hard to access the site the past 24 hours. I was getting this error:What is the faster way to add byte before each byte in char *-screen-shot-2019-01-03-3-37-36-am-jpg
    Last edited by Dren; 01-03-2019 at 07:24 PM.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by Dren View Post
    For those of us not familiar with assembly, couldn't the same be achieved by memcpy'ing into a uint16_t pointer? Would probably need to account for endianness though.
    Not as fast. The instruction pshufb will shuffle the bytes and insert 0's in 1 cycle, and the operations on two separate xmm registers occur in parallel.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-22-2016, 03:20 AM
  2. File Comparision byte by byte
    By anusha2489 in forum C Programming
    Replies: 12
    Last Post: 05-16-2011, 06:58 AM
  3. reading files byte by byte
    By cpsc in forum C++ Programming
    Replies: 12
    Last Post: 01-07-2011, 03:54 PM

Tags for this Thread