fork download
  1. #include <cstdio>
  2. #include <cmath>
  3. #include <cstdlib>
  4.  
  5. //#define USE_DOT
  6. //#define USE_SSE
  7.  
  8.  
  9. static const float CMP_EPS = 1e-4f;
  10. static const float NORMALIZE_EPS = 1e-12f;
  11.  
  12. typedef float v4sf __attribute__ ((vector_size (16))); // vector of three single floats
  13.  
  14. struct f3vector {
  15. union
  16. {
  17. v4sf v;
  18. struct { float x,y,z,w; };
  19. struct { int ix,iy,iz,iw; };
  20. };
  21. };
  22.  
  23.  
  24. struct float3 : public f3vector {
  25. float3() {}
  26. float3(const float _x, const float _y, const float _z) {
  27. x = _x; y = _y; z = _z;
  28. }
  29.  
  30. #ifdef USE_SSE
  31. float3& operator+= (const float3& f) {
  32. v += f.v;
  33. return *this;
  34. }
  35. #else
  36. float3& operator+= (const float3& f) {
  37. x += f.x; y += f.y; z += f.z;
  38. return *this;
  39. }
  40. #endif
  41.  
  42. inline float SqDistance(const float3& f) const {
  43. const float dx = x - f.x;
  44. const float dy = y - f.y;
  45. const float dz = z - f.z;
  46. return (dx*dx + dy*dy + dz*dz);
  47. }
  48.  
  49. #ifdef USE_DOT
  50. bool equals(const float3& f) const {
  51. return this->SqDistance(f) <= CMP_EPS;
  52. }
  53. #elif defined(USE_SSE)
  54. bool equals(float3& f) {
  55. f3vector n;
  56. n.v = (v - f.v);
  57. n.v *= n.v;
  58. return (n.x + n.y + n.z) <= CMP_EPS;
  59. }
  60. #else
  61. bool equals(const float3& f) const {
  62. return std::fabs(x - f.x) <= CMP_EPS
  63. && std::fabs(y - f.y) <= CMP_EPS
  64. && std::fabs(z - f.z) <= CMP_EPS;
  65. }
  66. #endif
  67. };
  68.  
  69. const int count = 100;
  70. float3 a[count],b[count],c[count],d[count];
  71.  
  72. int main() {
  73. for(int i=0; i<count; i++) {
  74. a[i] = float3(0.1f,0.1f,0.1f);
  75. b[i] = float3(100.1f,100.2f,100.3f);
  76. c[i] = float3(0.1f,0.2f,0.3f);
  77. d[i] = float3((float)rand()/(float)RAND_MAX, (float)rand()/(float)RAND_MAX, (float)rand()/(float)RAND_MAX);
  78. }
  79.  
  80.  
  81. for (int i = 1<<18; i>0; --i) {
  82. for(int j = 0; j < count; j++) {
  83. if (a[j].equals(b[j])) {
  84. b[j] += d[j];
  85. a[j] = b[j];
  86. }
  87. a[j] += d[j];
  88. }
  89. }
  90. printf("%f %f %f\n", a[0].x, a[0].y, a[0].z);
  91.  
  92. return 0;
  93. }
Success #stdin #stdout 0.13s 2728KB
stdin
Standard input is empty
stdout
204881.515625 103047.046875 220883.156250