Skip to content

Instantly share code, notes, and snippets.

@bgamari
Created February 6, 2020 19:51
Show Gist options
  • Save bgamari/2694ec04a4600c58af5e2ce06ea500d6 to your computer and use it in GitHub Desktop.
Save bgamari/2694ec04a4600c58af5e2ce06ea500d6 to your computer and use it in GitHub Desktop.
#include <stdint.h>
#include <fenv.h>
float hs_word2float32_old(uint64_t x) {
return (float)x;
}
float hs_word2float32_new(uint64_t x) {
int r = fegetround();
fesetround(FE_TOWARDZERO);
volatile float y = (float)x;
fesetround(r);
return y;
}
#include <iostream>
#include <cstdint>
#include <chrono>
#include <functional>
float hs_word2float32_old(uint64_t x);
float hs_word2float32_new(uint64_t x);
using namespace std::chrono;
template<typename clock = high_resolution_clock>
duration<double> time_it(std::function<void()> f)
{
typename clock::time_point start = clock::now();
for (int i=0; i<100000000; i++) {
f();
}
typename clock::time_point end = clock::now();
return end - start;
}
int main() {
auto t_old = time_it([](){ hs_word2float32_old(42); });
auto t_new = time_it([](){ hs_word2float32_new(42); });
std::cout << t_old.count() << std::endl;
std::cout << t_new.count() << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment