TestFixedPoint.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. {
  103. // FPTemplate operator<<(int numBits) const
  104. {
  105. SFixed16 a(16);
  106. auto expected = a.value << 1;
  107. a = (a << 1);
  108. if(!CompareValues(a.value, expected))
  109. nErrorCount++;
  110. }
  111. // FPTemplate operator>>(int numBits) const
  112. {
  113. SFixed16 a(16);
  114. auto expected = a.value >> 1;
  115. a = (a >> 1);
  116. if(!CompareValues(a.value, expected))
  117. nErrorCount++;
  118. }
  119. }
  120. // Reported regression - ensure operator<< and operator>> are implemented correctly.
  121. {
  122. SFixed16 a(16);
  123. auto expected = a.value;
  124. a = (a << 1);
  125. a = (a >> 1);
  126. if(!CompareValues(a.value, expected))
  127. nErrorCount++;
  128. }
  129. #ifndef EA_DLL
  130. // Test SFixed24
  131. {
  132. SFixed24 a(1), b(2), c(3.f), d(1.0);
  133. double e = 3.2;
  134. float f = 4.5;
  135. int g = 6;
  136. if(a.AsInt() != 1)
  137. nErrorCount++;
  138. if(c.AsUnsignedInt() != 3)
  139. nErrorCount++;
  140. if(a.AsLong() != 1)
  141. nErrorCount++;
  142. if(c.AsUnsignedLong() != 3)
  143. nErrorCount++;
  144. if(!CompareValues((double)a.AsFloat(), 1.0))
  145. nErrorCount++;
  146. if(!CompareValues(c.AsDouble(), 3.0))
  147. nErrorCount++;
  148. a = b * f;
  149. if(!CompareValues(a, 9.0))
  150. nErrorCount++;
  151. a = b / d;
  152. if(!CompareValues(a, 2.0))
  153. nErrorCount++;
  154. a = b + d;
  155. if(!CompareValues(a, 3.0))
  156. nErrorCount++;
  157. a = (c / e) + b + f;
  158. if(!CompareValues(a, 7.4375))
  159. nErrorCount++;
  160. a = c / e * (b % g) + f / c;
  161. if(!CompareValues(a, 3.375))
  162. nErrorCount++;
  163. a = g * -c / (b++);
  164. if(!CompareValues(a, -9.0))
  165. nErrorCount++;
  166. if(!CompareValues(b, 3.0))
  167. nErrorCount++;
  168. --b; //Restore it to its original value.
  169. if(!CompareValues(b, 2.0))
  170. nErrorCount++;
  171. a = sin(d) + pow(b, e) * sqrt(d);
  172. if(!CompareValues(a, 10.031))
  173. nErrorCount++;
  174. a = log(e) / log(f);
  175. if(!CompareValues(a, 0.77333))
  176. nErrorCount++;
  177. }
  178. #endif
  179. // Test core multiplication/division functions
  180. {
  181. //SFixed16 SFixed16::FixedMul (const T t1, const T t2);
  182. //SFixed16 SFixed16::FixedDiv (const T t1, const T t2);
  183. //SFixed16 SFixed16::FixedDivSafe (const T t1, const T t2);
  184. //SFixed16 SFixed16::FixedMulDiv (const T t1, const T t2, const T t3);
  185. //SFixed16 SFixed16::FixedMulDivSafe(const T t1, const T t2, const T t3);
  186. //SFixed16 SFixed16::FixedMod (const T t1, const T t2);
  187. //SFixed16 SFixed16::FixedModSafe (const T t1, const T t2);
  188. }
  189. return nErrorCount;
  190. }