Embedded Systems Building Blocks, Second Edition

Most low-end microprocessors (typical of embedded processors) do not provide hardware-assisted floating-point math. Microprocessor manufacturers unfortunately seem to feel that floating-point math is not very important in embedded systems. This has not been my experience. Fortunately, ANSI C compilers allow you to use floating-point math but at a cost; floating-point libraries require extra ROM and RAM but most importantly, they require more processing time than integer math. For example, floating-point addition could take hundreds of microseconds on a low-end, 8-bit microprocessor, whereas it typically takes only a few microseconds to perform a 16-bit integer addition. Multiplications and especially divisions are even worse. As an embedded system programmer, you are often confronted with the task of writing the fastest and smallest possible code for real-time operations. This chapter will show you how to perform basic arithmetic operations on fractional numbers by using only integers. In other words, this chapter will answer the questions: "Without using floating-point arithmetic, how would you add 12.34 and 987.654, multiply 3.1416 by 5.4, or divide 0.00456 by 98.7?"
Throughout this chapter, I will be using 16-bit integers, but most of the concepts presented here apply to any integer size. This chapter will show you how to use the concept of fixed-point math to get the most out of integer arithmetic. Chapter 10 will make use of the information presented in this chapter.
Fixed-point is an alternative form for expressing numerical values. Fixed-point math is integer math, but because it allows fractions,...