forked from parallel101/course
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
Binary file not shown.
This file contains 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
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; | ||
} | ||
} |
This file contains 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
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); | ||
} |
This file contains 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
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; | ||
} |
This file contains 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
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); | ||
} |
This file contains 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
Binary file not shown.