PIC32MX: Benchmarking Mathematical Operations

From Mech
Jump to navigationJump to search

Original Assignment

Do not erase this section!

Your assignment is to empirically test how long it takes to perform add, subtract, multiply, divide, sqrt, sin, and cos operations with the 80 MHz PIC32460F512L and our standard code optimization setting. You will do these tests with chars (8-bit integers), shorts (16-bit), integers (32-bit), long long integers (64-bit), floats (32-bit single precision floating point), and double (64-bit double-precision floating point). The integers can be unsigned or signed. Your end result will be a table with the operation on one axis (likely the horizontal axis) and the kind of variable on the other axis, and each cell of the table will have a normalized duration for the operation. The time will be normalized by the fastest operation, so the smallest number in the table will be 1.00. All other numbers will indicate how many times longer that operation takes. All numbers will have two decimal places, e.g., 2.57 or 24.72. You will also give the time that 1.00 corresponds to in nanoseconds.

Since bit-shifting left and right correspond to a version of multiplying and dividing, you should also include the operations >>1 and >>4 and <<1 and <<4. (If the results are identical, you can eliminate shift left from your table.)

To generate this table, you can set an output bit low before the operation, then high immediately after the operation, and measure the time on an oscilloscope. Two things to consider: (1) Time a single operation, over and over, with a short delay between the operation. This should create a pulse train on your oscilloscope. Can you get an accurate estimate of the time this way? You could also try doing five or ten operations between changing the digital output. See if this gives the same estimate. (This estimate might be more accurate as you are essentially averaging over a number of operations.) Avoid using arrays and for loops in your test, as indexing arrays and running the loop each take time. (2) Make sure the compiler doesn't compute the results in advance. You could try testing operations with numbers generated randomly (don't time this operation!) vs. numbers that you just type in manually to make sure that both are giving you the same result.

Overview

We were tasked with determining the real-time cost (measured in nanoseconds) of performing seven basic mathematical operations with each one of the six commonly used ANSI C data types.

The mathematical operations we tested were:

  • subtraction
  • addition
  • multiplication
  • division
  • square root
  • sine
  • cosine

We six data types we tested each operation on were:

  • char
  • short
  • integer
  • long long
  • float
  • double

Our testing procedure was simple: throw an output pin on the NU32 development board high, perform a mathematical operation with a given data type, and then pull the same pin low.

Placing the above three steps in an infinite while loop afforded us the opportunity to use an oscilloscope to measure the duration between each high-low pair in the output waveform. After subtracting the time it took for the PIC to raise and lower the voltage on the output pin (something we previously measured), we were able to determine the amount of time required for the PIC32 chip to execute any specific mathematical operation with a high level of accuracy.

With seven operations to perform on six different data types, we created the following table to help us assign and keep track of the various tests we planned to run:

!Operation vs. Data type
char (8-bit) short (16-bit) int (32-bit)
subtraction Test 2 Test 9 Test 16
addition Test 3 Test 10 Test 17
multiplication Test 4 Test 11 Test 18
division Test 5 Test 12 Test 19
square root Test 6 Test 13 Test 20
sine Test 7 Test 14 Test 21
cosine Test 8 Test 15 Test 22

Test 1 was used to determine the duration required for the PIC32 to throw a pin high and pull a pin low, while Tests 2 through 43 were used to measure the actual performance of each (operation, data-type) pair.

Circuit

Include a schematic and give any part numbers. A photo of your circuit is OK, but not as a replacement for a schematic.

Code

Where possible, make it a single piece of well-commented cut-and-pastable code, or at least make each function that way, so others can easily copy it. Most comments should be in the code itself; outside the code (on the wiki) should only be explanatory comments that are too cumbersome to include in the code.