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?

有帮助吗?

解决方案

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)
...

其他提示

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.

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top