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 = 1e-4f;
  13. static const float NORMALIZE_EPS = 1e-12f;
  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. #else
  38. float3& operator+= (const float3& f) {
  39. x += f.x; y += f.y; z += f.z;
  40. return *this;
  41. }
  42. #endif
  43.  
  44. inline float SqDistance(const float3& f) const {
  45. const float dx = x - f.x;
  46. const float dy = y - f.y;
  47. const float dz = z - f.z;
  48. return (dx*dx + dy*dy + dz*dz);
  49. }
  50.  
  51. #ifdef USE_DOT
  52. bool equals(const float3& f) const {
  53. return this->SqDistance(f) <= CMP_EPS;
  54. }
  55. #elif defined(USE_SSE)
  56. bool equals(float3& f) {
  57. f3vector n;
  58. n.v = (v - f.v);
  59. n.v *= n.v;
  60. return (n.x + n.y + n.z) <= CMP_EPS;
  61. }
  62. #else
  63. bool equals(const float3& f) const {
  64. return std::fabs(x - f.x) <= CMP_EPS
  65. && std::fabs(y - f.y) <= CMP_EPS
  66. && std::fabs(z - f.z) <= CMP_EPS;
  67. }
  68. #endif
  69. };
  70.  
  71.  
  72. int main() {
  73. float3 a(0.1f,0.1f,0.1f);
  74. float3 b(100.1f,100.2f,100.3f);
  75. float3 c(0.1f,0.2f,0.3f);
  76. float3 d((float)rand()/(float)RAND_MAX, (float)rand()/(float)RAND_MAX, (float)rand()/(float)RAND_MAX);
  77. for (int i = 1<<27; i>0; --i) {
  78. if (a.equals(b)) {
  79. b += d;
  80. a = b;
  81. }
  82. a += d;
  83. }
  84. /*for (int i = 1<<27; i>0; --i) {
  85. if (a.equals(b)) {
  86. b += a;
  87. a += c;
  88. }
  89. a += c;
  90.   }*/
  91. printf("%f %f %f\n", a.x, a.y, a.z);
  92.  
  93. return 0;
  94. }
Success #stdin #stdout 0.98s 2724KB
stdin
Standard input is empty
stdout
16777216.783099 8388608.394383 16777216.840188