title: C# CCS PIC - Enhance equations processing
body: <p>I'm using a PIC 18F2550 programmed with CCS to do several things. The root of my problem is that one of my interruptions handles an equation that I need to process often, and that controls several variables. This equation process involves typecasting, it has <em>int16</em>'s as input variables and it's result is an <em>int8</em> variable. This process takes too long and it slows down my other interruptions, causing several problems. </p>
  
  <p>How can I speed up this function as much as possible but without hindering it's accuracy as much?</p>
  
  <pre><code>void dynamicControl(){
     refDelta = phRef - procSignal[0]; //all 3 are int16 variables.
     int16 prOffset = procSignal[1] - 500; //Their values will change on every iteration
     int16 _fixed(2) calcVar; //Tried using float and double here but with no success.
     if (refDelta &lt; 0) {
        risePr = TRUE; dropPr = FALSE;
        calcVar = ((int16 _fixed(2))prOffset / 65.00) * (int16 _fixed(2))(4 - refDelta) + 1;
        nValPuls = (int8)calcVar;
     } else if(refDelta &gt; 0){
        risePr = FALSE; dropPr = TRUE;
        nValPuls = (refDelta - 1);
     } else {
        risePr = dropPr = FALSE;
        nValPuls = 0x00;
     }
     if (refDelta &lt; 10){
        calcVar = ((int16 _fixed(2))prOffset / 100.00) * (int16 _fixed(2))(5 - refDelta) * 0.70 + 18.00;
        pumpDutyLock = (int8)calcVar;
        if(pumpDutyLock &gt; 30) pumpDutyLock = 30;
     } else { pumpDutyLock = 0x00; }
  
  }
  </code></pre>
  
  <p>The 4 lines of code that slow down my code are the following. If I comment them all, the function runs at a good speed.</p>
  
  <pre><code>  calcVar = ((int16 _fixed(2))prOffset / 65.00) * (int16 _fixed(2))(4 - refDelta) + 1;
    nValPuls = (int8)calcVar;
  
    calcVar = ((int16 _fixed(2))prOffset / 100.00) * (int16 _fixed(2))(5 - refDelta) * 0.70 + 18.00;
    pumpDutyLock = (int8)calcVar;
  </code></pre>
  
  <p>I realize that typecasting takes a long time here, I've tried using <em>float</em> or <em>double</em> for <em>calcVar</em>, instead of <em>int16 _fixed(2)</em>, with no progress.</p>
  
  <p>EDIT: After some further research I've found that, as stated by the comments, most of these operations take at least 50 us to complete, which means my problem cannot be solved by modifying the equation. I've resorted to transfering most of the calculations over to the PC side of the system.</p>