DSPRelated.com
Forums

Strange situation with float return value. VDSP++ 3.5 SHARC.

Started by Unknown October 21, 2004
Hello,

The following strange situation is happening to me, I'd like you help me to
find out why, and how to solve it.

I have several functions, prototyped like this (in .h files):

float function1 (void);
float function2 (float input);

Their definitions are something like this:

float function1 (void)
{
 float output;

 // Do some processing based on data defined with global scope
 // Save results in "output"

 return output;
}

float function2 (float input)
{
 float output;

 // Do some processing based on "input"
 // Save results in "output"

 return output;
}

Main program uses them like this:

void main void
{
 float tmpData;

 tmpData = function1();
 tmpData = function2(tmpData);
}

Nothing strange so far. The weirdness comes from the fact that, although the
return value from "function1" is declared as float, and the calculated
floating point value "output" in funcion 1 is OK, the compiler generates
code like this:

[008429] cjump function1(db);
[00842A] dm(i7,m7)=r2;
[00842B] dm(i7,m7)=pc;
[00842C] f4=float r0;
[00842D] dm(0xfffffffe,i6)=r4;

Here the instruction "f4=float r0;" messes everything up. The generated code
for function2 and returning is OK, what I'd expect:

[008432] r4=r0;
[008433] cjump function2(db);
[008434] dm(i7,m7)=r2;
[008435] dm(i7,m7)=pc;
[008436] dm(0xfffffffe,i6)=r0;

Which doesn't convert the returned falue from int to float again.

Why does the compiler think that the returned value from function1 is an
int, and does the conversion? I tried having function1 with parameters being
passed, like "float function1 (float dummy)", and passing 0.0 as the value
for "dummy", but the generated code is the same.

I need your advice, guys.

Thanks a lot in advance,

--
Jaime Andr�s Aranguren Cardona
jaac@nospam.sanjaac.com
SanJaaC Electronics
Soluciones en DSP
www.sanjaac.com

(Remove "nospam" from e-mail address)


On Thu, 21 Oct 2004 15:52:55 -0500, "Jaime Andr�s Aranguren Cardona"
<jaac@nospam.sanjaac.com> wrote in comp.dsp:

> Hello, > > The following strange situation is happening to me, I'd like you help me to > find out why, and how to solve it. > > I have several functions, prototyped like this (in .h files): > > float function1 (void); > float function2 (float input);
Are you SURE that these header files, containing the prototypes for the called functions, are included in the source file that calls the functions? [snip]
> Nothing strange so far. The weirdness comes from the fact that, although the > return value from "function1" is declared as float, and the calculated > floating point value "output" in funcion 1 is OK, the compiler generates > code like this: > > [008429] cjump function1(db); > [00842A] dm(i7,m7)=r2; > [00842B] dm(i7,m7)=pc; > [00842C] f4=float r0; > [00842D] dm(0xfffffffe,i6)=r4; > > Here the instruction "f4=float r0;" messes everything up. The generated code > for function2 and returning is OK, what I'd expect: > > [008432] r4=r0; > [008433] cjump function2(db); > [008434] dm(i7,m7)=r2; > [008435] dm(i7,m7)=pc; > [008436] dm(0xfffffffe,i6)=r0; > > Which doesn't convert the returned falue from int to float again. > > Why does the compiler think that the returned value from function1 is an > int, and does the conversion? I tried having function1 with parameters being > passed, like "float function1 (float dummy)", and passing 0.0 as the value > for "dummy", but the generated code is the same. > > I need your advice, guys. > > Thanks a lot in advance,
The behavior you describe sounds like the compiler is calling the functions without a prototype in scope, which in all versions of C prior to the 1999 standard, requires that the compiler assume the function returns int. Since you assign the "implicit int" return value of a function, it generates code to convert int to float. So you have prototypes in a header, but is that header included? -- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
Hello,

Jack, thank you for making me review the inclusion of the header file in the
.c file.

The header was included, no doubt of that. Indeed, while I got response from
you, I changed the prototypes to:

void function1 (float* pSample)

and called if like this:

function1(&data);

