Guest User

Untitled

a guest
May 14th, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.65 KB | None | 0 0
  1. // gcc.godbolt.org
  2. // x86-64 CL 19 2017 RTW
  3. // x86-86 clang 4.0.0, -O2 -Wall -std=c++1z
  4. // x86-86 gcc 7.1, -O2 -Wall -std=c++1z
  5. // x86-64 icc 17
  6.  
  7. #include <cstddef>
  8. #include <cassert>
  9. #include <array>
  10.  
  11. namespace
  12. {
  13.  
  14. template <typename ValueT>
  15. class vector3
  16. {
  17. public:
  18.     typedef ValueT value_type;
  19.     typedef ValueT& reference;
  20.     typedef const ValueT& const_reference;
  21.  
  22.     vector3()
  23.     {
  24.         m_data.fill(ValueT());
  25.     }
  26.  
  27.     vector3(const_reference x_, const_reference y_, const_reference z_):m_data{x_,y_,z_}
  28.     {
  29.     }
  30.  
  31.     const_reference x() const
  32.     {
  33.         return m_data[0];
  34.     }
  35.  
  36.     reference x()
  37.     {
  38.         return m_data[0];
  39.     }
  40.  
  41.     const_reference y() const
  42.     {
  43.         return m_data[1];
  44.     }
  45.  
  46.     reference y()
  47.     {
  48.         return m_data[1];
  49.     }
  50.  
  51.     const_reference z() const
  52.     {
  53.         return m_data[2];
  54.     }
  55.  
  56.     reference z()
  57.     {
  58.         return m_data[2];
  59.     }
  60.  
  61.     vector3& operator+=(const vector3& other)
  62.     {
  63.         m_data[0] += other.m_data[0];
  64.         m_data[1] += other.m_data[1];
  65.         m_data[2] += other.m_data[2];
  66.         return *this;
  67.     }
  68.  
  69.     vector3& operator*=(const_reference value)
  70.     {
  71.         m_data[0] *= value;
  72.         m_data[1] *= value;
  73.         m_data[2] *= value;
  74.         return *this;
  75.     }
  76.  
  77. private:
  78.     std::array<value_type, 3> m_data;
  79. };
  80.  
  81. typedef double real_type;
  82. typedef vector3<real_type> point3;
  83.  
  84. template <typename ValueT>
  85. inline vector3<ValueT> operator*(typename vector3<ValueT>::const_reference a, vector3<ValueT> b)
  86. {
  87.     return b *= a;
  88. }
  89.  
  90. template <typename ValueT>
  91. inline vector3<ValueT> operator+(vector3<ValueT> a, const vector3<ValueT>& b)
  92. {
  93.     return a += b;
  94. }
  95.  
  96. static inline point3 calc(const point3& p_point, const point3& p_direction_x,
  97.     const point3& p_direction_y, const point3& p_direction_z, const point3& p_origin)
  98. {
  99.     return (p_point.x() * p_direction_x + p_point.y() * p_direction_y + p_point.z() * p_direction_z) + p_origin;
  100. }
  101.  
  102. // 0=run calc function using structs
  103. // 1=hand inlined calc function using structs
  104. // 2=hand inlined calc function using x,y,z,direction,origin direct
  105. // 3=hand optimized result of the calc function
  106. #define TEST_LEVEL 0
  107.  
  108. static point3 test(const real_type& p_x, const real_type& p_y, const real_type& p_z)
  109. {
  110. #if TEST_LEVEL == 0 || TEST_LEVEL == 1
  111.     const point3 point(p_x, p_y, p_z);
  112.     const point3 direction_x(0.0, -1.0, 0.0);
  113.     const point3 direction_y(1.0, 0.0, 0.0);
  114.     const point3 direction_z(0.0, 0.0, 1.0);
  115.     const point3 origin(0.0, 0.0, 0.0);
  116. #endif
  117.  
  118. #if TEST_LEVEL == 0
  119.     const point3 result = calc(point, direction_x, direction_y, direction_z, origin);
  120. #elif TEST_LEVEL == 1
  121.     const point3 result = (point.x() * direction_x + point.y() * direction_y + point.z() * direction_z) + origin;
  122. #elif TEST_LEVEL == 2
  123.     const point3 result = (p_x * point3(0.0, -1.0, 0.0) + p_y * point3(1.0, 0.0, 0.0) + p_z * point3(0.0, 0.0, 1.0)) + point3(0.0, 0.0, 0.0);
  124. #elif TEST_LEVEL == 3
  125.     const point3 result(p_y, -p_x, p_z);
  126. #else
  127. #error unknown TEST_LEVEL
  128. #endif
  129.     return result;
  130. }
  131.  
  132. }
  133.  
  134. //#define ONLY_CONST_VALUES
  135.  
  136. int main(int argc, char* argv[])
  137. {
  138.     const real_type x = 1.3;
  139.     const real_type y = 2.2;
  140.     const real_type z = 3.1;
  141.  
  142. #if defined(ONLY_CONST_VALUES)
  143.     const point3 result = test(x, y, z);
  144. #else    
  145.     //+ argv == anti-optimizer trick
  146.     const point3 result = test(x+argv[0][0], y+argv[0][1], z+argv[0][2]);
  147. #endif    
  148.  
  149.     const int dummy = (result.x() + result.y() + result.z()) * 1000;
  150. #if defined(ONLY_CONST_VALUES)
  151.     assert(dummy == 4000);
  152. #endif
  153.    
  154.     return dummy; // anti-optimizer-trick
  155. }
Advertisement
Add Comment
Please, Sign In to add comment