News for hackers. Stuff that matters.
Re:GodBolt: Enter C, get Assembly by Veedrac on Thursday December 15, 2016 @09:19PM (#13190558)
First it normalizes to

    if (num <= 0) {
    	return 0;
    }
  
    int a = 0;
    int loop_n = (unsigned)(num - 1) / 5;
    for (int x = 0; x <= loop_n; x++) {
        a += x * 5 - 1;
    }
    return a;
Division by 5 is done by multiplying by the modular inverse, 3435973837.

From there you just have a summation, which has two components.

    ((loop_n + 1) * loop_n / 2) * 5    // 5x
    - loop_n - 1                       // -1
The assembly between these the manual and automatic methods is slightly different, but the difference is fairly trivial and opaque.

There are no comments on this comment yet.