Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to rotate c++ fast

Status
Not open for further replies.

Wiljan

Junior Member level 3
Joined
Jun 27, 2015
Messages
28
Helped
3
Reputation
6
Reaction score
3
Trophy points
3
Activity points
303
I do work on some software in c++ in MSVS express 2013 where I need to rotate a lot and as fast as possible.

so in C, C++ and probably many other langue you can write

A=B >> 5 ... and then B will rotate 5 times right and store it in A.

But what does actually happens under the hood?

The c code will be compiled to asm and probably use a SHR which only rotate reg 1 bit at a time so now you will have some loop counting from 5 to 0 .... not so quick.

if you are on a intel x64 PC and you could end up with SHDL which can accept the rotate value... so my question are will it tale the same time to rotate different values ?like
A=B >> 5
A=B >> 27
A=B >> 8

Any good suggestions for fast rotaing?
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
48,365
Helped
14,245
Reputation
28,751
Reaction score
12,931
Trophy points
1,393
Location
Bochum, Germany
Activity points
279,954
A=B >> 5 is shift rather than rotate. There's no C or C++ operator for rotation. Some compilers have custom functions for it.

Implementation is a matter of CPU capabilities. MSVC will surely use multi-rotate instructions on X86 platform. Review assembly listings in case of doubt.

Depending on the context, shifts by a multiple of eight might be implemented as byte or word swaps.
 

Wiljan

Junior Member level 3
Joined
Jun 27, 2015
Messages
28
Helped
3
Reputation
6
Reaction score
3
Trophy points
3
Activity points
303
Sorry for the rotate it's for sure shifting I wanted to do.

Typical to pick just 1 bit out of 32 of 64 bit

So what you say are if I use inline asm for the shifting intel x64 it will take the same amount of clock cycles to shift as long the shift are with in the register width?

A=B >> 5
A=B >> 27
A=B >> 8

The 3 examples will take same time?
Thank you
 

srizbf

Advanced Member level 5
Joined
Apr 3, 2010
Messages
1,995
Helped
417
Reputation
840
Reaction score
328
Trophy points
1,363
Activity points
10,985
it all depends on the processor lying behind.

if the CPU has shifter(barrel type) , then all shift in same time.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top