{"id":1964,"date":"2011-07-11T09:26:17","date_gmt":"2011-07-11T08:26:17","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=1964"},"modified":"2011-07-11T09:26:17","modified_gmt":"2011-07-11T08:26:17","slug":"data-types-and-code-portability","status":"publish","type":"post","link":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/2011\/07\/11\/data-types-and-code-portability\/","title":{"rendered":"Data types and code portability"},"content":{"rendered":"<p>I recently attended a presentation about the challenges with porting code from 8\/16-bit CPUs to 32-bit devices. The speaker addressed all the major issues, giving me a few things to think about. One area that particularly caught my attention was the thorny issue of C language data types. With the intention of making it easy to write portable code, the specification for (traditional ANSI) C explicitly avoids specifying the bit size of data types. The effect upon some embedded code is quite the reverse, as data size may be a concern &#8230;<!--more--><\/p>\n<p>In the presentation, the speaker pointed out that a compiler for an 8-bit CPU would probably size int to be 16 bits. So code would need to be modified to make any int variable a short instead. I think that this is very short-sighted [no pun intended!]. A better approach is to replace all the standard data types with typedefs that indicate size &#8211; U8, U16, U32, S8, S16 &#8230; &#8211; and store these in a header file. Then modify the code to suit. Porting the code to another compiler just needs that header file to be edited appropriately.<\/p>\n<p>I was rather taken aback by an example used in this presentation. It showed a loop, which simply incremented a variable (on a timer tick). A particular function was called each time the value wrapped around to zero. So changing from 16 to 32 bits would result in it being called every 4 billion ticks instead of every 65 thousand. Although this is true, it is appalling programming style. Clearly the variable should be loaded with the number of ticks and counted down as that code would be no less efficient, but infinitely more readable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently attended a presentation about the challenges with porting code from 8\/16-bit CPUs to 32-bit devices. The speaker addressed&#8230;<\/p>\n","protected":false},"author":71677,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spanish_translation":"","french_translation":"","german_translation":"","italian_translation":"","polish_translation":"","japanese_translation":"","chinese_translation":"","footnotes":""},"categories":[1],"tags":[313,300,340],"industry":[],"product":[],"coauthors":[],"class_list":["post-1964","post","type-post","status-publish","format-standard","hentry","category-news","tag-c","tag-embedded-software","tag-programming-languages"],"_links":{"self":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/1964","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/users\/71677"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/comments?post=1964"}],"version-history":[{"count":0,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/1964\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=1964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=1964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=1964"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=1964"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=1964"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=1964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}