Index: trunk/Mars/mcore/factfits.h
===================================================================
--- trunk/Mars/mcore/factfits.h	(revision 16546)
+++ trunk/Mars/mcore/factfits.h	(revision 16560)
@@ -58,11 +58,39 @@
          int16_t *data      = reinterpret_cast<int16_t*>(fBufferRow.data() + offset + fOffsetData);
 
+         /*
          for (uint32_t i=0; i<1440*1024; i+=1024, startCell++)
          {
+             if (*startCell < 0)
+                 continue;
+
              for (uint32_t j=0; j<fNumRoi; j++, data++)
+                 *data += fOffsetCalibration[i + (*startCell+j)%1024];
+         }
+         */
+
+         // This version is faster because the compilers optimization
+         // is not biased by the evaluation of %1024
+         for (int ch=0; ch<1440; ch++)
+         {
+             if (startCell[ch]<0)
              {
-                 if ((*startCell) < 0) continue;
-                 *data += fOffsetCalibration[i + (*startCell+j)%1024];
+                 data += fNumRoi;
+                 continue;
              }
+
+             const int16_t *off = fOffsetCalibration.data() + ch*1024;
+
+             const int16_t *cal = off+startCell[ch];
+             const int16_t *end = data+fNumRoi;
+
+             if (startCell[ch]+fNumRoi>1024)
+             {
+                 while (cal<off+1024)
+                     *data++ += *cal++;
+
+                 cal = off;
+             }
+             while (data<end)
+                 *data++ += *cal++;
          }
     }
