Skip to content

Commit

Permalink
slides
Browse files Browse the repository at this point in the history
  • Loading branch information
archibate committed Aug 20, 2022
1 parent 7abcc54 commit 5e483e6
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 1 deletion.
Binary file removed Presentation1.pptx
Binary file not shown.
7 changes: 7 additions & 0 deletions specifelse/1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
int func(int x) {
if (x > 0) {
return 1;
} else {
return 2;
}
}
11 changes: 11 additions & 0 deletions specifelse/2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
int slow(int x) {
if (x > 0) {
return 1;
} else {
return 0;
}
}

int fast(int x) {
return (x > 0);
}
11 changes: 11 additions & 0 deletions specifelse/3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
int slow(int x) {
if (x > 0) {
return 42;
} else {
return 32;
}
}

int fast(int x) {
return 32 + (x > 0) * 10;
}
47 changes: 47 additions & 0 deletions specifelse/4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <vector>
#include <algorithm>
#include "ticktock.h"
#include "randint.h"

static int ifelse_clamp(int x) {
if (x < 0) {
return 0;
} else if (x > 255) {
return 1;
} else {
return x;
}
}

static int bailan_clamp(int x) {
return (x < 0) ? 0 : ((x > 255) ? 1 : x);
}

static int addmul_clamp(int x) {
return (x >= 0) * (x <= 255) * x + (x > 255);
}

__attribute__((noinline)) void test(int *a, int n, int (*clamp)(int)) {
for (int i = 0; i < n; i++) {
a[i] = clamp(a[i]);
}
}

int main() {
std::vector<int> a((int)1e7);

std::generate(a.begin(), a.end(), randint<int, -512, 512>);
TICK(ifelse);
test(a.data(), a.size(), ifelse_clamp);
TOCK(ifelse);

std::generate(a.begin(), a.end(), randint<int, -512, 512>);
TICK(bailan);
test(a.data(), a.size(), bailan_clamp);
TOCK(bailan);

std::generate(a.begin(), a.end(), randint<int, -512, 512>);
TICK(addmul);
test(a.data(), a.size(), addmul_clamp);
TOCK(addmul);
}
8 changes: 7 additions & 1 deletion specifelse/randint.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
template <class T>
static T randint(T minVal, T maxVal) {
static std::mt19937 gen(0);
std::uniform_int_distribution<char> uni(minVal, maxVal);
std::uniform_int_distribution<T> uni(minVal, maxVal);
return uni(gen);
}

template <class T, T minVal, T maxVal>
static T randint() {
static std::mt19937 gen(0);
std::uniform_int_distribution<T> uni(minVal, maxVal);
return uni(gen);
}
Binary file added specifelse/slides.pptx
Binary file not shown.

0 comments on commit 5e483e6

Please sign in to comment.