Jump to content

Possible Bug In Long Var Shift


Recommended Posts

Hello,

 

while waiting for float variables to be available in BoostC i'm testing and adapting a little routine i have found online to extract the integer part of a floating point number. I obtained the following code:

 

long finalLong;
long tmpLong;
char exponent;
long mantissa;

long FloatToLong(long floatToBeConverted)
{	
tmpLong = *(long *)&floatToBeConverted;

mantissa = (tmpLong & 0x07fffff) | 0x800000;
exponent = 150 - ((tmpLong >> 23) & 0xff);

if (exponent < 0)
	finalLong = (mantissa << -exponent);		      
else
	finalLong = (mantissa >> exponent);

if (tmpLong & 0x80000000)
	finalLong = -finalLong;

return finalLong;
}

 

To debug this code i tryed to pass to this function the float 4.0, that is coded as: 0x40800000

 

	long test = FloatToLong(0x40800000);

 

The problem is that during simulation the execution of line:

 

		finalLong = (mantissa >> exponent);

 

is evaluated in the wrong way. If you try settign a breakpont on this line you will have the following vars values bedore execution:

 

mantissa = 0b100000000000000000000000
exponent = 21 (decimal)
finalLong = 0

 

after the execution of the right shift (21 times) the vars value is:

 

mantissa = 0b100000000000000000000000
exponent = 21 (decimal)
finalLong = 0b100000000000000000000000

 

As you can see the instruction mantissa >> 21 was not evaluated and so finally finalLong = mantissa that is wrong.

 

I tryed to search in the assembly, but i was not able to find the bug.

 

Is it my fault or is there something not working in the right way?

 

Walter

Link to post
Share on other sites

Wally,

 

This code works OK for me (target 16F876A).

 

long finalLong;
long tmpLong;
char exponent;
long mantissa;

long FloatToLong(long floatToBeConverted)
{    
   tmpLong = *(long *)&floatToBeConverted;
   
   mantissa = (tmpLong & 0x07fffff) | 0x800000;
   exponent = 150 - ((tmpLong >> 23) & 0xff);
   
   if (exponent < 0)
       finalLong = (mantissa << -exponent);              
   else
       finalLong = (mantissa >> exponent);
   
   if (tmpLong & 0x80000000)
       finalLong = -finalLong;

   return finalLong;
}

void main()
{
    long test = FloatToLong(0x40800000);
    
    // "test" has value of 4 here
    while( 1 );
}

 

Regards

Dave

Link to post
Share on other sites
Wally,

 

This code works OK for me (target 16F876A).

 

...

 

Dave,

 

that's so strange.

 

I'm working with the last SourceBoost version, and on target 18F2455 (i forgot this information)

 

I'll try again switching to the target you tryed.

 

Can you please try again on a 18F2455 target so we can make a cross test?

 

Thank you,

 

Walter

Link to post
Share on other sites

Wally,

 

I'm working with the last SourceBoost version, and on target 18F2455 (i forgot this information)

 

I'll try again switching to the target you tryed.

 

Can you please try again on a 18F2455 target so we can make a cross test?

I've tried it with 18F2455 on BoostC V6.33, and it works for me.

Please send your bad project to support@sourceboost.com and we will take a look.

 

Regards

Dave

Link to post
Share on other sites
Wally,

 

Use BoostC V6.33, then you will find it works.

 

Regards

Dave

 

I was trying it on a 'temporary' PC... so i installed the demo version. I runned the old installer... :(

 

Please forgive me for this loss of time.

 

Thank you for the fast reply.

 

Walter

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...