I would like to swap two variables. and i would like to do it through the pipeline using a Read After Write hazard to my advantage.
Pipeline:
OPERXXXXXX FetchXXXXX DecodeXXXX ExecuteXXX WriteBkXXX
STORE X, Y ---------- ---------- ---------- ----------
STORE Y, X STORE X, Y ---------- ---------- ----------
---------- STORE Y, X STORE X, Y ---------- ----------
---------- ---------- STORE Y, X STORE X, Y ----------
---------- ---------- ---------- STORE Y, X STORE X, Y
---------- ---------- ---------- ---------- STORE Y, X
how do i go about telling the compiler to do that (and exactly that) without automatic locks and warning flags? can you suggest any literature/keywords?
specs:
-> target: modern architectures which support multistation (more than 4) pipelining
-> this is not related to any particular 'problem'. just for the sake of science.
current hurdles:
- if you know how to ignore datahazards, please share.
T tmp = x; x = y; y = tmp;
(or simplystd::swap(x,y)
). I would hope that the compiler would always do the most optimal thing given the limitations of the architecture. – AlloT tmp = x; x = y; y = tmp;
as a swap, and it's written in the standard that std::swap is to be executed linearly, which takes approx 280% longer to do on an individual basis. so there's not really a way to implement this into the existing language, thus I don't expect it to be there even if the authors did know about it. I absolutely see what you're saying though. – Breviarystd::swap
is to be executed linearly, and why doesn't the "as-if" rule apply? As far as I can tell, afterstd::swap(a, b);
,a
andb
have exchanged values, and if there are no side effects in evaluatinga
andb
the compiler is free to do as it likes behind the scenes. – Aetnastd::swap
works is completely up to the implementation, so the question isn't about C++ so much as an implementation of C++. – Aetna