NO

Author Topic: Double variables in function calls  (Read 11674 times)

ks65

  • Guest
Double variables in function calls
« on: February 14, 2012, 11:18:22 PM »
Using PellesC 6.50.4 I develop a Pocket PC application (Windows Mobile 5.0). I have problems with function calls of double variables. I reduced the problems to the example:

double test(long i,long a,int b,double d)

Inside the function test() the value of d is 0.0 independent of the call. The variables a and b are used in the function. Otherwise there is no problem with d. The following examples work:

double test(long i,double a,long b,double d)
double test(long i,long a,long b,float d)
double test(long a,long b,double d)

Is the stack size for function parameters the problem? How can I solve the problem?

CommonTater

  • Guest
Re: Double variables in function calls
« Reply #1 on: February 15, 2012, 12:25:09 AM »
Please post the whole function...


czerny

  • Guest
Re: Double variables in function calls
« Reply #2 on: February 15, 2012, 12:06:21 PM »
Code: [Select]
#include <stdio.h>

double test(long i,long a,int b,double d)
{
printf("i=%d a=%ld b=%ld d=%f\n", i, a, b, d);
return d+b+a+i;
}

int main(int argc, char **argv)
{
printf("%f\n", test(2,5,10,3.5));

return 0;
}

ks65

  • Guest
Re: Double variables in function calls
« Reply #3 on: February 15, 2012, 06:40:24 PM »
Here comes the function:

double test(long i,long a,int b,double d)
   {
   if(a+b>1000)
      return(-1111.11);
   
   return(d*2.0);
   }

czerny

  • Guest
Re: Double variables in function calls
« Reply #4 on: February 15, 2012, 11:21:13 PM »
I can not find any problem with this.
Post your main() program, please.

czerny

ks65

  • Guest
Re: Double variables in function calls
« Reply #5 on: February 16, 2012, 12:31:49 AM »
Here you find the complete test Pocket PC Application as attach.

CommonTater

  • Guest
Re: Double variables in function calls
« Reply #6 on: February 16, 2012, 01:15:09 AM »
Other than using %lf in your sprintf() call it should work...
Try it with just %f and see what happens.


Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Double variables in function calls
« Reply #7 on: February 16, 2012, 12:04:47 PM »
Possible bug with optimization.
Code: [Select]
#define UNICODE
#define _UNICODE
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

double test(long a, long b, int c, double d)
{
if (b + c > 1000)
return (-1111.11);
return (d * 2.0);
}

double test1(long a, long b, int c, double d)
{
return d;
}

double test2(long a, long b, int c, double d)
{
if (b > 1000)
return (-1111.11);
return d;
}

double test3(long a, long b, int c, double d)
{
if (c > 1000)
return (-1111.11);
return d;
}

int __cdecl WinMainCRTStartup(void)
{
double dbl = 1.0;
WCHAR str[200];
wsprintf(str, _T("test1: %f=%f"), dbl, test1(1,2,3,dbl));
OutputDebugString(str);
wsprintf(str, _T("test2: %f=%f"), dbl, test2(1,2,3,dbl));
OutputDebugString(str);
wsprintf(str, _T("test2: %f=%f"), dbl, test3(1,2,3,dbl));
OutputDebugString(str);
return 0;
}
Output where test3 fail:
Code: [Select]
ODS: test1: 1.000000=1.000000
ODS: test2: 1.000000=1.000000
ODS: test2: 1.000000=0.000000
EDIT:
Code: [Select]
test3:
  [00011090] E92D4030     stmdb         sp!, {r4, r5, lr}
  [00011094] E1A04003     mov           r4, r3  <- ? overwrite register
  [00011098] E1A05004     mov           r5, r4  <- ?
  [0001109C] E3520FFA     cmp           r2, #3E8
  [000110A0] DA000004     ble           000110B8
  [000110A4] E59F0004     ldr           r0, [pc, #+4]  ; PC+8+4 = 000110B0
  [000110A8] E59F1004     ldr           r1, [pc, #+4]  ; PC+8+4 = 000110B4
  [000110AC] EA000003     b             000110C0
  [000110B0] A3D70A3D     bicges        r0, r7, #3D000
  [000110B4] C0915C70     addgts        r5, r1, r0, ror ip
@294:
  [000110B8] E1A00004     mov           r0, r4
  [000110BC] E1A01005     mov           r1, r5
@293:
  [000110C0] E8BD8030     ldmia         sp!, {r4, r5, pc}
« Last Edit: February 16, 2012, 04:36:31 PM by timovjl »
May the source be with you

ks65

  • Guest
Re: Double variables in function calls
« Reply #8 on: February 16, 2012, 09:36:52 PM »
Common Tater: %f gives no solution, independent of sprintf or wsprintf

timovjl: Thank you for your suggestion to analyse the optimization

I solved the problem of my test application by switching off the speed optimization: -Ot -> -Ob1

This shows that there may be a bug in the compilers optimization for ARM code.