Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For the 16.16 representation, the simple ternary is unlikely to be bettered speed-wise.</p> <p>And for doubles, because you need it standard/portable C, bit-fiddling of any kind will end badly. </p> <p>Even if a bit-fiddle was possible (which I doubt), you'd be relying on the binary representation of doubles. THIS (and their size) IS IMPLEMENTATION-DEPENDENT.</p> <p>Possibly you could "guess" this using sizeof(double) and then comparing the layout of various double values against their common binary representations, but I think you're on a hiding to nothing.</p> <p>The best rule is TELL THE COMPILER WHAT YOU WANT (ie ternary), and let it optimise for you.</p> <p><strong>EDIT:</strong> Humble pie time. I just tested quinmars idea (below), and it works - if you have IEEE-754 floats. This gave a speedup of about 20% on the code below. IObviously non-portable, but I think there may be a standardised way of asking your compiler if it uses IEEE754 float formats with a #IF...?</p> <pre><code> double FMIN = 3.13; double FMAX = 300.44; double FVAL[10] = {-100, 0.23, 1.24, 3.00, 3.5, 30.5, 50 ,100.22 ,200.22, 30000}; uint64 Lfmin = *(uint64 *)&amp;FMIN; uint64 Lfmax = *(uint64 *)&amp;FMAX; DWORD start = GetTickCount(); for (int j=0; j&lt;10000000; ++j) { uint64 * pfvalue = (uint64 *)&amp;FVAL[0]; for (int i=0; i&lt;10; ++i) *pfvalue++ = (*pfvalue &lt; Lfmin) ? Lfmin : (*pfvalue &gt; Lfmax) ? Lfmax : *pfvalue; } volatile DWORD hacktime = GetTickCount() - start; for (int j=0; j&lt;10000000; ++j) { double * pfvalue = &amp;FVAL[0]; for (int i=0; i&lt;10; ++i) *pfvalue++ = (*pfvalue &lt; FMIN) ? FMIN : (*pfvalue &gt; FMAX) ? FMAX : *pfvalue; } volatile DWORD normaltime = GetTickCount() - (start + hacktime); </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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