faster double to 32.32 fixed?

G

Gernot Frisch

I have a platform that does not store IEEE doubles, but swaps
upper/lower 4 bytes in comparison to x86. Anyway, I solved the problem
by converting my double to 32.32 fixed point like this:

inline sll dbl2sll(double dbl)
{
return (sll)(dbl * (double)(1LL<<32LL));
}

which, however is quite expensive, since the platform has no floating
point processor. Is there any faster way of doing this?
 
T

Thad Smith

Gernot said:
I have a platform that does not store IEEE doubles, but swaps
upper/lower 4 bytes in comparison to x86. Anyway, I solved the problem
by converting my double to 32.32 fixed point like this:

inline sll dbl2sll(double dbl)
{
return (sll)(dbl * (double)(1LL<<32LL));
}

which, however is quite expensive, since the platform has no floating
point processor. Is there any faster way of doing this?

You can write platform specific code by making a union contains the
double and a structure containing unsigned characters and probably other
integer types to allow you access to the binary representation. You
will probably do some masking and shifting based on the exponent.
 
G

Gernot Frisch

Thad Smith said:
You can write platform specific code by making a union contains the
double and a structure containing unsigned characters and probably
other integer types to allow you access to the binary
representation. You will probably do some masking and shifting
based on the exponent.


is there any way to determine the float layout at compile time?
 
E

Eric Sosman

Gernot Frisch wrote On 02/14/07 08:31,:
is there any way to determine the float layout at compile time?

To the best of my knowledge, no.

If the macro __STDC_IEC_559__ is defined, the compiler claims
to use IEEE floating-point. But that's not of much help to you
because it still doesn't tell you what the various bits of a
float or double or long double signify when you manipulate them
as an array of unsigned char, for example.

Your best bet is to study the various platforms of interest
and #define a platform-specific macro for each, indicating what
you've learned about the floating-point formats. Then test those
macros inside dbl2sll() and similar functions. You could probably
write a "helper" program that would study what some selected
floating-point values look like when viewed as unsigned char[]
and then output the appropriate set of #define lines; you'd
run this program ahead of time and direct its output to a .h file
to be #include'd in the "real" build.
 
G

Gernot Frisch

Eric Sosman said:
You could probably
write a "helper" program that would study what some selected
floating-point values look like when viewed as unsigned char[]
and then output the appropriate set of #define lines; you'd
run this program ahead of time and direct its output to a .h file
to be #include'd in the "real" build.

Great idea. I'll do that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top