<?xml version="1.0" encoding="iso-8859-1"?><!-- generator="b2evolution/2.4.2" -->
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
	<channel>
		<title>axel's dev corner</title>
		<link>http://blogs.plinge.de/dev/</link>
		<description></description>
		<language>en-US</language>
		<docs>http://blogs.law.harvard.edu/tech/rss</docs>
		<admin:generatorAgent rdf:resource="http://b2evolution.net/?v=2.4.2"/>
		<ttl>60</ttl>
				<item>
			<title>MFC Memory Leak Reporting</title>
			<link>http://blogs.plinge.de/dev/2010/06/29/mfc-memory-leak-reporting</link>
			<pubDate>Tue, 29 Jun 2010 11:06:18 +0000</pubDate>			<dc:creator>axel</dc:creator>
			<category domain="alt">programming</category>
<category domain="alt">C++</category>
<category domain="main">MFC</category>			<guid isPermaLink="false">83@http://blogs.plinge.de/</guid>
						<description>&lt;p&gt;When using some MFC-Linking DLLs in a non-MFC project, I stubled on sth quite annoying. According to &lt;a href=&quot;http://www.eggheadcafe.com/software/aspnet/32623009/why-does-mfc-call-crtdum.aspx&quot;&gt;various sources&lt;/a&gt;, an wierd destructor calls  &lt;code&gt;_CrtDumpMemoryLeaks&lt;/code&gt; each time the MFC is unloaded. In my case, that ment every time a DLL was scanned via LoadLibrary/FreeLibrary. As a result, each and every block of allocated memory was reported as leak for each call of FreeLibrary. This lead to a runtime of serveral minutes in which VisualStudio was unusable. &lt;/p&gt;
&lt;p&gt;Thankfully, further search turned up a &lt;a href=&quot;http://groups.google.com/group/microsoft.public.vc.mfc/browse_frm/thread/73493ddec165a5cb/e651b944da9c619d?#e651b944da9c619d&quot;&gt;solution &lt;/a&gt; by Pieter Op de Beeck posted in microsoft.public.vc.mfc &lt;em&gt;ten years ago&lt;/em&gt;. go figure.&lt;/p&gt; 
&lt;p&gt;In case you stuble onto this, you'll probably want the fix right away, so here it is: &lt;a href=&quot;http://blogs.plinge.de/media/blogs/adv/mfcmemleak.zip&quot; title=&quot;&quot;&gt;MemoryLeakDetector.zip&lt;/a&gt;&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.plinge.de/dev/2010/06/29/mfc-memory-leak-reporting&quot;&gt;Original post&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>When using some MFC-Linking DLLs in a non-MFC project, I stubled on sth quite annoying. According to <a href="http://www.eggheadcafe.com/software/aspnet/32623009/why-does-mfc-call-crtdum.aspx">various sources</a>, an wierd destructor calls  <code>_CrtDumpMemoryLeaks</code> each time the MFC is unloaded. In my case, that ment every time a DLL was scanned via LoadLibrary/FreeLibrary. As a result, each and every block of allocated memory was reported as leak for each call of FreeLibrary. This lead to a runtime of serveral minutes in which VisualStudio was unusable. </p>
<p>Thankfully, further search turned up a <a href="http://groups.google.com/group/microsoft.public.vc.mfc/browse_frm/thread/73493ddec165a5cb/e651b944da9c619d?#e651b944da9c619d">solution </a> by Pieter Op de Beeck posted in microsoft.public.vc.mfc <em>ten years ago</em>. go figure.</p> 
<p>In case you stuble onto this, you'll probably want the fix right away, so here it is: <a href="http://blogs.plinge.de/media/blogs/adv/mfcmemleak.zip" title="">MemoryLeakDetector.zip</a></p><div class="item_footer"><p><small><a href="http://blogs.plinge.de/dev/2010/06/29/mfc-memory-leak-reporting">Original post</a></small></p></div>]]></content:encoded>
								<comments>http://blogs.plinge.de/dev/2010/06/29/mfc-memory-leak-reporting#comments</comments>
		</item>
				<item>
			<title>classicthesis with figures using full width</title>
			<link>http://blogs.plinge.de/dev/2010/02/01/classicthesis-widefigure</link>
			<pubDate>Mon,  1 Feb 2010 18:52:10 +0000</pubDate>			<dc:creator>axel</dc:creator>
			<category domain="alt">programming</category>
<category domain="alt">science</category>
<category domain="main">TeX</category>			<guid isPermaLink="false">78@http://blogs.plinge.de/</guid>
						<description>&lt;p&gt;When using &lt;a href=&quot;http://tug.ctan.org/pkg/classicthesis&quot;&gt;calssicthesis&lt;/a&gt; most of the defaults look good and are there for a typographical, aesthetical or arbitrary reason. &lt;a href=&quot;http://mrunix.de/forums/showthread.php?t=61824&quot;&gt;Some people&lt;/a&gt; immediately want turn on &lt;code&gt;dottedtoc&lt;/code&gt; for instance.&lt;/p&gt;

