Fixed-Point Math and Other Optimizations

Similar documents
Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

Divide: Paper & Pencil

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3

Irecently had an assignment in

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

Number Systems and Computer Arithmetic

Number Systems. Both numbers are positive

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

CHW 261: Logic Design

ECE260: Fundamentals of Computer Engineering

Kinds Of Data CHAPTER 3 DATA REPRESENTATION. Numbers Are Different! Positional Number Systems. Text. Numbers. Other

Chapter 4 Section 2 Operations on Decimals

Arithmetic and Bitwise Operations on Binary Data

Lecture 12 Integers. Computer and Network Security 19th of December Computer Science and Engineering Department

COMPUTER ARITHMETIC (Part 1)

CPS 104 Computer Organization and Programming

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Math 1 Variable Manipulation Part 2 Exponents & Roots

Introduction to C. Why C? Difference between Python and C C compiler stages Basic syntax in C

The. Binary. Number System

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

COMPUTER ARCHITECTURE AND ORGANIZATION. Operation Add Magnitudes Subtract Magnitudes (+A) + ( B) + (A B) (B A) + (A B)

Chapter 4. Operations on Data

Computer Arithmetic Ch 8

Computer Arithmetic Ch 8

September, Saeid Nooshabadi. Review: Floating Point Representation COMP Single Precision and Double Precision

Organisasi Sistem Komputer

Module 2: Computer Arithmetic

More about Binary 9/6/2016

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Chapter 5 : Computer Arithmetic

Special Topics for Embedded Programming

Computer Arithmetic. Appendix A Fall 2003 Lec.03-58

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Thomas Polzer Institut für Technische Informatik

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Floating Point January 24, 2008

Bits, Bytes, and Integers Part 2

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Computer Organisation CS303

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

UNIT- 3 Introduction to C++

Learning Log Title: CHAPTER 3: ARITHMETIC PROPERTIES. Date: Lesson: Chapter 3: Arithmetic Properties

CPE300: Digital System Architecture and Design

2. Define Instruction Set Architecture. What are its two main characteristics? Be precise!

Bits, Bytes, and Integers

ME 461 C review Session Fall 2009 S. Keres

CPE300: Digital System Architecture and Design

Programming in C++ 5. Integral data types

Numbering systems. Dr Abu Arqoub

The CPU and Memory. How does a computer work? How does a computer interact with data? How are instructions performed? Recall schematic diagram:

CS313D: ADVANCED PROGRAMMING LANGUAGE

Manipulating Integers

Timing for Ripple Carry Adder

COMPUTER ORGANIZATION AND. Edition. The Hardware/Software Interface. Chapter 3. Arithmetic for Computers

Compiler Optimization

Internal Data Representation

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Lecture 4 - Number Representations, DSK Hardware, Assembly Programming

Lesson 1: Arithmetic Review

Math in MIPS. Subtracting a binary number from another binary number also bears an uncanny resemblance to the way it s done in decimal.

Objective Simplify expressions using the properties of exponents.

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Floating Point. The World is Not Just Integers. Programming languages support numbers with fraction

CHAPTER 2 Data Representation in Computer Systems

Systems I. Floating Point. Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties

CHAPTER 2 Data Representation in Computer Systems

Operators in C. Staff Incharge: S.Sasirekha

Characters, Strings, and Floats

CHAPTER 5: Representing Numerical Data

CIS133J. Working with Numbers in Java

PESIT Bangalore South Campus

Floating Point Puzzles The course that gives CMU its Zip! Floating Point Jan 22, IEEE Floating Point. Fractional Binary Numbers.

Easy Accurate Reading and Writing of Floating-Point Numbers. Aubrey Jaffer 1. August 2018

Python Numbers. Learning Outcomes 9/19/2012. CMSC 201 Fall 2012 Instructor: John Park Lecture Section 01 Discussion Sections 02-08, 16, 17

