<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Optimierung von sqrtf]]></title><description><![CDATA[<p>Hi ich hab schon wieder ein Problemchen<br />
Und zwar hab ich mir mal angeschaut wie das mit der<br />
Wurzelberechnung implementiert ist<br />
Das habe ich gefunden für float</p>
<pre><code class="language-cpp">inline float sqrtf(float _X)
        {return ((float)sqrt((double)_X)); }
</code></pre>
<p>Quelle: math.h</p>
<p>Gut. Jetzt frag ich mich ob das gecaste und die funktion, die die eigentliche Wurzel berechnen vieleicht noch optimiert werden können.</p>
<p>Dabei bin ich auf eine Funktion gestoßen, die ich gern noch weiter optimieren würde, ich weis aber nicht so richtig wo ich anfangen soll.</p>
<pre><code class="language-cpp">inline float sqrtFast(float r)
{
    if(r == 0.f)
        return 0.0f;
    r = (r&lt;0 ? -r : r);
    Value = r;
    halfValue = 0.5f*r;

    ptr = (unsigned long*)&amp;r;
    *ptr=(0xbe6f0000-*ptr)&gt;&gt;1;

    temp1Wurzel = r;

    temp1Wurzel *= 1.5f-temp1Wurzel*temp1Wurzel*halfValue;
    temp1Wurzel *= 1.5f-temp1Wurzel*temp1Wurzel*halfValue;
    temp1Wurzel *= 1.5f-temp1Wurzel*temp1Wurzel*halfValue;
    temp1Wurzel *= 1.5f-temp1Wurzel*temp1Wurzel*halfValue;
    temp1Wurzel *= 1.5f-temp1Wurzel*temp1Wurzel*halfValue;

    return Value*temp1Wurzel; 
}
</code></pre>
<p>Kann das mal jemand optimieren, aber bitte KEIN ASSEMBLER.<br />
Es wäre echt nett, und ich wäre echt froh, wenn sich von auch mal jemand ne Minute Zeit nehmen würde.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/198365/optimierung-von-sqrtf</link><generator>RSS for Node</generator><lastBuildDate>Fri, 02 Oct 2026 03:29:39 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/198365.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 20 Nov 2007 21:59:42 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Optimierung von sqrtf on Tue, 20 Nov 2007 21:59:42 GMT]]></title><description><![CDATA[<p>Hi ich hab schon wieder ein Problemchen<br />
Und zwar hab ich mir mal angeschaut wie das mit der<br />
Wurzelberechnung implementiert ist<br />
Das habe ich gefunden für float</p>
<pre><code class="language-cpp">inline float sqrtf(float _X)
        {return ((float)sqrt((double)_X)); }
</code></pre>
<p>Quelle: math.h</p>
<p>Gut. Jetzt frag ich mich ob das gecaste und die funktion, die die eigentliche Wurzel berechnen vieleicht noch optimiert werden können.</p>
<p>Dabei bin ich auf eine Funktion gestoßen, die ich gern noch weiter optimieren würde, ich weis aber nicht so richtig wo ich anfangen soll.</p>
<pre><code class="language-cpp">inline float sqrtFast(float r)
{
    if(r == 0.f)
        return 0.0f;
    r = (r&lt;0 ? -r : r);
    Value = r;
    halfValue = 0.5f*r;

    ptr = (unsigned long*)&amp;r;
    *ptr=(0xbe6f0000-*ptr)&gt;&gt;1;

    temp1Wurzel = r;

    temp1Wurzel *= 1.5f-temp1Wurzel*temp1Wurzel*halfValue;
    temp1Wurzel *= 1.5f-temp1Wurzel*temp1Wurzel*halfValue;
    temp1Wurzel *= 1.5f-temp1Wurzel*temp1Wurzel*halfValue;
    temp1Wurzel *= 1.5f-temp1Wurzel*temp1Wurzel*halfValue;
    temp1Wurzel *= 1.5f-temp1Wurzel*temp1Wurzel*halfValue;

    return Value*temp1Wurzel; 
}
</code></pre>
<p>Kann das mal jemand optimieren, aber bitte KEIN ASSEMBLER.<br />
Es wäre echt nett, und ich wäre echt froh, wenn sich von auch mal jemand ne Minute Zeit nehmen würde.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1406987</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1406987</guid><dc:creator><![CDATA[OTTO]]></dc:creator><pubDate>Tue, 20 Nov 2007 21:59:42 GMT</pubDate></item><item><title><![CDATA[Reply to Optimierung von sqrtf on Tue, 20 Nov 2007 22:11:00 GMT]]></title><description><![CDATA[<p><a href="http://www.azillionmonkeys.com/qed/sqroot.html" rel="nofollow">http://www.azillionmonkeys.com/qed/sqroot.html</a><br />
<img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /><br />
.............<br />
.............<br />
.............<br />
.............<br />
.............<br />
.............<br />
.............<br />
.............<br />
.............<br />
.............<br />
.............<br />
.............<br />
.............<br />
.............<br />
.............<br />
.............<br />
.............<br />
.............<br />
.............</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1406999</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1406999</guid><dc:creator><![CDATA[Rootman]]></dc:creator><pubDate>Tue, 20 Nov 2007 22:11:00 GMT</pubDate></item><item><title><![CDATA[Reply to Optimierung von sqrtf on Tue, 20 Nov 2007 22:24:33 GMT]]></title><description><![CDATA[<p>Danke, soweit war ich auch schon <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1407010</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1407010</guid><dc:creator><![CDATA[OTTO]]></dc:creator><pubDate>Tue, 20 Nov 2007 22:24:33 GMT</pubDate></item><item><title><![CDATA[Reply to Optimierung von sqrtf on Tue, 20 Nov 2007 22:44:21 GMT]]></title><description><![CDATA[<p>OTTO schrieb:</p>
<blockquote>
<p>Danke, soweit war ich auch schon</p>
</blockquote>
<p>perfekt! wozu dann noch die frage hier?<br />
<img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1407018</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1407018</guid><dc:creator><![CDATA[Wurzelsepp]]></dc:creator><pubDate>Tue, 20 Nov 2007 22:44:21 GMT</pubDate></item><item><title><![CDATA[Reply to Optimierung von sqrtf on Tue, 20 Nov 2007 22:51:52 GMT]]></title><description><![CDATA[<p>Meiner Meinung nach versteh ich unter optimierung folgendes<br />
&quot;Optimierung = Beschleunigung&quot;</p>
<p>Beispielsweise bin ich mir nicht ganz sicher ob ich das wirklich 5mal brauche</p>
<pre><code class="language-cpp">temp1Wurzel *= 1.5f-temp1Wurzel*temp1Wurzel*halfValue;
</code></pre>
<p>Sowas versteh ich unter Code-Optimierung</p>
<p>Ach ja:<br />
Value ist float<br />
HalfValue ist float<br />
ptr ist (unsigned int <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title="*)"
      alt="😉"
    /></p>
<p>Fällt jemandem noch was ein wie der Code schneller wird oder antworten mir nur Wurzeksepp etc?</p>
<p>Ich bin für jede brauchbare Antwort dankbar</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1407024</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1407024</guid><dc:creator><![CDATA[OTTO]]></dc:creator><pubDate>Tue, 20 Nov 2007 22:51:52 GMT</pubDate></item><item><title><![CDATA[Reply to Optimierung von sqrtf on Tue, 20 Nov 2007 23:06:17 GMT]]></title><description><![CDATA[<blockquote>
<p>Meiner Meinung nach versteh ich unter optimierung folgendes<br />
&quot;Optimierung = Beschleunigung&quot;<br />
Sowas versteh ich unter Code-Optimierung</p>
</blockquote>
<p>ROFL! Das tut aber nicht jeder und das ist sogar Fakt.</p>
<p>Schreib dir das ganze mal auf einen Zettel auf und vereinfache bzw. hebe hervor. Besonders die lustigen templWurzel Multiplikationen. Da is noch einiges drin.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1407029</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1407029</guid><dc:creator><![CDATA[Gator]]></dc:creator><pubDate>Tue, 20 Nov 2007 23:06:17 GMT</pubDate></item><item><title><![CDATA[Reply to Optimierung von sqrtf on Wed, 21 Nov 2007 14:04:01 GMT]]></title><description><![CDATA[<p>Also ich habs schon soweit</p>
<pre><code class="language-cpp">inline float GTSqrt(float r)
{
    if(r == 0.0f)
        return 0.0f;

    r = (r&lt;0 ? -r : r);

    float Value = r;
    float halfValue=0.5F*r;

    unsigned long *ptr = (unsigned long*)&amp;r;
    *ptr=(0xbe6f0000-*ptr)&gt;&gt;1;

    float temp1Wurzel = r;

    temp1Wurzel *= 1.5f-temp1Wurzel*temp1Wurzel*halfValue;
    temp1Wurzel *= 1.5f-temp1Wurzel*temp1Wurzel*halfValue;

    return Value*temp1Wurzel;
}
</code></pre>
<p>Kanns das mal jemand BITTE noch weiter optimieren, ich weis nehmlich nicht wo ich da noch was machen soll. Bitte...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1407358</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1407358</guid><dc:creator><![CDATA[OTTO]]></dc:creator><pubDate>Wed, 21 Nov 2007 14:04:01 GMT</pubDate></item><item><title><![CDATA[Reply to Optimierung von sqrtf on Wed, 21 Nov 2007 14:33:32 GMT]]></title><description><![CDATA[<p>du kannst noch eine zeile entfernen von dieser tmpWurzel sache, nur wird dann die genauigkeit immer geringer,</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1407384</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1407384</guid><dc:creator><![CDATA[grdgdr]]></dc:creator><pubDate>Wed, 21 Nov 2007 14:33:32 GMT</pubDate></item><item><title><![CDATA[Reply to Optimierung von sqrtf on Wed, 21 Nov 2007 14:36:34 GMT]]></title><description><![CDATA[<p>Wie oben schon geschehen, reichen zwei Iterationen i.d.R. schon aus... Hier aber eine verkürzte Variante aus dem QuakeIII Quellcode:</p>
<pre><code class="language-cpp">float Q_rsqrt( float number ){
    long i;
    float x2, y;
    const float threehalfs = 1.5F;

    x2 = number * 0.5F;
    y  = number;
    i  = * ( long * ) &amp;y;  // evil floating point bit level hacking
    i  = 0x5f3759df - ( i &gt;&gt; 1 ); // what the fuck?
    y  = * ( float * ) &amp;i;
    y  = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
    // y  = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
    return y;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1407387</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1407387</guid><dc:creator><![CDATA[mikey]]></dc:creator><pubDate>Wed, 21 Nov 2007 14:36:34 GMT</pubDate></item><item><title><![CDATA[Reply to Optimierung von sqrtf on Wed, 21 Nov 2007 16:02:02 GMT]]></title><description><![CDATA[<p>Danke!<br />
Aber der Code ist falsch<br />
ich hab ihn überarbeitet und jetzt klappts wirklich</p>
<pre><code class="language-cpp">float Q_rsqrt( float number ){
    long i;
    float x2, y;
    const float threehalfs = 1.5F;

    x2 = number * 0.5F;
    y  = number;
    i  = * ( long * ) &amp;y;  // evil floating point bit level hacking
    i  = 0x5f3759df - ( i &gt;&gt; 1 ); // what the love?
    y  = * ( float * ) &amp;i;
    y  = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
    // y  = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
    return y*number;
}
</code></pre>
<p>Kleines Problemchen wäre da nur noch dass beispielsweise Wurzel von 2 nur die ersten 2 stellen richtig sind, bei meiner sind es ganze 6 Stellen sind, schon schade, aber mit 2 Iterationen klappts<br />
Aber kriegt man die noch schneller?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1407490</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1407490</guid><dc:creator><![CDATA[OTTO]]></dc:creator><pubDate>Wed, 21 Nov 2007 16:02:02 GMT</pubDate></item><item><title><![CDATA[Reply to Optimierung von sqrtf on Wed, 21 Nov 2007 16:16:32 GMT]]></title><description><![CDATA[<p>Wiegesagt, du kannst die Genauigkeit mit beliebig vielen Iterationen hochschrauben, es war ja auch nur ein Denkanstoß. Ich denke, was schnelleres wirst du nicht in die Hände bekommen...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1407505</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1407505</guid><dc:creator><![CDATA[mikey]]></dc:creator><pubDate>Wed, 21 Nov 2007 16:16:32 GMT</pubDate></item><item><title><![CDATA[Reply to Optimierung von sqrtf on Wed, 21 Nov 2007 19:21:39 GMT]]></title><description><![CDATA[<p>Wozu das ganze? Die Hardwareimplementierungen von sqrt auf aktuellen Architekturen (zB x86) sind schneller als jede manuelle Version, zumindest bei vergleichbarer Genauigkeit.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1407685</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1407685</guid><dc:creator><![CDATA[groovemaster]]></dc:creator><pubDate>Wed, 21 Nov 2007 19:21:39 GMT</pubDate></item></channel></rss>