&lt;p&gt;I had a very different concern. The package uses a rather wide margin, in order to place nice &lt;code&gt;marginpars&lt;/code&gt; thruout the text. Thats fine by me, it also allows for a nice small text layout without resorting to multiple columns. I did not like this for the typesetting of figures, especially large figures spanning a whole page or the top half thereof. So, I ventured out to change this. After some searching I came across the neat &lt;a href=&quot;http://tug.ctan.org/pkg/changepage&quot;&gt;changepage&lt;/a&gt; package, which most of all allows for save determination of even and odd pages. With this, it was fairly straightforward to implement. I chose to provide a &lt;code&gt;widefigure&lt;/code&gt; environment to typeset full-width figures with &lt;code&gt;[p]&lt;/code&gt; or &lt;code&gt;[t]&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;
% wider floats by use of changepage. 
% see changepage documentation for details on the underlying quirks
\usepackage[strict]{changepage}  
% calculate size of margin to use
\newlength\totalmargin
\setlength\totalmargin\marginparwidth
\addtolength\totalmargin\marginparsep
% layout using the full margin as well
\newenvironment{layoutfullwidth}{%
\checkoddpage%
\ifoddpage%
\begin{adjustwidth}{0pt}{-\totalmargin}
\else%
\begin{adjustwidth}{-\totalmargin}{0pt}
\fi%
% advance textwidth for use in float,
% e.g. includegraphics[width=\textwidth]
\advance\textwidth\totalmargin
}{%
\end{adjustwidth}
}
% new, wider figure to use where wanted.
% note that the caption will be layouted wide as well.
\newenvironment{widefigure}[1][!tpb]{%
\begin{figure}[#1] 
\begin{layoutfullwidth}
}{
\end{layoutfullwidth}
\end{figure}
&lt;/pre&gt;

&lt;p&gt;This is a straightforeward way of doing it, changing the typesetting of all floats in general requires some really ugly plain TeX hacking. I got it to work out of playful ambition but decided against using it in order to keep the code clean and understandable.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.plinge.de/dev/2010/02/01/classicthesis-widefigure&quot;&gt;Original post&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>When using <a href="http://tug.ctan.org/pkg/classicthesis">calssicthesis</a> most of the defaults look good and are there for a typographical, aesthetical or arbitrary reason. <a href="http://mrunix.de/forums/showthread.php?t=61824">Some people</a> immediately want turn on <code>dottedtoc</code> for instance.</p>

<p>I had a very different concern. The package uses a rather wide margin, in order to place nice <code>marginpars</code> thruout the text. Thats fine by me, it also allows for a nice small text layout without resorting to multiple columns. I did not like this for the typesetting of figures, especially large figures spanning a whole page or the top half thereof. So, I ventured out to change this. After some searching I came across the neat <a href="http://tug.ctan.org/pkg/changepage">changepage</a> package, which most of all allows for save determination of even and odd pages. With this, it was fairly straightforward to implement. I chose to provide a <code>widefigure</code> environment to typeset full-width figures with <code>[p]</code> or <code>[t]</code>.</p>

<pre>
% wider floats by use of changepage. 
% see changepage documentation for details on the underlying quirks
\usepackage[strict]{changepage}  
% calculate size of margin to use
\newlength\totalmargin
\setlength\totalmargin\marginparwidth
\addtolength\totalmargin\marginparsep
% layout using the full margin as well
\newenvironment{layoutfullwidth}{%
\checkoddpage%
\ifoddpage%
\begin{adjustwidth}{0pt}{-\totalmargin}
\else%
\begin{adjustwidth}{-\totalmargin}{0pt}
\fi%
% advance textwidth for use in float,
% e.g. includegraphics[width=\textwidth]
\advance\textwidth\totalmargin
}{%
\end{adjustwidth}
}
% new, wider figure to use where wanted.
% note that the caption will be layouted wide as well.
\newenvironment{widefigure}[1][!tpb]{%
\begin{figure}[#1] 
\begin{layoutfullwidth}
}{
\end{layoutfullwidth}
\end{figure}
</pre>

<p>This is a straightforeward way of doing it, changing the typesetting of all floats in general requires some really ugly plain TeX hacking. I got it to work out of playful ambition but decided against using it in order to keep the code clean and understandable.</p><div class="item_footer"><p><small><a href="http://blogs.plinge.de/dev/2010/02/01/classicthesis-widefigure">Original post</a></small></p></div>]]></content:encoded>
								<comments>http://blogs.plinge.de/dev/2010/02/01/classicthesis-widefigure#comments</comments>
		</item>
				<item>
			<title>Setting up NVidia TwinView</title>
			<link>http://blogs.plinge.de/dev/2009/12/02/twinview</link>
			<pubDate>Wed,  2 Dec 2009 02:56:20 +0000</pubDate>			<dc:creator>axel</dc:creator>
			<category domain="main">linux</category>			<guid isPermaLink="false">75@http://blogs.plinge.de/</guid>
						<description>&lt;p&gt;Well, it was time to discard my old Xinerama &lt;code&gt;xorg.conf&lt;/code&gt; in &lt;a href=&quot;http://www.kubuntu.org/&quot;&gt;Kubuntu&lt;/a&gt; Jaunty. From a few friendly hints I finally managed to establish a simple procedure. I installed the &lt;a href=&quot;http://www.nvidia.com/Download/index.aspx?lang=en-us&quot;&gt;manufacturer driver&lt;/a&gt; a while ago. &lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Start X with an empty configuration or &lt;code&gt;sudo nvidia-xconfig&lt;/code&gt; and only one monitor&lt;/li&gt;
  &lt;li&gt;Connect the second monitor&lt;/li&gt;
  &lt;li&gt;Start nvidia-settings and add the second monitor. Save the configuration.&lt;/li&gt;
  &lt;li&gt;If the Resolution is not sufficent, add Monitor sections for all Monitors and set the metamodes to auto&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, my &lt;code&gt;xorg.conf&lt;/code&gt; now looks like this:&lt;/p&gt;

&lt;pre&gt;
# twinview, both monitors set via section and nvidia-auto-select

Section &quot;ServerLayout&quot;
    Identifier     &quot;Layout0&quot;
    Screen      0  &quot;Screen0&quot; 0 0
    InputDevice    &quot;Keyboard0&quot; &quot;CoreKeyboard&quot;
    InputDevice    &quot;Mouse0&quot; &quot;CorePointer&quot;
EndSection

Section &quot;Files&quot;
EndSection

Section &quot;ServerFlags&quot;
    Option         &quot;Xinerama&quot; &quot;0&quot;
EndSection

Section &quot;InputDevice&quot;
    # generated from default
    Identifier     &quot;Mouse0&quot;
    Driver         &quot;mouse&quot;
    Option         &quot;Protocol&quot; &quot;auto&quot;
    Option         &quot;Device&quot; &quot;/dev/psaux&quot;
    Option         &quot;Emulate3Buttons&quot; &quot;no&quot;
    Option         &quot;ZAxisMapping&quot; &quot;4 5&quot;
EndSection

Section &quot;InputDevice&quot;
    # generated from default
    Identifier     &quot;Keyboard0&quot;
    Driver         &quot;kbd&quot;
EndSection

Section &quot;Monitor&quot;
  Identifier &quot;Scott&quot;
  Modelname &quot;hr17c&quot;
  VertRefresh 60-100
  HorizSync 42-80
  DisplaySize 325 243
  Modeline &quot;1280x1024@75&quot;  135.00  1280 1296 1440 1688  1024 1025 1028 1066 +hsync +vsync
  Option &quot;DPMS&quot;
EndSection

Section &quot;Monitor&quot;
  Identifier &quot;Samsung&quot;
  Modelname &quot;226BW&quot;
  VertRefresh 56-75
  HorizSync 30-80
  Option &quot;DPMS&quot;
  DisplaySize 474 296
EndSection

Section &quot;Device&quot;
    Identifier     &quot;Device0&quot;
    Driver         &quot;nvidia&quot;
    VendorName     &quot;NVIDIA Corporation&quot;
    BoardName      &quot;GeForce 7600 GT&quot;
EndSection

Section &quot;Screen&quot;
    Identifier     &quot;Screen0&quot;
    Device         &quot;Device0&quot;
    Monitor        &quot;Samsung&quot;
    DefaultDepth    24
    Option         &quot;TwinView&quot; &quot;1&quot;
    Option         &quot;TwinViewXineramaInfoOrder&quot; &quot;DFP-0&quot;
    Option         &quot;metamodes&quot; &quot;DFP: nvidia-auto-select +0+0, CRT: nvidia-auto-select +1680+0&quot;
    SubSection     &quot;Display&quot;
        Depth       24
    EndSubSection
EndSection
&lt;/pre&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.plinge.de/dev/2009/12/02/twinview&quot;&gt;Original post&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>Well, it was time to discard my old Xinerama <code>xorg.conf</code> in <a href="http://www.kubuntu.org/">Kubuntu</a> Jaunty. From a few friendly hints I finally managed to establish a simple procedure. I installed the <a href="http://www.nvidia.com/Download/index.aspx?lang=en-us">manufacturer driver</a> a while ago. </p>

<ol>
  <li>Start X with an empty configuration or <code>sudo nvidia-xconfig</code> and only one monitor</li>
  <li>Connect the second monitor</li>
  <li>Start nvidia-settings and add the second monitor. Save the configuration.</li>
  <li>If the Resolution is not sufficent, add Monitor sections for all Monitors and set the metamodes to auto</li>
</ol>

<p>So, my <code>xorg.conf</code> now looks like this:</p>

<pre>
# twinview, both monitors set via section and nvidia-auto-select

Section "ServerLayout"
    Identifier     "Layout0"
    Screen      0  "Screen0" 0 0
    InputDevice    "Keyboard0" "CoreKeyboard"
    InputDevice    "Mouse0" "CorePointer"
EndSection

Section "Files"
EndSection

Section "ServerFlags"
    Option         "Xinerama" "0"
EndSection

Section "InputDevice"
    # generated from default
    Identifier     "Mouse0"
    Driver         "mouse"
    Option         "Protocol" "auto"
    Option         "Device" "/dev/psaux"
    Option         "Emulate3Buttons" "no"
    Option         "ZAxisMapping" "4 5"
EndSection

Section "InputDevice"
    # generated from default
    Identifier     "Keyboard0"
    Driver         "kbd"
EndSection

Section "Monitor"
  Identifier "Scott"
  Modelname "hr17c"
  VertRefresh 60-100
  HorizSync 42-80
  DisplaySize 325 243
  Modeline "1280x1024@75"  135.00  1280 1296 1440 1688  1024 1025 1028 1066 +hsync +vsync
  Option "DPMS"
EndSection

Section "Monitor"
  Identifier "Samsung"
  Modelname "226BW"
  VertRefresh 56-75
  HorizSync 30-80
  Option "DPMS"
  DisplaySize 474 296
EndSection

Section "Device"
    Identifier     "Device0"
    Driver         "nvidia"
    VendorName     "NVIDIA Corporation"
    BoardName      "GeForce 7600 GT"
EndSection

Section "Screen"
    Identifier     "Screen0"
    Device         "Device0"
    Monitor        "Samsung"
    DefaultDepth    24
    Option         "TwinView" "1"
    Option         "TwinViewXineramaInfoOrder" "DFP-0"
    Option         "metamodes" "DFP: nvidia-auto-select +0+0, CRT: nvidia-auto-select +1680+0"
    SubSection     "Display"
        Depth       24
    EndSubSection
EndSection
</pre><div class="item_footer"><p><small><a href="http://blogs.plinge.de/dev/2009/12/02/twinview">Original post</a></small></p></div>]]></content:encoded>
								<comments>http://blogs.plinge.de/dev/2009/12/02/twinview#comments</comments>
		</item>
				<item>
			<title>Spectral Calculations with OpenMP</title>
			<link>http://blogs.plinge.de/dev/2009/10/19/spectral-calculations-with-openmp</link>
			<pubDate>Mon, 19 Oct 2009 01:32:40 +0000</pubDate>			<dc:creator>axel</dc:creator>
			<category domain="main">programming</category>
<category domain="alt">C++</category>			<guid isPermaLink="false">74@http://blogs.plinge.de/</guid>
						<description>&lt;p&gt;Since it is high time to get into multicore programming and all major compilers support the &lt;a href=&quot;http://openmp.org&quot;&gt;OpenMP&lt;/a&gt; standard, I choose that road for my first endeavor. One particular lengthy operation in some of my projects is that of diverse &lt;a href=&quot;http://en.wikipedia.org/wiki/Spectrogram&quot;&gt;Spectrograms&lt;/a&gt;. Since an &lt;a href=&quot;http://en.wikipedia.org/wiki/FFT&quot;&gt;FFT&lt;/a&gt; is calculated for every column of pixels independently, these can be splitted straightforewardly into threads by the number of virtual processors. A couple of tests  showed that it is indeed most advised to split as much data as possible, i.e. at the highest level avaliable. I hacked together a slightly off use of the for directive to achieve what I wanted.&lt;/p&gt;

&lt;pre&gt;
// div width by number of virtual processors, min = 128, max = none
int slice = AP_MP::getSlice(width,128,-1); 
int xStart;
#pragma omp parallel for private(xStart)
for (xStart=0;xStart&amp;lt;width;xStart+=slice) {
  int xEnd = xStart+slice;
  if (xEnd&gt;width) xEnd=width;
  calcSlice(..,xStart,xEnd); 
}
&lt;/pre&gt;


&lt;p&gt;
As I hoped, the calculations were indeed 1.3-1.8 times faster on a dual-core machine, which is quite nice for such limited effort.
The following table gives the min/median/max times in ms for a 2.8s audio file sampled with 16bit at 22050Hz.
Spectrum is a pure magnitude spectrum calculation, Spectrogram represents a rendering of a spectrogram image, the Auditory Spectrogram includes a rather lengthy masking calculation. &lt;code&gt;g++ -O3 -ffast-math&lt;/code&gt; is up to four times faster for the core routines, but that lessens for larger code segments. The Auditory Spectrogram is slightly faster on Windows.
&lt;/p&gt;

&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&amp;nbsp; &lt;/td&gt;
&lt;td colspan='8' align='center'&gt;Linux, g++ -O3 -ffast-math&lt;/td&gt;
&lt;td&gt;&amp;nbsp; &lt;/td&gt;
&lt;td colspan='8' align='center'&gt;Windows, VS2008&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;

&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/td&gt;
&lt;td colspan='3' align='center'&gt;1 thread&lt;/td&gt;
&lt;td colspan='3' align='center'&gt;2 threads&lt;/td&gt;
&lt;td&gt;&amp;nbsp; &lt;/td&gt;
&lt;td&gt; speedup&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/td&gt;
&lt;td colspan='3' align='center'&gt;1 thread&lt;/td&gt;
&lt;td colspan='3' align='center'&gt;2 threads&lt;/td&gt;
&lt;td&gt;&amp;nbsp; &lt;/td&gt;
&lt;td&gt; speedup&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;

&lt;td&gt;Spectrum&lt;/td&gt;
&lt;td&gt;&amp;nbsp; &lt;/td&gt;
&lt;td align='right'&gt;10.1 &lt;/td&gt;
&lt;td align='right'&gt;10.3 &lt;/td&gt;
&lt;td align='right'&gt;12.3 &amp;nbsp;&lt;/td&gt;

&lt;td align='right'&gt; 5.4&lt;/td&gt;
&lt;td align='right'&gt; 7.0&lt;/td&gt;
&lt;td align='right'&gt; 11.9 &amp;nbsp;&lt;/td&gt;
&lt;td&gt;&amp;nbsp; &lt;/td&gt;
&lt;td align='right'&gt;  1.32  &lt;/td&gt;
&lt;td&gt;&amp;nbsp; &lt;/td&gt;
&lt;td align='right'&gt; 31.1&lt;/td&gt;
&lt;td align='right'&gt; 31.4&lt;/td&gt;
&lt;td align='right'&gt; 71.1 &amp;nbsp;&lt;/td&gt;

&lt;td align='right'&gt; 18.0&lt;/td&gt;
&lt;td align='right'&gt; 18.4&lt;/td&gt;
&lt;td align='right'&gt; 51.4 &amp;nbsp;&lt;/td&gt;
&lt;td&gt;&amp;nbsp; &lt;/td&gt;
&lt;td align='right'&gt;  1.64  &lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
&lt;td&gt;Spectrogram&lt;/td&gt;
&lt;td&gt;&amp;nbsp; &lt;/td&gt;
&lt;td align='right'&gt; 80&lt;/td&gt;
&lt;td align='right'&gt; 89&lt;/td&gt;
&lt;td align='right'&gt; 95 &amp;nbsp;&lt;/td&gt;

&lt;td align='right'&gt; 60&lt;/td&gt;
&lt;td align='right'&gt; 67&lt;/td&gt;
&lt;td align='right'&gt; 70 &amp;nbsp;&lt;/td&gt;
&lt;td&gt;&amp;nbsp; &lt;/td&gt;
&lt;td align='right'&gt;  1.30 &lt;/td&gt;
&lt;td&gt;&amp;nbsp; &lt;/td&gt;
&lt;td align='right'&gt; 138&lt;/td&gt;
&lt;td align='right'&gt; 141&lt;/td&gt;
&lt;td align='right'&gt; 188 &amp;nbsp;&lt;/td&gt;

&lt;td align='right'&gt; 78&lt;/td&gt;
&lt;td align='right'&gt; 82&lt;/td&gt;
&lt;td align='right'&gt; 95 &amp;nbsp;&lt;/td&gt;
&lt;td&gt;&amp;nbsp; &lt;/td&gt;
&lt;td align='right'&gt;  1.75  &lt;/td&gt;

&lt;/tr&gt;&lt;tr&gt;
&lt;td&gt;Auditory Spectrogram&lt;/td&gt;
&lt;td&gt;&amp;nbsp; &lt;/td&gt;
&lt;td align='right'&gt; 4463&lt;/td&gt;
&lt;td align='right'&gt; 4670&lt;/td&gt;
&lt;td align='right'&gt; 4763 &amp;nbsp;&lt;/td&gt;

&lt;td align='right'&gt; 2320&lt;/td&gt;
&lt;td align='right'&gt; 2464&lt;/td&gt;
&lt;td align='right'&gt; 2725 &amp;nbsp;&lt;/td&gt;
&lt;td&gt;&amp;nbsp; &lt;/td&gt;
&lt;td align='right'&gt;  1.89 &lt;/td&gt;
&lt;td&gt;&amp;nbsp; &lt;/td&gt;
&lt;td align='right'&gt; 4283&lt;/td&gt;
&lt;td align='right'&gt; 4300&lt;/td&gt;
&lt;td align='right'&gt; 4339 &amp;nbsp;&lt;/td&gt;

&lt;td align='right'&gt; 2246&lt;/td&gt;
&lt;td align='right'&gt; 2322&lt;/td&gt;
&lt;td align='right'&gt; 2647&amp;nbsp;&lt;/td&gt;

&lt;td&gt;&amp;nbsp; &lt;/td&gt;
&lt;td align='right'&gt;  1.84  &lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;
Undusting my  &lt;a href=&quot;http://www.r-project.org/&quot;&gt;R&lt;/a&gt;, I was able to generate a nice box-plot of the results:
&lt;/p&gt;

&lt;img src=&quot;http://blogs.plinge.de/media/blogs/adv/11c-specs-alice-gin-win-lin.png&quot; /&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.plinge.de/dev/2009/10/19/spectral-calculations-with-openmp&quot;&gt;Original post&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>Since it is high time to get into multicore programming and all major compilers support the <a href="http://openmp.org">OpenMP</a> standard, I choose that road for my first endeavor. One particular lengthy operation in some of my projects is that of diverse <a href="http://en.wikipedia.org/wiki/Spectrogram">Spectrograms</a>. Since an <a href="http://en.wikipedia.org/wiki/FFT">FFT</a> is calculated for every column of pixels independently, these can be splitted straightforewardly into threads by the number of virtual processors. A couple of tests  showed that it is indeed most advised to split as much data as possible, i.e. at the highest level avaliable. I hacked together a slightly off use of the for directive to achieve what I wanted.</p>

<pre>
// div width by number of virtual processors, min = 128, max = none
int slice = AP_MP::getSlice(width,128,-1); 
int xStart;
#pragma omp parallel for private(xStart)
for (xStart=0;xStart&lt;width;xStart+=slice) {
  int xEnd = xStart+slice;
  if (xEnd>width) xEnd=width;
  calcSlice(..,xStart,xEnd); 
}
</pre>


<p>
As I hoped, the calculations were indeed 1.3-1.8 times faster on a dual-core machine, which is quite nice for such limited effort.
The following table gives the min/median/max times in ms for a 2.8s audio file sampled with 16bit at 22050Hz.
Spectrum is a pure magnitude spectrum calculation, Spectrogram represents a rendering of a spectrogram image, the Auditory Spectrogram includes a rather lengthy masking calculation. <code>g++ -O3 -ffast-math</code> is up to four times faster for the core routines, but that lessens for larger code segments. The Auditory Spectrogram is slightly faster on Windows.
</p>

<table>
<tr>
<td></td>
<td>&nbsp; </td>
<td colspan='8' align='center'>Linux, g++ -O3 -ffast-math</td>
<td>&nbsp; </td>
<td colspan='8' align='center'>Windows, VS2008</td>
</tr><tr>

<td></td>
<td>&nbsp;&nbsp;&nbsp;</td>
<td colspan='3' align='center'>1 thread</td>
<td colspan='3' align='center'>2 threads</td>
<td>&nbsp; </td>
<td> speedup</td>
<td>&nbsp;&nbsp;&nbsp;</td>
<td colspan='3' align='center'>1 thread</td>
<td colspan='3' align='center'>2 threads</td>
<td>&nbsp; </td>
<td> speedup</td>
</tr><tr>

<td>Spectrum</td>
<td>&nbsp; </td>
<td align='right'>10.1 </td>
<td align='right'>10.3 </td>
<td align='right'>12.3 &nbsp;</td>

<td align='right'> 5.4</td>
<td align='right'> 7.0</td>
<td align='right'> 11.9 &nbsp;</td>
<td>&nbsp; </td>
<td align='right'>  1.32  </td>
<td>&nbsp; </td>
<td align='right'> 31.1</td>
<td align='right'> 31.4</td>
<td align='right'> 71.1 &nbsp;</td>

<td align='right'> 18.0</td>
<td align='right'> 18.4</td>
<td align='right'> 51.4 &nbsp;</td>
<td>&nbsp; </td>
<td align='right'>  1.64  </td>
</tr><tr>
<td>Spectrogram</td>
<td>&nbsp; </td>
<td align='right'> 80</td>
<td align='right'> 89</td>
<td align='right'> 95 &nbsp;</td>

<td align='right'> 60</td>
<td align='right'> 67</td>
<td align='right'> 70 &nbsp;</td>
<td>&nbsp; </td>
<td align='right'>  1.30 </td>
<td>&nbsp; </td>
<td align='right'> 138</td>
<td align='right'> 141</td>
<td align='right'> 188 &nbsp;</td>

<td align='right'> 78</td>
<td align='right'> 82</td>
<td align='right'> 95 &nbsp;</td>
<td>&nbsp; </td>
<td align='right'>  1.75  </td>

</tr><tr>
<td>Auditory Spectrogram</td>
<td>&nbsp; </td>
<td align='right'> 4463</td>
<td align='right'> 4670</td>
<td align='right'> 4763 &nbsp;</td>

<td align='right'> 2320</td>
<td align='right'> 2464</td>
<td align='right'> 2725 &nbsp;</td>
<td>&nbsp; </td>
<td align='right'>  1.89 </td>
<td>&nbsp; </td>
<td align='right'> 4283</td>
<td align='right'> 4300</td>
<td align='right'> 4339 &nbsp;</td>

<td align='right'> 2246</td>
<td align='right'> 2322</td>
<td align='right'> 2647&nbsp;</td>

<td>&nbsp; </td>
<td align='right'>  1.84  </td>
</tr><tr>
</tr>
</table>

<p>
Undusting my  <a href="http://www.r-project.org/">R</a>, I was able to generate a nice box-plot of the results:
</p>

<img src="http://blogs.plinge.de/media/blogs/adv/11c-specs-alice-gin-win-lin.png" /><div class="item_footer"><p><small><a href="http://blogs.plinge.de/dev/2009/10/19/spectral-calculations-with-openmp">Original post</a></small></p></div>]]></content:encoded>
								<comments>http://blogs.plinge.de/dev/2009/10/19/spectral-calculations-with-openmp#comments</comments>
		</item>
				<item>
			<title>MulDiv64</title>
			<link>http://blogs.plinge.de/dev/2009/10/08/muldiv64</link>
			<pubDate>Thu,  8 Oct 2009 16:08:11 +0000</pubDate>			<dc:creator>axel</dc:creator>
			<category domain="alt">programming</category>
<category domain="main">C++</category>			<guid isPermaLink="false">73@http://blogs.plinge.de/</guid>
						<description>&lt;p&gt;Well, that turned out to be effort than expected. At some point, I needed to multiply a 64 and a 32 bit value and divide them by a 64 bit value. Given that I'm using a 64 bit CPU, I assumed there would be an instruction or at least a library function. But no such luck. So I had to take my dusty bitmanipulation skills out of the closet and write it myself. Here goes &lt;/p&gt;

