Skip to content

Instantly share code, notes, and snippets.

@DocBohn
DocBohn / base-two-log.c
Created May 13, 2025 14:38
Code demonstrating that branchless programming often results in unreadable code
#include <stdint.h>
int switch_lg(uint32_t power_of_two) {
switch (power_of_two) {
case 0x1 :
return 0;
case 0x2 :
return 1;
case 0x4 :
return 2;
@DocBohn
DocBohn / trim.c
Created May 13, 2025 02:17
Code demonstrating that early loop termination using `break` is not an optimization
#include <string.h>
int trim_k_and_r(char s[]) {
int n;
for (n = strlen(s) - 1; n >= 0; n--) {
if (s[n] != ' ' && s[n] != '\t' && s[n] != '\n') {
break;
}
}
s[n + 1] = '\0';
@DocBohn
DocBohn / store-migration.c
Last active May 9, 2025 11:50
Code that demonstrations elimination of redundant stores
void multiply_add(int *accumulator, int multiplier, int addend) {
*accumulator *= multiplier;
*accumulator += addend;
}
@DocBohn
DocBohn / impossible-if-conversion.c
Created May 6, 2025 03:07
Code demonstrating that some "branchless" transformations cannot be made by the compiler
// The ternary operator version and the bit manipuation version is *not* equivalent to the "if" version
// since they'll have a memory access regardless of array[i]'s value
void clamp_if(int *array, int length) {
for (int i = 0; i < length; i++) {
if (array[i] < 0) {
array[i] = 0;
}
}
}
@DocBohn
DocBohn / branchless-loop-body.c
Created May 5, 2025 21:47
Code demonstrating that the compiler can eliminate branches to make vectorization possible
// extracting the load in each conditional path or each half of the conditional assignment was necessary for vectorization
// duplicate loads is the impediment to vectorization, not if/else statements
void loop_feedback_if(int *restrict levels, int *restrict deviations, int length, int feedback) {
for (int i = 0; i < length; i++) {
int level = levels[i];
if (deviations[i] < 0) {
levels[i] = level + feedback;
} else {
levels[i] = level - feedback;
@DocBohn
DocBohn / branchless.c
Created May 5, 2025 21:40
Code demonstrating that branchless programming isn't always useful
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;
@DocBohn
DocBohn / loop-unrolling.c
Last active May 9, 2025 20:55
Code demonstrating hand-crafted and compiler-generated loop unrolling
static int values[] = {5, 6, 2, 9, 8, 2, 9, 7};
long basic_loop(unsigned int count) {
long sum = 0;
for(unsigned int i = 0; i < count; i++) {
sum += values[i % 8];
}
return sum;
}
@DocBohn
DocBohn / vectorization.c
Last active May 11, 2025 17:49
Code demonstrating hand-crafted and compiler-generated vectorization
void simple_memory_copy(char *restrict destination, char const *restrict source, unsigned int length) {
for (unsigned int i = 0; i < length; i++) {
destination[i] = source[i];
}
}
@DocBohn
DocBohn / minimize-loadstore.c
Last active May 5, 2025 17:48
Code demonstrating hand-crafted and compiler-generated migration of redundant loads/stores out of a loop
void nonoptimizable_accumulate(int *accumulator, int const *array, int length) {
for (int i = 0; i < length; i++) {
*accumulator += array[i];
}
}
void optimizable_accumulate(int *restrict accumulator, int const *restrict array, int length) {
for (int i = 0; i < length; i++) {
*accumulator += array[i];
}
@DocBohn
DocBohn / inlining.c
Created May 5, 2025 16:06
Code demonstrating hand-crafted and compiler-generated function inlining
static int compute_triangle_number(int base) {
int base_incremented = base + 1;
int numerator = base * base_incremented;
return numerator/2;
}
long simple_bonus_threshold(unsigned int number) {
int triangle_number = compute_triangle_number(number);
return 3 * triangle_number;
}