Skip to content

Instantly share code, notes, and snippets.

@mrbid
Last active June 12, 2023 20:20
Show Gist options
  • Save mrbid/7f5e528b862a2183cff51682ccbbffdb to your computer and use it in GitHub Desktop.
Save mrbid/7f5e528b862a2183cff51682ccbbffdb to your computer and use it in GitHub Desktop.
A simple benchmark of FPU math functions using RDTSC.
/*
James William Fletcher (github.com/mrbid)
August 2021
Simple math function benchmark using RDTSC.
https://james-william-fletcher.medium.com/rdtsc-the-only-way-to-benchmark-fc84562ef734
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <locale.h>
#include <sys/time.h>
#include <x86intrin.h>
#define AVGITER 3000000
static inline float rsqrtss(float f) // this is not a fair comparison to sqrt(double)
{
return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(f)));
}
int main()
{
setlocale(LC_NUMERIC, "");
float ret = 0;
uint64_t st = 0, et = 0, avg = 0;
// cbrt
avg = 0;
for(int i = 0; i < AVGITER; i++)
{
st = __rdtsc();
ret += cbrt(st);
avg += __rdtsc()-st;
}
printf("cbrt() Cycles: %'lu\n", avg / AVGITER);
// exp10
avg = 0;
for(int i = 0; i < AVGITER; i++)
{
st = __rdtsc();
ret += exp10(st);
avg += __rdtsc()-st;
}
printf("exp10() Cycles: %'lu\n", avg / AVGITER);
// exp2
avg = 0;
for(int i = 0; i < AVGITER; i++)
{
st = __rdtsc();
ret += exp2(st);
avg += __rdtsc()-st;
}
printf("exp2() Cycles: %'lu\n", avg / AVGITER);
// exp
avg = 0;
for(int i = 0; i < AVGITER; i++)
{
st = __rdtsc();
ret += exp(st);
avg += __rdtsc()-st;
}
printf("exp() Cycles: %'lu\n", avg / AVGITER);
// hypot
avg = 0;
for(int i = 0; i < AVGITER; i++)
{
st = __rdtsc();
ret += hypot(st, st);
avg += __rdtsc()-st;
}
printf("hypot() Cycles: %'lu\n", avg / AVGITER);
// log10
avg = 0;
for(int i = 0; i < AVGITER; i++)
{
st = __rdtsc();
ret += log10(st);
avg += __rdtsc()-st;
}
printf("log10() Cycles: %'lu\n", avg / AVGITER);
// log2
avg = 0;
for(int i = 0; i < AVGITER; i++)
{
st = __rdtsc();
ret += log2(st);
avg += __rdtsc()-st;
}
printf("log2() Cycles: %'lu\n", avg / AVGITER);
// log
avg = 0;
for(int i = 0; i < AVGITER; i++)
{
st = __rdtsc();
ret += log(st);
avg += __rdtsc()-st;
}
printf("log() Cycles: %'lu\n", avg / AVGITER);
// pow
avg = 0;
for(int i = 0; i < AVGITER; i++)
{
st = __rdtsc();
ret += pow(st, st);
avg += __rdtsc()-st;
}
printf("pow() Cycles: %'lu\n", avg / AVGITER);
// sqrt
avg = 0;
for(int i = 0; i < AVGITER; i++)
{
st = __rdtsc();
ret += sqrt(st);
avg += __rdtsc()-st;
}
printf("sqrt() Cycles: %'lu\n", avg / AVGITER);
// rsqrtss
avg = 0;
for(int i = 0; i < AVGITER; i++)
{
st = __rdtsc();
ret += rsqrtss(st);
avg += __rdtsc()-st;
}
printf("rsqrtss() Cycles: %'lu\n", avg / AVGITER);
// acos
avg = 0;
for(int i = 0; i < AVGITER; i++)
{
st = __rdtsc();
ret += acos(st);
avg += __rdtsc()-st;
}
printf("acos() Cycles: %'lu\n", avg / AVGITER);
// asin
avg = 0;
for(int i = 0; i < AVGITER; i++)
{
st = __rdtsc();
ret += asin(st);
avg += __rdtsc()-st;
}
printf("asin() Cycles: %'lu\n", avg / AVGITER);
// atan
avg = 0;
for(int i = 0; i < AVGITER; i++)
{
st = __rdtsc();
ret += atan(st);
avg += __rdtsc()-st;
}
printf("atan() Cycles: %'lu\n", avg / AVGITER);
// atan2
avg = 0;
for(int i = 0; i < AVGITER; i++)
{
st = __rdtsc();
ret += atan2(st, st);
avg += __rdtsc()-st;
}
printf("atan2() Cycles: %'lu\n", avg / AVGITER);
// cos
avg = 0;
for(int i = 0; i < AVGITER; i++)
{
st = __rdtsc();
ret += cos(st);
avg += __rdtsc()-st;
}
printf("cos() Cycles: %'lu\n", avg / AVGITER);
// tan
avg = 0;
for(int i = 0; i < AVGITER; i++)
{
st = __rdtsc();
ret += tan(st);
avg += __rdtsc()-st;
}
printf("tan() Cycles: %'lu\n", avg / AVGITER);
// done
printf("\n%c\n", (char)ret); // forces the compiler to not disregard the functions we are testing
return 0;
}
@mrbid
Copy link
Author

mrbid commented Aug 18, 2021

cbrt() Cycles: 31
exp10() Cycles: 19
exp2() Cycles: 17
exp() Cycles: 17
hypot() Cycles: 16
log10() Cycles: 22
log2() Cycles: 21
log() Cycles: 18
pow() Cycles: 32
sqrt() Cycles: 18
rsqrtss() Cycles: 18
acos() Cycles: 20
asin() Cycles: 21
atan() Cycles: 24
atan2() Cycles: 34
cos() Cycles: 168
tan() Cycles: 205

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment