<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Efficient C Tips #6 &#8211; Don&#8217;t use the ternary operator</title>
	<atom:link href="http://embeddedgurus.com/stack-overflow/2009/02/efficient-c-tips-6-dont-use-the-ternary-operator/feed/" rel="self" type="application/rss+xml" />
	<link>http://embeddedgurus.com/stack-overflow/2009/02/efficient-c-tips-6-dont-use-the-ternary-operator/</link>
	<description>Thoughts on embedded systems by Nigel Jones</description>
	<lastBuildDate>Thu, 09 Feb 2012 07:32:39 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Nigel Jones</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/02/efficient-c-tips-6-dont-use-the-ternary-operator/comment-page-1/#comment-13212</link>
		<dc:creator>Nigel Jones</dc:creator>
		<pubDate>Wed, 18 Jan 2012 00:26:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/02/18/efficient-c-tips-6-dont-use-the-ternary-operator/#comment-13212</guid>
		<description>Yes. I have come across plenty of compilers that perform as gcc has done here. However I have also come across a number of compilers, particularly for low end embedded systems where this is *not* the case.</description>
		<content:encoded><![CDATA[<p>Yes. I have come across plenty of compilers that perform as gcc has done here. However I have also come across a number of compilers, particularly for low end embedded systems where this is *not* the case.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jörg Seebohn</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/02/efficient-c-tips-6-dont-use-the-ternary-operator/comment-page-1/#comment-13197</link>
		<dc:creator>Jörg Seebohn</dc:creator>
		<pubDate>Tue, 17 Jan 2012 13:49:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/02/18/efficient-c-tips-6-dont-use-the-ternary-operator/#comment-13197</guid>
		<description>I&#039;ve disassembled the generated object module from the following C-module constisting only of these
two functions. The compiler is gcc 4.5.2 on a x86 processor.

int f1(int a, int b, int c, int d)
{
   int y ;
   y = (a &gt; b) ? c : d ;
   return y ;
}

int f2(int a, int b, int c, int d) 
{
   int y ;
   if (a &gt; b)  {  y = c ;  } else {  y = d ;  }
   return y ;
}

The generated assembler code is exactly the same for both functions.
Even better cause of the CMOVcc instruction of the x86 processor the
generated code is branchless.

Disassembly of section .text:

00000000 :
   0:		55                   	push   %ebp
   1:		89 e5                	mov    %esp,%ebp
   3:		8b 45 0c             	mov    0xc(%ebp),%eax
   6:		39 45 08             	cmp    %eax,0x8(%ebp)
   9:		8b 45 14             	mov    0x14(%ebp),%eax
   c:		0f 4f 45 10          cmovg  0x10(%ebp),%eax
  10: 	5d                   	pop    %ebp
  11:	 	c3                   		ret    

00000012 :
  12:	55                   	push   %ebp
  13:	89 e5                	mov    %esp,%ebp
  15:	8b 45 0c             	mov    0xc(%ebp),%eax
  18:	39 45 08             	cmp    %eax,0x8(%ebp)
  1b:	8b 45 14             	mov    0x14(%ebp),%eax
  1e:		0f 4f 45 10          cmovg  0x10(%ebp),%eax
  22:	5d                   	pop    %ebp
  23:	c3                   		ret</description>
		<content:encoded><![CDATA[<p>I&#8217;ve disassembled the generated object module from the following C-module constisting only of these<br />
two functions. The compiler is gcc 4.5.2 on a x86 processor.</p>
<p>int f1(int a, int b, int c, int d)<br />
{<br />
   int y ;<br />
   y = (a &gt; b) ? c : d ;<br />
   return y ;<br />
}</p>
<p>int f2(int a, int b, int c, int d)<br />
{<br />
   int y ;<br />
   if (a &gt; b)  {  y = c ;  } else {  y = d ;  }<br />
   return y ;<br />
}</p>
<p>The generated assembler code is exactly the same for both functions.<br />
Even better cause of the CMOVcc instruction of the x86 processor the<br />
generated code is branchless.</p>
<p>Disassembly of section .text:</p>
<p>00000000 :<br />
   0:		55                   	push   %ebp<br />
   1:		89 e5                	mov    %esp,%ebp<br />
   3:		8b 45 0c             	mov    0xc(%ebp),%eax<br />
   6:		39 45 08             	cmp    %eax,0&#215;8(%ebp)<br />
   9:		8b 45 14             	mov    0&#215;14(%ebp),%eax<br />
   c:		0f 4f 45 10          cmovg  0&#215;10(%ebp),%eax<br />
  10: 	5d                   	pop    %ebp<br />
  11:	 	c3                   		ret    </p>
<p>00000012 :<br />
  12:	55                   	push   %ebp<br />
  13:	89 e5                	mov    %esp,%ebp<br />
  15:	8b 45 0c             	mov    0xc(%ebp),%eax<br />
  18:	39 45 08             	cmp    %eax,0&#215;8(%ebp)<br />
  1b:	8b 45 14             	mov    0&#215;14(%ebp),%eax<br />
  1e:		0f 4f 45 10          cmovg  0&#215;10(%ebp),%eax<br />
  22:	5d                   	pop    %ebp<br />
  23:	c3                   		ret</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jill</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/02/efficient-c-tips-6-dont-use-the-ternary-operator/comment-page-1/#comment-13179</link>
		<dc:creator>Jill</dc:creator>
		<pubDate>Tue, 17 Jan 2012 03:19:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/02/18/efficient-c-tips-6-dont-use-the-ternary-operator/#comment-13179</guid>
		<description>&quot;I find this frustrating, as I’ve consumed 8 lines doing what is more easily and elegantly performed in 1 line.&quot;

You mean you&#039;ve expressed more clearly in 4 lines what you could have obfuscated in 1 line.</description>
		<content:encoded><![CDATA[<p>&#8220;I find this frustrating, as I’ve consumed 8 lines doing what is more easily and elegantly performed in 1 line.&#8221;</p>
<p>You mean you&#8217;ve expressed more clearly in 4 lines what you could have obfuscated in 1 line.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nigel Jones</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/02/efficient-c-tips-6-dont-use-the-ternary-operator/comment-page-1/#comment-2866</link>
		<dc:creator>Nigel Jones</dc:creator>
		<pubDate>Fri, 03 Dec 2010 13:24:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/02/18/efficient-c-tips-6-dont-use-the-ternary-operator/#comment-2866</guid>
		<description>I will see if I can dig up some examples.</description>
		<content:encoded><![CDATA[<p>I will see if I can dig up some examples.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/02/efficient-c-tips-6-dont-use-the-ternary-operator/comment-page-1/#comment-2847</link>
		<dc:creator>Peter</dc:creator>
		<pubDate>Fri, 03 Dec 2010 07:10:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/02/18/efficient-c-tips-6-dont-use-the-ternary-operator/#comment-2847</guid>
		<description>Hello Nigel,
Interesting post. Can you add some examples of code generated for the ternary operator and for the if/else construct, for comparison?

That would make the post more complete.
Thanks!</description>
		<content:encoded><![CDATA[<p>Hello Nigel,<br />
Interesting post. Can you add some examples of code generated for the ternary operator and for the if/else construct, for comparison?</p>
<p>That would make the post more complete.<br />
Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/02/efficient-c-tips-6-dont-use-the-ternary-operator/comment-page-1/#comment-1310</link>
		<dc:creator>Doug</dc:creator>
		<pubDate>Wed, 28 Jul 2010 00:20:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/02/18/efficient-c-tips-6-dont-use-the-ternary-operator/#comment-1310</guid>
		<description>My personal favorite &quot;do not use unless absolutely necessary&quot; construct is modulo. Modulo looks simple and elegant when written, but try it out -- write a simple modulo construct, compile it and look at the assembly output. It&#039;s horrific. Think the problem through and simplify it instead. I haven&#039;t yet found a modulo solution I couldn&#039;t implement much more simply and completely by rethinking the problem, resulting in code that&#039;s as accurate, uses less memory, and runs quicker.</description>
		<content:encoded><![CDATA[<p>My personal favorite &#8220;do not use unless absolutely necessary&#8221; construct is modulo. Modulo looks simple and elegant when written, but try it out &#8212; write a simple modulo construct, compile it and look at the assembly output. It&#8217;s horrific. Think the problem through and simplify it instead. I haven&#8217;t yet found a modulo solution I couldn&#8217;t implement much more simply and completely by rethinking the problem, resulting in code that&#8217;s as accurate, uses less memory, and runs quicker.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Greg Nelson</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/02/efficient-c-tips-6-dont-use-the-ternary-operator/comment-page-1/#comment-132</link>
		<dc:creator>Greg Nelson</dc:creator>
		<pubDate>Wed, 24 Feb 2010 19:47:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/02/18/efficient-c-tips-6-dont-use-the-ternary-operator/#comment-132</guid>
		<description>The printf() comment reminds me of something else... Nigel, since you said you are often interested in people&#039;s weird coding conventions, this one might fit the bill.  Want to guess why I write:char tmp1[10];char tmp2[10];char tmp3[10];char final[50];snprintf(tmp1, &quot;%7.1f&quot;, f1);snprintf(tmp2, &quot;%7.1f&quot;, f2);snprintf(tmp3, &quot;%7.1f&quot;, f3);snprintf(final, &quot;%s %s %s&quot;, tmp1, tmp2, tmp3);&lt;b&gt;Answer:&lt;/b&gt;Because one of the compilers/libraries (still haven&#039;t found the culprit) used for our code can&#039;t handle multiple double arguments to a varargs call!If I do it the conventional way with &quot;%7.1f %7.1f %7.1f&quot; instead, I either get garbage output, or the program hangs.My guess is this has something to do with poor floating point emulation and/or different ways of stacking doubles versus ints/ptrs/etc.</description>
		<content:encoded><![CDATA[<p>The printf() comment reminds me of something else&#8230; Nigel, since you said you are often interested in people&#39;s weird coding conventions, this one might fit the bill.  Want to guess why I write:char tmp1[10];char tmp2[10];char tmp3[10];char final[50];snprintf(tmp1, &quot;%7.1f&quot;, f1);snprintf(tmp2, &quot;%7.1f&quot;, f2);snprintf(tmp3, &quot;%7.1f&quot;, f3);snprintf(final, &quot;%s %s %s&quot;, tmp1, tmp2, tmp3);<b>Answer:</b>Because one of the compilers/libraries (still haven&#39;t found the culprit) used for our code can&#39;t handle multiple double arguments to a varargs call!If I do it the conventional way with &quot;%7.1f %7.1f %7.1f&quot; instead, I either get garbage output, or the program hangs.My guess is this has something to do with poor floating point emulation and/or different ways of stacking doubles versus ints/ptrs/etc.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bandit</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/02/efficient-c-tips-6-dont-use-the-ternary-operator/comment-page-1/#comment-131</link>
		<dc:creator>bandit</dc:creator>
		<pubDate>Tue, 26 Jan 2010 01:50:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/02/18/efficient-c-tips-6-dont-use-the-ternary-operator/#comment-131</guid>
		<description>I use them usually only for printf():printf( &quot;%s&quot;, (bletch ? &quot;foo&quot; : &quot;bar&quot;) );otherwise I use if() else for clarity. I normally dofoo = 0;if( bar )   foo = 1;because this handles the default case &amp;&amp; the exceptional case.</description>
		<content:encoded><![CDATA[<p>I use them usually only for printf():printf( &quot;%s&quot;, (bletch ? &quot;foo&quot; : &quot;bar&quot;) );otherwise I use if() else for clarity. I normally dofoo = 0;if( bar )   foo = 1;because this handles the default case &amp;&amp; the exceptional case.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ashleigh</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/02/efficient-c-tips-6-dont-use-the-ternary-operator/comment-page-1/#comment-130</link>
		<dc:creator>ashleigh</dc:creator>
		<pubDate>Tue, 26 Jan 2010 01:16:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/02/18/efficient-c-tips-6-dont-use-the-ternary-operator/#comment-130</guid>
		<description>I&#039;ve seen the same as Nigel - often the ternary operator is worse.I still use it where it improves readability of the source and I don&#039;t  care about performance.I guess the lesson is - think about what you write. Write with maintenance, readability AND performance in mind. Know what the compiler does (ie go in with your eyes open), and then write whats best for the situation you are handling.</description>
		<content:encoded><![CDATA[<p>I&#39;ve seen the same as Nigel &#8211; often the ternary operator is worse.I still use it where it improves readability of the source and I don&#39;t  care about performance.I guess the lesson is &#8211; think about what you write. Write with maintenance, readability AND performance in mind. Know what the compiler does (ie go in with your eyes open), and then write whats best for the situation you are handling.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/02/efficient-c-tips-6-dont-use-the-ternary-operator/comment-page-1/#comment-129</link>
		<dc:creator>Mike</dc:creator>
		<pubDate>Wed, 20 Jan 2010 16:34:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/02/18/efficient-c-tips-6-dont-use-the-ternary-operator/#comment-129</guid>
		<description>Just ran a test with HC08 and CodeWarrior -- ternary operator uses 4 more bytes than if/else in one particular test case.</description>
		<content:encoded><![CDATA[<p>Just ran a test with HC08 and CodeWarrior &#8212; ternary operator uses 4 more bytes than if/else in one particular test case.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

