<?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[IDCT Algorithmus optimieren]]></title><description><![CDATA[<p>Hi Leute,<br />
gibt es eine Möglichkeit den Algorithmus zu optimieren:</p>
<pre><code>void IDCT::iDCT(int startmatrix[8][8], int check){
  Check = check;
  for(int i = 0; i&lt;8; ++i){
    for(int j = 0; j&lt;8; ++j){
      for(int k = 0; k&lt;8; ++k){
        for(int l = 0; l&lt;8; ++l){
          Result += 0.25*(ccalc(k)*ccalc(l)*startmatrix[k][l]*coscalc(i, j, k, l));//iDCT
        }//l
      }//k
      Result += 128;
      colorfiller(Result, Check);
      Result = 0;
    }//j
  }//i
  Check = 0;
}

/**************************************
 *********Inline functions imp*********
 **************************************/

float IDCT::ccalc(int wert){
  if(wert == 0){
    return SQT; //return the define
  }else{
    return 1;
  }
}

float IDCT::coscalc(int i, int j, int k, int l){
  return std::cos((((2*i)+1)*k*PI)/16)*std::cos((((2*j)+1)*l*PI)/16); //main part(cos) of iDCT
}
</code></pre>
<p>Also das</p>
<pre><code>std::cos
</code></pre>
<p>ist extrem lahm, weil es auch sehr oft aufgerufen wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/319063/idct-algorithmus-optimieren</link><generator>RSS for Node</generator><lastBuildDate>Sun, 26 Jul 2026 05:17:23 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/319063.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 05 Aug 2013 15:55:47 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to IDCT Algorithmus optimieren on Mon, 05 Aug 2013 15:55:47 GMT]]></title><description><![CDATA[<p>Hi Leute,<br />
gibt es eine Möglichkeit den Algorithmus zu optimieren:</p>
<pre><code>void IDCT::iDCT(int startmatrix[8][8], int check){
  Check = check;
  for(int i = 0; i&lt;8; ++i){
    for(int j = 0; j&lt;8; ++j){
      for(int k = 0; k&lt;8; ++k){
        for(int l = 0; l&lt;8; ++l){
          Result += 0.25*(ccalc(k)*ccalc(l)*startmatrix[k][l]*coscalc(i, j, k, l));//iDCT
        }//l
      }//k
      Result += 128;
      colorfiller(Result, Check);
      Result = 0;
    }//j
  }//i
  Check = 0;
}

/**************************************
 *********Inline functions imp*********
 **************************************/

float IDCT::ccalc(int wert){
  if(wert == 0){
    return SQT; //return the define
  }else{
    return 1;
  }
}

float IDCT::coscalc(int i, int j, int k, int l){
  return std::cos((((2*i)+1)*k*PI)/16)*std::cos((((2*j)+1)*l*PI)/16); //main part(cos) of iDCT
}
</code></pre>
<p>Also das</p>
<pre><code>std::cos
</code></pre>
<p>ist extrem lahm, weil es auch sehr oft aufgerufen wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2343619</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343619</guid><dc:creator><![CDATA[Fuchs aus dem Wald]]></dc:creator><pubDate>Mon, 05 Aug 2013 15:55:47 GMT</pubDate></item><item><title><![CDATA[Reply to IDCT Algorithmus optimieren on Mon, 05 Aug 2013 16:15:45 GMT]]></title><description><![CDATA[<p>wenn du keine Speicherplatzprobleme hast, bau dir doch ne lookup tabelle. soweit ich das sehe hängt dein std::cos ja nur von i,j,k,l ab...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2343625</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343625</guid><dc:creator><![CDATA[asdfasd]]></dc:creator><pubDate>Mon, 05 Aug 2013 16:15:45 GMT</pubDate></item><item><title><![CDATA[Reply to IDCT Algorithmus optimieren on Mon, 05 Aug 2013 16:17:40 GMT]]></title><description><![CDATA[<p>Mach doch einfach ein Lookuptable. Untegesteter Pseudocode:</p>
<pre><code class="language-cpp">float IDCT::coscalc(int i, int j, int k, int l){
  return discrete_cos(i, j)*discrete_cos(k, l);
}

std::vector&lt;float&gt; make_type()
{
  std::vector&lt;float&gt; ret(8*8);
  for (int i=0; i&lt;8; ++i)
    for (int j=0; j&lt;8; ++j)
      ret[8*i+j] = std::cos((((2*i)+1)*j*PI)/16);
  return ret;
}

float discrete_cos(int i, int j) {
  static vector&lt;float&gt; table = make_type();
  return table[8*i + j];
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2343627</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343627</guid><dc:creator><![CDATA[lucky uppe]]></dc:creator><pubDate>Mon, 05 Aug 2013 16:17:40 GMT</pubDate></item><item><title><![CDATA[Reply to IDCT Algorithmus optimieren on Mon, 05 Aug 2013 16:33:28 GMT]]></title><description><![CDATA[<p>Wow das hat schon mal was gebracht! Ich hab es so gemacht:</p>
<pre><code>void IDCT::iDCT(int startmatrix[8][8], int check){
  Check = check;
  coscalc();
  for(int i = 0; i&lt;8; ++i){
    for(int j = 0; j&lt;8; ++j){
      for(int k = 0; k&lt;8; ++k){
        for(int l = 0; l&lt;8; ++l){
          Result += 0.25*(ccalc(k)*ccalc(l)*startmatrix[k][l]*LUT_idct[i][j]*LUT_idct[k][l]);//iDCT
        }//l
      }//k
      Result += 128;
      colorfiller(Result, Check);
      Result = 0;
    }//j
  }//i
  Check = 0;
}
</code></pre>
<pre><code>void IDCT::coscalc(){
  for(int i = 0; i&lt;8; ++i){
    for(int j = 0; j&lt;8; ++j){
      LUT_idct[i][j] = std::cos((((2*i)+1)*j*PI)/16);
    }
  }
}
</code></pre>
<p>Na gut das kann man jetzt noch weiter optimieren hab es nur auf die schnelle mal ausprobiert. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /> Morgen geht es weiter jetzt ist Feierabend.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2343637</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343637</guid><dc:creator><![CDATA[Fuchs aus dem Wald]]></dc:creator><pubDate>Mon, 05 Aug 2013 16:33:28 GMT</pubDate></item><item><title><![CDATA[Reply to IDCT Algorithmus optimieren on Mon, 05 Aug 2013 16:34:46 GMT]]></title><description><![CDATA[<p>Oder du lagerst wirklich alles aus und nimmst 4KB overhead in kauf:</p>
<pre><code>void set_lookup(int i, int j)
{
	for(int i = 0; i&lt;8; ++i){ 
     for(int j = 0; j&lt;8; ++j){ 
       for(int k = 0; k&lt;8; ++k){ 
         for(int l = 0; l&lt;8; ++l){ 
			lookup[i][j][k][l]=	0.25 * std::cos((((2*i)+1)*k*PI)/16)
								* std::cos((((2*j)+1)*l*PI)/16)
								* ccalc(k) * ccalc(l);

}

void IDCT::iDCT(int startmatrix[8][8], int check){ 
   Check = check; 
   for(int i = 0; i&lt;8; ++i){ 
     for(int j = 0; j&lt;8; ++j){ 
       for(int k = 0; k&lt;8; ++k){ 
         for(int l = 0; l&lt;8; ++l){ 
           Result += startmatrix[k][l]*lookup[i][j][k][l]);//iDCT 
         }//l 
       }//k 
       Result += 128; 
       colorfiller(Result, Check); 
       Result = 0; 
     }//j 
   }//i 
   Check = 0; 
} 

/************************************** 
  *********Inline functions imp********* 
  **************************************/ 

float IDCT::ccalc(int wert){ 
   if(wert == 0){ 
     return SQT; //return the define 
   }else{ 
     return 1; 
   } 
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2343639</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343639</guid><dc:creator><![CDATA[asdfasdf]]></dc:creator><pubDate>Mon, 05 Aug 2013 16:34:46 GMT</pubDate></item><item><title><![CDATA[Reply to IDCT Algorithmus optimieren on Mon, 05 Aug 2013 16:36:58 GMT]]></title><description><![CDATA[<p>Der Cosinus ist hier bloß eine Nebenbaustelle. Das erste sollte wohl die Benutzung einer fast cosinus transform sein. So senkt man die Komplexität von 4096 Berechnungen auf etwas in der Gegend um 384.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2343640</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343640</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 05 Aug 2013 16:36:58 GMT</pubDate></item><item><title><![CDATA[Reply to IDCT Algorithmus optimieren on Mon, 05 Aug 2013 16:38:09 GMT]]></title><description><![CDATA[<p>Du brauchst natürlich noch nen array</p>
<pre><code>double lookup[8][8][8][8];
</code></pre>
<p>und set_lookup muss keine argumente nehmen. Und Overhead ist 32KB (8*8*8*8*sizeof(double) / 1024) = 32 KB <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2343641</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343641</guid><dc:creator><![CDATA[asdfasd]]></dc:creator><pubDate>Mon, 05 Aug 2013 16:38:09 GMT</pubDate></item><item><title><![CDATA[Reply to IDCT Algorithmus optimieren on Mon, 05 Aug 2013 16:38:41 GMT]]></title><description><![CDATA[<blockquote>
<p>Transforms of real even/odd data: the discrete cosine transform (DCT) and the discrete sine transform (DST), types I-IV</p>
</blockquote>
<p><a href="http://www.fftw.org" rel="nofollow">www.fftw.org</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2343642</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343642</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Mon, 05 Aug 2013 16:38:41 GMT</pubDate></item><item><title><![CDATA[Reply to IDCT Algorithmus optimieren on Mon, 05 Aug 2013 16:41:38 GMT]]></title><description><![CDATA[<p>Was für Compileroptionen verwendest du denn? Diesen Algorithmus könnte man prima mit SSE Instruktionen beschleunigen.</p>
<p>Wenn es nicht so genau sein muss, ist auch eine Annäherung an die Cosinusfunktion möglich. Hierbei ist aber wohl das Lookuptable die bessere Alternative.</p>
<p>Vielleicht kannst du ccalc auch so schreiben, ich bin mir aber ziemlich sicher, dass der Compileroptimierer aus beidem das gleiche bzw. gleich schnelles macht.</p>
<pre><code>float ccalc(int k, int l)
{
    static constexpr float array[3]= { 1, SQT, SQT*SQT };
    return array[(k==0)+(l==0)];
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2343644</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343644</guid><dc:creator><![CDATA[Marthog]]></dc:creator><pubDate>Mon, 05 Aug 2013 16:41:38 GMT</pubDate></item><item><title><![CDATA[Reply to IDCT Algorithmus optimieren on Mon, 05 Aug 2013 16:45:25 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">void IDCT::iDCT(int startmatrix[8][8], int check){
  Check = check;
  coscalc();
  for(int i = 0; i&lt;8; ++i){
    for(int j = 0; j&lt;8; ++j){
      for(int k = 0; k&lt;8; ++k){
        for(int l = 0; l&lt;8; ++l){
          Result += 0.25*(ccalc(k)*ccalc(l)*startmatrix[k][l]*LUT_idct[i][j]*LUT_idct[k][l]);//iDCT
        }//l
      }//k
      Result += 128;
      colorfiller(Result, Check);
      Result = 0;
    }//j
  }//i
  Check = 0;
}
</code></pre>
<p>Jetzt kommt der nächste Schritt: Schau dir das doch mal an von was das jeweils abhängig ist:</p>
<pre><code class="language-cpp">0.25 // konstante
*ccalc(k)*ccalc(l) // l+k
*startmatrix[k][l]* // l+k
LUT_idct[i][j]* // i+j
LUT_idct[k][l]; // l+k
</code></pre>
<p>Konstanten kann man ausklammern und das, was nur von i+j abhängig ist kann man ausklammern.</p>
<p>Wenn ich nichts übersehe, wäre das ja:</p>
<pre><code class="language-cpp">void IDCT::iDCT(int startmatrix[8][8], int check){
  Check = check;
  coscalc();

  // der k+l-Teil:
  float kl_result = 0;
      for(int k = 0; k&lt;8; ++k){
        for(int l = 0; l&lt;8; ++l){
          Result += ccalc(k)*ccalc(l)*startmatrix[k][l]*LUT_idct[k][l];// loop unrolling macht hoffentlich das ccalc weg
        }//l
      }//k
  kl_result *= 0.25;

  for(int i = 0; i&lt;8; ++i){
    for(int j = 0; j&lt;8; ++j){
      float Result = kl_result * LUT_idct[i][j];
      Result += 128;
      colorfiller(Result, Check);
      Result = 0;
    }//j
  }//i
  Check = 0;
}
</code></pre>
<p>Von O(8^4) auf O(8^2), das bringt mehr als SSE oder Lookup.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2343648</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343648</guid><dc:creator><![CDATA[lucky uppe]]></dc:creator><pubDate>Mon, 05 Aug 2013 16:45:25 GMT</pubDate></item><item><title><![CDATA[Reply to IDCT Algorithmus optimieren on Mon, 05 Aug 2013 16:45:42 GMT]]></title><description><![CDATA[<p>asdfasdf schrieb:</p>
<blockquote>
<p>Oder du lagerst wirklich alles aus und nimmst 4KB overhead in kauf:</p>
<pre><code>void set_lookup(int i, int j)
{
	for(int i = 0; i&lt;8; ++i){ 
     for(int j = 0; j&lt;8; ++j){ 
       for(int k = 0; k&lt;8; ++k){ 
         for(int l = 0; l&lt;8; ++l){ 
			lookup[i][j][k][l]=	0.25 * std::cos((((2*i)+1)*k*PI)/16)
								* std::cos((((2*j)+1)*l*PI)/16)
								* ccalc(k) * ccalc(l);
								
}
	
void IDCT::iDCT(int startmatrix[8][8], int check){ 
   Check = check; 
   for(int i = 0; i&lt;8; ++i){ 
     for(int j = 0; j&lt;8; ++j){ 
       for(int k = 0; k&lt;8; ++k){ 
         for(int l = 0; l&lt;8; ++l){ 
           Result += startmatrix[k][l]*lookup[i][j][k][l]);//iDCT 
         }//l 
       }//k 
       Result += 128; 
       colorfiller(Result, Check); 
       Result = 0; 
     }//j 
   }//i 
   Check = 0; 
} 
   
/************************************** 
  *********Inline functions imp********* 
  **************************************/ 
   
float IDCT::ccalc(int wert){ 
   if(wert == 0){ 
     return SQT; //return the define 
   }else{ 
     return 1; 
   } 
}
</code></pre>
</blockquote>
<p>Da ist meins was ich gepostet hab schneller.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2343649</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343649</guid><dc:creator><![CDATA[Fuchs aus dem Wald]]></dc:creator><pubDate>Mon, 05 Aug 2013 16:45:42 GMT</pubDate></item><item><title><![CDATA[Reply to IDCT Algorithmus optimieren on Mon, 05 Aug 2013 16:48:44 GMT]]></title><description><![CDATA[<p>Vielen dank ich werde die weiteren Tipps morgen umsetzen muss jetzt echt nach Hause. <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="😉"
    /> Bin schon viel zu lange hier.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2343652</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343652</guid><dc:creator><![CDATA[Fuchs aus dem Wald]]></dc:creator><pubDate>Mon, 05 Aug 2013 16:48:44 GMT</pubDate></item><item><title><![CDATA[Reply to IDCT Algorithmus optimieren on Tue, 06 Aug 2013 06:21:20 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/15540">@Fuchs</a> aus dem Wald<br />
Wie geht's deinem JPEG Decoder?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2343738</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343738</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Tue, 06 Aug 2013 06:21:20 GMT</pubDate></item><item><title><![CDATA[Reply to IDCT Algorithmus optimieren on Tue, 06 Aug 2013 06:27:56 GMT]]></title><description><![CDATA[<p>lucky uppe schrieb:</p>
<blockquote>
<pre><code class="language-cpp">void IDCT::iDCT(int startmatrix[8][8], int check){
  Check = check;
  coscalc();
  for(int i = 0; i&lt;8; ++i){
    for(int j = 0; j&lt;8; ++j){
      for(int k = 0; k&lt;8; ++k){
        for(int l = 0; l&lt;8; ++l){
          Result += 0.25*(ccalc(k)*ccalc(l)*startmatrix[k][l]*LUT_idct[i][j]*LUT_idct[k][l]);//iDCT
        }//l
      }//k
      Result += 128;
      colorfiller(Result, Check);
      Result = 0;
    }//j
  }//i
  Check = 0;
}
</code></pre>
<p>Jetzt kommt der nächste Schritt: Schau dir das doch mal an von was das jeweils abhängig ist:</p>
<pre><code class="language-cpp">0.25 // konstante
*ccalc(k)*ccalc(l) // l+k
*startmatrix[k][l]* // l+k
LUT_idct[i][j]* // i+j
LUT_idct[k][l]; // l+k
</code></pre>
<p>Konstanten kann man ausklammern und das, was nur von i+j abhängig ist kann man ausklammern.</p>
<p>Wenn ich nichts übersehe, wäre das ja:</p>
<pre><code class="language-cpp">void IDCT::iDCT(int startmatrix[8][8], int check){
  Check = check;
  coscalc();

  // der k+l-Teil:
  float kl_result = 0;
      for(int k = 0; k&lt;8; ++k){
        for(int l = 0; l&lt;8; ++l){
          Result += ccalc(k)*ccalc(l)*startmatrix[k][l]*LUT_idct[k][l];// loop unrolling macht hoffentlich das ccalc weg
        }//l
      }//k
  kl_result *= 0.25;

  for(int i = 0; i&lt;8; ++i){
    for(int j = 0; j&lt;8; ++j){
      float Result = kl_result * LUT_idct[i][j];
      Result += 128;
      colorfiller(Result, Check);
      Result = 0;
    }//j
  }//i
  Check = 0;
}
</code></pre>
<p>Von O(8^4) auf O(8^2), das bringt mehr als SSE oder Lookup.</p>
</blockquote>
<p>Wow danke schön das hat wirklich viel gebracht!</p>
<p>hustbaer schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/15540">@Fuchs</a> aus dem Wald<br />
Wie geht's deinem JPEG Devocer?</p>
</blockquote>
<p>Ganz gut läuft zu mindestens. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /> Wie du siehst optimiere ich grade noch ein bisschen. Also es ist irgendwie nicht wunderschön aber es funktioniert! <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="😉"
    /> Ja geschafft hab ich es!</p>
<p>EDIT:<br />
Für die ccalc könnte ich auch einfach ein Array nehmen mit den Werten: array = {0,1,1,1,1,1,1,1}</p>
<p>EDIT2:<br />
Bin jetzt mal vorher nachher mit callgrind rüber. ^^<br />
Von 460.000.000 auf 3.600.000 aufrufe. Ich finde das schon ziemlich gut! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2343774</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343774</guid><dc:creator><![CDATA[Fuchs aus dem Wald]]></dc:creator><pubDate>Tue, 06 Aug 2013 06:27:56 GMT</pubDate></item></channel></rss>