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;
}
-- 
Henri Sivonen
hsivo...@hsivonen.fi
https://hsivonen.fi/
_______________________________________________
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform

Reply via email to