Experiment Objectives. 2. Preparation. 3. Tasks. 3.1 Task A: String to Integer Conversion

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Arithmetic type issues

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

At the ith stage: Input: ci is the carry-in Output: si is the sum ci+1 carry-out to (i+1)st state

Computer Systems A Programmer s Perspective 1 (Beta Draft)

Digital Fundamentals. CHAPTER 2 Number Systems, Operations, and Codes

EC 413 Computer Organization

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

ECE232: Hardware Organization and Design

EE 486 Winter The role of arithmetic. EE 486 : lecture 1, the integers. SIA Roadmap - 2. SIA Roadmap - 1

Floating Point Arithmetic

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Floating Point Arithmetic

Unit 3. Operators. School of Science and Technology INTRODUCTION

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

Lecture 13: (Integer Multiplication and Division) FLOATING POINT NUMBERS

Arithmetic Logic Unit

9 Multiplication and Division

Transcription:

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

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 0000000 0000 0000001 1100 0000110 0011 Integer 0/1 = 0 28/1 = 28 99/1 = 99 3.34375 in a fixed point binary representation Bit Weight Weight 1 2 1 2 1 2 0 1 0 2-1 ½ Radix Point 6.2 0/4 = 0 28/4 = 7 99/4 = 24.75 1 2-2 ¼ 0 2-3 1/8 2-4 1/16 2-5 1/32 1.10 0/1024 = 0 28/1024 = 0.0273 99/1024 = 0.0966 1 1 Radix Point Locations Embedded Systems 8-2

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 * 1.25 12.5 6.2 Format 001010.00 *000001.01 00000000 1100.1000 Embedded Systems 8-3

Division Dividend Divisor Quotient Remainder 7 2 3 1 3.1 Format 111.0 010.0 0011 001.0 3.2 Format 111.00 010.00 00011 001.00 Division Quotient is integer, may want to convert back to fixed point by shifting Radix point doesn t move in remainder Embedded Systems 8-4

Division, Part II Dividend Divisor Quotient 7 2 3 3.1 Format (7*2)1110.0 010.0 0011.1 3.2 Format (7*4)11100.00 010.00 00011.10 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 Embedded Systems 8-5

Example Code for 12.4 Fixed Point Math Representation Converting to and from fixed point representation Math!"" #$%! && '(()* '(()* +* +*,-.)*,-.)* /* 0-)* 0-)**&& ( 1)*%*"" 230)* 230)* 4* 4* Embedded Systems 8-6

More Fixed Point Math Examples * 10 + 1.5 11.5 8.4 Format 0000 1010.0000 +0000 0001.1000 00000000 1100.1000 * 9.0625 * 6.5 58.90625 4.4 Format 1001.0001 *0110.1000 0011 1010.1110 1000 Embedded Systems 8-7

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 Systems 8-8

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 Systems 8-9

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 Systems 8-10

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 Systems 8-11

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 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-12

Floating-Point Specifier Example No f f Assembler:!" # ## $% Assembler:!" # $% Embedded Systems 8-13

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 Systems 8-14

Automatic Promotions Example Automatic!& ' ( '()(! )#* ## ( ) Casting to avoid promotion!& ' ( '()( ( )! )#* ## ) + )#*## ( #( + )#*## Embedded Systems 8-15

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 Systems 8-16

Examples!& ' is better written as:!& ',-. as:,-&.,-&. as: /-&.; 0$-&.,/ 0$/ Embedded Systems 8-17

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

Literal Definitions #defines are prone to error 1234 #(1234"&5 r = c/2*3.14 is wrong! Evaluates to r = (c/2) * 3.14 #(1234"&5 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 #(1234"&5 Avoids extra operations Embedded Systems 8-19

The Standard Math Library Revisited Double precision is likely expected by the standard math library. Look for workarounds: abs() ))/ #() could be written as: ##()6 ))#() ))#() Embedded Systems 8-20

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 Systems 8-21

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 Embedded Systems 8-22