Skip to content

Instantly share code, notes, and snippets.

@baiwfg2
Last active January 10, 2025 12:48
Show Gist options
  • Save baiwfg2/3f087cc0c16ba39d623d6f0122f2d4c2 to your computer and use it in GitHub Desktop.
Save baiwfg2/3f087cc0c16ba39d623d6f0122f2d4c2 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#ifdef NOINLINE
#define NOINLINE_SYM __attribute__((noinline))
#else
#define NOINLINE_SYM
#endif
NOINLINE_SYM int calc_orignal(int n) {
int fact = 1;
for (int i = 1; i < n; i++) {
fact *= i;
}
return fact;
}
NOINLINE_SYM int calc_using_pipeline4(int n) {
int fact0 = 1, fact1 = 1, fact2 = 1, fact3 = 1;
// 为方便起见,这里假设n是4的倍数
for (int i = 1; i < n; i += 4) {
fact0 *= i;
fact1 *= i + 1;
fact2 *= i + 2;
fact3 *= i + 3;
}
return fact0 * fact1 * fact2 * fact3;
}
NOINLINE_SYM int calc_using_pipeline8(int n) {
int fact0 = 1, fact1 = 1, fact2 = 1, fact3 = 1;
int fact4 = 1, fact5 = 1, fact6 = 1, fact7 = 1;
for (int i = 1; i < n; i += 8) {
fact0 *= i;
fact1 *= i + 1;
fact2 *= i + 2;
fact3 *= i + 3;
fact4 *= i + 4;
fact5 *= i + 5;
fact6 *= i + 6;
fact7 *= i + 7;
}
return fact0 * fact1 * fact2 * fact3 * fact4 * fact5 * fact6 * fact7;
}
NOINLINE_SYM int calc_using_fake_pipeline(int n) {
int fact = 1;
for (int i = 1; i < n; i += 4) {
fact *= i;
fact *= i + 1;
fact *= i + 2;
fact *= i + 3;
}
return fact;
}
int main(int argc, char const *argv[])
{
char c = argv[1][0];
int n = 1000000000;
int ret;
if (c == '1') {
ret = calc_orignal(n);
} else if (c == '2') {
ret = calc_using_pipeline4(n);
} else if (c == '3') {
ret = calc_using_fake_pipeline(n);
} else if (c == '4') {
ret = calc_using_pipeline8(n);
}
return ret;
}
// Here is the script that run the test conveniently
/*
#!/bin/bash
set -e
# just for making output prettier
echo_red() {
echo -e "\033[31m$1\033[0m"
}
echo_green() {
echo -e "\033[32m$1\033[0m"
}
echo_blue() {
echo -e "\033[34m$1\033[0m"
}
echo_cyan() {
echo -e "\033[36m$1\033[0m"
}
build_factorial() {
build_args="-DNOINLINE"
echo "---------- using compiler $1"
f=loop_unrolling_example.c
$1 $build_args $f -o a.O0
echo_red "running non-optimized original version ..."
time ./a.O0 1
echo_green "running non-optimized using pipeline4 ..."
time ./a.O0 2
echo_blue "running non-optimized using fake pipeline ..."
time ./a.O0 3
echo_cyan "running non-optimized using pipeline8 ..."
time ./a.O0 4
$1 $build_args $f -O3 -o a.O3
echo_red "running optimized original version ..."
time ./a.O3 1
echo_green "running optimized using pipeline4 ..."
time ./a.O3 2
echo_blue "running optimized using fake pipeline ..."
time ./a.O3 3
echo_cyan "running optimized using pipeline8 ..."
time ./a.O3 4
}
CC=$1
build_factorial $CC
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment