android ndk - What effect will branch prediction have on the following C loop? -
my experience c relatively modest, , lack understanding of compiled output on modern cpus. context: i'm working on image processing android app. have read branch-free machine code preferred inner loops, i'd know whether there significant performance difference between this:
if (p) { double loop, computing f() } else if (q) { double loop, computing g() } else { double loop, computing h() }
versus less verbose version condition checking within loop:
for (int = 0; < xres; i++) { (int j = 0; j < yres; j++) { image[i][j] = p ? f() : (q ? g() : h()); } }
in code, p , q expressions mode == 3
, mode
passed function , never changed within it. have 3 simple questions:
(1) first, more verbose version compile more efficient code second version?
(2) second version, performance improve if evaluate , store results of p
, q
above loop, can replace boolean expressions in loop variables?
(3) should worried this, or branch prediction (or other optimization) ensure boolean expressions in loop(s) never evaluated anyway?
finally, i'd delighted if can whether answers these 3 questions depend on architecture. i'm interested in main android ndk platforms: arm, mips, x86 etc. in advance!
it looks question well-answered here: compiler performs loop unswitching, removing conditional loop , automatically generating 3 copies of loop, stark suggested. moreover, comments given there , above, seems branch prediction works loops these.
Comments
Post a Comment