DOC PREVIEW
UNCC ECGR 6185 - Fixed Point Math and Other Optimizations

This preview shows page 1-2-21-22 out of 22 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Fixed-Point Math and Other OptimizationsFixed Point Math – Why and HowRules for Fixed Point MathDivisionDivision, Part IIExample Code for 12.4 Fixed Point MathMore Fixed Point Math ExamplesStatic RevisitedVolatile and ConstMoreStarting Points for Efficient CodeFloating Point Data Type SpecificationsFloating-Point Specifier ExampleAutomatic PromotionsAutomatic Promotions ExampleRewriting and Rearranging ExpressionsExamplesAlgebraic Simplifications and the Laws of ExponentsLiteral DefinitionsThe Standard Math Library RevisitedFunctions: Parameters and VariablesMore about Functions8-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 radix point where covers therange of numbers youneed to representI.F Terminology–I = number of integer bits–F= number of fraction bitsBit Pattern0000 00000001 11000110 0011Integer 0/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 LocationsBit 1 1 0 1 0 1 1Weight 21202-12-22-32-42-5Weight 2 1 ½ ¼ 1/8 1/16 1/32Radix 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–typedef unsigned int FX_12_4;Converting to and from fixed point representation–#define FL_TO_FX(a) (unsigned int)(a*16.0)–#define INT_TO_FX(a) (a<<4)–#define FX_TO_FL(a) (float)(a/16.0)–#define FX_TO_INT(a) (unsigned int)(a>>4)Math–#define FX_ADD(a,b) (a+b) –#define FX_SUB(a,b) (a-b)–#define FX_MUL(a,b) ((a*b)>>4)–#define FX_DIV(a,b) ((a/b)<<4)–#define FX_REM(a,b) ((a%b))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 passed as a parameter hasn’t been changed by the functionEmbedded Systems 8-10MoreConst Volatile Variables–Yes, it’s possible•Volatile: A memory location that can change unexpectedly•Const: it is only read by the program–Example: hardware status registerEmbedded Systems 8-11Starting Points for Efficient CodeWrite correct code first, optimize second.Use a top-down approach.Know your microprocessors’ architecture, compiler (features and also object model used), and programming language.Leave assembly language for unported designs, interrupt service routines, and frequently used functions.Embedded Systems 8-12Floating Point Data Type SpecificationsUse the smallest adequate data type, or else…–Conversions without an FPU are very slow–Extra space is used–C standard allows compiler or preprocessor to convert automatically, slowing down code moreSingle-precision (SP) vs. double-precision (DP)–ANSI/IEEE 754-1985, Standard for Binary Floating Point Arithmetic•Single precision: 32 bits–1 sign bit, 8 exponent bits, 23 fraction bits•Double precision–1 sign bit, 11 exponent bits, 52 fraction bits–Single-precision is likely all that is needed–Use single-precision floating point specifier “f”Embedded Systems 8-13Floating-Point Specifier ExampleNo “f”float res = val / 10.0; Assembler:move.l -4(a6),-(sp)jsr __ftod //SP to DPclr.l -(sp)move.l #1076101120,-(sp)jsr __ddiv //DP dividejsr __dtof //DP to SPmove.l (s)+,-8(a6)“f” float res = val / 10.0 f; Assembler:move.l -4(a6),-(sp)move.l #1076101120,-(sp)jsr __fdivmove.l (s)+,-8(a6)Embedded Systems 8-14Automatic PromotionsStandard math routines usually accept double-precision inputs and return double-precision outputs.Again it is likely only single-precision is needed.–Cast to single-precision if accuracy and overflow conditions are satisfiedEmbedded Systems 8-15Automatic Promotions ExampleAutomaticfloat res = (17.0f*sqrt(val)) / 10.0f;// load val// convert to DP// _sqrt() on DP returns DP// load DP of 17.0// DP multiply// load DP of 10.0// DP divide// convert DP to SP// save resulttwo DP loads, DP multiply, DP divide, DP to SP conversionCasting to avoid promotionfloat res = (17.0f*(float)(sqrt(val)))/10.0f;// load val// convert to DP// _sqrt() on DP returns DP// convert result to SP// load SP of 17.0// SP multiply// load SP of 10.0// SP divide// save resulttwo SP loads, SP multiply, SP divideEmbedded Systems


View Full Document

UNCC ECGR 6185 - Fixed Point Math and Other Optimizations

Documents in this Course
Zigbee

Zigbee

33 pages

Load more
Download Fixed Point Math and Other Optimizations
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Fixed Point Math and Other Optimizations and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Fixed Point Math and Other Optimizations 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?