Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programming without jumps

Tags:

c

optimization

I try to find articles, books or anything about programming without jumps (x86 arch). I know that generally it is impossible but I try to avoid jumps but gcc even with inline func uses jumps many times. Coding only in Assembly is some sort of solution, but writing equivalent of 1000 lines in C is like hell party to my eyes..

like image 361
Xamael Avatar asked Sep 04 '26 03:09

Xamael


2 Answers

Unless your jumps are really random, branch prediction should eliminate most of overhead involved.

I would dedicate more effort to optimizing memory access patterns in order to improve locality and reduce cache misses. These days, memory latency is the major bottleneck to performance.

Another good direction is improving parallelism (using both vectorized SIMD instructions and, if possible, more than one core).

like image 83
nimrodm Avatar answered Sep 05 '26 19:09

nimrodm


Optimize only performance critical code, and only once you really know it is performance critical. Do not try to optimize jumps only because you read they case a performance hit. Everything causes a performance hit, and the fastest possible code is the code which does nothing. There are other things much worse than jumps.

If you will show a particular example of a jump in the generated code, chance is there will be some way to avoid it, but it is more likely the code you will show will still contain more serious issues.

One particular way how to avoid branches is to use "conditional move" instructions. They can be used e.g. to compute max or min. If you allow the compiler to use SSE architecture, it assumes the CPU also supports CMOV/FCOMI/FCOMIP/FUCOMI/FUCOMIP instructions and will use them (beware: sometimes it may be tricky to make the compiler to do what you want, see e.g. this gamedev.net discussion).

like image 21
Suma Avatar answered Sep 05 '26 18:09

Suma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!