1

I'm using VS2012 Ultimate for some embedded development in C. I compile the code with a platform abstraction and simulate it on my PC. Another person in the company uses CodeWarrior with a PPC abstraction layer and runs the thing on an MPC565 chip. The tasks on the embedded chip occasionally overrun the CPU or time boundaries.

There is quite a bit of trigonometry in the code. I know that the trig execution on the embedded chip is slow. How can I exaggerate the time spent in trig code on my PC? I'm thinking something like this:

#define cos(x) ({ while(asiTimeMsec64() % 10 != 0); cos(x);})
#define sin(x) ({ while(asiTimeMsec64() % 10 != 0); sin(x);})
#define tan(x) ({ while(asiTimeMsec64() % 10 != 0); tan(x);})

However, that code doesn't quite work. I get compiler errors about my cos calls not returning a number. I would like some kind of spin-lock -- something that doesn't allow other threads to run.

How do I override the math.h trig functions to make them artificially slow?

Brannon
  • 5,324
  • 4
  • 35
  • 83
  • Profiling on the wrong platform isn't going to tell you anything useful. You should either find some kind of emulation platform (I have no idea whether one exists for the target chip), or profile on the actual platform whose performance you're interested in! – Oliver Charlesworth Sep 26 '13 at 20:24
  • I'll look into some emulation platforms. An old PowerPC or ARM chip is probably very similar. I wonder which ones are easy to profile... – Brannon Sep 26 '13 at 21:10

3 Answers3

3

I'm not sure if your macro idea would give you useful results but that's how you can you can make it work:

void slowup( )
{
     while(asiTimeMsec64() % 10 != 0);
}

#define cos(x) (slowup(),cos(x))
...

or, using a function pointer:

double slowup( double (*trig)( double ), double val )
{
     while(asiTimeMsec64() % 10 != 0);
     return (*trig)( val );
}

#define cos(x) slowup(cos, x)
...
Ingo Leonhardt
  • 9,435
  • 2
  • 24
  • 33
  • Do you expect `#define cos(x) (slowup(),cos(x))` to work in all compilers or only GCC? My MSVC seems to compile it fine, but it returns the wrong value for the `cos` function. – Brannon Oct 10 '13 at 16:37
  • That's strange because the comma operator is defined to first evaluate the first expression and to discard the result and then to evaluate the second one and return it's value. But honestly I would prefer the second variant, which gives you some more possibilities to refine `slowup()` – Ingo Leonhardt Oct 10 '13 at 16:44
  • Actually, after further review, I think the #define is working. My data is being sabotaged through some other route. Thanks. – Brannon Oct 10 '13 at 16:47
1

Define a new function:

 double slowed_cos(double x)
 {
    SLOW_TRIG;

    return cos(x);
 }

with macros

 #define cos(Var) slowed_cos(Var)
 #define SLOW_TRIG while(asiTimeMSec64() % 10 !=0)

rinse and repeat for the others. However, you will have to ensure that the function prototype is available wherever the default functions are used with the proper include statement.

Also, verify the math.h function specification for the arguments.

Sebastien
  • 1,439
  • 14
  • 27
  • Is there any way to do this generically so that I don't have to make a new function for each existing trig function? – Brannon Oct 10 '13 at 16:38
0

If you can, switch to some other C compiler, basically gcc from mingw32. With gcc, you can use this neat trick to replace library functions with your own code, as explained in this answer. Also look if if Visual C can do something similar!

(A side note: If you're stuck with Windows, then I personally find it easiest for C development to just grab the Qt SDK for mingw, with Qt Creator included, then use that for plain C project, which is supported out-of-the-box.)


Alternatively, grab sources of C math library, for example the ones from this answer, edit them to include delay, and link against that instead of standard math library. With gcc you would simply not use -lm linker switch, instead link against the custom lib like any other lib. With VC it could be as simple, but it is prossible math lib is linked in by default, and in that case you need to find out how to disable this default linking.

Also, make sure to disable any optimizations, which may create custom inlined code for math library functions! Though just compiling in debug mode is likely to achieve this.


Note that your profiling results will be very rough. You probably should calibrate things a bit, both for general C optimization settings, as well as measuring how much delay you need to add to any floating point instructions. But if profiling the real code on the real device is not an option, and this is not just for one project but will be useful in future too, then I can see some benefit in setting up some kind of comparable environment for native PC code.

Community
  • 1
  • 1
hyde
  • 60,639
  • 21
  • 115
  • 176