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

  • Home
  • SEARCH
  • 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 5945039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:37:32+00:00 2026-05-22T16:37:32+00:00

Consider the simple code: UINT64 result; UINT32 high, low; … result = ((UINT64)high <<

  • 0

Consider the simple code:

UINT64 result;
UINT32 high, low;
...
result = ((UINT64)high << 32) | (UINT64)low;

Do modern compilers turn that into a real barrel shift on high, or optimize it to a simple copy to the right location?

If not, then using a union would seem to be more efficient than the shift that most people appear to use. However, having the compiler optimize this is the ideal solution.

I’m wondering how I should advise people when they do require that extra little bit of performance.

  • 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-22T16:37:33+00:00Added an answer on May 22, 2026 at 4:37 pm

    I wrote the following (hopefully valid) test:

    #include <stdio.h>
    #include <stdint.h>
    #include <stdlib.h>
    
    void func(uint64_t x);
    
    int main(int argc, char **argv)
    {
    #ifdef UNION
      union {
        uint64_t full;
        struct {
          uint32_t low;
          uint32_t high;
        } p;
      } result;
      #define value result.full
    #else
      uint64_t result;
      #define value result
    #endif
      uint32_t high, low;
    
      if (argc < 3) return 0;
    
      high = atoi(argv[1]);
      low = atoi(argv[2]);
    
    #ifdef UNION
      result.p.high = high;
      result.p.low = low;
    #else
      result = ((uint64_t) high << 32) | low;
    #endif
    
      // printf("%08x%08x\n", (uint32_t) (value >> 32), (uint32_t) (value & 0xffffffff));
      func(value);
    
      return 0;
    }
    

    Running a diff of the unoptimized output of gcc -s:

    <   mov -4(%rbp), %eax
    <   movq    %rax, %rdx
    <   salq    $32, %rdx
    <   mov -8(%rbp), %eax
    <   orq %rdx, %rax
    <   movq    %rax, -16(%rbp)
    ---
    >   movl    -4(%rbp), %eax
    >   movl    %eax, -12(%rbp)
    >   movl    -8(%rbp), %eax
    >   movl    %eax, -16(%rbp)
    

    I don’t know assembly, so it’s hard for me to analyze that. However, it looks like some shifting is taking place as expected on the non-union (top) version.

    But with optimizations -O2 enabled, the output was identical. So the same code was generated and both ways will have the same performance.

    (gcc version 4.5.2 on Linux/AMD64)

    Partial output of optimized -O2 code with or without union:

        movq    8(%rsi), %rdi
        movl    $10, %edx
        xorl    %esi, %esi
        call    strtol
    
        movq    16(%rbx), %rdi
        movq    %rax, %rbp
        movl    $10, %edx
        xorl    %esi, %esi
        call    strtol
    
        movq    %rbp, %rdi
        mov     %eax, %eax
        salq    $32, %rdi
        orq     %rax, %rdi
        call    func
    

    The snippet begins immediately after the jump generated by the if line.

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

Sidebar

Related Questions

Consider the following simple C program that read a file into a buffer and
Consider this simple XML document. The serialized XML shown here is the result of
My questions are divided into three parts Question 1 Consider the below code, #include
Consider this simple code: int foo = 4; double d1 = sin (foo); double
Consider the following example code: http://code.google.com/apis/maps/documentation/javascript/examples/streetview-simple.html I can do scrollwheel: false on a mapOptions
consider this simple and pointless code. #include <iostream> struct A { template<int N> void
consider this simple code: echo iconv('UTF-8', 'ASCII//TRANSLIT', 'è'); it prints `e instead of just
consider this simple code: $q=mysql_query('SELECT [...]'); while($row=mysql_fetch_assoc($q)){ //Do something with data //And write how
Consider this simple example code: <form name=text id=frm1 method=post> <input type=checkbox name=name[] value=1000> Chk1<br>
Consider the following simple code to create a typesafe equals. This first section allows

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.