On 06/10/2017 11:00, Henri Sivonen wrote: > Do we already have a C++ analog of Rust's test::black_box() function? > I.e. a function that just passes through a value but taints it in such > a way that the optimizer can't figure out that there are no side > effects. (For the purpose of ensuring that the compiler can't > eliminate computation that's being benchmarked.) > > If we don't have one, how should one be written so that it works in > GCC, clang and MSVC? > > It's surprisingly hard to find an answer to this on Google or > StackOverflow, and experimentation on godbolt.org suggests that easy > answers that are found are also wrong. > > Specifically, this isn't the answer for GCC: > void* black_box(void* foo) { > asm ("":"=r" (foo): "r" (foo):"memory"); > return foo; > }
IIUC what you are looking for is the '+' constraint which implies the parameter is both read and written in the asm statement, e.g.: void* black_box(void* foo) { asm ("":"+r" (foo): "r" (foo):"memory"); return foo; } Gabriele
signature.asc
Description: OpenPGP digital signature
_______________________________________________ dev-platform mailing list dev-platform@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-platform