TestFixedPoint.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) Electronic Arts Inc. All rights reserved.
  3. ///////////////////////////////////////////////////////////////////////////////
  4. #include <EABase/eabase.h>
  5. #include <EAStdC/EAFixedPoint.h>
  6. #include <EAStdCTest/EAStdCTest.h>
  7. #include <EATest/EATest.h>
  8. static bool CompareValues(double a, double b)
  9. {
  10. return (fabs(a - b) < 0.01);
  11. }
  12. static bool CompareValues(EA::StdC::SFixed16 a, double b)
  13. {
  14. #define Fixed16ToDouble(a) (((double)a) / 65536.0)
  15. const double c = Fixed16ToDouble(a.AsFixed());
  16. return (fabs(c - b) < 0.01);
  17. }
  18. // In a DLL build, VC++ doesn't like it when you provide your own
  19. // versions of functions. So we just skip testing this, for simplicity.
  20. // If we need this to work, we can just add these functions to EAFixedPoint.cpp.
  21. #ifndef EA_DLL
  22. namespace EA
  23. {
  24. namespace StdC
  25. {
  26. static bool CompareValues(SFixed24 a, double b){
  27. #define Fixed24ToDouble(a) (((double)a) / 256.0)
  28. const double c = Fixed24ToDouble(a.AsFixed());
  29. return (fabs(c - b) < 0.01);
  30. }
  31. template<>
  32. int32_t SFixed24::FixedMul(const int32_t a, const int32_t b){
  33. const int64_t c = (int64_t)a * b;
  34. return (int32_t)(c >> 8);
  35. }
  36. template<>
  37. int32_t SFixed24::FixedDiv(const int32_t a, const int32_t b){
  38. const int64_t c = ((uint64_t)a) << 8;
  39. return (int32_t)(c / b);
  40. }
  41. template<>
  42. int32_t SFixed24::FixedMod(const int32_t a, const int32_t b){
  43. const volatile uint64_t c = ((uint64_t)a) << 8;
  44. return (int32_t)(uint32_t)(c % b);
  45. }
  46. }
  47. }
  48. #endif
  49. int TestFixedPoint()
  50. {
  51. using namespace EA::StdC;
  52. int nErrorCount(0);
  53. EA::UnitTest::Report("TestFixedPoint\n");
  54. // Test SFixed16
  55. {
  56. SFixed16 a(1), b(2), c(3.f), d(1.0);
  57. double e = 3.2;
  58. float f = 4.5;
  59. int g = 6;
  60. if(a.AsInt() != 1)
  61. nErrorCount++;
  62. if(c.AsUnsignedInt() != 3)
  63. nErrorCount++;
  64. if(a.AsLong() != 1)
  65. nErrorCount++;
  66. if(c.AsUnsignedLong() != 3)
  67. nErrorCount++;
  68. if(!CompareValues((double)a.AsFloat(), 1.0))
  69. nErrorCount++;
  70. if(!CompareValues(c.AsDouble(), 3.0))
  71. nErrorCount++;
  72. a = b * f;
  73. if(!CompareValues(a, 9.0))
  74. nErrorCount++;
  75. a = b / d;
  76. if(!CompareValues(a, 2.0))
  77. nErrorCount++;
  78. a = b + d;
  79. if(!CompareValues(a, 3.0))
  80. nErrorCount++;
  81. a = (c / e) + b + f;
  82. if(!CompareValues(a, 7.4375))
  83. nErrorCount++;
  84. a = c / e * (b % g) + f / c;
  85. if(!CompareValues(a, 3.375))
  86. nErrorCount++;
  87. a = g * -c / (b++);
  88. if(!CompareValues(a, -9.0))
  89. nErrorCount++;
  90. if(!CompareValues(b, 3.0))
  91. nErrorCount++;
  92. --b; //Restore it to its original value.
  93. if(!CompareValues(b, 2.0))
  94. nErrorCount++;
  95. a = sin(d) + pow(b, e) * sqrt(d);
  96. if(!CompareValues(a, 10.031))
  97. nErrorCount++;
  98. a = log(e) / log(f);
  99. if(!CompareValues(a, 0.77333))
  100. nErrorCount++;
  101. }
  102. #ifndef EA_DLL
  103. // Test SFixed24
  104. {
  105. SFixed24 a(1), b(2), c(3.f), d(1.0);
  106. double e = 3.2;
  107. float f = 4.5;
  108. int g = 6;
  109. if(a.AsInt() != 1)
  110. nErrorCount++;
  111. if(c.AsUnsignedInt() != 3)
  112. nErrorCount++;
  113. if(a.AsLong() != 1)
  114. nErrorCount++;
  115. if(c.AsUnsignedLong() != 3)
  116. nErrorCount++;
  117. if(!CompareValues((double)a.AsFloat(), 1.0))
  118. nErrorCount++;
  119. if(!CompareValues(c.AsDouble(), 3.0))
  120. nErrorCount++;
  121. a = b * f;
  122. if(!CompareValues(a, 9.0))
  123. nErrorCount++;
  124. a = b / d;
  125. if(!CompareValues(a, 2.0))
  126. nErrorCount++;
  127. a = b + d;
  128. if(!CompareValues(a, 3.0))
  129. nErrorCount++;
  130. a = (c / e) + b + f;
  131. if(!CompareValues(a, 7.4375))
  132. nErrorCount++;
  133. a = c / e * (b % g) + f / c;
  134. if(!CompareValues(a, 3.375))
  135. nErrorCount++;
  136. a = g * -c / (b++);
  137. if(!CompareValues(a, -9.0))
  138. nErrorCount++;
  139. if(!CompareValues(b, 3.0))
  140. nErrorCount++;
  141. --b; //Restore it to its original value.
  142. if(!CompareValues(b, 2.0))
  143. nErrorCount++;
  144. a = sin(d) + pow(b, e) * sqrt(d);
  145. if(!CompareValues(a, 10.031))
  146. nErrorCount++;
  147. a = log(e) / log(f);
  148. if(!CompareValues(a, 0.77333))
  149. nErrorCount++;
  150. }
  151. #endif
  152. // Test core multiplication/division functions
  153. {
  154. //SFixed16 SFixed16::FixedMul (const T t1, const T t2);
  155. //SFixed16 SFixed16::FixedDiv (const T t1, const T t2);
  156. //SFixed16 SFixed16::FixedDivSafe (const T t1, const T t2);
  157. //SFixed16 SFixed16::FixedMulDiv (const T t1, const T t2, const T t3);
  158. //SFixed16 SFixed16::FixedMulDivSafe(const T t1, const T t2, const T t3);
  159. //SFixed16 SFixed16::FixedMod (const T t1, const T t2);
  160. //SFixed16 SFixed16::FixedModSafe (const T t1, const T t2);
  161. }
  162. return nErrorCount;
  163. }