Chapter 4 NUMB3RS.

Slides:



Advertisements
Similar presentations
Chapter 3 Message in a Bottle Programming Loops in C.
Advertisements

Chapter 2 Walking in Circles Programming Loops in C.
Chapter 4 - NUMB3RS Learning to chose the right “types” Exploring The PIC32 - Lucio Di Jasio.
Arithmetic in Computers Chapter 4 Arithmetic in Computers2 Outline Data representation integers Unsigned integers Signed integers Floating-points.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Chapter 6 Under the Hood. Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Checklist The following tools will be used in this lesson:
COMP3221: Microprocessors and Embedded Systems Lecture 14: Floating Point Numbers Lecturer: Hui Wu Session 2, 2004.
1 Lecture 9: Floating Point Today’s topics:  Division  IEEE 754 representations  FP arithmetic Reminder: assignment 4 will be posted later today.
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter 2 Data Types, Declarations, and Displays
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
Chapter 1 The First Flight Creating the first project and saying “Hello to the World”
Chapter 5 Interrupts.
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
Computer Arithmetic. Instruction Formats Layout of bits in an instruction Includes opcode Includes (implicit or explicit) operand(s) Usually more than.
Chapter 3 More Loops. Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Checklist The following tools will be used in this lesson:
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
Number Systems So far we have studied the following integer number systems in computer Unsigned numbers Sign/magnitude numbers Two’s complement numbers.
Chapter 2 A Loop in the Pattern Designing the Main Loop and Timing.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /14/2013 Lecture 16: Floating Point Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
Lecture 9: Floating Point
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
EEE527 Embedded Systems Lecture 2: Chapter 2: C and Using more MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: voice mail on 6 th ring
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
1 ELEN 033 Lecture 4 Chapter 4 of Text (COD2E) Chapters 3 and 4 of Goodman and Miller book.
Renesas Technology America Inc. 1 SKP8CMINI Tutorial 2 Creating A New Project Using HEW.
Chapter 5 - Interrupts.
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.
Department of Electronic & Electrical Engineering Introduction to microcontrollers A microcontroller is a small computer on a single integrated circuit.
순천향대학교 정보기술공학부 이 상 정 1 3. Arithmetic for Computers.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Georgia Institute of Technology Introduction to Java, and DrJava part 1 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
William Stallings Computer Organization and Architecture 8th Edition
Variables, Operators, and Expressions
Computer Architecture & Operations I
Microcontroller Applications
Lecture 9: Floating Point
Topics IEEE Floating Point Standard Rounding Floating Point Operations
Tokens in C Keywords Identifiers Constants
Revision Lecture
L7 – Assembler Directives
Arithmetic for Computers
Type Conversion, Constants, and the String Object
Lecture 10: Floating Point, Digital Design
Fundamental Data Types
Introduction to Java, and DrJava part 1
Computer Arithmetic Multiplication, Floating Point
Lectures on Numerical Methods
Introduction to Java, and DrJava
Fundamental Data Types
Introduction to Java, and DrJava
Introduction to Java, and DrJava part 1
Variables and Constants
Presentation transcript:

Chapter 4 NUMB3RS

Checklist The following tools will be used in this lesson: MPLAB X, Integrated Development Environment (v1.8 or later, free) MPLAB XC16, C compiler (v1.11 or later, free) The following pieces of documentation will be used during this lesson: PIC24FJ128GA010 Datasheet –DS39747 (latest rev.) PIC24 Family Reference Manual - Section 14. Timers Make sure they are available and/or installed and ready to use on your computer. You can download them from Microchip web site at: http://www.microchip.com/mplabx And http://www.microchip.com/xc16

Integer Data Types

Under the Hood Type the following few lines of code in a new main.c file: unsigned int i,j,k;   main () { i = 0x1234; // assign an initial value to i j = 0x5678; // assign an initial value to j k = i * j; // perform product and store the result in k } Build the project and open the Window>Embedded Memory>Program window to inspect the code produced: i = 0x1234; 212340 MOV #0x1234, W0 // move literal to W0 884280 MOV W0, 0x400 // move from W0 to i j = 0x5678; 256780 MOV #0x5678, W0 // move literal to W0 884290 MOV W0, 0x401 // move from W0 to j k = i * j; 804281 MOV 0x400, W1 // move from i to W1 804290 MOV 0x401, W0 // move from j to W0 B98800 MUL.SS W1, W0, W0 8842A0 MOV W0, 0x402 // move result to k

Going “long” Try now with long (32-bit) integer values unsigned long i,j,k; main () { i = 0x01234567L; // assign an initial value to i j = 0x89ABCDEFL; // assign an initial value to j k = i * j; // perform product and store the result in k } And inspect the new code produced for the multiplication: k = i * j; 804038 MOV 0x403, W8 804049 MOV 0x404, W9 804056 MOV 0x405, W6 804067 MOV 0x406, W7 780088 MOV W8, W1 780006 MOV W6, W0 B80A00 MUL.UU W1, W0, W4 B9C007 MUL.SS W8, W7, W0 780105 MOV W5, W2 410100 ADD W2, W0, W2 B9B009 MUL.SS W6, W9, W0 780282 MOV W2, W5 884074 MOV W4, 0x407 884085 MOV W5, 0x408

“long long” Multiplications Finally let’s try long long (64-bit) integer values unsigned long long i,j,k; main () { i = 0x0123456789ABCDEFLL; // assign an initial value to i j = 0xFEDCBA9876543210LL; // assign an initial value to j k = i * j; // perform product and store the result in k } And inspect the new code produced for the multiplication: k = i * j; 07FDFC RCALL 0x290

Floating Point Types Notice how the MPLAB XC16 compiler, by default, allocates for both the float and the double types the same number of bits (32), using the single precision floating-point format defined in the IEEE754 standard. Only the long double data type is treated as a true double precision IEEE754 floating-point type (64-bit).

Notes for C Experts If porting existing C code from a PC or workstation project, you might want to force the XC16 compiler to use the more standard (true) definition of a double as a 64-bit integer. You can do so by accessing the Project Configuration Dialog and selecting the “Use 64-bit double” option!

Measuring Integer Performance /* * Numb3rs.c */ #include <config.h> int i1, i2, i3; long x1, x2, x3; long long xx1, xx2, xx3; main() { T1CON = 0x8000; // enable Timer1 1:1 with main clock i1 = 0x1234; // testing integers (16-bit) i2 = 0x5678; TMR1 = 0; // clear the timer i3 = i1 * i2; x1 = 0x01234567L; // testing long integers (32-bit) x2 = 0x89ABCDEFL; x3 = x1 * x2; xx1 = 0x0123456789ABCDEFLL; // testing 64-bit integers xx2 = 0xFEDCBA9876543210LL; xx3 = xx1 * xx2; } // main Single Step, then read TMR1 Single Step, then read TMR1 Single Step, then read TMR1

Measuring FP Performance /* * Numb3rs.c */ #include <config.h> float f1,f2, f3; long double d1, d2, d3; main() { T1CON = 0x8000; // enable Timer1 1:1 with main clock f1 = 12.34; // testing single precision floating point f2 = 56.78; TMR1 = 0; // clear the timer f3 = f1 * f2; d1 = 12.34L; // testing double precision floating point d2 = 56.78L; d3 = d1 * d2; } // main Single Step, then read TMR1 Single Step, then read TMR1

Measuring Performance Summary

Lessons Learned Use integers every time you can (i.e. when fractions are not required, or the algorithm can be re-written for integer arithmetic). Use the smallest integer type that will not produce an overflow or underflow. If you have to use a floating-point type (fractions are required), expect an order-of-magnitude reduction in the performance of the compiled program. Double precision floating point (long double) seems to only reduce the performance by a further factor of two.

Notes for the Assembly Experts Converting an integer type into a smaller/larger one: int x; // 16-bit long y; // 32-bit y = x; // implicit conversion Explicity Conversion using a “type cast”: int x; // 16-bit long y; // 32-bit x = (int) y; // type cast Extracting or manipulating one bit out of an integer variable, use “bitfields” Example from the PIC24 peripheral libraries:  extern unsigned int T1CON; extern union { struct { unsigned :1; unsigned TCS:1; ... unsigned TSIDL:1; unsigned TON:1; }; unsigned :4; unsigned TCKPS:2; } T1CONbits;  

Notes for the PIC MCU Experts 8-bit users will notice a considerable improvement in the performance both with integer arithmetic and floating point arithmetic. The 16-bit ALU available in the PIC24 architecture manipulates twice the number of bits per cycle Further improvement is due to the use of 8 working registers which make the coding of critical arithmetic routines and makes numerical algorithms more efficient

Tips and Tricks The MPLAB C compiler supports several standard ANSI C libraries including: limits.h - contains many useful macros defining implementation dependent limits, such as, for example, the number of bits composing a char type (CHAR_BIT) or the largest integer value (INT_MAX). float.h - contains similar implementation dependent limits for floating point data types, such as, for example the largest exponent for a single precision floating point variable (FLT_MAX_EXP). math.h - contains trigonometric functions, rounding functions, logarithms and exponentials.

Tips and Tricks Complex Data Types The XC16 compiler supports complex data types as an extension of both integer and floating point types. Here is an example declaration for a single precision floating point type: __complex__ float z; Notice the use of a double underscore before and after the keyword complex. The variable z defined so has now a real and an imaginary part that can be individually addressed using the syntax: __real__ z and __imag__ z respectively. Similarly, the next declaration produces a complex variable of 16-bit integer type: __complex__ int x; Complex constants are easily created adding the suffix i or j as in the following examples: x = 2 + 3j; z = 2.0f + 3.0fj; All standard arithmetic operations (+,-,*,/) are performed correctly on complex data types. Additionally the “~” operator produces the complex conjugate.

Suggested Excercises Write a program that uses Timer2 and Timer3 joined in the new 32-bit timer mode. Test the relative performance of the division for the various data types. Test the performance of the trigonometric functions relative to standard arithmetic operations. Test the relative performance of the multiplication for complex data types.

Recommended Readings Gahlinger, P. M. (2000), The cockpit, a flight of escape and discovery, Sagebrush Press, Salt Lake City, UT It’s an interesting journey around the world. Follow the author in search of ... his soul. Every instrument in the cockpit triggers a memory and starts a new chapter.

Online Resources http://en.wikipedia.org/wiki/Taylor_series If you are curious as to how the C compiler can approximate some of the functions in the math library.