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