Chapter 4 - NUMB3RS Learning to chose the right “types” Exploring The PIC32 - Lucio Di Jasio
Integer Types
Exploring The PIC32 - Lucio Di Jasio Long Multiplication (32-bit) main () { int i,j,k; i = 1234; // assign an initial value to i j = 5678; // assign an initial value to j k = i * j; // multiply and store the result in k } 12: i = 1234; 9D00000C D2 addiu v0,zero,1234 9D AFC20000 sw v0,0(s8) 13: j = 5678; 9D E addiu v0,zero,5678 9D AFC20004 sw v0,4(s8) 14: k = i*j; 9D00001C 8FC30000 lw v1,0(s8) 9D FC20004 lw v0,4(s8) 9D mul v0,v1,v0 9D AFC20008 sw v0,8(s8)
Exploring The PIC32 - Lucio Di Jasio Long Long Multiplication (64-bit) main () { long long i,j,k; i = 1234; // assign an initial value to i j = 5678; // assign an initial value to j k = i * j; // multiply and store the result in k } 15: k = i*j; 9D00002C 8FC30000 lw v1,0(s8) 9D FC20008 lw v0,8(s8) 9D multu v1,v0 9D mflo a0 9D00003C mfhi a1 9D FC30000 lw v1,0(s8) 9D FC2000C lw v0,12(s8) 9D mul v1,v1,v0 9D00004C 00A01021 addu v0,a1,zero 9D addu v0,v0,v1 9D FC60008 lw a2,8(s8) 9D FC30004 lw v1,4(s8) 9D00005C 70C31802 mul v1,a2,v1 9D addu v0,v0,v1 9D addu a1,v0,zero 9D AFC40010 sw a0,16(s8) 9D00006C AFC50014 sw a1,20(s8)
Exploring The PIC32 - Lucio Di Jasio Long Integer Division main () { int i, j, k; i = 1234;// assign an initial value to i j = 5678;// assign an initial value to j k = i / j; // divide and store the result in k } // main 15: k = i/j; 9D00001C 8FC30000 lw v1,0(s8) 9D FC20004 lw v0,4(s8) 9D A div v1,v0 9D F4 teq v0,zero 9D00002C mflo v0 9D AFC20008 sw v0,8(s8)
Exploring The PIC32 - Lucio Di Jasio Long Long Integer Division main () { int i, j, k; i = 1234;// assign an initial value to i j = 5678;// assign an initial value to j k = i / j; // divide and store the result in k } // main 15: k = i/j; 9D FC40010 lw a0,16(s8) 9D FC50014 lw a1,20(s8) 9D FC60018 lw a2,24(s8) 9D00003C 8FC7001C lw a3,28(s8) 9D F40001A jal 0x9d D nop 9D AFC20020 sw v0,32(s8) 9D00004C AFC30024 sw v1,36(s8)
Exploring The PIC32 - Lucio Di Jasio Floating Point Types
Exploring The PIC32 - Lucio Di Jasio The Simulator Stopwatch
Exploring The PIC32 - Lucio Di Jasio Multiplication Test Results Multiplication TestWidthCycle Count Performance relative to (Bits) Intfloat Char Integer ( char ) 861- Short Integer ( short ) Integer ( int, long ) Long Integer ( long long ) Single Precision FP ( float, double ) Double Precision FP ( long double )
Exploring The PIC32 - Lucio Di Jasio Type Conversions Implicit Integer Type Conversion Example: short s; // 16-bit int i; // 32-bit i = s; Explicit Integer Type Conversion Example: short s; // 16-bit int i;// 32-bit s = (short) i;
Exploring The PIC32 - Lucio Di Jasio Bit Fields extern unsigned int T1CON; extern union { struct { unsigned :1; unsigned TCS:1; unsigned TSYNC:1; unsigned :1; unsigned TCKPS0:1; unsigned TCKPS1:1; unsigned TGATE:1; unsigned :6; unsigned TSIDL:1; unsigned :1; unsigned TON:1; }; struct { unsigned :4; unsigned TCKPS:2; }; } T1CONbits; You can access each bit field using the “dot” notation, as in the following example : T1CONbits.TON = 1;
Exploring The PIC32 - Lucio Di Jasio Exact Width Types int8_t always an 8-bit signed type uint8_t always an 8-bit unsigned type int16_t always a 16-bit signed type uint16_t always a 16-bit unsigned type int32_t always a 32-bit signed type uint32_t always a 32-bit unsigned type int64_t always a 64-bit signed type uint64_t always a 64-bit unsigned type
Exploring The PIC32 - Lucio Di Jasio Math Libraries The MPLAB C32 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 but also many useful constants like PI or M_PI actually.
Exploring The PIC32 - Lucio Di Jasio Complex Types __complex__ float z; The variable z so defined has now a real and an imaginary part that can be individually addressed using respectively the syntax: __real__ z and __imag__ z Similarly the next declaration produces a complex variable of 32-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; NOTE Notice the use of a double underscore before and after the keyword complex.