Created
May 5, 2025 21:40
-
-
Save DocBohn/377ab44e98dc462154ee1eb810ee4664 to your computer and use it in GitHub Desktop.
Code demonstrating that branchless programming isn't always useful
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int apply_feedback_if(int level, int feedback, int deviation) { | |
if (deviation < 0) { | |
return level + feedback; | |
} else { | |
return level - feedback; | |
} | |
} | |
int apply_feedback_ternary(int level, int feedback, int deviation) { | |
return deviation < 0 ? level + feedback : level - feedback; | |
} | |
int apply_feedback_unconditional(int level, int feedback, int deviation) { | |
int add_subtract = -1 - 2 * (deviation >> (8 * sizeof(int) - 1)); | |
return level + add_subtract * feedback; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// -Og | |
apply_feedback_if_Og: | |
tbnz w2, #31, .L4 | |
sub w0, w0, w1 | |
.L1: | |
ret | |
.L4: | |
add w0, w0, w1 | |
b .L1 | |
apply_feedback_ternary_Og: | |
tbnz w2, #31, .L8 | |
sub w0, w0, w1 | |
.L5: | |
ret | |
.L8: | |
add w0, w0, w1 | |
b .L5 | |
apply_feedback_unconditional_Og: | |
asr w2, w2, 31 | |
mvn w2, w2, lsl 1 | |
madd w0, w2, w1, w0 | |
ret | |
// -O1 | |
apply_feedback_if_O1: | |
add w3, w0, w1 | |
sub w0, w0, w1 | |
cmp w2, 0 | |
csel w0, w0, w3, ge | |
ret | |
apply_feedback_ternary_O1: | |
add w3, w0, w1 | |
sub w0, w0, w1 | |
cmp w2, 0 | |
csel w0, w0, w3, ge | |
ret | |
apply_feedback_unconditional_O1: | |
asr w2, w2, 31 | |
mvn w2, w2, lsl 1 | |
madd w0, w2, w1, w0 | |
ret |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -Og | |
apply_feedback_if_Og: | |
testl %edx, %edx | |
js .L4 | |
movl %edi, %eax | |
subl %esi, %eax | |
ret | |
.L4: | |
leal (%rdi,%rsi), %eax | |
ret | |
apply_feedback_ternary_Og: | |
testl %edx, %edx | |
js .L8 | |
movl %edi, %eax | |
subl %esi, %eax | |
ret | |
.L8: | |
leal (%rdi,%rsi), %eax | |
ret | |
apply_feedback_unconditional_Og: | |
sarl $31, %edx | |
leal (%rdx,%rdx), %eax | |
notl %eax | |
imull %esi, %eax | |
addl %edi, %eax | |
ret | |
# -O1 | |
apply_feedback_if_O1: | |
leal (%rdi,%rsi), %eax | |
subl %esi, %edi | |
testl %edx, %edx | |
cmovns %edi, %eax | |
ret | |
apply_feedback_ternary_O1: | |
leal (%rdi,%rsi), %eax | |
subl %esi, %edi | |
testl %edx, %edx | |
cmovns %edi, %eax | |
ret | |
apply_feedback_unconditional_O1: | |
sarl $31, %edx | |
leal (%rdx,%rdx), %eax | |
notl %eax | |
imull %esi, %eax | |
addl %edi, %eax | |
ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment