This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Passing a float by value or reference

Hi,

We are seeing some strange stuff when trying to pass floating point values to a function. When we pass the parameter by value (meaning as an actual floating point number) the parameters' value changes. When we debug the code we can see that in the disassembly there is a call too __aeabi_f2d. However there should be no reason to call this since all of the values are floats and not doubles. When we pass the floating point number by reference everything works fine.

We are using the nRF52840 with the gcc compiler that comes with the 4.20a version of segger embedded studio.

Meaning this does not work, the value of variable intermediate is not the same value as the value found in input_param :

  float32_t intermediate = *pIncomingSample;
  turn_gyry(intermediate);

void turn_gyry(float32_t input_raw){
  float32_t output_low_pass; 
  float32_t output_moving_average;
  float32_t output_derivative;
  float32_t input_param = input_raw;

  filter_turn_gyry_low_pass(&input_param, &output_low_pass);
  filter_turn_gyry_moving_average(&output_low_pass, &output_moving_average);
  filter_turn_gyry_derivative(&output_moving_average, &output_derivative);

  printf("output low pass %f", output_low_pass );
  printf("output derivative %f", output_derivative);
}

But this works just fine:

  float32_t intermediate = *pIncomingSample;
  turn_gyry(pIncomingSample);

//the function definition of turn_gyry
void turn_gyry(float32_t *input_raw){
  float32_t output_low_pass; 
  float32_t output_moving_average;
  float32_t output_derivative;
  float32_t input_param = input_raw;

  filter_turn_gyry_low_pass(&input_param, &output_low_pass);
  filter_turn_gyry_moving_average(&output_low_pass, &output_moving_average);
  filter_turn_gyry_derivative(&output_moving_average, &output_derivative);

  printf("output low pass %f", output_low_pass );
  printf("output derivative %f", output_derivative);
}

Does anybody have an explanation for this behavior? I believe that the call to __aeabi_f2d implies that for some reason the compiler thinks he needs to convert a float to a double but there is no reason to do this...

Any ideas are welcome

Thanks!

Parents
  • This caught my eye:

    void turn_gyry(float32_t *input_raw){
    float32_t output_low_pass;
    float32_t output_moving_average;
    float32_t output_derivative;
    float32_t input_param = input_raw;

    You're assigning a float pointer to a float and then passing address of the float to your function, shouldn't the assignment be 

    float32_t input_param = *input_raw; //?

    I'd like to see the sources of the filter functions, maybe there's something similar where pointed value and pointer are mixed? Doesn't the compiler warn about the assignment?

  • Hi Otso

    Thanks for the reply! I 'll check the code and try what you suggested because now I'm not quite sure if what you are suggesting is actually right or if I forgot to completely change the example back to the pointer version. But you are right about the code as it is written above.

    Thanks again

    Michiel

Reply Children
No Data
Related