&lt;p&gt;
The calculation is performed by splitting the 64 bit multiplicant in two halves. So far, so obvious.
&lt;/p&gt;
&lt;p class=&quot;formulaDsp&quot;&gt;
&lt;img class=&quot;formulaDsp&quot; alt=&quot;\begin{eqnarray*} result &amp;amp;=&amp;amp; \frac{ number \cdot numerator}{ denominator } \\ &amp;amp;=&amp;amp; \frac{ number_l + number_h \cdot 2^{32} \cdot numerator }{ denominator } \quad=\quad \underbrace{\frac{ number_l \cdot numerator }{ denominator }}_{result_l} + \underbrace{\frac{ number_h \cdot 2^{32} \cdot numerator }{ denominator }}_{result_h} \end{eqnarray*}&quot; src=&quot;http://blogs.plinge.de/media/blogs/adv/muldiv_form_0.png&quot; /&gt;
&lt;/p&gt;
&lt;p&gt;
Where &lt;img class=&quot;formulaInl&quot; alt=&quot;$ result_h $&quot; src=&quot;http://blogs.plinge.de/media/blogs/adv/muldiv_form_1.png&quot; /&gt; is split again into the division canceled by 2^32 and the remainder (modulus).  The modulus part may still be 64 bit, so it may have to be calculated by canceling appropriate powers of two via shifted by &lt;img class=&quot;formulaInl&quot; alt=&quot;$ s $&quot; src=&quot;http://blogs.plinge.de/media/blogs/adv/muldiv_form_2.png&quot; /&gt; according to the MSBs.&lt;/p&gt;
&lt;p class=&quot;formulaDsp&quot;&gt;
&lt;img class=&quot;formulaDsp&quot; alt=&quot;\begin{eqnarray*} result_h &amp;amp;=&amp;amp; \frac{ number_h \cdot 2^{32} \cdot numerator }{ denominator } = \underbrace{\left\lfloor \frac{ number_h \cdot numerator }{ denominator } \right\rfloor}_{div_h} \cdot 2^{32} + \frac{ \overbrace{(number_h \:\mathrm{mod}\: numerator)}^{mod_h} }{ denominator } \cdot 2^{32} \\ &amp;amp;=&amp;amp; {div_h} \cdot 2^{32} + \frac{ {mod_h} \cdot 2^{32-s} }{ denominator \cdot 2^{-s} } \qquad s = \max\{ \mathrm{MSB}(mod_h)+32 , \mathrm{MSB}(denominator) \} - 63 \end{eqnarray*}&quot; src=&quot;http://blogs.plinge.de/media/blogs/adv/muldiv_form_3.png&quot; /&gt;
&lt;/p&gt;
&lt;p&gt;That last part took me a bit longer than anticipated, given that it has in fact been years since I saw bits at all while coding. The MSB determination origniates in the astounding &lt;a href=&quot;http://graphics.stanford.edu/~seander/bithacks.html&quot;&gt; Bit Twiddling Hacks&lt;/a&gt; by Sean Eron Anderson.
So, anyway, enjoy or abhorr the following end result:&lt;/p&gt;

&lt;pre&gt;
/** the two 32 bit parts of an 64 bit integer */
typedef struct  { 
    uint32_t l : 32;
    uint32_t h : 32;
} uint64_uint32;

/**
 * determine the msb of a value in O(log log n)
 * @author Sean Eron Anderson
 */
inline unsigned int msb(uint64_t value)
{
    const int MAX_LOGLOG = 6;
    const uint64_t BIT_LL[MAX_LOGLOG] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000, 0xFFFFFFFF00000000LL};
    const unsigned int EXP_LL[MAX_LOGLOG] = {1, 2, 4, 8, 16, 32};
    unsigned int r = 0; 
    for (int i = MAX_LOGLOG-1; i &gt;= 0; i--)  {
        if (value &amp;amp; BIT_LL[i])  {
            value &gt;&gt;= EXP_LL[i];
            r |= EXP_LL[i];
        } 
    }
    return r;
}

/** 
 * multiply a 64 and a 32 bit value and divide them by a 64 bit value.
 * result bits above 64 are ignored, so overflow flags are not set.
 * @author Axel Plinge
 * @param number       64 bit multiplicant
 * @param numerator    32 bit multiplicant
 * @param denominator  64 bit divisor
 * @return  (number*numerator)/denominator (+/-) 1 
 */
inline uint64_t muldiv(uint64_t number,uint32_t numerator,uint64_t denominator)
{
    uint64_t num_h = ((uint64_uint32*)&amp;amp;number)-&gt;h;
    uint64_t num_l = ((uint64_uint32*)&amp;amp;number)-&gt;l;
    uint64_t mul = numerator;
    uint64_t res;
    // lower 32bit portions yield 64 bit product
    // that can be divded directly giving 64 bits of result
    res = (num_l * mul)/denominator;
    // upper 32bit have to be shifted, calculate modulus 2^32
    uint64_t product_h = num_h*mul;
    uint64_t div_h = product_h/denominator; // division main
    uint64_t mod_h = product_h - denominator*div_h; // modulus
    // upper bits
    res += div_h&lt;&amp;lt;32;
    if (mod_h==0)  {
	return res;
    }
    // remainder of division
    // if msb modulus &lt; 32 we can be quick about it	
    if ((mod_h&gt;&gt;32)==0) {
        res += (mod_h&lt;&amp;lt;32)/denominator;
        return res;
    }
    // if we reach this point we have full 64 bit values i.e. a 96bit dividend
    // calculate an approximate result by shifting according to msb set
    int msb_nominator = msb(mod_h)+32;
    int msb_denominator = msb(denominator);
    int msb = std::max&lt;int&gt;(msb_nominator,msb_denominator);
    int shift = msb-63;
    res += (mod_h &lt;&lt; (32-shift)) / (denominator&gt;&gt;shift);
    return res;
}
&lt;/int&gt;&lt;/pre&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.plinge.de/dev/2009/10/08/muldiv64&quot;&gt;Original post&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>Well, that turned out to be effort than expected. At some point, I needed to multiply a 64 and a 32 bit value and divide them by a 64 bit value. Given that I'm using a 64 bit CPU, I assumed there would be an instruction or at least a library function. But no such luck. So I had to take my dusty bitmanipulation skills out of the closet and write it myself. Here goes </p>

<p>
The calculation is performed by splitting the 64 bit multiplicant in two halves. So far, so obvious.
</p>
<p class="formulaDsp">
<img class="formulaDsp" alt="\begin{eqnarray*} result &amp;=&amp; \frac{ number \cdot numerator}{ denominator } \\ &amp;=&amp; \frac{ number_l + number_h \cdot 2^{32} \cdot numerator }{ denominator } \quad=\quad \underbrace{\frac{ number_l \cdot numerator }{ denominator }}_{result_l} + \underbrace{\frac{ number_h \cdot 2^{32} \cdot numerator }{ denominator }}_{result_h} \end{eqnarray*}" src="http://blogs.plinge.de/media/blogs/adv/muldiv_form_0.png" />
</p>
<p>
Where <img class="formulaInl" alt="$ result_h $" src="http://blogs.plinge.de/media/blogs/adv/muldiv_form_1.png" /> is split again into the division canceled by 2^32 and the remainder (modulus).  The modulus part may still be 64 bit, so it may have to be calculated by canceling appropriate powers of two via shifted by <img class="formulaInl" alt="$ s $" src="http://blogs.plinge.de/media/blogs/adv/muldiv_form_2.png" /> according to the MSBs.</p>
<p class="formulaDsp">
<img class="formulaDsp" alt="\begin{eqnarray*} result_h &amp;=&amp; \frac{ number_h \cdot 2^{32} \cdot numerator }{ denominator } = \underbrace{\left\lfloor \frac{ number_h \cdot numerator }{ denominator } \right\rfloor}_{div_h} \cdot 2^{32} + \frac{ \overbrace{(number_h \:\mathrm{mod}\: numerator)}^{mod_h} }{ denominator } \cdot 2^{32} \\ &amp;=&amp; {div_h} \cdot 2^{32} + \frac{ {mod_h} \cdot 2^{32-s} }{ denominator \cdot 2^{-s} } \qquad s = \max\{ \mathrm{MSB}(mod_h)+32 , \mathrm{MSB}(denominator) \} - 63 \end{eqnarray*}" src="http://blogs.plinge.de/media/blogs/adv/muldiv_form_3.png" />
</p>
<p>That last part took me a bit longer than anticipated, given that it has in fact been years since I saw bits at all while coding. The MSB determination origniates in the astounding <a href="http://graphics.stanford.edu/~seander/bithacks.html"> Bit Twiddling Hacks</a> by Sean Eron Anderson.
So, anyway, enjoy or abhorr the following end result:</p>

<pre>
/** the two 32 bit parts of an 64 bit integer */
typedef struct  { 
    uint32_t l : 32;
    uint32_t h : 32;
} uint64_uint32;

/**
 * determine the msb of a value in O(log log n)
 * @author Sean Eron Anderson
 */
inline unsigned int msb(uint64_t value)
{
    const int MAX_LOGLOG = 6;
    const uint64_t BIT_LL[MAX_LOGLOG] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000, 0xFFFFFFFF00000000LL};
    const unsigned int EXP_LL[MAX_LOGLOG] = {1, 2, 4, 8, 16, 32};
    unsigned int r = 0; 
    for (int i = MAX_LOGLOG-1; i >= 0; i--)  {
        if (value &amp; BIT_LL[i])  {
            value >>= EXP_LL[i];
            r |= EXP_LL[i];
        } 
    }
    return r;
}

/** 
 * multiply a 64 and a 32 bit value and divide them by a 64 bit value.
 * result bits above 64 are ignored, so overflow flags are not set.
 * @author Axel Plinge
 * @param number       64 bit multiplicant
 * @param numerator    32 bit multiplicant
 * @param denominator  64 bit divisor
 * @return  (number*numerator)/denominator (+/-) 1 
 */
inline uint64_t muldiv(uint64_t number,uint32_t numerator,uint64_t denominator)
{
    uint64_t num_h = ((uint64_uint32*)&amp;number)->h;
    uint64_t num_l = ((uint64_uint32*)&amp;number)->l;
    uint64_t mul = numerator;
    uint64_t res;
    // lower 32bit portions yield 64 bit product
    // that can be divded directly giving 64 bits of result
    res = (num_l * mul)/denominator;
    // upper 32bit have to be shifted, calculate modulus 2^32
    uint64_t product_h = num_h*mul;
    uint64_t div_h = product_h/denominator; // division main
    uint64_t mod_h = product_h - denominator*div_h; // modulus
    // upper bits
    res += div_h<&lt;32;
    if (mod_h==0)  {
	return res;
    }
    // remainder of division
    // if msb modulus < 32 we can be quick about it	
    if ((mod_h>>32)==0) {
        res += (mod_h<&lt;32)/denominator;
        return res;
    }
    // if we reach this point we have full 64 bit values i.e. a 96bit dividend
    // calculate an approximate result by shifting according to msb set
    int msb_nominator = msb(mod_h)+32;
    int msb_denominator = msb(denominator);
    int msb = std::max<int>(msb_nominator,msb_denominator);
    int shift = msb-63;
    res += (mod_h << (32-shift)) / (denominator>>shift);
    return res;
}
</int></pre><div class="item_footer"><p><small><a href="http://blogs.plinge.de/dev/2009/10/08/muldiv64">Original post</a></small></p></div>]]></content:encoded>
								<comments>http://blogs.plinge.de/dev/2009/10/08/muldiv64#comments</comments>
		</item>
				<item>
			<title>avoiding virtual overloads with a template replacement</title>
			<link>http://blogs.plinge.de/dev/2009/08/17/virtual-overloads</link>
			<pubDate>Mon, 17 Aug 2009 20:45:39 +0000</pubDate>			<dc:creator>axel</dc:creator>
			<category domain="alt">programming</category>
<category domain="main">C++</category>			<guid isPermaLink="false">71@http://blogs.plinge.de/</guid>
						<description>&lt;p&gt;In most C++ Compilers virtual overloads work nowadays just as you'd expect.
Let us assume two classes &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt; like &lt;/p&gt;
&lt;pre&gt;
class A {
public:
   virtual void foo(int i) {
      cout &amp;lt;&amp;lt; &quot;A::foo(int &quot; &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &quot;)&quot; &amp;lt;&amp;lt; endl;
   }
   virtual void foo(double d) {
      cout &amp;lt;&amp;lt; &quot;A::foo(double &quot; &amp;lt;&amp;lt; d &amp;lt;&amp;lt; &quot;)&quot; &amp;lt;&amp;lt; endl;
   }
};

class B : public A {
public:
   virtual void foo(int i) {
      cout &amp;lt;&amp;lt; &quot;B::foo(int &quot; &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &quot;)&quot; &amp;lt;&amp;lt; endl;
   }
   virtual void foo(double d) {
      cout &amp;lt;&amp;lt; &quot;B::foo(double &quot; &amp;lt;&amp;lt; d &amp;lt;&amp;lt; &quot;)&quot; &amp;lt;&amp;lt; endl;
   }
};
&lt;/pre&gt;

&lt;p&gt;When calling them, the correct one is identified since &lt;code&gt;foo(int)&lt;/code&gt; and &lt;code&gt;foo(double)&lt;/code&gt; get seperate vtable entries. A quick test:&lt;/p&gt;
&lt;pre&gt;
void callFoo(A&amp;amp; a) {
   a.foo(1.5);
   a.foo(3);
}
..
A a;	
callFoo(a);
B b;
callFoo(b);
&lt;/pre&gt;

&lt;p&gt;However, things get confusing very easily. For both programmer and compiler. For example, when calling &lt;code&gt;foo&lt;/code&gt; with an &lt;code&gt;unsigned long&lt;/code&gt;, many compilers simply issue an error forcing you to cast explicitly or add another overload. Which has to be consistent over the whole class hierarchy, as everything you do here. This may not seem too difficult, however, its added risk. When working with some code I last touched 10 years ago, I stumbled more than once. So, to avoid confusion, one of the two has to go. Let's keep inheritance since it's most vital:&lt;/p&gt;
&lt;pre&gt;
class C  {
public:
   virtual void fooInt(int i) {
      cout &amp;lt;&amp;lt; &quot;C::fooInt(&quot; &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &quot;)&quot; &amp;lt;&amp;lt; endl;
   }
   virtual void fooDouble(double d) {
      cout &amp;lt;&amp;lt; &quot;C::fooDouble(&quot; &amp;lt;&amp;lt; d &amp;lt;&amp;lt; &quot;)&quot; &amp;lt;&amp;lt; endl;
   }
&lt;/pre&gt;
&lt;p&gt;Now &lt;b&gt;you&lt;/b&gt; as programmer have to choose the variant, not the compiler. A call to &lt;code&gt;fooInt&lt;/code&gt; will cast any argument to &lt;code&gt;(int)&lt;/code&gt; and that's that.&lt;/p&gt;

&lt;p&gt;So, what if you REALLY need the compiler to choose, e.g. when using &lt;code&gt;C&lt;/code&gt; in a template? The solution is a type-switch with a template method as proposed in the &lt;a href=&quot;http://www.parashift.com/c++-faq-lite/templates.html#faq-35.7&quot;&gt;CppFaqLite&lt;/a&gt;.
This is not especially beautiful, but leaves the above separation intact.&lt;/p&gt;
&lt;pre&gt;
   template &amp;lt;typename type&amp;gt;
   inline void fooTemplated(type arg);

