maroonrk's blog

By maroonrk, history, 2 years ago, In English

What if your solution got TLE due to denormal numbers? Would you add if(val<eps)val=0 everywhere? Today I found a simpler fix: let's include #include <xmmintrin.h> and insert _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON); at the beginning of the main function.

Example Problem

TLE Submission

AC Submission

I confirmed this magic works on AtCoder, Codechef, Yandex, and, of course, on Codeforces.

Since it's written on the Wikipedia page, I won't be surprised if this is a well-known trick in some countries. However, I and my friends haven't heard of this, so it must be worth sharing.

Note that I know nothing about what exactly this snippet does, and I'm happy to hear from computer experts what the possible defects it has if such exist.

  • Vote: I like it
  • +193
  • Vote: I do not like it

| Write comment?
»
2 years ago, # |
Rev. 2   Vote: I like it +67 Vote: I do not like it

It might be a little hard to remember, but you can actually manipulate mxcsr register yourself as below without including any additional headers to achieve the same effect.

int main() {
  __builtin_ia32_ldmxcsr(__builtin_ia32_stmxcsr() | 0x8000);
  return 0;
}
»
2 years ago, # |
  Vote: I like it -89 Vote: I do not like it

so learn silently don't brag here