<div dir="ltr"><div><div><div>Hello,<br><br></div>When working on parabolic interpolation in a Pd class, I wondered again what is the best method to specify literal constants as Pd&#39;s type t_float (which could be float or double). The interpolation goes like:<br>

<br>    ...<br>    t_float a = buf[peakindex-1];<br>    t_float b = buf[peakindex];<br>    t_float c = buf[peakindex+1];<br>    t_float realpeak;<br>    <br>    realpeak = b + 0.125 * (c - a) * (c - a) / (2. * b - a - c);<br>

    ...<br><br></div><div>Without float suffixes for the literals, single precision t_float variables would be promoted to double here, which would be an unintended waste of CPU cycles. For some time, I&#39;ve worked around this by using const variables instead of literals, like:<br>

<br>    ...<br></div><div>    const t_float two = 2.;<br></div><div>    const t_float eighth = 0.125;<br>    t_float a = buf[peakindex-1];<br>    t_float b = buf[peakindex];<br>    t_float c = buf[peakindex+1];<br>    t_float realpeak;<br>

    <br>    realpeak = b + eighth * (c - a) * (c - a) / (two * b - a - c);<br>    ...<br><br></div><div>While this avoids redundant type conversions, it clutters the code and does not result in such fast instructions as literals do. Therefore I am now using type casts where type specifiers are normally used:<br>

<br>    ...<br>    t_float a = buf[peakindex-1];<br>    t_float b = buf[peakindex];<br>    t_float c = buf[peakindex+1];<br>    t_float realpeak;<br>    <br>    realpeak = b + (t_float)0.125 * (c - a) * (c - a) / ((t_float)2. * b - a - c);<br>

    ...<br></div><br>For the above code I have checked assembly output as generated by GCC with -O3 optimization on Linux i386. Using literals without type specification, the whole routine is done on the FPU (80 bits precision). With the literals cast to t_float, it is done with single precision instructions for XMM registers. <br>
<br>As far as I can see, casting literals to t_float results in the same assembly output as using the float specifier. For single precision t_float,  &#39;(t_float)0.125&#39; is equivalent to &#39;0.125f&#39;. I can&#39;t think of a disadvantage, but let me know if I overlooked something.<br>
</div><div><br></div><div>
<div>Katja<br></div><div><br></div></div></div>