Skip to content

Commit

Permalink
Move struct and upfront tests to separate library
Browse files Browse the repository at this point in the history
  • Loading branch information
robertdfrench committed Sep 1, 2024
1 parent 8589d60 commit e74142a
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 35 deletions.
16 changes: 14 additions & 2 deletions code/speed_demo/struct/Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
main.exe: main.c
gcc $< -o $@
test: main.exe
time -p ./main.exe

main.exe: main.o libincrement.so
gcc -Wl,-rpath,$(CURDIR) -o $@ $< -L. -lincrement

libincrement.so: libincrement.o
gcc -shared -o $@ $<

%.o: %.c
gcc -fPIC -Wall -c $< -o $@

clean:
rm -f *.exe *.o *.so
10 changes: 10 additions & 0 deletions code/speed_demo/struct/libincrement.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Use this incrementer algorithm if AVX2 is available.
int fancy_incrementer(int x) {
return x + 1;
}

// Use this if AVX2 is not available. It's the same as above, because we don't
// actually rely on AVX2.
int normal_incrementer(int x) {
return x + 1;
}
23 changes: 7 additions & 16 deletions code/speed_demo/struct/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,15 @@
#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>
static int counter = 0;

struct myfuncs {
void (*increment_counter)();
int (*increment)(int);
};

static struct myfuncs *page;


// Use this incrementer algorithm if AVX2 is available.
void fancy_incrementer() {
counter += 1;
}

// Use this if AVX2 is not available. It's the same as above, because we don't
// actually rely on AVX2.
void normal_incrementer() {
counter += 1;
}
int fancy_incrementer(int);
int normal_incrementer(int);

void initfuncs(void) {
int pagesize = getpagesize();
Expand All @@ -34,9 +24,9 @@ void initfuncs(void) {

__builtin_cpu_init();
if (__builtin_cpu_supports("avx2")) {
page->increment_counter = fancy_incrementer;
page->increment = fancy_incrementer;
} else {
page->increment_counter = normal_incrementer;
page->increment = normal_incrementer;
}

if(mprotect(page, pagesize, PROT_READ))
Expand All @@ -48,8 +38,9 @@ void initfuncs(void) {
int main(void) {
initfuncs();

int counter = 0;
while (counter < INT_MAX) {
page->increment_counter();
counter = page->increment(counter);
}

return 0;
Expand Down
16 changes: 14 additions & 2 deletions code/speed_demo/upfront/Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
main.exe: main.c
gcc $< -o $@
test: main.exe
time -p ./main.exe

main.exe: main.o libincrement.so
gcc -Wl,-rpath,$(CURDIR) -o $@ $< -L. -lincrement

libincrement.so: libincrement.o
gcc -shared -o $@ $<

%.o: %.c
gcc -fPIC -Wall -c $< -o $@

clean:
rm -f *.exe *.o *.so
10 changes: 10 additions & 0 deletions code/speed_demo/upfront/libincrement.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Use this incrementer algorithm if AVX2 is available.
int fancy_incrementer(int x) {
return x + 1;
}

// Use this if AVX2 is not available. It's the same as above, because we don't
// actually rely on AVX2.
int normal_incrementer(int x) {
return x + 1;
}
22 changes: 7 additions & 15 deletions code/speed_demo/upfront/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,18 @@
#include <limits.h>
#include <stddef.h>
#include <stdbool.h>
static int counter = 0;
static bool cpu_has_avx2 = false;

// Use this incrementer algorithm if AVX2 is available.
void avx2_incrementer() {
counter += 1;
}

// Use this if AVX2 is not available. It's the same as above, because we don't
// actually rely on AVX2.
void normal_incrementer() {
counter += 1;
}
int fancy_incrementer(int);
int normal_incrementer(int);

// Select an "appropriate" incrementer based on CPU features. The actual choice
// doesn't matter in this case, we just need something for the resolver to do.
void increment_counter() {
int increment(int x) {
if (cpu_has_avx2) {
avx2_incrementer();
return fancy_incrementer(x);
} else {
normal_incrementer();
return normal_incrementer(x);
}
}

Expand All @@ -43,8 +34,9 @@ int main() {
detect_cpu_features();

// Count to ~ 2 Billion by calling a dynamically-resolved incrementer
int counter = 0;
while (counter < INT_MAX) {
increment_counter();
counter = increment(counter);
}
return 0;
}

0 comments on commit e74142a

Please sign in to comment.