Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > float to fixed

Reply
Thread Tools

float to fixed

 
 
Gernot Frisch
Guest
Posts: n/a
 
      02-09-2007

// 2 questions:
// On my gp2x (arm?) this code does not work (works on x86 and on
arm-pocketpc)

static inline sll dbl2sll(double dbl)
{
union {
double d;
unsigned u[2];
ull _ull;
sll _sll;
} in, retval;
register unsigned exp;

/* Move into memory as args might be passed in regs */
in.d = dbl;
/* Leading 1 is assumed by IEEE */
retval.u[1] = 0x40000000;

/* Unpack the mantissa into the unsigned long */
retval.u[1] |= (in.u[1] << 10) & 0x3ffffc00;
retval.u[1] |= (in.u[0] >> 22) & 0x000003ff;
retval.u[0] = in.u[0] << 10;

/* Extract the exponent and align the decimals */
exp = (in.u[1] >> 20) & 0x7ff;
if (exp)
retval._ull >>= 1053 - exp;
else
return 0L;

/* Negate if negative flag set */
if (in.u[1] & 0x80000000)
retval._sll = -retval._sll;

return retval._sll;
}

// but this does:
static inline sll dbl2sll(double dbl)
{
return (sll)(dbl * (double)(1LL<<32LL));
}


// 2.nd question
// is there a faster way instead of what I did (2nd function) to
convert an float/double to 32.32 fixed point?

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com


 
Reply With Quote
 
 
 
 
John Ratliff
Guest
Posts: n/a
 
      02-09-2007
Gernot Frisch wrote:
> // 2 questions:
> // On my gp2x (arm?) this code does not work (works on x86 and on
> arm-pocketpc)
>
> static inline sll dbl2sll(double dbl)
> {
> union {
> double d;
> unsigned u[2];
> ull _ull;
> sll _sll;
> } in, retval;
> register unsigned exp;
>
> /* Move into memory as args might be passed in regs */
> in.d = dbl;
> /* Leading 1 is assumed by IEEE */
> retval.u[1] = 0x40000000;
>
> /* Unpack the mantissa into the unsigned long */
> retval.u[1] |= (in.u[1] << 10) & 0x3ffffc00;
> retval.u[1] |= (in.u[0] >> 22) & 0x000003ff;
> retval.u[0] = in.u[0] << 10;
>
> /* Extract the exponent and align the decimals */
> exp = (in.u[1] >> 20) & 0x7ff;
> if (exp)
> retval._ull >>= 1053 - exp;
> else
> return 0L;
>
> /* Negate if negative flag set */
> if (in.u[1] & 0x80000000)
> retval._sll = -retval._sll;
>
> return retval._sll;
> }


You are assuming portability where none is guaranteed. Using unions in
this way is implementation dependent. The fact that it works on x86 and
powerpc is a coincidence.

>
> // but this does:
> static inline sll dbl2sll(double dbl)
> {
> return (sll)(dbl * (double)(1LL<<32LL));
> }


This looks pretty good from a platform-independent standpoint.

>
>
> // 2.nd question
> // is there a faster way instead of what I did (2nd function) to
> convert an float/double to 32.32 fixed point?
>


Sorry, this I do not know.

--John Ratliff
 
Reply With Quote
 
 
 
 
Andrew Koenig
Guest
Posts: n/a
 
      02-13-2007
"John Ratliff" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...

> You are assuming portability where none is guaranteed. Using unions in
> this way is implementation dependent. The fact that it works on x86 and
> powerpc is a coincidence.


Not to mention that the ordering of the bits in an IEEE floating-point
number is also implementation dependent.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
converting 64-bit fixed-point to float John Fisher Python 4 07-22-2007 06:46 AM
Free Fixed-Width/Fixed-Pitch fonts? johnp HTML 4 05-23-2005 06:14 AM
need code to convert float format to internal java float format which is kept in 4 bytes integer Andy Java 7 05-10-2004 09:26 PM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments