DOC PREVIEW
UNCC ECGR 4101 - Fixed Point Math

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

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 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 23 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 23 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 23 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Fixed-Point Math and Other OptimizationsAnnouncementsFixed 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 FunctionsFixed-Point Math Fixed-Point Math and and Other OptimizationsOther OptimizationsLecture 23AnnouncementsAnnouncements•Final Exam – Tuesday December 14, 1-4 PM, in this room•One-page cheat sheet allowed•Wolfware submission of Lab 4 allowed by midnight Monday•HW6 will be returned today at end of class•Exam hintsFixed Point Math – Why and HowFixed Point Math – Why and How•Floating 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-lined•Basic Idea: put the radix point where covers therange of numbers youneed to represent•I.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 representationRules for Fixed Point MathRules for Fixed Point MathAddition, Subtraction–Radix point stays where it started–…so we can treat fixed point numbers like integers•Multiplication –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.1000DivisionDivision•Division –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.00DividendDivisorDivision, Part IIDivision, Part II•Division –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.10DividendDivisorExample Code for 12.4 Fixed Point MathExample Code for 12.4 Fixed Point Math•Representation–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))More Fixed Point Math ExamplesMore 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.1000Static Static RevisitedRevisited•Static 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 sizeVolatile and ConstVolatile and Const•Volatile 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 used•Const 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 them•Const function parameters–Allow compiler to optimize, as it knows a variable passed as a parameter hasn’t been changed by the functionMoreMore•Const 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 registerStarting Points for Efficient CodeStarting Points for Efficient Code•Write 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.Floating Point Data Type SpecificationsFloating Point Data Type Specifications•Use 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 more•Single-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”Floating-Point Specifier ExampleFloating-Point Specifier Example•No “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)Automatic PromotionsAutomatic Promotions•Standard 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 satisfiedAutomatic Promotions ExampleAutomatic Promotions Example•Automaticfloat res = (17.0f*sqrt(val)) / 10.0f;// load val// convert to DP// _sqrt() on DP returns DP// load DP of 17.0// DP


View Full Document

UNCC ECGR 4101 - Fixed Point Math

Documents in this Course
Load more
Download Fixed Point Math
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 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 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?