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 7990651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:04:48+00:00 2026-06-04T13:04:48+00:00

I’m developing an application where performance is critical. I want GCC to translate some

  • 0

I’m developing an application where performance is critical. I want GCC to translate some specific calls to memset() as an instruction with a repeat prefix like “rep stos QWORD PTR es:[rdi],rax”. GCC does this automatically when the size is both known and small.

However, GCC maps calls to memset() with a random length through a call to memset() via the PLT, which causes a branch misprediction since the branch predictor cache is cold.

Is there a way to force GCC to do what I want (outside of inline assembly)? Note that I don’t want this behavior for the whole program, only for some specific memset() calls.

On a related topic, I’m also interested for any hack that prevents GCC from branching when a cmovcc instruction would do the job (I know about using &,+,etc. instead of &&).

Thanks a lot for any help.

@FrankH:

That’s basically what I ended up doing. Here is my code:

static finline void app_zero(void *dst, uint32_t size, uint32_t count)
{
    // Warning: we tell gcc to use 'dst' both as source and destination here.  
    // This does not cause problems because we don't reuse 'dst'.  
    #ifdef APP_ARCH_X86 
    #define STOS(X,Y) do { \  
        int c = (size/Y)*count; \  
        __asm__ __volatile__("cld; xor %%eax, %%eax; rep stos"X"\n\n" \
                             : "+D"(dst), "+c"(c) :: "rax", "flags"); \  
        } while (0)  
    if (size % 8 == 0)      STOS("q", 8);  
    else if (size % 4 == 0) STOS("l", 4);  
    else if (size % 2 == 0) STOS("w", 2);  
    else                    STOS("b", 1);  
    #undef STOS  
    #else  
    memset(dst, 0, size*count);  
    #endif  
}

Note that your example works in your test setup, but it won’t work
generally. GCC can change the direction flag, so a cld instruction is
necessary. Furthermore, you must tell gcc that %rdi and %rcx will be
changed by the stos instruction, and since gcc won’t allow you to
specify that a register is both an input and clobbered, you must use the
awkward "+" syntax (which will also corrupt your input values).

This is not optimal due to the ‘cld’ instruction, which has a latency of
4 cycles on Nehalem. GCC tracks the flag register state internally
(AFAICT) so it needs not issue that instruction every time.

  • 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-06-04T13:04:49+00:00Added an answer on June 4, 2026 at 1:04 pm

    I don’t know about GCC, but under newer builds of MSVC, using loops to do the setting/copying forced the used of REP STOS (and it still allows optimization for know sizes and auto-vectorization), it may be work a try under GCC.

    the alternative to to check if GCC has a builtin similar to __stosq, else you will probably need to go down to inline assembly, but thats not bad at all under GCC (and its probably the simplest and fastest way).

    your second question is way to generic to really get a good answer, cause it depends on the case at hand, however, GCC should do well enough in optimizing out branches except for specific corner cases (using SETCC/MOVCC/FMOVCC).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the

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.