473,320 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

float to fixed


// 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
Feb 9 '07 #1
2 1889
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
Feb 10 '07 #2
"John Ratliff" <us**@example.netwrote in message
news:fL******************************@insightbb.co 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.
Feb 13 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Pau Roldan | last post by:
Hi, sorry if this has been asked previously, but I couldn't find a suitable answer in previous posts. Here is a sample page illustrating the problem:...
16
by: Wolfgang Meier | last post by:
Hi, Although it might seem like I am firing out random posts in quick succession this matter is indeed one I thought about for quite some time without coming to a conclusion: Why is it that...
8
by: Kenny ODell | last post by:
I do not know how to convert from a byte array to a float, and back again. I read data from a serial port into a byte (entire command structure which I parse). I am able to sift the data and...
2
by: Chris | last post by:
Hi The following code is giving strange results........... float fTest = 536495.61f; cout << _T("float: ") << fTest << endl; int dec, sign; char* pszTest = _fcvt(fTest, 10, &dec, &sign);...
4
by: Geky | last post by:
Why when I multiply a float , for example X10, and than convert it as an integer the new value is changed? Example: float fl = 1.8 int x; x = int(fl * 10);
8
by: abdul_n_khan | last post by:
Hello, I have a basic question related to datatypes. I am trying to read a value using Microsoft's ADO recordset from a field (lets call it 'Price') with datatype decimal(19,6) => 19 = Precision,...
8
by: avsrk | last post by:
Hello Folks , General C data types question , more geared up towards embedded folks . I have a positive float quantity with a fractional part (one decimal point) which occupies 4 bytes ....
15
by: soni2926 | last post by:
Hi, I have the following: float.Parse(myproduct.Price.Value.ToString()); myproduct.Price.Value.ToString() returns $24.00 (with the $) Is there anyway to do the above cast, I know the...
7
by: Gary Baydo | last post by:
In an effort to write a simple rounding function, I wrote the following code out of curiosity. My question is, can I rely on the output to 'make sense'? As an added 'exercise' I tried to write...
15
dlite922
by: dlite922 | last post by:
I'm back again, Intro: I've got a floating div (outerDIV) with fixed width that contains an image (IMG) and a div that contains a short text (innerDIV) Problem: In FF, the innerDIV is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.