8-1 Embedded Systems Fixed-Point Math and Other Optimizations.

Slides:



Advertisements
Similar presentations
1/1/ / faculty of Electrical Engineering eindhoven university of technology Introduction Part 2: Data types and addressing modes dr.ir. A.C. Verschueren.
Advertisements

Princess Sumaya Univ. Computer Engineering Dept. Chapter 3:
Princess Sumaya Univ. Computer Engineering Dept. Chapter 3: IT Students.
Floating Point Numbers
1 Lecture 9: Floating Point Today’s topics:  Division  IEEE 754 representations  FP arithmetic Reminder: assignment 4 will be posted later today.
Chapter 3 Arithmetic for Computers. Multiplication More complicated than addition accomplished via shifting and addition More time and more area Let's.
Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
CMPE12cCyrus Bazeghi 1 What do floating-point numbers represent? Rational numbers with non-repeating expansions in the given base within the specified.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
S. Barua – CPSC 440 CHAPTER 2 INSTRUCTIONS: LANGUAGE OF THE COMPUTER Goals – To get familiar with.
CHAPTER 5: Floating Point Numbers
1 Lecture 4: Arithmetic for Computers (Part 5) CS 447 Jason Bakos.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
Prepared by: Hind J. Zourob Heba M. Matter Supervisor: Dr. Hatem El-Aydi Faculty Of Engineering Communications & Control Engineering.
The Binary Number System
Data Representation Number Systems.
Arithmetic for Computers
Simple Data Type Representation and conversion of numbers
Computer Organization and Architecture Computer Arithmetic Chapter 9.
Computer Arithmetic Nizamettin AYDIN
Computer Arithmetic. Instruction Formats Layout of bits in an instruction Includes opcode Includes (implicit or explicit) operand(s) Usually more than.
1 Lecture 5 Floating Point Numbers ITEC 1000 “Introduction to Information Technology”
Fixed-Point Arithmetics: Part II
IT253: Computer Organization
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /14/2013 Lecture 16: Floating Point Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
CH09 Computer Arithmetic  CPU combines of ALU and Control Unit, this chapter discusses ALU The Arithmetic and Logic Unit (ALU) Number Systems Integer.
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Fixed and Floating Point Numbers Lesson 3 Ioan Despi.
Lecture 9: Floating Point
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Conversion to Larger Number of Bits Ex: Immediate Field (signed 16 bit) to 32 bit Positive numbers have implied 0’s to the left. So, put 16 bit number.
Lecture notes Reading: Section 3.4, 3.5, 3.6 Multiplication
Copyright © – Curt Hill Types What they do.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
Chapter 3 Arithmetic for Computers. Chapter 3 — Arithmetic for Computers — 2 Arithmetic for Computers Operations on integers Addition and subtraction.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Monday, January 14 Homework #1 is posted on the website Homework #1 is posted on the website Due before class, Jan. 16 Due before class, Jan. 16.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
CS 232: Computer Architecture II Prof. Laxmikant (Sanjay) Kale Floating point arithmetic.
Gator Engineering Project 1 Grades released Re-grading –Within one week –TA: Fardad, or office hours: MW 2:00 – 4:00 PM TA Huiyuan’s office hour.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
William Stallings Computer Organization and Architecture 8th Edition
CHAPTER 5: Representing Numerical Data
Secure Coding Rules for C++ Copyright © 2016 Curt Hill
Integer Division.
Instructor: David Ferry
Secure Coding Rules for C++ Copyright © Curt Hill
Floating Point Math, Fixed-Point Math and Other Optimizations
CSCE 350 Computer Architecture
The University of Adelaide, School of Computer Science
ECE232: Hardware Organization and Design
ECEG-3202 Computer Architecture and Organization
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Data Types and Expressions
Introduction to Java, and DrJava
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

8-1 Embedded Systems Fixed-Point Math and Other Optimizations

Embedded Systems8-2 Fixed 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 the range of numbers you need to represent I.F Terminology –I = number of integer bits –F= number of fraction bits Bit Pattern Integer 0/1 = 0 28/1 = 28 99/1 = /4 = 0 28/4 = 7 99/4 = /1024 = 0 28/1024 = … 99/1024 = … 000 Radix Point Locations Bit Weight Weight21½¼1/81/161/32 Radix Point in a fixed point binary representation

