Shifting a double-wide variable - bit twiddling

  • Thread starter Thread starter CRGreathouse
  • Start date Start date
  • Tags Tags
    Bit Variable
AI Thread Summary
The discussion focuses on efficiently shifting a double-wide variable stored in two parts, left and right, for high-performance applications requiring over 1e12 shifts. The initial method proposed involves a right shift of 2 using five operations and two assignments. Concerns are raised about the efficiency of large shifts, particularly a shift of 62 bits, and whether modern processors handle such shifts efficiently compared to older ones. Suggestions include using a switch statement for precomputed shifts and optimizing the operation count. It is noted that replacing the addition with a bitwise OR may be beneficial, as it simplifies the operation and could potentially enhance performance. Overall, the conversation emphasizes the need for efficient bit manipulation techniques in programming for performance-critical applications.
CRGreathouse
Science Advisor
Homework Helper
Messages
2,832
Reaction score
0
Shifting a double-wide variable -- bit twiddling

I have a number too long to fit into the largest variable size (unsigned long long, 64 bits in this case) so I'm storing it in two variables, left and right. I want to shift it -- and to do it quickly, as I'll do this more than 1e12 times.

The 'obvious' way is

Code:
unsigned long long right, left;

. . .

right = ((left&3) << 62) + (right >> 2);
left >>= 2;

for a total of 5 operations and two assignments. This example is a right shift of 2, but right shifts by 1 and 4 are also important. In any case the size of the shift should be known at compile time.

1. Is a shift of 62 too large to be done efficiently? I know that older processors used to take O(n) time to shift n places (something like 3+n micro-ops), but I don't know if this holds on modern processors. Perhaps I should make a switch statement with constant to add (1 << 62, 2 << 62, and 3 << 62 precomputed)? Or would the branch misprediction outweigh this?
2. Is there a way to do this with fewer operations? I'm sure there are some people out there who have been doing bit manipulations far longer than I have.

For what it's worth I'm on a Core Duo, but I'm hoping for improvements to my program that work on most systems.
 
Technology news on Phys.org
You can replace "(left&3)" with just "left" since the bit mask operation is redundant given the left shift. It cuts your cost down to 4 operations.

I would also replace the "+" operator with a bitwise OR instead. You are not adding values but combining bits. It may not actually save any execution time, but a "|" is simpler than a "+" operation so there is a remote chance that it is faster on some systems.

right >>= 2;
right |= left << 62;
left >>= 2;
 
Ooh, thanks. That does help.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top