{"id":147,"date":"2012-12-03T11:08:35","date_gmt":"2012-12-03T19:08:35","guid":{"rendered":"http:\/\/www.earlevel.com\/main\/?p=147"},"modified":"2019-02-24T15:06:54","modified_gmt":"2019-02-24T23:06:54","slug":"a-note-about-de-normalization","status":"publish","type":"post","link":"https:\/\/www.earlevel.com\/main\/2012\/12\/03\/a-note-about-de-normalization\/","title":{"rendered":"A note about de-normalization"},"content":{"rendered":"<p>One ugly thing that we need to be aware of, especially for filters, is the de-normalization of numbers. Basically, computer processors try to keep floating point numbers normalized\u2014they try to keep them in the binary form 1.<em>xxxxxx<\/em> (where each <em>x<\/em> is 0 or 1) times 2 raised to a positive or negative power. When numbers become extremely small\u2014smaller than can be expressed by the largest negative exponent\u2014the processor tries to maintain precision by allowing the number to become de-normalized\u2014to slip to 0.<em>xxxxxx<\/em> times 2 raised to the lowest exponent, in order to allow smaller but less precise numbers. The catch is that most processors become much slower when performing computations with de-normalized numbers (&#8220;denormals&#8221;).<\/p>\n<p>Denormals happen when you multiply a small number by a tiny number. Typically, denormals are created in filters that ring out with no new input. Almost any new input, including background noise when recording, will keep denormals from appearing. However, if you process some sound, then follow it with samples of zero, a filter or reverb can decay to denormals; the denormals are then passed to the next processing block and cause it to slow as well.<\/p>\n<p>Some modern processors have specific instructions to treat denormals as zero, solving our problem\u2014if your code enforces that setting. You can repair a denormal by checking flags in a floating point number and substituting zero. But that&#8217;s a lot of work. A simple way to avoid denormals is to add a tiny but normalized number to calculations that are at risk\u20141E-18, for instance is -200 dB down from unity, and won&#8217;t affect audio paths, but when added to denormals will result in 1E-18; this won&#8217;t affect audible samples at all, due to the way floating point works.<\/p>\n<p>The Pentium 4 in particular becomes devastatingly slow\u2014so slow that it can&#8217;t keep up with real time audio processing. For audio purposes, denormals might as well be zero. They are so many dB down from unity that their only effect is the potential of bogging down our processing.<\/p>\n<p>While denormal protection could be built into the filter code, it&#8217;s more economical to use denormal protection on a more global level, instead of in each module. As an example of one possible solution, you could add a tiny constant (1E-18) to every incoming sample at the beginning of your processing chain, switching the sign of the constant (to -1E-18) after every buffer you process to avoid the possibility of a highpass filter in the chain removing the constant offset.<\/p>\n<p>The best solution depends on your exact circumstances, so you should make a point of understanding the threat. For instance, in the biquad code, there is a potential to generate denormals in the calculations of z1 and z2; if the filter input goes to zero, the output alone recirculates through z1 and z2, potentially getting closer and closer to zero until the values become de-normalized and continue to recirculate in slower de-normalized calculations, as well as passing to the next processing block and slowing its calculations.<\/p>\n<p>Lest you think that you need only be concerned about denormals when fine-tuning your code, take a look at the performance hit for various processors (special thanks to <a href=\"http:\/\/rcl-rs-vvg.blogspot.com\">RCL<\/a> for permission to reproduce his chart):<\/p>\n<p><a href=\"\/main\/wp-content\/uploads\/2012\/12\/times_slower.png\"><img loading=\"lazy\" decoding=\"async\" src=\"\/main\/wp-content\/uploads\/2012\/12\/times_slower.png\" alt=\"\" title=\"times_slower\" width=\"576\" height=\"330\" class=\"alignnone size-full wp-image-151\" srcset=\"https:\/\/www.earlevel.com\/main\/wp-content\/uploads\/2012\/12\/times_slower.png 576w, https:\/\/www.earlevel.com\/main\/wp-content\/uploads\/2012\/12\/times_slower-300x171.png 300w\" sizes=\"(max-width: 576px) 100vw, 576px\" \/><\/a><\/p>\n<p>Yes, that&#8217;s more than 100 times slower execution of test code on a Pentium 4 when the calculations become denormalized. I hit this one myself, years ago: After working on DSP chips, primarily, which are unaffected by this issue, I was developing a commercial product for Mac and PC that ran on the host CPU. I did the initial development on a Power PC processor, then gave it a run on my PC\u2014a P4. Audio processing came to a dead stop. Fortunately, I was aware of the issue with denormals, and went straight to a solution that fixed it.<\/p>\n<p>Please read RCL&#8217;s excellent article on denormals, &#8220;<a href=\"http:\/\/rcl-rs-vvg.blogspot.com\/2012\/02\/denormal-floats-across-architectures.html\">Denormal floats across architectures<\/a>&#8220;, in which he goes into more detail, including comparisons of various CPU architectures.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One ugly thing that we need to be aware of, especially for filters, is the de-normalization of numbers. Basically, computer processors try to keep floating point numbers normalized\u2014they try to keep them in the binary form 1.xxxxxx (where each x &hellip; <a href=\"https:\/\/www.earlevel.com\/main\/2012\/12\/03\/a-note-about-de-normalization\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/posts\/147"}],"collection":[{"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/comments?post=147"}],"version-history":[{"count":2,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/posts\/147\/revisions"}],"predecessor-version":[{"id":828,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/posts\/147\/revisions\/828"}],"wp:attachment":[{"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/media?parent=147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/categories?post=147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/tags?post=147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}