Same change as Revision r1395 for C++ port: Small speedup, per issue 422
[zxing.git] / cpp / core / src / zxing / common / reedsolomon / GF256.cpp
index 8add889..51b621e 100644 (file)
@@ -18,7 +18,6 @@
  * limitations under the License.
  */
 
-#include <valarray>
 #include <vector>
 #include <iostream>
 #include <zxing/common/reedsolomon/GF256.h>
@@ -42,7 +41,7 @@ static inline Ref<GF256Poly> refPoly(GF256 &field, int value) {
 }
 
 GF256::GF256(int primitive) :
-    exp_((const int)0, 256), log_((const int)0, 256), zero_(refPoly(*this, 0)), one_(refPoly(*this, 1)) {
+    exp_(256, (const int)0), log_(256, (const int)0), zero_(refPoly(*this, 0)), one_(refPoly(*this, 1)) {
   int x = 1;
   for (int i = 0; i < 256; i++) {
     exp_[i] = x;
@@ -110,13 +109,10 @@ int GF256::multiply(int a, int b) {
   if (a == 0 || b == 0) {
     return 0;
   }
-  if (a == 1) {
-    return b;
-  }
-  if (b == 1) {
-    return a;
-  }
-  return exp_[(log_[a] + log_[b]) % 255];
+  int logSum = log_[a] + log_[b];
+  // index is a sped-up alternative to logSum % 255 since sum
+  // is in [0,510]. Thanks to jmsachs for the idea
+  return exp_[(logSum & 0xFF) + (logSum >> 8)];
 }
 
 GF256 GF256::QR_CODE_FIELD(0x011D); // x^8 + x^4 + x^3 + x^2 + 1