Skip to content

Instantly share code, notes, and snippets.

@EgorBo
Last active April 28, 2024 23:12
Show Gist options
  • Save EgorBo/88196a218559ec93a197a7d1d5600548 to your computer and use it in GitHub Desktop.
Save EgorBo/88196a218559ec93a197a7d1d5600548 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <chrono>
// clang++ apple-silicon-ldp-repro.cpp -O2 -o ldp && ./ldp
// uncomment to use ldp:
//
//#define USE_LDP
__attribute__((noinline))
void Stall(int* ptr)
{
for (int i=0; i<1000000000; i++)
{
int tmp1, tmp2;
__asm__ volatile(
#ifdef USE_LDP
"ldp %w0, %w1, [%2]\n"
#else
// same, but without ldp:
"ldr %w0, [%2]\n"
"ldr %w1, [%2, #4]\n"
#endif
"str %w0, [%2]\n"
"str %w1, [%2, #4]\n"
// NOTE: stp also ruins perf
: "=&r" (tmp1), "=&r" (tmp2)
: "r" (ptr)
:
);
}
}
int main()
{
int data[64];
while (true)
{
using namespace std::chrono;
auto start = high_resolution_clock::now();
Stall(data);
auto stop = high_resolution_clock::now();
double elapsed = duration_cast<milliseconds>(stop - start).count() / 1000.0;
printf("it took: %f ms.\n", elapsed);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment