{"id":3672,"date":"2012-09-10T08:00:04","date_gmt":"2012-09-10T07:00:04","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=3672"},"modified":"2012-09-10T08:00:04","modified_gmt":"2012-09-10T07:00:04","slug":"the-floating-point-argument","status":"publish","type":"post","link":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/2012\/09\/10\/the-floating-point-argument\/","title":{"rendered":"The floating point argument"},"content":{"rendered":"<p>I am on vacation and, all being well, by the time this posting goes live, I will be sunning myself on a Greek island. A couple of weeks ago, I posted a <a href=\"https:\/\/blogs.mentor.com\/colinwalls\/blog\/2012\/08\/28\/floating-point\/\" target=\"_blank\" rel=\"noopener noreferrer\">blog<\/a> about the use of floating point. My colleague <a href=\"mailto:brooks_moses@mentor.com\" target=\"_blank\" rel=\"noopener noreferrer\">Brooks Moses<\/a> [who did a <a href=\"https:\/\/blogs.mentor.com\/colinwalls\/blog\/2012\/05\/21\/gtc-mission-accomplished\/\" target=\"_blank\" rel=\"noopener noreferrer\">guest blog<\/a> post a while back] made a comment on that posting, pointing out that I had over-simplified my example. I am always happy to get such feedback.<\/p>\n<p>It transpired that Brooks had some bigger issues with what I had to say, so I was pleased to offer him an opportunity to have his say. Over to you Brooks and, waiter, bring me another beer &#8230;<!--more--><\/p>\n<p>I was surprised to see Colin&#8217;s conclusion in a <a href=\"https:\/\/blogs.mentor.com\/colinwalls\/blog\/2012\/08\/28\/floating-point\/\" target=\"_blank\" rel=\"noopener noreferrer\">recent posting<\/a> that &#8220;floating point should only be used if it is essential and only after every creative way to do the calculations using integers has been investigated and eliminated.&#8221; My background is in computational fluid dynamics, and there floating-point arithmetic is our bread and butter. Even now that I&#8217;m working in high-performance embedded computing, almost everything I do uses floating point.<\/p>\n<p>In both of those fields, performance is king. So, if Colin&#8217;s right, why aren&#8217;t we using integers? There are several reasons:<\/p>\n<p><strong>Integer instructions aren&#8217;t faster on many processors<\/strong>. This has been true in the world of high-performance computing for a while; it&#8217;s recently become true in the middle range of embedded systems. These days you can get a hardware floating-point unit in something as small as an ARM Cortex-M4 microcontroller; most processors powerful enough to do a significant amount of computation will have one.<\/p>\n<p>For example, consider the ARM Cortex-M4 and the Freescale e600 Power Architecture cores. Like most modern processors, these are both pipelined architectures. The processor can start an integer or floating-point instruction (almost) every cycle if it doesn&#8217;t have to wait for an input, but it may take multiple cycles to produce the output. On the M4, floating-point instructions have a latency of one additional cycle, which means if the next instruction needs their output, the processor will stall for a cycle to catch up &#8212; but a good compiler can easily arrange the instructions so this is rare. On the e600, floating-point instructions have a latency of 5 cycles, but it&#8217;s also slow with 32-bit integer multiplication so in a lot of cases floating-point still comes out ahead.<\/p>\n<p><strong>Fixed-point multiplication is complicated<\/strong>. Since we usually care about numbers with fractional parts, we would have to use <a href=\"http:\/\/en.wikipedia.org\/wiki\/Fixed-point_arithmetic\" target=\"_blank\" rel=\"noopener noreferrer\">fixed-point arithmetic<\/a> to do computations with integers. The idea is simple; consider the integers to be scaled by a constant factor. (For example, an integer value of 34 would represent 34\/32768, if our scale factor is 2<sup>15<\/sup>.) This doesn&#8217;t affect addition, but it means that for multiplication we need compute all the high and low bits and then right-shift the answer. This takes several instructions, whereas with floating-point we can do the whole multiplication in one instruction.<\/p>\n<p><strong>And, often, instruction speed isn&#8217;t even the bottleneck<\/strong>. Doing a lot of computations means processing a lot of data, and that data has to come from somewhere. In a lot of the programs I work with, the limiting factor in performance is often the speed of moving data into and out of memory &#8212; especially when the data set doesn&#8217;t fit into the CPU cache. When that&#8217;s the case, 32 bits to move is 32 bits to move, regardless of whether the data in it is an integer or floating-point value.<\/p>\n<p>On the other hand&#8230; There was a similar debate, years ago, in computational fluid dynamics: Why use single-precision floating-point values, when most processors of the day were just as fast in double precision? So most software used double-precision &#8230; up until memory speeds were the bottleneck, and it started to matter that double-precision values were twice as large. Nowadays, if your algorithm can be written to use only 16-bit integers, the same argument applies.<\/p>\n<p>And then there&#8217;s the idea of using things like Xilinx&#8217;s Zynq &#8212; with an ARM CPU and a large FPGA &#8212; for this sort of number crunching. Floating-point arithmetic really is slow and painful on an FPGA, so we high-performance programmers will have to start learning to use integers after all if we want to use a system like that.<\/p>\n<p>So the real answer to whether to use floating point or integers? It all depends on your hardware and what you need to do with it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am on vacation and, all being well, by the time this posting goes live, I will be sunning myself&#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":[391,485,486,300,484,320,381,487,488,461],"industry":[],"product":[],"coauthors":[],"class_list":["post-3672","post","type-post","status-publish","format-standard","hentry","category-news","tag-arm","tag-cortex","tag-e600","tag-embedded-software","tag-floating-point","tag-fpga","tag-freescale","tag-m4","tag-xilinx","tag-zynq"],"_links":{"self":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/3672","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=3672"}],"version-history":[{"count":0,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/3672\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=3672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=3672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=3672"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=3672"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=3672"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=3672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}