Floating point parameter passing error

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Floating point parameter passing error

1,544 Views
mad4chip
Contributor I
Hi to everyone,
I'm having a strange problem with floating point parameter passing, if I write a function that use floating point parameters they are not handled correctly
This function perform the subtraction of a 3 dimensional float vector
T_3DCoordFloat Vectors_Diff(T_3DCoordFloat A, T_3DCoordFloat B)
{
     X -= B.X;
     Y -= B.Y;
     Z -= B.Z;
     return A;
}
is compiled in:
          Vectors_Diff:
20       {
00002d90:   push {r0-r3}
00002d92:   vpush {s0-s2}
00002d96:   push {r4}
00002d98:   cpy r4,r0
21        A.X -= B.X;
00002d9a:   vldr.32 s1,[sp,#-8]
00002d9e:   vldr.32 s0,[sp,#-8]
00002da2:   vsub.f32 s0,s1,s0
00002da6:   vstr.32 s0,[sp,#-8]
22        A.Y -= B.Y;
00002daa:   vldr.32 s1,[sp,#-4]
00002dae:   vldr.32 s0,[sp,#-4]
00002db2:   vsub.f32 s0,s1,s0
00002db6:   vstr.32 s0,[sp,#-4]
23        A.Z -= B.Z;
00002dba:   vldr.32 s1,[sp]
00002dbe:   vldr.32 s0,[sp]
00002dc2:   vsub.f32 s0,s1,s0
00002dc6:   vstr.32 s0,[sp]
24        return A;
00002dca:   movw r2,#0xfff8
00002dce:   movt r2,#0xffff
00002dd2:   add r2,sp
00002dd4:   ldm r2!,{r0-r1}
00002dd6:   stm r4!,{r0-r1}
00002dd8:   ldr r0,[r2,#0]
00002dda:   str r0,[r4,#0]
25       }
but in debug window both A and B parameters have the same address and the instruction that load parameters from the stack load twice the same location
vldr.32 s1,[sp,#-8]
vldr.32 s0,[sp,#-8]
and then compute the difference that is Always zero.
To get it working I've to use a pointer to struct A and B and not passing them directly.
If I use only one float parameter it seems it works
I'm using
codewarrior 10.4
K60 CPU
K60F120M CPU board
MQX 4.0
Processor Expert
Floating point Hardware vfpv4

 

Regards

     Giovanni

 

Labels (1)
0 Kudos
Reply
4 Replies

1,126 Views
mad4chip
Contributor I

Update:

I tried to change the struct from float to integer and the code works correctly, variables are at different address in the stack, so the error only appears when using floats.

Regards

     Giovanni

0 Kudos
Reply

1,126 Views
trytohelp
NXP Employee
NXP Employee

Hi Giovanni,

Very sorry for the delay.

Do you have a project example showing the behavior you can share with us ?

the MCU v10.5 has been released last month.

******************************

CodeWarrior for Microcontrollers v10.5 is the last version.

It integrates the development tools for the :

      ColdFire®, ColdFire+, DSC, Kinetis, Qorivva, PX, RS08, S08 and S12Z architectures

into a single product based on the Eclipse open development platform.

This version is running on Host platforms

      Microsoft® Windows XP 32/64-bit (Business)

      Windows Vista 32/64-bit (Business and Home Premium)

      Windows 7 32/64-bit (Professional and Home Premium)

      Windows 8 32/64-bit (Professional)

You can download an evaluation on the web site:

      http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=CW-MCU10

I recommend you to sign the news letter for the product.

Via the web site:

      http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=CW-MCU10

Click on Subscribe

Regards

Pascal

0 Kudos
Reply

1,126 Views
mad4chip
Contributor I

Hi Pascal,

thanks for the answer, I updated CodeWarrior and build a project to show up the bug, I used the Hello word demo project with the main.c that is attached here

This code produce an output like the following, as you can see parameters before calling the function are not zero but inside the function are zero.

If I use a float as a parameters the bug seems to not happen, now to make it work I use a pointer to the struct.

Many thanks for your help

     Giovanni

A.X:16838.000000

A.Y:5758.000000

A.Z:10113.000000

B.X:17515.000000

B.Y:31051.000000

B.Z:5627.000000

InnerA.X:0.000000

InnerA.Y:0.000000

InnerA.Z:0.000000

InnerB.X:0.000000

InnerB.Y:0.000000

InnerB.Z:0.000000

/*
* File:  hello_world.c
* Purpose:  Main process
*
*/

#include "common.h"

typedef struct
{
float X;
float Y;
float Z;
} T_3DCoordFloat;

T_3DCoordFloat Vectors_Sum(T_3DCoordFloat A, T_3DCoordFloat B)
{

/*
printf("InnerA.X:%f\n", A.X);
printf("InnerA.Y:%f\n", A.Y);
printf("InnerA.Z:%f\n", A.Z);

printf("InnerB.X:%f\n", B.X);
printf("InnerB.Y:%f\n", B.Y);
printf("InnerB.Z:%f\n", B.Z);

*/

A.X += B.X;
A.Y += B.Y;
A.Z += B.Z;
return A;
}


/********************************************************************/
void main (void)
{
char ch;
T_3DCoordFloat A;
T_3DCoordFloat B;
               
   printf("\nHello World!!\n");

   asm("LDR     R0, =0xE000ED88");   //; CPACR is located at address 0xE000ED88
   asm("LDR     R1, [R0]");    //; Read CPACR
   asm("ORR     R1, R1, #(0xF << 20)"); //; Set bits 20-23 to enable CP10 and CP11 coprocessors
   asm("STR     R1, [R0]");    //Write back the modified value to the CPACR

while(1)
{
  A.X = rand();
  A.Y = rand();
  A.Z = rand();
 
  B.X = rand();
  B.Y = rand();
  B.Z = rand();
 
  printf("A.X:%f\n", A.X);
  printf("A.Y:%f\n", A.Y);
  printf("A.Z:%f\n", A.Z);

  printf("B.X:%f\n", B.X);
  printf("B.Y:%f\n", B.Y);
  printf("B.Z:%f\n", B.Z);


  A = Vectors_Sum(A, B);
  printf("Result.X:%f\n", A.X);
  printf("Result.Y:%f\n", A.Y);
  printf("Result.Z:%f\n", A.Z);
}
}
/********************************************************************/

0 Kudos
Reply

1,126 Views
trytohelp
NXP Employee
NXP Employee

Hi Giovanni,

Sorry for the delay.

If you think there is a bug in the build tool chain, I recommend you to report the problem under Service Request system.

We need to know exactly the tool version used, the interface, board, etc ...

Regards

Pascal

0 Kudos
Reply