Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 278991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T01:19:39+00:00 2026-05-12T01:19:39+00:00

I would like to copy a relatively short sequence of memory (less than 1

  • 0

I would like to copy a relatively short sequence of memory (less than 1 KB, typically 2-200 bytes) in a time critical function. The best code for this on CPU side seems to be rep movsd. However I somehow cannot make my compiler to generate this code. I hoped (and I vaguely remember seeing so) using memcpy would do this using compiler built-in intrinsics, but based on disassembly and debugging it seems compiler is using call to memcpy/memmove library implementation instead. I also hoped the compiler might be smart enough to recognize following loop and use rep movsd on its own, but it seems it does not.

char *dst;
const char *src;
// ...
for (int r=size; --r>=0; ) *dst++ = *src++;

Is there some way to make the Visual Studio compiler to generate rep movsd sequence other than using inline assembly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T01:19:40+00:00Added an answer on May 12, 2026 at 1:19 am

    Using memcpy with a constant size

    What I have found meanwhile:

    Compiler will use intrinsic when the copied block size is compile time known. When it is not, is calls the library implementation. When the size is known, the code generated is very nice, selected based on the size. It may be a single mov, or movsd, or movsd followed by movsb, as needed.

    It seems that if I really want to use movsb or movsd always, even with a “dynamic” size I will have to use inline assembly or special intrinsic (see below). I know the size is “quite short”, but the compiler does not know it and I cannot communicate this to it – I have even tried to use __assume(size<16), but it is not enough.

    Demo code, compile with “-Ob1 (expansion for inline only):

      #include <memory.h>
    
      void MemCpyTest(void *tgt, const void *src, size_t size)
      {
        memcpy(tgt,src,size);
      }
    
      template <int size>
      void MemCpyTestT(void *tgt, const void *src)
      {
        memcpy(tgt,src,size);
      }
    
      int main ( int argc, char **argv )
      {
        int src;
        int dst;
        MemCpyTest(&dst,&src,sizeof(dst));
        MemCpyTestT<sizeof(dst)>(&dst,&src);
        return 0;
      }
    

    Specialized intrinsics

    I have found recently there exists very simple way how to make Visual Studio compiler copy characters using movsd – very natural and simple: using intrinsics. Following intrinsics may come handy:

    • __movsb
    • __movsw
    • __movsd
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like copy just file.xml without folder structure using overlays like this: <overlays>
I would like to copy lines from stdin to an array of character pointers.
I would like to copy stdout and stderr of my Perl script to a
I would like to copy an array of pixel data (colors) into a texture.
I would like to copy to clipboard what a Control of my WPF app
I would like to copy a macro from one Excel workbook to another using
I would like to copy one object to another object, and the fields with
I would like to prevent copy, cut and paste in my TEdit . How
I would like to create a copy of a database with approximately 40 InnoDB
I would like to write an application that will copy MP3 files to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.