TestFixedPoint.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. // Test SFixed16
  54. {
  55. SFixed16 a(1), b(2), c(3.f), d(1.0);
  56. double e = 3.2;
  57. float f = 4.5;
  58. int g = 6;
  59. if(a.AsInt() != 1)
  60. nErrorCount++;
  61. if(c.AsUnsignedInt() != 3)
  62. nErrorCount++;
  63. if(a.AsLong() != 1)
  64. nErrorCount++;
  65. if(c.AsUnsignedLong() != 3)
  66. nErrorCount++;
  67. if(!CompareValues((double)a.AsFloat(), 1.0))
  68. nErrorCount++;
  69. if(!CompareValues(c.AsDouble(), 3.0))
  70. nErrorCount++;
  71. a = b * f;
  72. if(!CompareValues(a, 9.0))
  73. nErrorCount++;
  74. a = b / d;
  75. if(!CompareValues(a, 2.0))
  76. nErrorCount++;
  77. a = b + d;
  78. if(!CompareValues(a, 3.0))
  79. nErrorCount++;
  80. a = (c / e) + b + f;
  81. if(!CompareValues(a, 7.4375))
  82. nErrorCount++;
  83. a = c / e * (b % g) + f / c;
  84. if(!CompareValues(a, 3.375))
  85. nErrorCount++;
  86. a = g * -c / (b++);
  87. if(!CompareValues(a, -9.0))
  88. nErrorCount++;
  89. if(!CompareValues(b, 3.0))
  90. nErrorCount++;
  91. --b; //Restore it to its original value.
  92. if(!CompareValues(b, 2.0))
  93. nErrorCount++;
  94. a = sin(d) + pow(b, e) * sqrt(d);
  95. if(!CompareValues(a, 10.031))
  96. nErrorCount++;
  97. a = log(e) / log(f);
  98. if(!CompareValues(a, 0.77333))
  99. nErrorCount++;
  100. }
  101. {
  102. // FPTemplate operator<<(int numBits) const
  103. {
  104. SFixed16 a(16);
  105. auto expected = a.value << 1;
  106. a = (a << 1);
  107. if(!CompareValues(a.value, expected))
  108. nErrorCount++;
  109. }
  110. // FPTemplate operator>>(int numBits) const
  111. {
  112. SFixed16 a(16);
  113. auto expected = a.value >> 1;
  114. a = (a >> 1);
  115. if(!CompareValues(a.value, expected))
  116. nErrorCount++;
  117. }
  118. }
  119. // Reported regression - ensure operator<< and operator>> are implemented correctly.
  120. {
  121. SFixed16 a(16);
  122. auto expected = a.value;
  123. a = (a << 1);
  124. a = (a >> 1);
  125. if(!CompareValues(a.value, expected))
  126. nErrorCount++;
  127. }
  128. #ifndef EA_DLL
  129. // Test SFixed24
  130. {
  131. SFixed24 a(1), b(2), c(3.f), d(1.0);
  132. double e = 3.2;
  133. float f = 4.5;
  134. int g = 6;
  135. if(a.AsInt() != 1)
  136. nErrorCount++;
  137. if(c.AsUnsignedInt() != 3)
  138. nErrorCount++;
  139. if(a.AsLong() != 1)
  140. nErrorCount++;
  141. if(c.AsUnsignedLong() != 3)
  142. nErrorCount++;
  143. if(!CompareValues((double)a.AsFloat(), 1.0))
  144. nErrorCount++;
  145. if(!CompareValues(c.AsDouble(), 3.0))
  146. nErrorCount++;
  147. a = b * f;
  148. if(!CompareValues(a, 9.0))
  149. nErrorCount++;
  150. a = b / d;
  151. if(!CompareValues(a, 2.0))
  152. nErrorCount++;
  153. a = b + d;
  154. if(!CompareValues(a, 3.0))
  155. nErrorCount++;
  156. a = (c / e) + b + f;
  157. if(!CompareValues(a, 7.4375))
  158. nErrorCount++;
  159. a = c / e * (b % g) + f / c;
  160. if(!CompareValues(a, 3.375))
  161. nErrorCount++;
  162. a = g * -c / (b++);
  163. if(!CompareValues(a, -9.0))
  164. nErrorCount++;
  165. if(!CompareValues(b, 3.0))
  166. nErrorCount++;
  167. --b; //Restore it to its original value.
  168. if(!CompareValues(b, 2.0))
  169. nErrorCount++;
  170. a = sin(d) + pow(b, e) * sqrt(d);
  171. if(!CompareValues(a, 10.031))
  172. nErrorCount++;
  173. a = log(e) / log(f);
  174. if(!CompareValues(a, 0.77333))
  175. nErrorCount++;
  176. }
  177. #endif
  178. // Test core multiplication/division functions
  179. {
  180. //SFixed16 SFixed16::FixedMul (const T t1, const T t2);
  181. //SFixed16 SFixed16::FixedDiv (const T t1, const T t2);
  182. //SFixed16 SFixed16::FixedDivSafe (const T t1, const T t2);
  183. //SFixed16 SFixed16::FixedMulDiv (const T t1, const T t2, const T t3);
  184. //SFixed16 SFixed16::FixedMulDivSafe(const T t1, const T t2, const T t3);
  185. //SFixed16 SFixed16::FixedMod (const T t1, const T t2);
  186. //SFixed16 SFixed16::FixedModSafe (const T t1, const T t2);
  187. }
  188. return nErrorCount;
  189. }