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