We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider this function that calculates the number of digits in n's base-10 representation (eg as part of a formatting library):
n
u8 src(u8 n) { u8 num_digits = 0; do { num_digits++; n /= 10; } while (n != 0); return num_digits; }
Since n is in the range 0-255, the loop will run exactly 1, 2 or 3 times, and the function can be optimized by unrolling the loop manually:
0-255
u8 intermediate(u8 n) { u8 num_digits = 0; // iteration 1 num_digits++; n /= 10; if (n == 0) return num_digits; // iteration 2 num_digits++; n /= 10; if (n == 0) return num_digits; // iteration 3 num_digits++; n /= 10; return num_digits; }
which then simplifies to:
u8 tgt(u8 n) { if (n < 10) return 1; if (n < 100) return 2; return 3; }
LLVM is smart enough to do this optimization if the base is 16, but not for any other base
The text was updated successfully, but these errors were encountered:
So do you mean this unrolling works for u16?
Sorry, something went wrong.
No branches or pull requests
Consider this function that calculates the number of digits in
n
's base-10 representation (eg as part of a formatting library):Since
n
is in the range0-255
, the loop will run exactly 1, 2 or 3 times, and the function can be optimized by unrolling the loop manually:which then simplifies to:
LLVM is smart enough to do this optimization if the base is 16, but not for any other base
The text was updated successfully, but these errors were encountered: