I bet Google Benchmark will have what you want. As a first guess, maybe this? https://github.com/google/benchmark/blob/master/include/benchmark/benchmark.h#L297
(And if godbolt says they are wrong, please send them a PR :)) On Fri, Oct 6, 2017 at 9:16 AM, Gabriele Svelto <gsve...@mozilla.com> wrote: > 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 > > > _______________________________________________ > dev-platform mailing list > dev-platform@lists.mozilla.org > https://lists.mozilla.org/listinfo/dev-platform > > _______________________________________________ dev-platform mailing list dev-platform@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-platform