three questions about computing

13 posts / 0 new
Author
Message

Hi, I use WinAVR and I have a few question:

1. How can I know how many cycles use a function sqrt for computing square root of some numbers?

2. I know that input variable for the function sqrt must be double (double sqrt(dobule x)), but what's about when input variable is uint16_t, for example sqrt(2704)?

3. Which is better solution:
int a;
a = (int)(some_number)*0.25;
or
a = (int)((some_number)/4);

I need only an integer value and it's produce the same result but which solution is better (faster) and why?

I'll take a crack at this, I'm sure I'll hear about it if I'm wrong.

slavko wrote:

1. How can I know how many cycles use a function sqrt for computing square root of some numbers?

Either look at the assembly file that your compiler spits out or read the math.h library to see how many commands are being run for that macro.

slavko wrote:

2. I know that input variable for the function sqrt must be double (double sqrt(dobule x)), but what's about when input variable is uint16_t, for example sqrt(2704)?

You need to typecast that variable:

sqrt((double)2704);

slavko wrote:

3. Which is better solution:
int a;
a = (int)(some_number)*0.25;
or
a = (int)((some_number)/4);

The second is better (divide by 4) because this way you avoid using floating point numbers. This is especially true since you don't need to get the fractions back, just the integer in the solution.

sqrt(2704.0) = 52.0

If you have a compile-time constant expression, the compiler may evaluate it first anyway.

If you really want to know the execution times of various constructs:

1. write some simple code that requires a variable input. ( so it cannot be pre-computed )
2. Just run the code through the simulator. This will give you both the microseconds and the cycle count.

But seriously, just use the form of expression that seems most clear to you. Your code is less likely to have bugs. The AVR is quite capable of coping with some inefficient code. You only need to worry with a very time-limited program.

David.

1). Use the simulator. It has a cycle counter so you can compare the value before and after the call. Be sure to do it many times with a wide variety of input values since the amount of time will vary depending on the value.

2).

Quote:
You need to typecast that variable:

Not really necessary, the compiler will cast it automatically for you. It does highlight the programmers intent, though.

3). I almost agree with barney here, but in order to get the integer divide instead of a floating point divide, you have to change your parentheses:

a = ( (int)some_number ) / 4;

The outer set of pares is not really necessary here, but it does highlight the point that you want to cast the float to an int before the divide. Your original did a floating point divide first, and then cast to an int.

One more point that you might not be aware of, in avr-gcc float and double are the same. Using double instead of float will neither help nor hurt.

Regards,
Steve A.

The Board helps those that help themselves.

slavko wrote:
1. How can I know how many cycles use a function sqrt for computing square root of some numbers?
You can empirically determine the execution time for any routine using several methods.

1) Use test equipment: If you have an oscilloscope, logic analyzer or similar equipment that can measure pulse width you can create a testing loop that makes a certain output high, calls the routine to be tested and then makes the output low. The approximate execution time is the width of the output pulse.

2) Use timing capabilities of your system: If your system already has timing functions (RTC, milliseconds, ticks, etc.), you can retrieve a time stamp before invoking the routine to be tested, execute the routine and then get another timestamp after it returns. The approximate execution time will be given by the difference in timestamps. Depending on the resolution of the timestamp and the anticipated execution time, you may have to execute the routine multiple times between the timestamp retrievals.

3) Use a stopwatch: Create a loop to execute the routine to be tested a large number of times. The iteration count needs to be chosen so that the total execution time is at least a few seconds or tens of seconds. Add code before and after the loop to give you cues to begin timing and to end timing. The cues can be short messages on the serial port, an LED going on and then off, and audible signal, etc. Dividing the measured elapsed time by the iteration count gives the approximate average execution time.

4) Use the AVR simulator: load the code into the simulator, execute the test code and check the cycle count.

Note that for many routines, the execution time depends on the parameters passed to it on any given invocation. You may want to test a range of parameter values, including boundary conditions, etc.

Don Kinzer
ZBasic Microcontrollers
http://www.zbasic.net

Last Edited: Sun. Dec 21, 2008 - 08:15 PM

Ok, I will try it, thanks all for answers.

Bottom line - try to avoid float/double at all costs. It adds size and reduces speed (often quite dramatically)

And this nice general question was gcc specific how? Pls move to general avr forum so all avr users, especially new ones, can see it.

Imagecraft compiler user

Bob to get threads moved from GCC you need to contact one of the mods of the GCC forum - so that'd be either Eric or Joerg

Cliff

(PS forgot, eivind is also a mod in this forum)

Now that AVR studio is used with winavr, are there two gcc forums to ask c questions in?

Imagecraft compiler user

Nope, questions about GCC should be directed here, questions about the avr-gcc plugin to Studio should be asked in the AVR Studio 4 forum

But if you really think there's a way to persuade new users to read the forum guidelines in each forum and pick the most appropriate to post questions in then best of luck - in the fora where I'm mod. I spend half the moderation time moving threads to more appropriate fora. So some folks are bound to ask GCC questions in Tutorial, Academy, General Electronics, Off Topic, Studio 4 and AVR Forum just as they are in this one. Same goes for Studio, the AVR Forum is stock full of threads about Studio 4 and Atmel tools use (how many "can't upgrade my AVRISPmkII" have you read there?) and while we try to shuffle them about there's always the odd one that gets lost.

By the way there's no magic to becoming a mod. all you have to do is volunteer and as long as eivind knows (and hopefully trusts?) you he'll give you mod. rights in whatever fora you choose.

It'd kind of be an interesting balance if you were to volunteer to be a mod in GCC in fact! ;)

Cliff

It doesn't make sense for Bob to be a moderator in the AVR GCC forum when he advertises that he is an Imagecraft user. I don't know if Bob knows much about the AVR GCC toolchain to be of help...