title: C/C++ optimization: negate doubles fast
body: <p>I need to negate very large number of doubles quickly. If <strong>bit_generator</strong> generates 0, then the sign  must be changed. If <strong>bit_generator</strong> generates 1, then nothing happens. The loop is run many times over and <strong>bit_generator</strong> is extremely fast. On my platform case 2 is noticeably faster than case 1. Looks like my CPU doesn't like branching. Is there any faster and portable way to do it? What do you think about case 3?</p>
  
  <pre><code>// generates 0 and 1
  int bit_generator();
  
  // big vector (C++)
  vector&lt;double&gt; v;
  
  // case 1
  for (size_t i=0; i&lt;v.size(); ++i)
      if (bit_generator()==0)
          v[i] = -v[i];
  
  // case 2
  const int sign[] = {-1, 1};
  for (size_t i=0; i&lt;v.size(); ++i)
          v[i] *= sign[bit_generator()];
  
  // case 3
  const double sign[] = {-1, 1};
  for (size_t i=0; i&lt;v.size(); ++i)
          v[i] *= sign[bit_generator()];
  
  // case 4 uses C-array
  double a[N];
  double number_generator(); // generates doubles
  double z[2]; // used as buffer
  for (size_t i=0; i&lt;N; ++i) {
          z[0] = number_generator();
          z[1] = -z[0];
          a[i] = z[bit_generator()];
  }
  </code></pre>
  
  <p><strong>EDIT:</strong> Added case 4 and C-tag, because the vector can be a plain array. Since I can control how doubles are generated, I redesigned the code as shown in case 4. It avoids extra multiplication and branching at the same. I presume it should be quite fast on all platforms.</p>