Skip to content

Instantly share code, notes, and snippets.

@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;
}
@DocBohn
DocBohn / restrict-demonstration.c
Last active May 12, 2025 21:32
Code demonstrating the use of the `restrict` keyword
#include <string.h>
void simple_string_copy_without_restrict(char *destination, char const *source) {
for (unsigned int i = 0; i <= strlen(source); i++) {
destination[i] = source[i];
}
}
void simple_string_copy_with_restrict(char *restrict destination, char const *restrict source) {
for (unsigned int i = 0; i <= strlen(source); i++) {
@DocBohn
DocBohn / pure-function-migration.c
Created May 5, 2025 03:08
Code demonstrating hand-crafted migration of a call to a pure function out of the loop
#include <string.h>
size_t strlen(char const *string) [[unsequenced]]; // may help in the future
void simple_string_copy(char *restrict destination, char const *restrict source) {
for (unsigned int i = 0; i < strlen(source); i++) {
destination[i] = source[i];
}
}
@DocBohn
DocBohn / loop-invariant-migration.c
Created May 4, 2025 22:58
Code demonstrating hand-crafted and compiler-generated migration of loop-invariant code out of the loop
void migration(int *destination, int const *source, int factor, int limit) {
for (int i = 0; i < limit; i++) {
destination[factor * limit + i] = source[i];
}
}
@DocBohn
DocBohn / common-subexpression.c
Last active May 4, 2025 23:01
Code demonstrating hand-crafted and compiler-generated common subexpression elimination
double average_neighbors(double const *matrix, unsigned int const neighbors[]);
double no_cse(unsigned int i, unsigned int j, unsigned int width, double values[][width]) {
unsigned int neighbors[4];
neighbors[0] = (i-1) * width + j; // values[i-1][j]
neighbors[1] = (i+1) * width + j; // values[i+1][j]
neighbors[2] = i * width + (j-1); // values[i][j-1]
neighbors[3] = i * width + (j+1); // values[i][j+1]
return average_neighbors((double const *) values, neighbors);
}
@DocBohn
DocBohn / loop_indexing.c
Last active July 26, 2025 13:40
Code that measures the time to iterate over nested arrays
#include <stdio.h>
#include <time.h>
#define RANGE 16384
int src1[RANGE][RANGE];
int src2[RANGE][RANGE];
int dest[RANGE][RANGE];
int main() {
@DocBohn
DocBohn / print_stack_address.c
Last active July 25, 2025 21:53
Code that prints the address of a variable that is stored on the stack.
#include <stdio.h>
int main() {
long l = 1L;
printf("Address: %p\n", &l);
return 0;
}
@DocBohn
DocBohn / catch-all.c
Created March 19, 2025 15:23
Demonstration of a program that must be killed with a signal other than SIGINT
#include <stdio.h>
#include <signal.h>
void count_loop_iterations(int ignored) ;
void catch_all(int signal);
int main() {
sigaction(SIGINT, &((struct sigaction) {.sa_handler = alarm_handler}), nullptr);
sigaction(SIGVTALRM, &((struct sigaction) {.sa_handler = alarm_handler}), nullptr);
count_loop_iterations(2000);