Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Based on some discussion in the comments of David Wallace's answer, there is a significantly faster way to compute the correct <code>double</code>.</p> <p>Probably the very fastest way to convert it is like this:</p> <pre><code>double intFixedPoint24ToDouble(int bits){ return ((double) bits) / 1&lt;&lt;24; } </code></pre> <p>The <em>reason</em> that this is faster is because of the way double-precision floating point arithmatic works. In this case, the above sequence can be converted to some extremely simple additions and bit shifting. When this gets run, the actual steps it takes look like this:</p> <ol> <li>Convert an <code>int</code> (<code>bits</code>) to a <code>double</code> (done on FPU, usually). This is quite fast.</li> <li>Subtract <code>0x00180000</code> from the upper 32 bits of that result. This is extremely fast.</li> </ol> <p>A very similar optimization can be applied whenever you multiply or divide any floating point number by any compile-time constant integer that is a power of two.</p> <p>This compiler optimization does not apply if you are instead dividing by a <code>double</code>, or if you divide by a non-compile-time-constant expression (any expression involving anything other than <code>final</code> compile-time-constant variables, literal numbers, or operators). In that case, it must be performed as a double-precision floating-point division, which is probably the <em>slowest single operation</em>, except for block transfers and advanced mathematical functions.</p> <p>However, as you can see, <code>1&lt;&lt;24</code> is a compile-time constant power of two, and so the optimization <em>does</em> apply in this case.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload