Case Study-Code Optimizing

Embed Size (px)

Citation preview

  • 8/6/2019 Case Study-Code Optimizing

    1/33

    Starting a new job as an

    Embedded System Programmer.

    1

  • 8/6/2019 Case Study-Code Optimizing

    2/33

    ` To personalise this introduction well call our new

    programmer Zelko Debanic.

    ` If Zelko does exist then I apologise to him for

    using his name.` If Zelko exists he needs to apologise to me for all

    the junk mail Ive received over the years!!!

    2

  • 8/6/2019 Case Study-Code Optimizing

    3/33

    Create a C function for the 68HC12 target (2Mhz

    System clock).

    The function prototype:

    void frequencystring(unsigned int period, char str[])

    The function definition: The function is to take a 16bit value representing the period of

    the mains frequency in 0.5microsecond increments and to

    return a four character string showing the frequency to one

    decimal place. The normal operational range for the frequency is between

    49.8 and 50.2Hz but the conversion is to be to within 0.05Hz

    over a range from 48 to 52HZ.

    3

  • 8/6/2019 Case Study-Code Optimizing

    4/33

    /* 0.5microseconds per cycle gives

    2,000,000 per second */

    #define countspersec 2000000

    void frequencystring(unsigned int period, charstr[])

    { float freq;

    int error;

    freq = countspersec/period;error = sprintf(str,"%4.1f",freq);

    }

    4

  • 8/6/2019 Case Study-Code Optimizing

    5/33

    I suppose I had better test it anyway.

    void main (void) {

    unsigned int period;char freqstr[5];

    printf("Enter period> ");scanf("%u",&period);

    while (period > 0) {

    frequencystring(period,freqstr);

    printf("The frequency is:>%s ");

    scanf("%u",&period);

    }

    }

    5

  • 8/6/2019 Case Study-Code Optimizing

    6/33

    ` 48Hz = 41666

    ` 49Hz = 40816

    ` 50Hz = 40000

    ` 51Hz = 39215` 52Hz = 38461

    ` 51.5Hz = 38835

    ` Whoops!

    6

  • 8/6/2019 Case Study-Code Optimizing

    7/33

    /* 0.5microseconds per cycle gives 2,000,000 per

    second */

    #define countspersec 2000000

    void frequencystring(unsigned int period, charstr[])

    { float freq; int error;

    freq = countspersec/period;

    error = sprintf(str,"%4.1f",freq);

    }

    7

  • 8/6/2019 Case Study-Code Optimizing

    8/33

    countspersec should be a float

    /* 0.5microseconds per cycle gives 2,000,000 persecond */

    #define countspersec 2000000.

    void frequencystring(unsigned int period, char str[])

    { float freq; int error;

    freq = countspersec/period;error = sprintf(str,"%4.1f",freq);

    }

    8

  • 8/6/2019 Case Study-Code Optimizing

    9/33

    ` 49.949Hz = 40041

    ` 49.951Hz = 40039

    ` 50.049Hz = 39961

    ` 50.051Hz = 39959` etc

    9

  • 8/6/2019 Case Study-Code Optimizing

    10/33

    Zelko takes it to the supervisor expecting

    congratulations.

    What did he get?

    How much code space does it take? How am I supposed to know?

    Find out! Go and check the output from the target

    compiler.

    What target compiler?

    For the 68HC12 of course!

    10

  • 8/6/2019 Case Study-Code Optimizing

    11/33

    ` An empty C file uses 1 byte of code and 441 bytes

    of code memory in total. This covers all the initialisation that C needs.

    ` Question: What instruction would that 1 byte ofcode represent?

    11

  • 8/6/2019 Case Study-Code Optimizing

    12/33

    ` A simple call to the function results in 79 bytes of

    code. Not bad.

    ` The total code goes to 2532 bytes of code

    because of the library functions used. Anadditional 2091 bytes for this function.

    ` Thats 3.2% of the total memory space already

    used

    12

  • 8/6/2019 Case Study-Code Optimizing

    13/33

    ` Your doing embedded programming where

    memory space is limited and so is compute power!

    ` Memory use efficiency is very important and when

    you get on to real time systems so is executionefficiency.

    13

  • 8/6/2019 Case Study-Code Optimizing

    14/33

    ` These require a lot of support in an embedded

    processor.

    ` Can we scale the division to simplify the code?

    ` What does that mean?` 2000000/40000 is the same as:

    1000000/20000 etc

    There is no loss of information.

    14

  • 8/6/2019 Case Study-Code Optimizing

    15/33

    ` Scaling of integer data and code in embedded

    systems is a very useful technique to retain

    accuracy while using the intrinsic variable types of

    the processor.` Scaling by powers of two only requires shifts to

    work.

    15

  • 8/6/2019 Case Study-Code Optimizing

    16/33

    ` The original specification said that we must be

    correct to within 0.05Hz or 1/1000 of the original

    value.

    ` Within 40 in 40000.` Scaling by a power of two is very efficient

    It is a shift.

    Try 32 as the scale factor

    ` 2000000 becomes 62500. Yes!!!

    16

  • 8/6/2019 Case Study-Code Optimizing

    17/33

    ` The maximum value that an unsigned 16 bit

    integer can represent is 65535.

    ` Scaling by a factor of 32 brings the 2,000,000 into

    a range directly representable in themicrocontroller.

    17

  • 8/6/2019 Case Study-Code Optimizing

    18/33

  • 8/6/2019 Case Study-Code Optimizing

    19/33

    /* 0.5microseconds per cycle gives 2,000,000 / second *//* Scale by a factor of 32 */#define countspersec 62500void frequencystring(unsigned int period, char str[])

    { int freq, fraction, count;int error;period = period>>5; /* Scaled by 32 */freq = countspersec/period; /* This is the integer part */fraction = ((countspersec%period)*10)/period;error = sprintf(str,"%2d.%1d", freq, fraction);

    }

    19

  • 8/6/2019 Case Study-Code Optimizing

    20/33

    ` This routine idea needed to be tested for

    accuracy.

    ` What about code size?

    ` The simple function call check requires 128 bytes(compared with the 79 previously)

    ` The total code came to 1542 bytes (compared

    with 2091 previously) a reduction of about 26%.

    20

  • 8/6/2019 Case Study-Code Optimizing

    21/33

    ` But is it good?

    ` One of the problems is that the function is dealing

    with a reciprocal relationship and so it needs

    division (which is generally slow onmicrocontrollers).

    ` Can the program get rid of the reciprocals and

    maybe use subtraction?

    21

  • 8/6/2019 Case Study-Code Optimizing

    22/33

    22

  • 8/6/2019 Case Study-Code Optimizing

    23/33

    Over the range that looks almost linear.Is that good enough?

    23

  • 8/6/2019 Case Study-Code Optimizing

    24/33

    ` Using an integer subtraction it can be determined

    if the answer is >=50.0 or

  • 8/6/2019 Case Study-Code Optimizing

    25/33

    #define FiftyHz 40040 /* Rounding Included */#define OneHz 800#define PointOneHz 80void frequencystring(unsigned int period, char str[])

    { int count;count = period - FiftyHz;if (count > 0) { /* Below 50.0 HZ */str[0] = '4'; str[1] = '9'; str[2] = '.'; str[3] = '9'; str[4] = 0;/* Do decrements */ }else { /* Above 50.0 Hz */

    str[0] = '5'; str[1] = '0'; str[2] = '.'; str[3] = '0'; str[4] = 0;/* Do increments */ }

    25

  • 8/6/2019 Case Study-Code Optimizing

    26/33

    while ((count -= OneHz) >= 0) {

    str[1] -=1;

    }

    count += OneHz;while ((count -= PointOneHz) >= 0) {

    str[3] -=1;

    }

    26

  • 8/6/2019 Case Study-Code Optimizing

    27/33

    count = -count;

    while ((count -= OneHz) >= 0) {

    str[1] +=1;

    }count += OneHz;

    while ((count -= PointOneHz) >= 0) {

    str[3] +=1;

    }

    27

  • 8/6/2019 Case Study-Code Optimizing

    28/33

    ` The function code size is now 219 bytes.

    ` The total code size is 219 bytes or 10.5% of the

    original attempt and no libraries.

    ` It is still an ANSI C function and so is portable.

    28

  • 8/6/2019 Case Study-Code Optimizing

    29/33

    ` If it was only required for the 68HC12 this

    algorithm could easily be coded in assembler

    resulting in further reduced code.

    `

    A 68HC12 assembly coded version takes 93bytes.

    ` This is only 4.5% of the original code!!

    29

  • 8/6/2019 Case Study-Code Optimizing

    30/33

    ` Our new Embedded Systems Programmer has

    learnt a few lessons.

    ` Test the code extensively.

    ` Look for memory and speed efficient algorithms.` Scaling variables can be useful.

    ` Eliminating the need forANSI C library routines

    may be possible and helpful.

    30

  • 8/6/2019 Case Study-Code Optimizing

    31/33

    ` This is some feedback from a recent graduate:

    ` Another big thankyou to you for all of your help

    last year. (and third year) All those embedded

    programming practices and tips are coming in realhandy here at Nam Erehwon.

    31

  • 8/6/2019 Case Study-Code Optimizing

    32/33

    I managed to find a few bugs in their OS (wellmainly in the device drivers) Including the classicuse of ++ in a min/max macro so it is incrementedtwice per loop.(which we were warned about by

    you!) This was included in the OS releasecurrently residing in over 10,000 devicesimplemented around the world. Luckily theirsystem performs automatic updates when it

    periodically communicates with the back end.(...well it's a big event for me!)

    32

  • 8/6/2019 Case Study-Code Optimizing

    33/33

    33