chapter 4 - numb3rs learning to chose the right “types” exploring the pic32 - lucio di jasio
TRANSCRIPT
Chapter 4 - NUMB3RSChapter 4 - NUMB3RS
Learning to chose the right “types”
Exploring The PIC32 - Lucio Di Jasio
Exploring The PIC32 - Lucio Di Jasio
Integer TypesInteger Types
Exploring The PIC32 - Lucio Di Jasio
Long Multiplication (32-bit)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 240204D2 addiu v0,zero,1234
9D000010 AFC20000 sw v0,0(s8)
13: j = 5678;
9D000014 2402162E addiu v0,zero,5678
9D000018 AFC20004 sw v0,4(s8)
14: k = i*j;
9D00001C 8FC30000 lw v1,0(s8)
9D000020 8FC20004 lw v0,4(s8)
9D000024 70621002 mul v0,v1,v0
9D000028 AFC20008 sw v0,8(s8)
Exploring The PIC32 - Lucio Di Jasio
Long Long Multiplication Long Long Multiplication (64-bit)(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)
9D000030 8FC20008 lw v0,8(s8)
9D000034 00620019 multu v1,v0
9D000038 00002012 mflo a0
9D00003C 00002810 mfhi a1
9D000040 8FC30000 lw v1,0(s8)
9D000044 8FC2000C lw v0,12(s8)
9D000048 70621802 mul v1,v1,v0
9D00004C 00A01021 addu v0,a1,zero
9D000050 00431021 addu v0,v0,v1
9D000054 8FC60008 lw a2,8(s8)
9D000058 8FC30004 lw v1,4(s8)
9D00005C 70C31802 mul v1,a2,v1
9D000060 00431021 addu v0,v0,v1
9D000064 00402821 addu a1,v0,zero
9D000068 AFC40010 sw a0,16(s8)
9D00006C AFC50014 sw a1,20(s8)
Exploring The PIC32 - Lucio Di Jasio
Long Integer DivisionLong 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)
9D000020 8FC20004 lw v0,4(s8)
9D000024 0062001A div v1,v0
9D000028 004001F4 teq v0,zero
9D00002C 00001012 mflo v0
9D000030 AFC20008 sw v0,8(s8)
Exploring The PIC32 - Lucio Di Jasio
Long Long Integer DivisionLong 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;
9D000030 8FC40010 lw a0,16(s8)
9D000034 8FC50014 lw a1,20(s8)
9D000038 8FC60018 lw a2,24(s8)
9D00003C 8FC7001C lw a3,28(s8)
9D000040 0F40001A jal 0x9d000068
9D000044 00000000 nop
9D000048 AFC20020 sw v0,32(s8)
9D00004C AFC30024 sw v1,36(s8)
Exploring The PIC32 - Lucio Di Jasio
Floating Point TypesFloating Point Types
Exploring The PIC32 - Lucio Di Jasio
The Simulator StopwatchThe Simulator Stopwatch
Exploring The PIC32 - Lucio Di Jasio
Multiplication Test ResultsMultiplication Test Results
Multiplication Test Width Cycle Count
Performance relative to
(Bits) Int float
Char Integer (char) 8 6 1 -
Short Integer (short) 16 6 1 -
Integer (int, long) 32 6 1 -
Long Integer (long long) 64 21 3.5 -
Single Precision FP
(float, double)
32 71 11.8 1
Double Precision FP
(long double)
64 159 26.5 2.23
Exploring The PIC32 - Lucio Di Jasio
Type ConversionsType 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 FieldsBit Fieldsextern 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 TypesExact Width Types
int8_t always an 8-bit signed typeuint8_t always an 8-bit unsigned typeint16_t always a 16-bit signed typeuint16_t always a 16-bit unsigned typeint32_t always a 32-bit signed typeuint32_t always a 32-bit unsigned typeint64_t always a 64-bit signed typeuint64_t always a 64-bit unsigned type
Exploring The PIC32 - Lucio Di Jasio
Math LibrariesMath 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 TypesComplex 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;
NOTENotice the use of a double underscore before and after the keyword complex.