   template &amp;lt;&amp;gt;
   inline void fooTemplated(int arg) {
      cout &amp;lt;&amp;lt; &quot;templated &quot;;
      fooInt(arg);
   }

   template &amp;lt;&amp;gt;
   inline void fooTemplated(double arg) {
      cout &amp;lt;&amp;lt; &quot;templated &quot;;
      fooDouble(arg);
   }

};

template &amp;lt;typename type&amp;gt;
inline void C::fooTemplated(type arg) {
   throw runtime_error(&quot;fooTemplated called with unforseen type&quot;);
}
&lt;/pre&gt;

&lt;p&gt;Suffice to say that the above type-switch has to be written &lt;code&gt;inline&lt;/code&gt; as shown above to avoid visibility issues when compiling.&lt;/p&gt;

&lt;p&gt;I may also remark that I wrote C++ happily for 12 year before introducing this construct. ;-) Nevertheless, remember to beware of clever code. It may come back to haunt you..&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.plinge.de/dev/2009/08/17/virtual-overloads&quot;&gt;Original post&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>In most C++ Compilers virtual overloads work nowadays just as you'd expect.
Let us assume two classes <code>A</code> and <code>B</code> like </p>
<pre>
class A {
public:
   virtual void foo(int i) {
      cout &lt;&lt; "A::foo(int " &lt;&lt; i &lt;&lt; ")" &lt;&lt; endl;
   }
   virtual void foo(double d) {
      cout &lt;&lt; "A::foo(double " &lt;&lt; d &lt;&lt; ")" &lt;&lt; endl;
   }
};

class B : public A {
public:
   virtual void foo(int i) {
      cout &lt;&lt; "B::foo(int " &lt;&lt; i &lt;&lt; ")" &lt;&lt; endl;
   }
   virtual void foo(double d) {
      cout &lt;&lt; "B::foo(double " &lt;&lt; d &lt;&lt; ")" &lt;&lt; endl;
   }
};
</pre>

<p>When calling them, the correct one is identified since <code>foo(int)</code> and <code>foo(double)</code> get seperate vtable entries. A quick test:</p>
<pre>
void callFoo(A&amp; a) {
   a.foo(1.5);
   a.foo(3);
}
..
A a;	
callFoo(a);
B b;
callFoo(b);
</pre>

<p>However, things get confusing very easily. For both programmer and compiler. For example, when calling <code>foo</code> with an <code>unsigned long</code>, many compilers simply issue an error forcing you to cast explicitly or add another overload. Which has to be consistent over the whole class hierarchy, as everything you do here. This may not seem too difficult, however, its added risk. When working with some code I last touched 10 years ago, I stumbled more than once. So, to avoid confusion, one of the two has to go. Let's keep inheritance since it's most vital:</p>
<pre>
class C  {
public:
   virtual void fooInt(int i) {
      cout &lt;&lt; "C::fooInt(" &lt;&lt; i &lt;&lt; ")" &lt;&lt; endl;
   }
   virtual void fooDouble(double d) {
      cout &lt;&lt; "C::fooDouble(" &lt;&lt; d &lt;&lt; ")" &lt;&lt; endl;
   }
</pre>
<p>Now <b>you</b> as programmer have to choose the variant, not the compiler. A call to <code>fooInt</code> will cast any argument to <code>(int)</code> and that's that.</p>

<p>So, what if you REALLY need the compiler to choose, e.g. when using <code>C</code> in a template? The solution is a type-switch with a template method as proposed in the <a href="http://www.parashift.com/c++-faq-lite/templates.html#faq-35.7">CppFaqLite</a>.
This is not especially beautiful, but leaves the above separation intact.</p>
<pre>
   template &lt;typename type&gt;
   inline void fooTemplated(type arg);

   template &lt;&gt;
   inline void fooTemplated(int arg) {
      cout &lt;&lt; "templated ";
      fooInt(arg);
   }

   template &lt;&gt;
   inline void fooTemplated(double arg) {
      cout &lt;&lt; "templated ";
      fooDouble(arg);
   }

};

template &lt;typename type&gt;
inline void C::fooTemplated(type arg) {
   throw runtime_error("fooTemplated called with unforseen type");
}
</pre>

<p>Suffice to say that the above type-switch has to be written <code>inline</code> as shown above to avoid visibility issues when compiling.</p>

<p>I may also remark that I wrote C++ happily for 12 year before introducing this construct. ;-) Nevertheless, remember to beware of clever code. It may come back to haunt you..</p><div class="item_footer"><p><small><a href="http://blogs.plinge.de/dev/2009/08/17/virtual-overloads">Original post</a></small></p></div>]]></content:encoded>
								<comments>http://blogs.plinge.de/dev/2009/08/17/virtual-overloads#comments</comments>
		</item>
				<item>
			<title>Deutscher bibtex Stil mit konsistener Autorennennung</title>
			<link>http://blogs.plinge.de/dev/2009/05/20/deutscher-bibtex-stil-mit-konsistener-au</link>
			<pubDate>Wed, 20 May 2009 01:39:21 +0000</pubDate>			<dc:creator>axel</dc:creator>
			<category domain="alt">programming</category>
<category domain="alt">science</category>
<category domain="main">TeX</category>			<guid isPermaLink="false">65@http://blogs.plinge.de/</guid>
						<description>&lt;p&gt;Nachdem mir die Eigenheit vom &lt;code&gt;gerplain.bst&lt;/code&gt; nicht gefiel, die Reihenfolge der Vornamen und Namen in einem Literarturverweis umzudrehen, etwa so:&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&amp;nbsp;&lt;b&gt;[5] Fred, H., Z. Sp&amp;#228;t und H. M&amp;#252;ller: &lt;em&gt;Hobeln f&amp;#252;r Fortgeschrittene&lt;/em&gt;, 1. Auflage, Spanverlag, 2009&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Habe ich mich nach Alternativen umgesehen. Da ich nicht gleich auf die  &lt;a href=&quot;http://www.tug.org/texlive/Contents/live/texmf-dist/doc/bibtex/dinat/dinat-index.html&quot;&gt;volle DIN1505 Variante&lt;/a&gt; wechseln wollte, habe ich in einer Runde Mittern&amp;#228;chlichem Syntaxraten den Stil zu einem f&amp;#252;r mich akzeptabeln Kompromiss angepasst. Es ware mit der &amp;#196;nderung von nur zwei Zeilen in &lt;code&gt;format.names&lt;/code&gt; getan:&lt;/p&gt;
