8-1Embedded SystemsFixed-Point Math and Other OptimizationsEmbedded Systems 8-2Fixed Point Math – Why and HowFloating point is too slow and integers truncate the data– Floating point subroutines: slower than native, overhead of passing arguments, calling subroutines… simple fixed point routines can be in-linedBasic Idea: put the radixpoint where covers therange of numbers youneed to representI.F Terminology– I = number of integer bits– F= number of fraction bitsBit Pattern0000 00000001 11000110 0011Integer0/1 = 028/1 = 2899/1 = 996.20/4 = 028/4 = 799/4 = 24.751.100/1024 = 028/1024 = 0.0273…99/1024 = 0.0966…000000000Radix Point Locations1/321/161/8¼½12Weight2-52-42-32-22-12021Weight1101011BitRadix Point3.34375 in a fixed point binary representationEmbedded Systems 8-3Rules for Fixed Point MathAddition, Subtraction– Radix point stays where it started– …so we can treat fixed point numbers like integersMultiplication – Radix point moves left by F digits– … so we need to normalize result afterwards, shifting the result right by F digits+*10* 1.2512.56.2 Format 001010.00*000001.0100000000 1100.1000Embedded Systems 8-4DivisionDivision – Quotient is integer, may want to convert back to fixed point by shifting– Radix point doesn’t move in remainderQuotientRemainder÷7÷ 2313.1 Format 111.0÷ 010.00011001.03.2 Format 111.00÷ 010.0000011001.00DividendDivisorEmbedded Systems 8-5Division, Part IIDivision – To make quotient have same format as dividend and divisor, multiply dividend by 2F(shift left by F bits) – Quotient is in fixed point format nowQuotient÷7÷ 233.1 Format (7*2)1110.0÷ 010.00011.13.2 Format (7*4)11100.00÷ 010.0000011.10DividendDivisorEmbedded Systems 8-6Example Code for 12.4 Fixed Point MathRepresentation Converting to and from fixed point representation ! ! ! !"""""""" #$%#$%#$%#$% ! ! ! !&&&&&&&&Math '(()*'(()*'(()*'(()*+*+*+*+* ,-.)*,-.)*,-.)*,-.)*////**** 0-)*0-)*0-)*0-)**&&*&&*&&*&& ( 1)*( 1)*( 1)*( 1)*%*""%*""%*""%*"" 230)*230)*230)*230)*4*4*4*4*Embedded Systems 8-7More Fixed Point Math Examples*9.0625* 6.558.906254.4 Format 1001.0001*0110.10000011 1010.1110 1000*10+ 1.511.58.4 Format 0000 1010.0000+0000 0001.100000000000 1100.1000Embedded Systems 8-8Static RevisitedStatic variable– A local variable which retains its value between function invocations– Visible only within its module, so compiler/linker can allocate space more wisely (recall limited pointer offsets)Static function– Visible only within its module, so compiler knows who is calling the functions, – Compiler/linker can locate function to optimize calls (short call)– Compiler can also inline the function to reduce run-time and often code sizeEmbedded Systems 8-9Volatile and ConstVolatile variable– Value can be changed outside normal program flow• ISR, variable is actually a hardware register– Compiler reloads the variable from memory each time it is usedConst variable– const does not mean constant, but rather read-only– consts are implemented as real variables (taking space in RAM) or in ROM, requiring loading operations (often requiring pointer manipulation)• A #define value can be converted into an immediate operand, which is much faster• So avoid themConst function parameters– Allow compiler to optimize, as it knows a variable
View Full Document