Embedded Systems8-3 Rules for Fixed Point Math Addition, 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 * Format *

Embedded Systems8-4 Division –Quotient is integer, may want to convert back to fixed point by shifting –Radix point doesn’t move in remainder Quotient Remainder ÷ 7 ÷ Format ÷ Format ÷ Dividend Divisor

Embedded Systems8-5 Division, Part II Division –To make quotient have same format as dividend and divisor, multiply dividend by 2 F (shift left by F bits) –Quotient is in fixed point format now Quotient ÷ 7 ÷ Format (7*2) ÷ Format (7*4) ÷ Dividend Divisor

Embedded Systems8-6 Example 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))

Embedded Systems8-7 More Fixed Point Math Examples * * Format * * Format

Embedded Systems8-8 Static Revisited 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 size

Embedded Systems8-9 Volatile 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 function

Embedded Systems8-10 More 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 register

Embedded Systems8-11 Starting 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.

Embedded Systems8-12 Floating 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 , 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 Systems8-13 Floating-Point Specifier Example No “f” float res = val / 10.0; Assembler: move.l -4(a6),-(sp) jsr __ftod //SP to DP clr.l -(sp) move.l # ,- (sp) jsr __ddiv //DP divide jsr __dtof //DP to SP move.l (s)+,-8(a6) “f” float res = val / 10.0 f; Assembler: move.l -4(a6),-(sp) move.l # ,- (sp) jsr __fdiv move.l (s)+,-8(a6)

Embedded Systems8-14 Automatic 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 satisfied

Embedded Systems8-15 Automatic Promotions Example Automatic float 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 result two DP loads, DP multiply, DP divide, DP to SP conversion Casting to avoid promotion float 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 result two SP loads, SP multiply, SP divide

Embedded Systems8-16 Rewriting and Rearranging Expressions Divides take much longer to execute than multiplies. Branches taken are usually faster than those not taken. Repeated evaluation of same expression is a waste of time.

Embedded Systems8-17 Examples float res = ( 17.0f*(float)(sqrt(val)))/10.0f; is better written as: float res = 1.7f*(float)(sqrt(val)); D = A / B / C; as: D = A / (B*C); D = A / (B * C); as: bc = B * C ; E = 1/(1+(B*C)); D = A / bc; E = 1/(1+bc);

Embedded Systems8-18 Algebraic Simplifications and the Laws of Exponents Original ExpressionOptimized Expression a 2 – 3a + 2 2*, 1-, 1+ (a – 1) * (a – 2) 1*, 2- (a - 1)*(a + 1) 1*, 1-, 1+ a *, 1- 1/(1+a/b) 2/, 1+ b/(b+a) 1/, 1+ a m * a n 2 ^, 1* a m+n 1^, 1+ (a m ) n 2 ^ a m*n 1^, 1*

Embedded Systems8-19 Literal Definitions #defines are prone to error float c, r; r = c/TWO_PI; #define TWO_PI 2 * 3.14 –r = c/2*3.14 is wrong! Evaluates to r = (c/2) * 3.14 #define TWO_PI (2*3.14) –Avoids a divide error –However, 2*3.14 is loaded as DP leading to extra operations convert c to DP DP divide convert quotient to SP #define TWO_PI (float)(2.0*3.14) –Avoids extra operations

Embedded Systems8-20 The Standard Math Library Revisited Double precision is likely expected by the standard math library. Look for workarounds: –abs() float output=fabs(input); could be written as: if(input<0) output=-input; else output=input ;

Embedded Systems8-21 Functions: Parameters and Variables Consider a function which uses a global variable and calls other functions –Compiler needs to save the variable before the call, in case it is used by the called function, reducing performance –By loading the global into a local variable, it may be promoted into a register by the register allocator, resulting in better performance Can only do this if the called functions don’t use the global Taking the address of a local variable makes it more difficult or impossible to promote it into a register

Embedded Systems8-22 More about Functions Prototype your functions, or else the compiler may promote all arguments to ints or doubles! Group function calls together, since they force global variables to be written back to memory Make local functions static, keep in same module (source file) –Allows more aggressive function inlining –Only functions in same module can be inlined