3

I am trying to walk through and understand a chi squared program that uses intrinsic for speed. In this process I have come across a line of code I can’t understand.

I have tried reviewing textbooks, Google and search of this site with no luck. I think the problem is that without knowing anything about the syntax, I can’t describes the search in terms or keyword sufficiently to get any pertinent results.

Here is the line of code I don’t understand:

float (*chi2_float)(const int, const float*, const float*) = chi2_baseline_float;

And here is the function that contains it:

float chi2_float(const int dim, const float* const x, const float* const y) {
    float (*chi2_float)(const int, const float*, const float*) = chi2_baseline_float;
#ifdef __SSE__
    chi2_float = chi2_intrinsic_float;
#endif
    return chi2_float(dim, x, y);
}

It looks to me that it is probably declaring and defining a function and when I comment out the line and re-compile I get:

error C2659: '=' : function as left operand on line chi2_float = chi2_intrinsic_float;

I can send the .h file that contains this function if needed but it is as you would expect from the parameters.

Any help would be greatly appreciated.

1
  • 3
    Not so mysterious; it's just a function pointer. Got your C++ textbook handy? Commented Nov 17, 2012 at 22:12

5 Answers 5

11

The line in question is setting a variable of type function pointer to one of two other functions depending on the value of __SSE__.

It then calls the function pointed to by chi2_float and returns the result.

1
  • Thanks dma. Now that I know its a type function pointer I understand.
    – James Crow
    Commented Nov 18, 2012 at 16:34
7

This:

float (*chi2_float)(const int, const float*, const float*)= chi2_baseline_float;

declares a function pointer names chi2_float and assigned to it a pointer to the function named chi_baseline_float.

Then, if the __SSE__ macro is defined, the pointer is reassigned with a pointer to the function chi2_intrinsic_float.

The net effect of all this is something similar to:

float chi2_float(const int dim, const float* const x, const float* const y) 
{
#ifdef __SSE__
    return chi2_intrinsic_float(dim, x, y);
#else
    return chi2_baseline_float(dim, x, y);
#endif
}
2
  • 3
    Similar, but not the same. Unfortunately, the code in the question will not be as efficient since the call is made through a function pointer, which not only adds a level of indirection but prevents any possible inlining optimization. The code you've compared it to should be faster, and I would recommend that the OP replace his code with this example if possible.
    – cdhowie
    Commented Nov 17, 2012 at 22:52
  • Thanks Michael Burr. I am taking you suggestion and redoing the code. It is much cleaner and understandable.
    – James Crow
    Commented Nov 18, 2012 at 16:38
1

The code of your question with some comments:

// beginning of the definition of function chi2_float 
float chi2_float(const int dim, const float* const x, const float* const y) {
    // declare the variable chi2_float as a function pointer
    // set variable chi2_float to the address of the function chi2_baseline_float
    float (*chi2_float)(const int, const float*, const float*) = chi2_baseline_float;
// if macro __SSE__ is defined (if the compiler enables SSE instructions set)
// [this is your case because you got an error in the below line when you have commented the above line] 
#ifdef __SSE__
    // then preprocessor adds the following line that sets again the variable chi2_float (but to another function) 
    chi2_float = chi2_intrinsic_float;
#endif
    // call the function pointed by the variable chi2_float
    return chi2_float(dim, x, y);
}
2
  • Thanks olibre your comments are very illustrative and make the "mysterious" syntax easy to understand.
    – James Crow
    Commented Nov 18, 2012 at 16:50
  • You are welcome @JamesCrow ;-) See you soon on another question/answer ;-)
    – oHo
    Commented Nov 18, 2012 at 17:31
1
float chi2_float(const int dim, const float* const x, const float* const y) {
   float (*chi2_float)(const int, const float*, const float*) = chi2_baseline_float;
#ifdef __SSE__
   chi2_float = chi2_intrinsic_float;
#endif
   return chi2_float(dim, x, y);
}

Ugly.

The first thing to do to fix this code is to use something other than the function name as the name of the function pointer variable. That this variable shadows the name of the function is, I think, the source of James Crow's confusion.

The second thing to do to fix this code is to get rid of the function pointer entirely, resulting in the code Michael Burr posted in his answer.

1
  • Thanks David Hammen. You are correct, I think that having the function name and the function pointer variable name the same added to my confusion.
    – James Crow
    Commented Nov 18, 2012 at 16:41
1

Michael Burr suggested the following:

float chi2_float(const int dim, const float* const x, const float* const y) 
{
#ifdef __SSE__
    return chi2_intrinsic_float(dim, x, y);
#else
    return chi2_baseline_float(dim, x, y);
#endif
}

Ignoring the compiler's optimiser for a moment; this may be improved. Mr Burr's solution uses two function calls: main() (or whatever) calls chi2_float() which then calls the appropriate implementation. That can be reduced to just one function call with the following:

#ifdef __SSE__
    #define chi2_float(dim, x, y) chi2_intrinsic_float(dim, x, y)
#else
    #define chi2_float(dim, x, y) chi2_baseline_float(dim, x, y)
#endif

The same result can likely be achieved by declaring Mr Burr's chi2_float() as "inline".

However, returning to the real world (where compilers aggressively optimise code), you would expect a good optimiser to remove the extra function call, thus rendering Mr Burr's solution to be just as fast.

I have posted this discussion for completeness. Although this does not improve on Mr Burr's solution, it adds a bit more context. Technically it ought to have been a comment, rather than a new answer, but it isn't possible to format source code in a comment. Such is life.

1
  • Thanks Michael J for you answer. I am changing the code as Michael Burr suggested. I will also probably reduce the code to one function call but I need to understand the overall logic more first.
    – James Crow
    Commented Nov 18, 2012 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.