Skip to main content

Practice ยท 2 of 5

Folding vs Runtime

CHALLENGE
Difficulty: advanced+25 XP

Implement int folded_sq(int x) returning x*x with locals; int unfolded_sq(int x) computing x*x through a volatile local; and long long fold_penalty(int x) returning the *number of multiplications* each must perform โ€” implement it as folded_count - unfolded_count where folded_count is 0 (the multiply folds away) and unfolded_count is 1 (volatile forces the runtime multiply).

Back to lesson: Practice: UB and Optimizer Drills