Skip to content

Instantly share code, notes, and snippets.

@HugoGuiroux
Created November 8, 2015 09:03
Show Gist options
  • Select an option

  • Save HugoGuiroux/4f32fc59320c78dcea7e to your computer and use it in GitHub Desktop.

Select an option

Save HugoGuiroux/4f32fc59320c78dcea7e to your computer and use it in GitHub Desktop.
Code generating an optimization bug with clang
#include <iostream>
#include <cstring>
#define LOOP_COUNT 1000000000
unsigned long long rdtscl(void)
{
unsigned int lo, hi;
__asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
}
int main()
{
unsigned long long before = rdtscl();
size_t ret;
for (int i = 0; i < LOOP_COUNT; i++)
ret = strlen("abcd");
unsigned long long after = rdtscl();
std::cout << "Strlen " << (after - before) << " ret=" << ret << std::endl;
before = rdtscl();
for (int i = 0; i < LOOP_COUNT; i++)
ret = sizeof("abcd");
after = rdtscl();
std::cout << "Sizeof " << (after - before) << " ret=" << ret << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment