Skip to content

Instantly share code, notes, and snippets.

@i-saint
Last active January 8, 2023 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save i-saint/cbce1044c32d75a3f47828d5f39bd59d to your computer and use it in GitHub Desktop.
Save i-saint/cbce1044c32d75a3f47828d5f39bd59d to your computer and use it in GitHub Desktop.
#include <cstdio>
// https://www.johndcook.com/blog/2019/11/12/rump-floating-point/
template <typename T>
T f(T x, T y) {
T x2 = x*x;
T y2 = y*y;
T y4 = y2*y2;
T y6 = y2*y4;
T y8 = y2*y6;
return T(333.75)*y6 + x2*(T(11)*x2*y2 - y6 - T(121)*y4 - T(2)) + T(5.5)*y8 + x/(T(2)*y);
}
template <typename T>
__declspec(noinline)
T f2(T x, T y) {
return f(x, y);
}
int main(int argc, char **argv)
{
constexpr double a = 77617.0;
constexpr double b = 33096.0;
printf("inline: %e\n", f(a, b));
printf("no-inline: %e\n", f2(a, b));
}
// compilers may or may not use FMA for compile-time folding. using FMA improves precision. so, inlining affects result.
// msvc (v19 x64) with /O2 /fp:fast /arch:AVX2 outputs:
// inline: -1.180592e+21
// no-inline: -1.328166e+21
//
// see also:
// https://godbolt.org/z/WYd6EW1G6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment