<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>calling convention | Learn C Games Programming Blog</title>
	<atom:link href="https://learncgames.com/tag/calling-convention/feed/" rel="self" type="application/rss+xml" />
	<link>https://learncgames.com</link>
	<description>A blog about C, programming games and my ebook(s).</description>
	<lastBuildDate>Tue, 02 Feb 2021 20:18:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://learncgames.com/wp-content/uploads/2020/03/cropped-favicon-32x32.png</url>
	<title>calling convention | Learn C Games Programming Blog</title>
	<link>https://learncgames.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">181446779</site>	<item>
		<title>How much faster is C code compiled with fastcall?</title>
		<link>https://learncgames.com/how-much-faster-is-c-code-compiled-with-fastcall/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-much-faster-is-c-code-compiled-with-fastcall</link>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Wed, 03 Feb 2021 00:00:23 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Techniques]]></category>
		<category><![CDATA[calling convention]]></category>
		<category><![CDATA[fastcall]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=2596</guid>

					<description><![CDATA[<p>Something I read about the other day was the __fastcall convention. In Visual Studio you enable this with the /Gr flag and in gcc (it&#8217;s __attribute__((fastcall)). For clang it&#8217;s fastcall but see this. So what does fastcall do? It changes the calling convention, so instead of pushing parameters to a function on the stack, it [&#8230;]</p>
The post <a href="https://learncgames.com/how-much-faster-is-c-code-compiled-with-fastcall/">How much faster is C code compiled with fastcall?</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></description>
										<content:encoded><![CDATA[<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:207,&quot;href&quot;:&quot;https:\/\/clang.llvm.org\/docs\/AttributeReference.html#fastcall&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20260205105620\/https:\/\/clang.llvm.org\/docs\/AttributeReference.html&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 12:45:18&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-09 18:38:28&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-18 10:08:49&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-02 09:21:37&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-14 01:30:52&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-22 19:21:32&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-28 01:27:21&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-31 22:35:39&quot;,&quot;http_code&quot;:200}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-03-31 22:35:39&quot;,&quot;http_code&quot;:200},&quot;process&quot;:&quot;done&quot;}]'></div>
<p><img fetchpriority="high" decoding="async" class="alignleft wp-image-2597" src="https://learncgames.com/wp-content/uploads/2021/02/fastcall.png" alt="Setting fastcall in VS 2019" width="588" height="406" srcset="https://learncgames.com/wp-content/uploads/2021/02/fastcall.png 786w, https://learncgames.com/wp-content/uploads/2021/02/fastcall-300x207.png 300w, https://learncgames.com/wp-content/uploads/2021/02/fastcall-768x531.png 768w" sizes="(max-width: 588px) 100vw, 588px" />Something I read about the other day was the __fastcall convention. In Visual Studio you enable this with the /Gr flag and in gcc (it&#8217;s __attribute__((fastcall)). For clang it&#8217;s fastcall but <a title="Liok to clang docs for fastcall" href="https://clang.llvm.org/docs/AttributeReference.html#fastcall" target="_blank" rel="nofollow noopener">see this</a>.</p>
<p>So what does <strong>fastcall</strong> do? It changes the calling convention, so instead of pushing parameters to a function on the stack, it passes them in the registers starting with ECX then EDX and so on.  Let&#8217;s look at an example.</p>
<pre><code class="language-c" data-line="">#include &lt;stdio.h&gt;

int add(int a, int b, int c) {
	return a + b * 2 + c * 3;
}

int main() {
	printf(&quot;Add(4,5,6)=%d\n&quot;, add(4, 5, 6));
}</code></pre>
<p>This is the disassembly code from VS 2019. I pressed F10 to start debugging then Debug =&gt; Windows =&gt; Disassembly to get the listing. Note thei is x86, ie 32-bit.</p>
<pre>005B18DC  lea         edi,[ebp-0C0h]  
005B18E2  mov         ecx,30h  
005B18E7  mov         eax,0CCCCCCCCh  
005B18EC  rep stos    dword ptr es:[edi]  
005B18EE  mov         ecx,offset _9831A1D6_test@c (05BC003h)  
005B18F3  call        @__CheckForDebuggerJustMyCode@4 (05B131Bh)  
	printf("Add(4,5,6)=%d\n", add(4, 5, 6));
<strong>005B18F8  push        6  
005B18FA  push        5  
005B18FC  push        4 </strong> 
005B18FE  call        _add (05B1023h)  
005B1903  add         esp,0Ch  
005B1906  push        eax  
005B1907  push        offset string "Add(4,5,6)=%d\n" (05B7B30h)  
005B190C  call        _printf (05B10D2h)  
</pre>
<p>Now if I build it after setting the /Gr flag. In Vs 2019, on the project property pages, click advanced then the Calling Convention and switch from cdecl (/Gd) to &#8211;fastcall (/Gr).</p>
<pre>008118EC  lea         edi,[ebp-0C0h]  
008118F2  mov         ecx,30h  
008118F7  mov         eax,0CCCCCCCCh  
008118FC  rep stos    dword ptr es:[edi]  
008118FE  mov         ecx,offset _9831A1D6_test@c (081C003h)  
00811903  call        @__CheckForDebuggerJustMyCode@4 (081131Bh)  
	printf("Add(4,5,6)=%d\n", add(4, 5, 6));
<strong>00811908  push        6  
0081190A  mov         edx,5  
0081190F  mov         ecx,4  </strong>
00811914  call        @add@12 (0811276h)  
00811919  push        eax  
0081191A  push        offset string "Add(4,5,6)=%d\n" (0817B30h)  
0081191F  call        _printf (08110CDh) 
</pre>
<p>I&#8217;ve highlighted the differences in bold. However the function Add is also different as the fastcall version doesn&#8217;t have to pop parameters off the stack.</p>
<p>Note, to get this to compile I had to prefix main with <strong>__cdecl</strong>. Using /Gr means that every function in the program uses registers and that&#8217;s not allowed with main as it&#8217;s called from Windows end must use the cdecl (default stack passing) convention.</p>
<p>This is what main looks like now.</p>
<pre><code class="language-c" data-line="">int __cdecl main() {</code></pre>
<h3>Notes</h3>
<p>This is only for 32-bit. 64-bit code is done somewhat differently so possibly wouldn&#8217;t be that different. Next I have to write a program that does lots of function calls and use high precision timing to see how much of a difference it makes. <strong>To be continued.</strong></p>The post <a href="https://learncgames.com/how-much-faster-is-c-code-compiled-with-fastcall/">How much faster is C code compiled with fastcall?</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2596</post-id>	</item>
	</channel>
</rss>