&lt;pre&gt;
257c256
&lt;         { s nameptr &quot;{ll}{, jj}{, ff}{~vv}&quot; format.name$ 't :=
---
&gt;         { s nameptr &quot;{ff~}{vv~}{ll}{, jj}&quot; format.name$ 't :=
259c258
&lt;             { &quot;; &quot; * t smallcaps * }
---
&gt;             { &quot;, &quot; * t smallcaps * }
&lt;/pre&gt;
&lt;p&gt;Hier die ge&amp;#228;nderte Datei: &lt;a href=&quot;http://axel.plinge.de/research/misc/gerplain2.bst&quot;&gt;gerplain2.bst (Name, V.; Name, V. und Name, V.)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Somit habe ich jetzt Verweise der etwas kanonischeren Form&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&amp;nbsp;&lt;b&gt;[5] Fred, H.; Sp&amp;#228;t, Z. und M&amp;#252;ller, H.: &lt;em&gt;Hobeln f&amp;#252;r Fortgeschrittene&lt;/em&gt;, 1. Auflage, Spanverlag, 2009&lt;/b&gt;&lt;/p&gt;



&lt;p&gt;Alternativ kann man auch grunds&amp;#228;tlich mit den Initalen beginnen, hier ist nur eine Zeile zu ersetzen.&lt;/p&gt;
&lt;pre&gt;
277c276
&lt;         { s nameptr &quot;{ff~}{vv~}{ll}{, jj}&quot; format.name$ smallcaps }
---
&gt;         { s nameptr &quot;{ll}{, jj}{, ff}{~vv}&quot; format.name$ smallcaps }
&lt;/pre&gt;
&lt;p&gt;Hier die fertige Datei: &lt;a href=&quot;http://axel.plinge.de/research/misc/gerplain3.bst&quot;&gt;gerplain3.bst (V. Name, V. Name und V. Name)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Diese Variante liesst sich etwas angenehmer, beg&amp;#252;nstigt aber weniger das vertikale Scannen der Nachnahmen:&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&amp;nbsp;&lt;b&gt;[5] H. Fred, Z. Sp&amp;#228;t und H. M&amp;#252;ller: &lt;em&gt;Hobeln f&amp;#252;r Fortgeschrittene&lt;/em&gt;, 1. Auflage, Spanverlag, 2009&lt;/b&gt;&lt;/p&gt;


&lt;p&gt;Ist nat&amp;#252;rlich alles Geschmacksache, aber ich hoffe erstmal das ich sobald nicht wieder eine bst Datei lesen mu&amp;#223;.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.plinge.de/dev/2009/05/20/deutscher-bibtex-stil-mit-konsistener-au&quot;&gt;Original post&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>Nachdem mir die Eigenheit vom <code>gerplain.bst</code> nicht gefiel, die Reihenfolge der Vornamen und Namen in einem Literarturverweis umzudrehen, etwa so:</p>

<p>&nbsp;&nbsp;<b>[5] Fred, H., Z. Sp&#228;t und H. M&#252;ller: <em>Hobeln f&#252;r Fortgeschrittene</em>, 1. Auflage, Spanverlag, 2009</b></p>

<p>Habe ich mich nach Alternativen umgesehen. Da ich nicht gleich auf die  <a href="http://www.tug.org/texlive/Contents/live/texmf-dist/doc/bibtex/dinat/dinat-index.html">volle DIN1505 Variante</a> wechseln wollte, habe ich in einer Runde Mittern&#228;chlichem Syntaxraten den Stil zu einem f&#252;r mich akzeptabeln Kompromiss angepasst. Es ware mit der &#196;nderung von nur zwei Zeilen in <code>format.names</code> getan:</p>
<pre>
257c256
<         { s nameptr "{ll}{, jj}{, ff}{~vv}" format.name$ 't :=
---
>         { s nameptr "{ff~}{vv~}{ll}{, jj}" format.name$ 't :=
259c258
<             { "; " * t smallcaps * }
---
>             { ", " * t smallcaps * }
</pre>
<p>Hier die ge&#228;nderte Datei: <a href="http://axel.plinge.de/research/misc/gerplain2.bst">gerplain2.bst (Name, V.; Name, V. und Name, V.)</a></p>

<p>Somit habe ich jetzt Verweise der etwas kanonischeren Form</p>

<p>&nbsp;&nbsp;<b>[5] Fred, H.; Sp&#228;t, Z. und M&#252;ller, H.: <em>Hobeln f&#252;r Fortgeschrittene</em>, 1. Auflage, Spanverlag, 2009</b></p>



<p>Alternativ kann man auch grunds&#228;tlich mit den Initalen beginnen, hier ist nur eine Zeile zu ersetzen.</p>
<pre>
277c276
<         { s nameptr "{ff~}{vv~}{ll}{, jj}" format.name$ smallcaps }
---
>         { s nameptr "{ll}{, jj}{, ff}{~vv}" format.name$ smallcaps }
</pre>
<p>Hier die fertige Datei: <a href="http://axel.plinge.de/research/misc/gerplain3.bst">gerplain3.bst (V. Name, V. Name und V. Name)</a></p>

<p>Diese Variante liesst sich etwas angenehmer, beg&#252;nstigt aber weniger das vertikale Scannen der Nachnahmen:</p>

<p>&nbsp;&nbsp;<b>[5] H. Fred, Z. Sp&#228;t und H. M&#252;ller: <em>Hobeln f&#252;r Fortgeschrittene</em>, 1. Auflage, Spanverlag, 2009</b></p>


<p>Ist nat&#252;rlich alles Geschmacksache, aber ich hoffe erstmal das ich sobald nicht wieder eine bst Datei lesen mu&#223;.</p><div class="item_footer"><p><small><a href="http://blogs.plinge.de/dev/2009/05/20/deutscher-bibtex-stil-mit-konsistener-au">Original post</a></small></p></div>]]></content:encoded>
								<comments>http://blogs.plinge.de/dev/2009/05/20/deutscher-bibtex-stil-mit-konsistener-au#comments</comments>
		</item>
				<item>
			<title>refbase</title>
			<link>http://blogs.plinge.de/dev/2009/05/10/refbase</link>
			<pubDate>Sun, 10 May 2009 21:22:19 +0000</pubDate>			<dc:creator>axel</dc:creator>
			<category domain="alt">science</category>
<category domain="main">TeX</category>			<guid isPermaLink="false">64@http://blogs.plinge.de/</guid>
						<description>&lt;p&gt;After years of collecting papers in more or less orderly folder hirarchies if finnally accepeted the fact that single hirarchies are not the reasonable way to collect literature, is searched for alternatives. After reviewing a number of projects I choose to give &lt;a href=&quot;http://www.refbase.net/&quot;&gt;refbase&lt;/a&gt; a try. &lt;/p&gt;

&lt;p&gt;So, not without some complications, I set up my very own &lt;a href=&quot;http://axel.plinge.de/refbase/&quot;&gt;refbase installation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It basically unties the usage of bibtex with web storage of the PDF files and a Database backend. While the latter could be more advanced, the possibilty to use and store plain SQL Queries makes up for a lot. I may not resist to contribute to the code, though.&lt;/p&gt;

&lt;p&gt;As for now, I'll give it a try as it is, and see how it feels.&lt;/p&gt;

&lt;p&gt;Cheers&lt;/p&gt;

&lt;p&gt;Axel&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.plinge.de/dev/2009/05/10/refbase&quot;&gt;Original post&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>After years of collecting papers in more or less orderly folder hirarchies if finnally accepeted the fact that single hirarchies are not the reasonable way to collect literature, is searched for alternatives. After reviewing a number of projects I choose to give <a href="http://www.refbase.net/">refbase</a> a try. </p>

<p>So, not without some complications, I set up my very own <a href="http://axel.plinge.de/refbase/">refbase installation</a>.</p>

<p>It basically unties the usage of bibtex with web storage of the PDF files and a Database backend. While the latter could be more advanced, the possibilty to use and store plain SQL Queries makes up for a lot. I may not resist to contribute to the code, though.</p>

<p>As for now, I'll give it a try as it is, and see how it feels.</p>

<p>Cheers</p>

<p>Axel</p><div class="item_footer"><p><small><a href="http://blogs.plinge.de/dev/2009/05/10/refbase">Original post</a></small></p></div>]]></content:encoded>
								<comments>http://blogs.plinge.de/dev/2009/05/10/refbase#comments</comments>
		</item>
			</channel>
</rss>
