<div class="gmail_quote">On 8 January 2012 00:31, Manu <span dir="ltr"><<a href="mailto:turkeyman@gmail.com">turkeyman@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="HOEnZb"><div class="h5"><div class="gmail_quote">On 7 January 2012 22:44, Piotr Szturmaj <span dir="ltr"><<a href="mailto:bncrbme@jadamspam.pl" target="_blank">bncrbme@jadamspam.pl</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
The idea is to make versions of code that are environment dependent and never change during runtime, _without_ resorting to if statements. This statement would be valid only inside function bodies.<br>
<br>
Examples of such versions may be:<br>
* supported SIMD CPU extensions MMX, SSE, SSE2, etc.<br>
* AMD vs Intel CPU, to use instructions that are not available on both<br>
* different OS versions (XP/Vista/7, Linux kernel versions)<br>
<br>
Why that instead of current if statement?<br>
* some additional speed, avoids multiple checks in frequent operations<br>
* making specific executables (f.i. SSE4 only) by limiting set of supported runtime options during compile time<br>
<br>
Code example:<br>
<br>
void main()<br>
{<br>
    version(rt_SSE4)<br>
    {<br>
        ...<br>
    }<br>
    else version(rt_SSE2)<br>
    {<br>
        ...<br>
    }<br>
    else<br>
    {<br>
        // portable code<br>
    }<br>
}<br>
<br>
In this example program checks the supported extensions only once, before calling main(). Then it modifies the function code to make it execute only versions that match.<br>
<br>
Runtime version identifiers may be set inside shared static constructors of modules (this implies that rt-version may not be used inside of them). SIMD extensions would preferably be set by druntime with help of core.cpuid.<br>


<br>
Code modification mechanism is up to implementation. One that come to my mind is inserting unconditional jumps by the compiler then and fixing them up before calling main().<br>
<br>
Additional advantage is possibility to generate executables for particular environments. This may help reduce execucutable size when targeting specific CPU, especially some constrained/embedded system. Also many cpuid checks inside druntime may be avoided.<br>


<br>
Just thinking loud :)<br>
</blockquote></div><div><br></div></div></div><div>... you could just use if() ?</div><div>Perhaps detection of these features could be added to the standard library. But I have a feeling it already IS actually (core.cpuid, something I am extremely cynical of)</div>

</blockquote></div><br><div>Sorry, I only half read your post >_<</div><div>Many(/most?) systems won't let you write to code memory, so using self modifying code in this way this may be a problem...</div><div>Additionally, many modern systems use encrypted code memory, so self modifying code is impossible on these systems. But it's an interesting idea, which I have thought about myself too on occasion.</div>