原创 Using fixed point math

2015-10-17 20:39 2017 20 21 分类: 消费电子

Colin Walls recently had an article on this site about floating point math. Once it was common for embedded engineers to scoff at floats; many have told me they have no place in this space. That’s simply wrong. The very first embedded program I wrote in 1972 or so used an 8008 to control a near-infrared instrument. Even though we were limited to 4KB of EPROM (it was incredibly expensive then), all of the math had to be in floating point using a library written by one Cal Ohne. Amazingly, he crammed that into just 1KB of program memory. And that was on an 8 bitter with a ferociously-bad stack and very limited instruction set.

 

Today even MCUs sometimes have on-board floating point hardware. ST’s STM32F4 parts, for instance, have this feature and some are under four bucks in 1000 piece lots.

 

But most working in the microcontroller space don’t have hardware floating point, so have to use a software solution, which is slow and consumes a lot of memory.

 

We use integers because they are fast and convenient. But the dynamic range is limited and fractional arithmetic impossible. Floats give us enormous ranges but suffer from performance. An alternative, well known to DSP developers, is fixed point math.

 

Integers, of course, look like this (ignoring a sign bit):

 

Can you see the binary point? It’s not shown, but there is one all the way to the right of the 20 bit.

 

Suppose we move that binary point four bits to the left. Now, in the same 16 bit word, the format looks like this:

 

The number stored in this format is the 16 bit integer value times 2-4. So if the word looks like 11528 (decimal) it’s really 720.5, because 720.5= 11528 x 2-4.

 

Obviously, we lose some range; the biggest number expressible is smaller than devoting those 16 bits to ints. But we gain precision.

 

Here’s where the magic comes in: to add two fixed point numbers one just does a normal integer addition. Multiplication is little more than multiplies plus shifting. Yes, there are some special cases one must watch for, but math is very fast compared to floats. So if you need fractional values and speed, fixed point might be your new best friend. (These algorithms are well documented and not worth repeating here, but two references are Embedded Systems Building Blocks by Jean LaBrosse and Fixed Point Math in C by Joe Lemieux.

 

There’s no rule about where the binary point should be; you select a precision that works for your application. “Q notation” is a standardized way of talking about where the binary point is located. Qf means there are f fractional bits (e.g., Q5 means 5 bits to the right of the binary point). Qn.f tells us there are n bits to the left of the binary point and f to the right.

 

Integers are wonderful: they are easy to understand and very fast. Floats add huge ranges and fractional values, but may be costly in CPU cycles. Sometimes fixed point is just the ticket when one needs a compromise between the other two formats. It’s not unusual to see them used in computationally-demanding control applications, like PID loops. Add them to your tool kit!

文章评论1条评论)

登录后参与讨论

用户1406868 2016-5-2 10:57

Felt so hopeless looking for answers to my quoeuisns...tntil now.

用户1366145 2015-5-11 10:13

很有启发

用户1366145 2015-5-11 10:09

看似搞笑。

用户1454308 2015-5-11 09:03

Good

用户1229643 2015-5-11 07:53

革新

用户1406868 2015-5-8 17:22

以后机器人会不会越来越接近真人?

自做自受 2015-5-8 16:04

呵呵~~难怪当初神——太阳系中第十二个星体来人就是开采黄金......后来累了,就研发制造了原始人工人代替他们劳动,并一举两得,带进家门(伊甸园)寻欢作乐......后来原始人成为智人......到现在,智人也累了,开始重复神一样的工作——制造机器人以替代劳动,并也在开始和机器人寻欢作乐...... 有意思,也神秘。
相关推荐阅读
用户3671694 2016-04-18 17:49
What would you change about C?
If you’re an old-timer you’ve most likely written code in a large number of languages that have ma...
用户3671694 2016-04-18 17:33
A look at a new embedded heap manager
Many of us don’t give much thought about the math our compilers do. Toss off a call to a sine func...
用户3671694 2016-04-15 17:12
Why names are critical
The Linux printk function has various logging levels, which include KERN_EMERG, KERN_ERR and other...
用户3671694 2016-03-14 19:02
What do you think of ultra-low power watchdogs?
I have written extensively about designing ultra-low power systems that operate from coin cells. U...
用户3671694 2016-02-26 21:58
Comment headers: The best and the worst
I read a great deal of code. The vast majority is in C with some C++ and a bit of assembly sprinkl...
用户3671694 2016-02-12 17:58
What's your take on knobs?
In a recent Embedded Muse Richard Wall reviews the latest version of Digilent’s Analog Discovery U...
我要评论
1
20
关闭 站长推荐上一条 /2 下一条