fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. struct Point4
  5. {
  6. Point4()
  7. {
  8. data[0] = 0;
  9. data[1] = 0;
  10. data[2] = 0;
  11. data[3] = 0;
  12. }
  13.  
  14. float data[4];
  15. };
  16.  
  17. static float SumOfDifferences(const Point4& a, const Point4& b)
  18. {
  19. // This function only returns the sum of the sum of the components
  20. float sumValues = 0.0f;
  21. for(unsigned int i = 0; i < 4; ++i)
  22. {
  23. sumValues += a.data[i] + b.data[i];
  24. }
  25. return sumValues;
  26. }
  27.  
  28. void Standard()
  29. {
  30. Point4 a;
  31. a.data[0] = 1;
  32. a.data[1] = 2;
  33. a.data[2] = 3;
  34. a.data[3] = 4;
  35.  
  36. Point4 b;
  37. b.data[0] = 1;
  38. b.data[1] = 6;
  39. b.data[2] = 3;
  40. b.data[3] = 5;
  41.  
  42. float total = 0.0f;
  43. for(unsigned int i = 0; i < 1e9; ++i)
  44. {
  45. total += SumOfDifferences(a, b);
  46. }
  47.  
  48. std::cout << "total: " << total << std::endl;
  49. }
  50.  
  51. void Vectorized()
  52. {
  53. typedef float v4sf __attribute__ (( vector_size(4*sizeof(float)) ));
  54.  
  55. v4sf a;
  56. float* aPointer = (float*)&a;
  57. aPointer[0] = 1; aPointer[1] = 2; aPointer[2] = 3; aPointer[3] = 4;
  58.  
  59. v4sf b;
  60. float* bPointer = (float*)&b;
  61. bPointer[0] = 1; bPointer[1] = 6; bPointer[2] = 3; bPointer[3] = 5;
  62.  
  63. v4sf result;
  64. float* resultPointer = (float*)&result;
  65.  
  66. for(unsigned int i = 0; i < 1e9; ++i)
  67. {
  68. result += a + b; // Vectorized operation
  69. }
  70.  
  71. // Sum the components of the result (this is done with the "total += " in the Standard() loop
  72. float total = 0.0f;
  73. for(unsigned int component = 0; component < 4; ++component)
  74. {
  75. total += resultPointer[component];
  76. }
  77. std::cout << "total: " << total << std::endl;
  78. }
  79.  
  80. int main()
  81. {
  82.  
  83. Standard();
  84.  
  85. // Vectorized();
  86.  
  87. return 0;
  88. }
  89.  
Time limit exceeded #stdin #stdout 5s 2676KB
stdin
Standard input is empty
stdout
Standard output is empty