<?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 Tip #12 &#8211; Be wary of switch statements</title>
	<atom:link href="http://embeddedgurus.com/stack-overflow/2010/04/efficient-c-tip-12-be-wary-of-switch-statements/feed/" rel="self" type="application/rss+xml" />
	<link>http://embeddedgurus.com/stack-overflow/2010/04/efficient-c-tip-12-be-wary-of-switch-statements/</link>
	<description>Thoughts on embedded systems by Nigel Jones</description>
	<lastBuildDate>Sun, 06 May 2012 10:34:02 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Nigel Jones</title>
		<link>http://embeddedgurus.com/stack-overflow/2010/04/efficient-c-tip-12-be-wary-of-switch-statements/comment-page-1/#comment-14095</link>
		<dc:creator>Nigel Jones</dc:creator>
		<pubDate>Tue, 07 Feb 2012 01:00:12 +0000</pubDate>
		<guid isPermaLink="false">http://embeddedgurus.com/stack-overflow/?p=450#comment-14095</guid>
		<description>You are most welcome.</description>
		<content:encoded><![CDATA[<p>You are most welcome.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Raul</title>
		<link>http://embeddedgurus.com/stack-overflow/2010/04/efficient-c-tip-12-be-wary-of-switch-statements/comment-page-1/#comment-14077</link>
		<dc:creator>Raul</dc:creator>
		<pubDate>Mon, 06 Feb 2012 17:10:12 +0000</pubDate>
		<guid isPermaLink="false">http://embeddedgurus.com/stack-overflow/?p=450#comment-14077</guid>
		<description>Mr. Jones,

Thank you for sharing your knowledge and experience.

I read all 12 tips and learnt a lot!</description>
		<content:encoded><![CDATA[<p>Mr. Jones,</p>
<p>Thank you for sharing your knowledge and experience.</p>
<p>I read all 12 tips and learnt a lot!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mac</title>
		<link>http://embeddedgurus.com/stack-overflow/2010/04/efficient-c-tip-12-be-wary-of-switch-statements/comment-page-1/#comment-1556</link>
		<dc:creator>Mac</dc:creator>
		<pubDate>Mon, 16 Aug 2010 07:25:47 +0000</pubDate>
		<guid isPermaLink="false">http://embeddedgurus.com/stack-overflow/?p=450#comment-1556</guid>
		<description>Another way that switch statements can be compiled are as decrementing value.

So switch (x)
{
  case 0:Code_Zero(); break;
  case 1:Code_One(); break;
  case 2:Code_Two(); break;
..
}

.. gets compiled as:

R0 = (x);
JEQ CODE_ZERO();
dec(R0);
JEQ CODE_ONE();
dec(R0);
JEQ CODE_TWO();
etc...

It&#039;s an optimization I&#039;ve seen for switch statements but not for the equivalent &#039;if&#039; structure.</description>
		<content:encoded><![CDATA[<p>Another way that switch statements can be compiled are as decrementing value.</p>
<p>So switch (x)<br />
{<br />
  case 0:Code_Zero(); break;<br />
  case 1:Code_One(); break;<br />
  case 2:Code_Two(); break;<br />
..<br />
}</p>
<p>.. gets compiled as:</p>
<p>R0 = (x);<br />
JEQ CODE_ZERO();<br />
dec(R0);<br />
JEQ CODE_ONE();<br />
dec(R0);<br />
JEQ CODE_TWO();<br />
etc&#8230;</p>
<p>It&#8217;s an optimization I&#8217;ve seen for switch statements but not for the equivalent &#8216;if&#8217; structure.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bernhard Weller</title>
		<link>http://embeddedgurus.com/stack-overflow/2010/04/efficient-c-tip-12-be-wary-of-switch-statements/comment-page-1/#comment-1429</link>
		<dc:creator>Bernhard Weller</dc:creator>
		<pubDate>Mon, 09 Aug 2010 08:42:42 +0000</pubDate>
		<guid isPermaLink="false">http://embeddedgurus.com/stack-overflow/?p=450#comment-1429</guid>
		<description>It might also help to have a look at the compilers intrinsics. Although the code will then get compiler-specific and less portable, in some applications it might be useful.

Like the MSP430 compiler used in the Code Composer Studio has an intrinsic built in (_never_executed()) which tells the compiler, that the default case is never executed, and thus the variable in the switch statement has to be one of the cases handled above. (see TI document SLAU132) The compiler is then able to generate more efficient code.

This puts some pressure on the programmer of course, he now has to ensure throughout the code, that the variable always maintains valid values.</description>
		<content:encoded><![CDATA[<p>It might also help to have a look at the compilers intrinsics. Although the code will then get compiler-specific and less portable, in some applications it might be useful.</p>
<p>Like the MSP430 compiler used in the Code Composer Studio has an intrinsic built in (_never_executed()) which tells the compiler, that the default case is never executed, and thus the variable in the switch statement has to be one of the cases handled above. (see TI document SLAU132) The compiler is then able to generate more efficient code.</p>
<p>This puts some pressure on the programmer of course, he now has to ensure throughout the code, that the variable always maintains valid values.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nigel Jones</title>
		<link>http://embeddedgurus.com/stack-overflow/2010/04/efficient-c-tip-12-be-wary-of-switch-statements/comment-page-1/#comment-1125</link>
		<dc:creator>Nigel Jones</dc:creator>
		<pubDate>Mon, 21 Jun 2010 11:11:20 +0000</pubDate>
		<guid isPermaLink="false">http://embeddedgurus.com/stack-overflow/?p=450#comment-1125</guid>
		<description>I took the liberty of editing your original comment such that the operators are now correct.  HTML is most definitely not conducive to leaving comments concerning C operators.</description>
		<content:encoded><![CDATA[<p>I took the liberty of editing your original comment such that the operators are now correct.  HTML is most definitely not conducive to leaving comments concerning C operators.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lundin</title>
		<link>http://embeddedgurus.com/stack-overflow/2010/04/efficient-c-tip-12-be-wary-of-switch-statements/comment-page-1/#comment-1101</link>
		<dc:creator>Lundin</dc:creator>
		<pubDate>Mon, 21 Jun 2010 06:51:23 +0000</pubDate>
		<guid isPermaLink="false">http://embeddedgurus.com/stack-overflow/?p=450#comment-1101</guid>
		<description>The nonsense &quot;less than&quot; and &quot;greater than&quot; symbols are supposed to be the left and right shift operators. The form is mistaking it for HTML tags or some such.</description>
		<content:encoded><![CDATA[<p>The nonsense &#8220;less than&#8221; and &#8220;greater than&#8221; symbols are supposed to be the left and right shift operators. The form is mistaking it for HTML tags or some such.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lundin</title>
		<link>http://embeddedgurus.com/stack-overflow/2010/04/efficient-c-tip-12-be-wary-of-switch-statements/comment-page-1/#comment-1089</link>
		<dc:creator>Lundin</dc:creator>
		<pubDate>Fri, 18 Jun 2010 08:06:02 +0000</pubDate>
		<guid isPermaLink="false">http://embeddedgurus.com/stack-overflow/?p=450#comment-1089</guid>
		<description>For the dip-switch example, there is a rather likely scenario is some sort of subtle bug introduced when masking, because of the integer promotions in C. The ~, &lt;&lt;, &gt;&gt; operators are particularly notorious for such. If such a bug slips through into production code, the product will go haywire, instead of reporting that an unexpected error has occured. It will be quite easy to find that bug too, with a proper error report from the program.

And naturally, many defensive programming methods will be very resource-consuming. I&#039;d be wary of taking things to the extreme, as preached by standards like 60730 or its evil brother 61508, that encourage crazy practices like &quot;walking pattern&quot; memory tests, where you in runtime move a bit pattern through the whole RAM to check for corrupt cells.  I&#039;m quite sure that such algorithms will not only be incredible slow, they will be introduce notable hazards and needless complexity. I don&#039;t think programmers should fiddle around with such, it should be implemented in hardware.  Such efforts are also currently being made by MCU manufacturers.

As a sidenote: after spending considerable time with standards like 61508 and ISO 13849, I always come to the conclusion it they are the work of bureaucrat madmen, and that the encouraged practices aren&#039;t based on scientific proof, nor that they have any relevance to safety nor engineering.</description>
		<content:encoded><![CDATA[<p>For the dip-switch example, there is a rather likely scenario is some sort of subtle bug introduced when masking, because of the integer promotions in C. The ~, &lt;&lt;, &gt;&gt; operators are particularly notorious for such. If such a bug slips through into production code, the product will go haywire, instead of reporting that an unexpected error has occured. It will be quite easy to find that bug too, with a proper error report from the program.</p>
<p>And naturally, many defensive programming methods will be very resource-consuming. I&#8217;d be wary of taking things to the extreme, as preached by standards like 60730 or its evil brother 61508, that encourage crazy practices like &#8220;walking pattern&#8221; memory tests, where you in runtime move a bit pattern through the whole RAM to check for corrupt cells.  I&#8217;m quite sure that such algorithms will not only be incredible slow, they will be introduce notable hazards and needless complexity. I don&#8217;t think programmers should fiddle around with such, it should be implemented in hardware.  Such efforts are also currently being made by MCU manufacturers.</p>
<p>As a sidenote: after spending considerable time with standards like 61508 and ISO 13849, I always come to the conclusion it they are the work of bureaucrat madmen, and that the encouraged practices aren&#8217;t based on scientific proof, nor that they have any relevance to safety nor engineering.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nigel Jones</title>
		<link>http://embeddedgurus.com/stack-overflow/2010/04/efficient-c-tip-12-be-wary-of-switch-statements/comment-page-1/#comment-1072</link>
		<dc:creator>Nigel Jones</dc:creator>
		<pubDate>Fri, 11 Jun 2010 10:20:10 +0000</pubDate>
		<guid isPermaLink="false">http://embeddedgurus.com/stack-overflow/?p=450#comment-1072</guid>
		<description>An interesting comment Daniel. To the first part as to how can I know that a variable is range limited? This can occur in a number of ways. For example, let&#039;s say I read a 4 bit DIP switch attached to an 8-bit port. Having read the port, I mask off the unused bits leaving me with a 4 bit variable that can only take on the values of 0-15. If I follow this with a switch statement that has explicit values for 0-15, then the default statement is, in theory, pointless. 

To your second point concerning defensive programming. Let&#039;s assume that between masking the DIP switch value and testing it, some form of corruption occurs, such that it is no longer range limited to 0-15. In this case, a default statement &lt;em&gt;may &lt;/em&gt;provide a modicum of protection. I say &#039;may&#039; because if the compiler chooses to implement the switch statement as a jump table, then it will probably emit code that consists of:

Check that the value is less than 16
If it is, index into the jump table
Else jump to the default handler


Of course, this begs the question, what happens if corruption occurs between the check and the index into the jump table? 

Notwithstanding this, I think your comment illustrates that there is a natural tension between efficient programming and defensive programming. Efficiency is typically about doing just enough to get the job done. Defensive programming is about trying to ensure that the real world doesn&#039;t intrude and cause your system to crash.  I use defensive programming by default (if you would pardon the pun) and efficient programming when I have to. Incidentally I wrote a &lt;a href=&quot;http://embeddedgurus.com/stack-overflow/2008/04/iec60730/&quot; rel=&quot;nofollow&quot;&gt;posting&lt;/a&gt; a few years ago about another defensive issue - IEC60730. I&#039;d be interested on your comments on that.</description>
		<content:encoded><![CDATA[<p>An interesting comment Daniel. To the first part as to how can I know that a variable is range limited? This can occur in a number of ways. For example, let&#8217;s say I read a 4 bit DIP switch attached to an 8-bit port. Having read the port, I mask off the unused bits leaving me with a 4 bit variable that can only take on the values of 0-15. If I follow this with a switch statement that has explicit values for 0-15, then the default statement is, in theory, pointless. </p>
<p>To your second point concerning defensive programming. Let&#8217;s assume that between masking the DIP switch value and testing it, some form of corruption occurs, such that it is no longer range limited to 0-15. In this case, a default statement <em>may </em>provide a modicum of protection. I say &#8216;may&#8217; because if the compiler chooses to implement the switch statement as a jump table, then it will probably emit code that consists of:</p>
<p>Check that the value is less than 16<br />
If it is, index into the jump table<br />
Else jump to the default handler</p>
<p>Of course, this begs the question, what happens if corruption occurs between the check and the index into the jump table? </p>
<p>Notwithstanding this, I think your comment illustrates that there is a natural tension between efficient programming and defensive programming. Efficiency is typically about doing just enough to get the job done. Defensive programming is about trying to ensure that the real world doesn&#8217;t intrude and cause your system to crash.  I use defensive programming by default (if you would pardon the pun) and efficient programming when I have to. Incidentally I wrote a <a href="http://embeddedgurus.com/stack-overflow/2008/04/iec60730/" rel="nofollow">posting</a> a few years ago about another defensive issue &#8211; IEC60730. I&#8217;d be interested on your comments on that.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lundin</title>
		<link>http://embeddedgurus.com/stack-overflow/2010/04/efficient-c-tip-12-be-wary-of-switch-statements/comment-page-1/#comment-1070</link>
		<dc:creator>Lundin</dc:creator>
		<pubDate>Fri, 11 Jun 2010 09:35:20 +0000</pubDate>
		<guid isPermaLink="false">http://embeddedgurus.com/stack-overflow/?p=450#comment-1070</guid>
		<description>&quot;Now if you know that the index variable can only possibly take on the values zero to ten, then you can of course eliminate the default statement – and then get excoriated by the coding standards / MISRA folks.&quot;

The question is, how can you know? The reason MISRA enforces &quot;default&quot;, as well as &quot;else&quot; after every &quot;if-else if&quot;, is for one purpose, and that is defensive programming. If your memory gets corrupted by bugs or by hardware-related problems (EMI memory corruption, broken ADCs, brownout etc etc), a default statement can catch those and you can gracefully terminate/reset the program, instead of running haywire.

The term defensive programming is typically completely alien to pure software academics. There is just no way they can get in their head that the program may be exposed to the real world outside their desktop, and that an algorithm that is perfect in theory may not be perfect in practice. An -engineer- should however understand and practice defensive programming, especially in safety-critical systems.</description>
		<content:encoded><![CDATA[<p>&#8220;Now if you know that the index variable can only possibly take on the values zero to ten, then you can of course eliminate the default statement – and then get excoriated by the coding standards / MISRA folks.&#8221;</p>
<p>The question is, how can you know? The reason MISRA enforces &#8220;default&#8221;, as well as &#8220;else&#8221; after every &#8220;if-else if&#8221;, is for one purpose, and that is defensive programming. If your memory gets corrupted by bugs or by hardware-related problems (EMI memory corruption, broken ADCs, brownout etc etc), a default statement can catch those and you can gracefully terminate/reset the program, instead of running haywire.</p>
<p>The term defensive programming is typically completely alien to pure software academics. There is just no way they can get in their head that the program may be exposed to the real world outside their desktop, and that an algorithm that is perfect in theory may not be perfect in practice. An -engineer- should however understand and practice defensive programming, especially in safety-critical systems.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nigel Jones</title>
		<link>http://embeddedgurus.com/stack-overflow/2010/04/efficient-c-tip-12-be-wary-of-switch-statements/comment-page-1/#comment-721</link>
		<dc:creator>Nigel Jones</dc:creator>
		<pubDate>Sat, 24 Apr 2010 23:35:45 +0000</pubDate>
		<guid isPermaLink="false">http://embeddedgurus.com/stack-overflow/?p=450#comment-721</guid>
		<description>&lt;blockquote&gt;What I like most about this blog is not necessarily the advice given but the fact that it makes you think twice about how you do things and how you could improve.&lt;/blockquote&gt;
I thinks that&#039;s one of the best things anyone has ever said about this blog Daryl - so thank you. Developing this idea a little bit, while there are indeed some fundamental truths in the embedded world, the space is in general so large and diverse that almost any advice can be shown to be poor in certain circumstances. Thus read, learn, but above all think!</description>
		<content:encoded><![CDATA[<blockquote><p>What I like most about this blog is not necessarily the advice given but the fact that it makes you think twice about how you do things and how you could improve.</p></blockquote>
<p>I thinks that&#8217;s one of the best things anyone has ever said about this blog Daryl &#8211; so thank you. Developing this idea a little bit, while there are indeed some fundamental truths in the embedded world, the space is in general so large and diverse that almost any advice can be shown to be poor in certain circumstances. Thus read, learn, but above all think!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

