<?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: Do you ever think about endianess?</title>
	<atom:link href="http://www.embeddedinsights.com/channels/2012/02/08/do-you-ever-think-about-endianess/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.embeddedinsights.com/channels/2012/02/08/do-you-ever-think-about-endianess/</link>
	<description>Shedding Light on the Hidden World of Embedded Systems</description>
	<lastBuildDate>Mon, 28 Jul 2014 16:18:37 -0400</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: Jeet</title>
		<link>http://www.embeddedinsights.com/channels/2012/02/08/do-you-ever-think-about-endianess/#comment-175439</link>
		<dc:creator>Jeet</dc:creator>
		<pubDate>Fri, 21 Mar 2014 21:41:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.embeddedinsights.com/channels/?p=687#comment-175439</guid>
		<description>Hi, you wrote: Despite the C language standard do not specify endianness, you can write C code that is completely endian-insensitive also. The problems arise when you assume some object representation and access types under the hood, like using unions or pointer casts to get the chars under floats or ints. 

What are some techniques to deal with this issue as I am currently trying to write a communications module using Modbus. If the struct containing ints, floats etc is typecasted to chars for Tx/Rx, how to dynamically detect a variable change and thus a required byte swap?

Thanks!
Jeet</description>
		<content:encoded><![CDATA[<p>Hi, you wrote: Despite the C language standard do not specify endianness, you can write C code that is completely endian-insensitive also. The problems arise when you assume some object representation and access types under the hood, like using unions or pointer casts to get the chars under floats or ints. </p>
<p>What are some techniques to deal with this issue as I am currently trying to write a communications module using Modbus. If the struct containing ints, floats etc is typecasted to chars for Tx/Rx, how to dynamically detect a variable change and thus a required byte swap?</p>
<p>Thanks!<br />
Jeet</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: L.H. @ LI</title>
		<link>http://www.embeddedinsights.com/channels/2012/02/08/do-you-ever-think-about-endianess/#comment-16824</link>
		<dc:creator>L.H. @ LI</dc:creator>
		<pubDate>Sun, 27 May 2012 16:22:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.embeddedinsights.com/channels/?p=687#comment-16824</guid>
		<description>IBM 360 and derivatives are big endian

as far as structure fields with bit fields, they are reversed</description>
		<content:encoded><![CDATA[<p>IBM 360 and derivatives are big endian</p>
<p>as far as structure fields with bit fields, they are reversed</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: J.P. @ LI</title>
		<link>http://www.embeddedinsights.com/channels/2012/02/08/do-you-ever-think-about-endianess/#comment-16658</link>
		<dc:creator>J.P. @ LI</dc:creator>
		<pubDate>Tue, 22 May 2012 15:44:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.embeddedinsights.com/channels/?p=687#comment-16658</guid>
		<description>Htonl, htons, ntohl, ntohs are your best friends, and both bitfields and struct padding are typically compiler-dependent.</description>
		<content:encoded><![CDATA[<p>Htonl, htons, ntohl, ntohs are your best friends, and both bitfields and struct padding are typically compiler-dependent.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: B.G. @ LI</title>
		<link>http://www.embeddedinsights.com/channels/2012/02/08/do-you-ever-think-about-endianess/#comment-16517</link>
		<dc:creator>B.G. @ LI</dc:creator>
		<pubDate>Sat, 19 May 2012 14:15:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.embeddedinsights.com/channels/?p=687#comment-16517</guid>
		<description>@Steve Simon: I am constaltly amazed by the number of people who overlay a struct pointer (in c) over a protocol buffer and then byteswap the members of the structure.

I do this when needed, and see no issue with it.

@steve: The C compiler is not required to pack structures, and though many compilers can be forced to do this it is silly to rely on it.

Compilers have some sort of &quot;#pragma packed&quot; for this very issue. I jusrt dealt with it reading in a structure on an external EPROM on a replaceable item (the EPROM provides info on the item&#039;s use).

BUT - packing and byte sex are unrelated.

@steve: The argument that if the local byte order is &quot;correct&quot; then it will be more efficent is also foolish, again I/O is the limiting factor.

Agreed - but the real issue is you still need to read a struct from something external and turn it into the &quot;native&quot; format - sometimes a packing issue, sometimes a byte sex issue; sometimes both.

The only reason to create an unpacked version of the struct and laboriously transfer the fields from the original to the copy is the native version will not support the packed fields, ie a byte takes 16 bits, or you cannot access a byte on an odd address.

@steve: Oh dear, have I started ranting again...

It&#039;s OK, you are in good company, and the drugs should kick in right about ... now :^)

@OP: If you stay completely in the native CPU/memory/peripherals, you rarely need to worry about byte sex. HOWEVER, the embedded world does not have this luxury. Embedded systems of any complexity *always* deals with protocols and devices on the &quot;outside of the CPU&quot;, so byte sex is one of the issues.

As noted before, the C preprocessor can easily deal with this:

#if SWAP_BYTE_SEX
#define SWAP16(x) swap16( x )
#else
#define SWAP16( x ) x
#endif

ptr-&gt;field = SWAP16( ptr-&gt;field );

is perfectly OK. I prefer to pass in the address of the field, and have the code swap in place, but that is a style issue:

swap16( &amp;(ptr-&gt;field) );

put all of the swap calls in a single function that can be ignored if needed.</description>
		<content:encoded><![CDATA[<p>@Steve Simon: I am constaltly amazed by the number of people who overlay a struct pointer (in c) over a protocol buffer and then byteswap the members of the structure.</p>
<p>I do this when needed, and see no issue with it.</p>
<p>@steve: The C compiler is not required to pack structures, and though many compilers can be forced to do this it is silly to rely on it.</p>
<p>Compilers have some sort of &#8220;#pragma packed&#8221; for this very issue. I jusrt dealt with it reading in a structure on an external EPROM on a replaceable item (the EPROM provides info on the item&#8217;s use).</p>
<p>BUT &#8211; packing and byte sex are unrelated.</p>
<p>@steve: The argument that if the local byte order is &#8220;correct&#8221; then it will be more efficent is also foolish, again I/O is the limiting factor.</p>
<p>Agreed &#8211; but the real issue is you still need to read a struct from something external and turn it into the &#8220;native&#8221; format &#8211; sometimes a packing issue, sometimes a byte sex issue; sometimes both.</p>
<p>The only reason to create an unpacked version of the struct and laboriously transfer the fields from the original to the copy is the native version will not support the packed fields, ie a byte takes 16 bits, or you cannot access a byte on an odd address.</p>
<p>@steve: Oh dear, have I started ranting again&#8230;</p>
<p>It&#8217;s OK, you are in good company, and the drugs should kick in right about &#8230; now :^)</p>
<p>@OP: If you stay completely in the native CPU/memory/peripherals, you rarely need to worry about byte sex. HOWEVER, the embedded world does not have this luxury. Embedded systems of any complexity *always* deals with protocols and devices on the &#8220;outside of the CPU&#8221;, so byte sex is one of the issues.</p>
<p>As noted before, the C preprocessor can easily deal with this:</p>
<p>#if SWAP_BYTE_SEX<br />
#define SWAP16(x) swap16( x )<br />
#else<br />
#define SWAP16( x ) x<br />
#endif</p>
<p>ptr-&gt;field = SWAP16( ptr-&gt;field );</p>
<p>is perfectly OK. I prefer to pass in the address of the field, and have the code swap in place, but that is a style issue:</p>
<p>swap16( &amp;(ptr-&gt;field) );</p>
<p>put all of the swap calls in a single function that can be ignored if needed.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: QNP @ LI</title>
		<link>http://www.embeddedinsights.com/channels/2012/02/08/do-you-ever-think-about-endianess/#comment-16408</link>
		<dc:creator>QNP @ LI</dc:creator>
		<pubDate>Wed, 16 May 2012 03:37:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.embeddedinsights.com/channels/?p=687#comment-16408</guid>
		<description>I have been faced with the big and little endian problem in C. In deed, when we optimize code to cast a struct of bit-field with 32 bits length into a scalar integer value to make a comparison with a fixed value. It could lead to the error!</description>
		<content:encoded><![CDATA[<p>I have been faced with the big and little endian problem in C. In deed, when we optimize code to cast a struct of bit-field with 32 bits length into a scalar integer value to make a comparison with a fixed value. It could lead to the error!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: R.D. @ LI</title>
		<link>http://www.embeddedinsights.com/channels/2012/02/08/do-you-ever-think-about-endianess/#comment-16293</link>
		<dc:creator>R.D. @ LI</dc:creator>
		<pubDate>Sat, 12 May 2012 05:45:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.embeddedinsights.com/channels/?p=687#comment-16293</guid>
		<description>@Richard, I know a number of the newer processors, such as ARM and PowerPC provide boot configurations that can let you set the endianness of the CPU if your project requirements mandate this, but it is not really transparent to the coder and it doesn&#039;t switch on the fly based on the data set. There is a small efficiency cost for every command, not just the first couple of operands. You can use conditional compilation and casts to create adaptable code that adjusts to this setting. That&#039;s basically the path used in the document Keith referenced. The endianness is also almost always defined in the platform headers for the compiler to support network packet functions like ntoh() and hton(). 

It&#039;s been a while since I worked on an x-86 platform, but last I saw, they used either a fixed little endian or defaulted to little endian with a boot configurable option for big endian. If they have an on-the-fly adjustment now, I&#039;d like to see the details, but last I was aware, this was also just a boot option. I don&#039;t think an auto-detect scheme would work well since, even if you could detect CPU opcodes, you would still never know the format of external data and there would be no way to auto detect that. In any case, I would expect this to produce a small efficiency cost on the CPU as well.

The problem with the boot configuration option is that it tends to just make things more confusing. It&#039;s almost always less efficient for the CPU core and it just adds extra points of potential confusion and failure to the platform development when developers may expect the platform to operate in the native configuration. Most production or architecture teams will never use the option since it creates more complexity than it solves.

Most standardized software suites have headers and driver interfaces that bury this at the lower levels, so it might be semi-transparent to application layer coders. Applications would rarely need to deal with this unless they were looking directly at a raw data packet that had not been processed for the host format or if a file with raw numeric data is transferred from one host file system to another without adjusting (like between MSWin and NIX systems.)

Driver developers need to deal with this at peripheral device input or other external data sources. Most external data devices and protocols do not include the endianness data in the data stream or packet, they define it hard-coded in the device data interface spec based on the device family or manufacturer. Some communications protocols, like ethernet, define a fixed format for all standard data on the network side. Others, like CAN or SPI, define endianness in the definitions for messages or individual values.

Once the data is conditioned for the host, you rarely need to deal with this for internal system processing. As Dennis mentioned above, it&#039;s generally an issue at system boundaries.</description>
		<content:encoded><![CDATA[<p>@Richard, I know a number of the newer processors, such as ARM and PowerPC provide boot configurations that can let you set the endianness of the CPU if your project requirements mandate this, but it is not really transparent to the coder and it doesn&#8217;t switch on the fly based on the data set. There is a small efficiency cost for every command, not just the first couple of operands. You can use conditional compilation and casts to create adaptable code that adjusts to this setting. That&#8217;s basically the path used in the document Keith referenced. The endianness is also almost always defined in the platform headers for the compiler to support network packet functions like ntoh() and hton(). </p>
<p>It&#8217;s been a while since I worked on an x-86 platform, but last I saw, they used either a fixed little endian or defaulted to little endian with a boot configurable option for big endian. If they have an on-the-fly adjustment now, I&#8217;d like to see the details, but last I was aware, this was also just a boot option. I don&#8217;t think an auto-detect scheme would work well since, even if you could detect CPU opcodes, you would still never know the format of external data and there would be no way to auto detect that. In any case, I would expect this to produce a small efficiency cost on the CPU as well.</p>
<p>The problem with the boot configuration option is that it tends to just make things more confusing. It&#8217;s almost always less efficient for the CPU core and it just adds extra points of potential confusion and failure to the platform development when developers may expect the platform to operate in the native configuration. Most production or architecture teams will never use the option since it creates more complexity than it solves.</p>
<p>Most standardized software suites have headers and driver interfaces that bury this at the lower levels, so it might be semi-transparent to application layer coders. Applications would rarely need to deal with this unless they were looking directly at a raw data packet that had not been processed for the host format or if a file with raw numeric data is transferred from one host file system to another without adjusting (like between MSWin and NIX systems.)</p>
<p>Driver developers need to deal with this at peripheral device input or other external data sources. Most external data devices and protocols do not include the endianness data in the data stream or packet, they define it hard-coded in the device data interface spec based on the device family or manufacturer. Some communications protocols, like ethernet, define a fixed format for all standard data on the network side. Others, like CAN or SPI, define endianness in the definitions for messages or individual values.</p>
<p>Once the data is conditioned for the host, you rarely need to deal with this for internal system processing. As Dennis mentioned above, it&#8217;s generally an issue at system boundaries.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: R.H. @ LI</title>
		<link>http://www.embeddedinsights.com/channels/2012/02/08/do-you-ever-think-about-endianess/#comment-16292</link>
		<dc:creator>R.H. @ LI</dc:creator>
		<pubDate>Sat, 12 May 2012 05:45:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.embeddedinsights.com/channels/?p=687#comment-16292</guid>
		<description>Bi-endian RISC and other x-86 CISC I thought these days made it transparent to coders and applications but there is a cost for that until the first two operands are hashed and endianess is determined and stored [set] in the register</description>
		<content:encoded><![CDATA[<p>Bi-endian RISC and other x-86 CISC I thought these days made it transparent to coders and applications but there is a cost for that until the first two operands are hashed and endianess is determined and stored [set] in the register</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: D.A. @ LI</title>
		<link>http://www.embeddedinsights.com/channels/2012/02/08/do-you-ever-think-about-endianess/#comment-16291</link>
		<dc:creator>D.A. @ LI</dc:creator>
		<pubDate>Sat, 12 May 2012 05:45:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.embeddedinsights.com/channels/?p=687#comment-16291</guid>
		<description>You typically only deal with this when you are crossing system boundaries, e.g. bus, network, serial, etc.</description>
		<content:encoded><![CDATA[<p>You typically only deal with this when you are crossing system boundaries, e.g. bus, network, serial, etc.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: K.S. @ LI</title>
		<link>http://www.embeddedinsights.com/channels/2012/02/08/do-you-ever-think-about-endianess/#comment-16290</link>
		<dc:creator>K.S. @ LI</dc:creator>
		<pubDate>Sat, 12 May 2012 05:44:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.embeddedinsights.com/channels/?p=687#comment-16290</guid>
		<description>Here is a page from the QNX operating system documentation that discusses how to work with endianness issues.</description>
		<content:encoded><![CDATA[<p>Here is a page from the QNX operating system documentation that discusses how to work with endianness issues.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike</title>
		<link>http://www.embeddedinsights.com/channels/2012/02/08/do-you-ever-think-about-endianess/#comment-16240</link>
		<dc:creator>Mike</dc:creator>
		<pubDate>Thu, 10 May 2012 21:51:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.embeddedinsights.com/channels/?p=687#comment-16240</guid>
		<description>The most recent time I had to think about endianness was when I reverse engineered a file format, without source code or even a detailed spec. We had a vague idea of the types of data stored in the file, and the order in which some of the fields were stored, but no specifics. And we only had two examples of the file, with no ability to get or generate more before we had to produce our analysis. Some fields in the file were big endian, some were little endian, and some were BCD. Some (but not all) multi-byte fields were stored in every other byte of the file, with intervening bytes containing 0.  Other fields stored 12-bit data in the middle 12 bits of a 16 bit field, with two 1-bits on top and two 0-bits on the bottom. That was one odd file format!

Other than that, the main time I think about endianness is during debugging, when I call a generic dump function that dumps bytes in hex and ASCII. If it is a structure that I am dumping, I have to be pay attention to both endianness and alignment issues.</description>
		<content:encoded><![CDATA[<p>The most recent time I had to think about endianness was when I reverse engineered a file format, without source code or even a detailed spec. We had a vague idea of the types of data stored in the file, and the order in which some of the fields were stored, but no specifics. And we only had two examples of the file, with no ability to get or generate more before we had to produce our analysis. Some fields in the file were big endian, some were little endian, and some were BCD. Some (but not all) multi-byte fields were stored in every other byte of the file, with intervening bytes containing 0.  Other fields stored 12-bit data in the middle 12 bits of a 16 bit field, with two 1-bits on top and two 0-bits on the bottom. That was one odd file format!</p>
<p>Other than that, the main time I think about endianness is during debugging, when I call a generic dump function that dumps bytes in hex and ASCII. If it is a structure that I am dumping, I have to be pay attention to both endianness and alignment issues.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
