title: How to optimize this formula in iOS
body: <p>I have a line of code in my app that I've identified as a bottleneck:</p>
  
  <pre><code>Byte rSource, rSource1, rSource2, rSource3, rSource4;
  Float32 mult1, mult2, mult3, mult4;
  
  // this gets called many many times inside a loop:
  rSource = (int)(rSource1 * mult1 + rSource2 * mult2 + rSource3 * mult3 
      + rSource4 * mult4);
  </code></pre>
  
  <p>Is there any way to speed up how long this call takes? I've seen some posts about inline assembly code which I'm guessing might be a way to do this faster, but I have no idea how to go about using inline assembly or what exact assembly instructions I would need in order to perform this calculation.</p>
  
  <p>Interestingly, if I change the line to this:</p>
  
  <pre><code>rSource = (Byte)(rSource1 * mult1 + rSource2 * mult2 + rSource3 * mult3 
      + rSource4 * mult4);
  </code></pre>
  
  <p>the calculation takes almost twice as long (I'm guessing this is because the compiler adds checks to ensure that the value being cast to <code>Byte</code> is actually between 0 and 255 whereas with the <code>int</code> cast it doesn't do any checking).</p>
  
  <p>I can add an asm method to my code like so which does a saturation or something (online code, just an example):</p>
  
  <pre><code>int a, y;
  __asm__("usat %0, #8, %1\n\t" : "=r"(y) : "r"(a));
  </code></pre>
  
  <p>How would I write something like this to handle my method above?</p>