being "data" a variable declared as float. I changed the name of the header
file from "SRC.h" to "Src.h", changed to my origila declarations (returning
"float") and everything worked just fine!!!!

I didn't notice the case sensitiveness for the pre-processor, and I just
thought that if the header file wasn't included, my modification wouldn't
work either. Why did it work (my modification with "void function1 (float*
pSample)" if the header file was not visible?

Regards,

--
Jaime Andr&#4294967295;s Aranguren Cardona
jaac@nospam.sanjaac.com
SanJaaC Electronics
Soluciones en DSP
www.sanjaac.com

(Remove "nospam" from e-mail address)

"Jack Klein" <jackklein@spamcop.net> escribi&#4294967295; en el mensaje
news:6f0hn05lach173rrhk14ipuir5arf62msr@4ax.com...
> On Thu, 21 Oct 2004 15:52:55 -0500, "Jaime Andr&#4294967295;s Aranguren Cardona" > <jaac@nospam.sanjaac.com> wrote in comp.dsp: > > > Hello, > > > > The following strange situation is happening to me, I'd like you help me
to
> > find out why, and how to solve it. > > > > I have several functions, prototyped like this (in .h files): > > > > float function1 (void); > > float function2 (float input); > > Are you SURE that these header files, containing the prototypes for > the called functions, are included in the source file that calls the > functions? > > [snip] > > > Nothing strange so far. The weirdness comes from the fact that, although
the
> > return value from "function1" is declared as float, and the calculated > > floating point value "output" in funcion 1 is OK, the compiler generates > > code like this: > > > > [008429] cjump function1(db); > > [00842A] dm(i7,m7)=r2; > > [00842B] dm(i7,m7)=pc; > > [00842C] f4=float r0; > > [00842D] dm(0xfffffffe,i6)=r4; > > > > Here the instruction "f4=float r0;" messes everything up. The generated
code
> > for function2 and returning is OK, what I'd expect: > > > > [008432] r4=r0; > > [008433] cjump function2(db); > > [008434] dm(i7,m7)=r2; > > [008435] dm(i7,m7)=pc; > > [008436] dm(0xfffffffe,i6)=r0; > > > > Which doesn't convert the returned falue from int to float again. > > > > Why does the compiler think that the returned value from function1 is an > > int, and does the conversion? I tried having function1 with parameters
being
> > passed, like "float function1 (float dummy)", and passing 0.0 as the
value
> > for "dummy", but the generated code is the same. > > > > I need your advice, guys. > > > > Thanks a lot in advance, > > The behavior you describe sounds like the compiler is calling the > functions without a prototype in scope, which in all versions of C > prior to the 1999 standard, requires that the compiler assume the > function returns int. > > Since you assign the "implicit int" return value of a function, it > generates code to convert int to float. > > So you have prototypes in a header, but is that header included? > > -- > Jack Klein > Home: http://JK-Technology.Com > FAQs for > comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html > comp.lang.c++ http://www.parashift.com/c++-faq-lite/ > alt.comp.lang.learn.c-c++ > http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

Jaime Andr&#4294967295;s Aranguren Cardona wrote:

> Jack, thank you for making me review the inclusion of the
> header file in the .c file.
> The header was included, no doubt of that. Indeed, while
> I got response from you, I changed the prototypes to:
> void function1 (float* pSample)
> and called if like this:
> function1(&data);
> being "data" a variable declared as float. I changed the name of the header > file from "SRC.h" to "Src.h", changed to my origila declarations (returning > "float") and everything worked just fine!!!!
> I didn't notice the case sensitiveness for the pre-processor, and I just > thought that if the header file wasn't included, my modification wouldn't > work either. Why did it work (my modification with "void function1 (float* > pSample)" if the header file was not visible?
Without a prototype the system assumes int for return values, and it assumes that all function parameters have the right type, after standard promotions. char and short are converted to int, float is converted to double. Pointers stay pointers. It is reasonably likely that a function returning a pointer will work if assumed to return an int, though with some warnings. Many systems pass pointer and int the same way. I presume you are using a unix-like system. While C and the preprocessor are case sensitive the DOS/Windows file systems are not. That could be confusing porting from Windows to unix. -- glen