1

I am generating C-code to run on embedding systems and the code that I want to generate contains calls to functions with a large number of parameters. The number of these parameters is always known when I generate the functions and they always consist of a number of input and output arrays.

In my current version of the code, the C-code that I generate contains functions of the form:

void f(const double* x0, const double* x1, const double* x2, double* r0, double* r1);

In this case, I have 3 inputs arrays and 2 output arrays. In general, however, the number of input and output arrays can be very large, as in hundreds or thousands. Note that the code is not intended to be read by humans.

Now, I've learnt that the C standard only guarantees support for up to 127 function parameters. Moreover, I also want my generated code to conform to strict coding standards for embedded systems and I have seen that the Jet Propulsion Laboratory's coding standard for C-code only allows up to 6 parameters.

So, how can I rewrite the code above with at most 6 function parameters in the most efficient way? Note that I am only interested in execution speed, not code readability.

My first idea is to handle the function above as follows:

void f(const double* x[], double* r[]);

and then call this code as follows (thread safety is not an issue):

static const double* x[] = {x0, x1, x2};
static double* r[] = {r0, r1};
f(x,r);

Is this a good idea? Are there other, more efficient alternatives?

Community
  • 1
  • 1
Joel
  • 1,295
  • 15
  • 30
  • 8
    Indeed, using an array seems like the natural answer. – Matt Ball Jul 04 '13 at 17:44
  • Are the parameters always the same or are they are computed just before? Do you can use static groups of pointers? – jeb Jul 04 '13 at 18:54
  • @jeb The arrays themselves are kept in static memory, but the content of the (input) arrays is calculated just before. – Joel Jul 04 '13 at 19:09

2 Answers2

2

Slightly more efficient than your solution is to be able to access all of the parameters via a single pointer, which you can do with a struct, which can also handle arguments of different types:

typedef struct
{
    const double* in[3];
    double* out[2];
} MyFuncArgs;

MyFuncArgs myFuncArgs = { { x0, x1, x2 }, { r0, r1 } };
void myFunc(MyFuncArgs*);
myFunc(&myFuncArgs); 
...
void myFunc(MyFuncArgs* args)
{
     // do stuff with args->in[...], put results in args->out[...].
}

Alternatively:

typedef struct
{
    const double *in0, *in1, *in2;
    double *out0, *out1;
} MyFuncArgs;

MyFuncArgs myFuncArgs = { x0, x1, x2, r0, r1 };
void myFunc(MyFuncArgs*);
myFunc(&myFuncArgs); 
...
void myFunc(MyFuncArgs* args)
{
     // do stuff with args->in0 ..., put results in args->out0 ...
}
Jim Balter
  • 16,163
  • 3
  • 43
  • 66
  • Good proposal, especially the alternative version. I'll accept this as my answer. Thanks! – Joel Jul 04 '13 at 19:01
0

I think you are doing good. Arrays suits your requirement

Chinna
  • 3,930
  • 4
  • 25
  • 55