https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80328

            Bug ID: 80328
           Summary: With -ffloat-store std::array operator[] no longer
                    cost-free
           Product: gcc
           Version: 6.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pavel.celba at ricardo dot com
  Target Milestone: ---

Following code:

#include <array>
#include <cstdlib>
#include <ctime>
#include <iostream>

int main()
{
  static const size_t numIters = 100000000u;

  srand(static_cast<unsigned int>(time(nullptr)));  

  // Simple type timing
  float vec[3];
  for (int i = 0; i < 3; ++i)
    vec[i] = static_cast<float>(rand());

  clock_t simpleBegin = clock();

  for (size_t iter = 0u; iter < numIters; ++iter)
    for (int i = 0; i < 3; ++i)
      vec[i] += vec[i];

  clock_t simpleEnd = clock();     

  // Simple type timing
  std::array<float, 3> vec2;
  for (int i = 0; i < 3; ++i)
    vec2[i] = static_cast<float>(rand());

  clock_t arrayBegin = clock();

  for (size_t iter = 0u; iter < numIters; ++iter)
    for (int i = 0; i < 3; ++i)
      vec2[i] += vec2[i];

  clock_t arrayEnd = clock();  

  // Suppress optimizing out whole computation
  volatile float suppressOptimizingOut[3];
  for (int i = 0; i < 3; ++i)
  {
    suppressOptimizingOut[i] = vec[i];
    suppressOptimizingOut[i] = vec2[i];    
  }
  (void)suppressOptimizingOut; // Must use the value to suppress unused warning


  std::cout << "Simple case time: " << double(simpleEnd - simpleBegin) /
CLOCKS_PER_SEC << std::endl;
  std::cout << "Array case time: " << double(arrayEnd - arrayBegin) /
CLOCKS_PER_SEC << std::endl;
}

Compile options: -Wall -std=c++11 -ffloat-store -O2 -o a.out source_file.cpp

There is really no reason too - shouldn't do any additional operations compared
to simple case after all optimizations.

Reply via email to