D2007: Typecasting 32 bit integer/longword to 32 bit single floating point not possible ?!?

D2007: Typecasting 32 bit integer/longword to 32 bit single floating point not possible ?!?

Post by Skybuck Fl » Wed, 06 Jan 2010 09:59:27


Currently I want to experiment with moving memory values/integers/longwords
onto the gpu.

Since the gpu works with floating points only, I was wondering if it might
be possible to simply "stuff" the integer/longwords into the gpu floating
point variables/memory cells and see what happens...

I was hoping for "bitpattern perfect" copies.

I now realize this is pretty much a pipe dream since floating point format
works totally different to integer format... so "stuffing integers" via
memory copy into "floating points" probably makes no sense at all... the
floating point value would end up being something weird... and that could
mean the gpu is not able to work with it correctly ?!?

Unless maybe the gpu/cg shader typecasts to int might work ? But I doubt
that's gonna work because that's probably something special... it's probably
the "gpu pretending to support integers ?" while in reality it's still using
floats ?

However it was a worthy Delphi experiment since it shows "converting from
longword/integer to single and back again via assignments/rounds" is not
flawless... and produces imprecision.

I am not completely happy with D2007 not supporting these type of wacky
typecasts... if I want to typecast then what's the fricking problem ?! I
feel slightly frustrated about that... but at the same time... typecasting
it in this case would have made little sense but still...

Should Delphi have support for wacky typecasts like this ? It remains open
for debate me thinks ?! ;) :)

(I will start a new thread asking how to convert between integers and single
without loss of precision if possible at all ? This thread is just to prove
that "assignments" doesn't cut it and leads to problems ! ;) :))

// *** Begin of Program ***

program TestProgram;



Test typecasting a 32 bit longword, or 32 bit integer to a 32 bit single
(floating point)

In theory this should be possible since they both use 32 bits ?!

version 0.01 created on 5 january 2010 by Skybuck Flying

Short story:

Delphi 2007 does not allow it,
Using assignments produces different bit patterns and precision loss, which
is bad !

Long story:

In Delphi 2007 cannot typecast integer/longword to single ?!?

I want to use a typecast to "copy" the integer/longword bitpattern towards
the floating point variable to see what happens and for a bitpattern perfect
copy ?!?

I assume an assignment will not give a bit perfect copy ?
vSingle := vLongword;
vSingle := vInteger;

Let's test this assumption with a quick loop that compares the bit
patterns/memory ;)

My assumption was correct, assignments do not give a bit perfect copy.

However this does not mean that conversion might produce imprecision that
to be tested seperatedly... and has been done as well...

And indeed conversion also produces imprecision/errors which proves
is necessary in case single floating point values are to be filled with bit
patterns ! otherwise a memcopy/move is necessary ! ;)

Example of program output:

*** begin of program output ***:

program started

CompareBitPatterns... aborted.
Longword to single assignment difference detected for value: 1
Single: 1.00000000000000E+0000
Longword: 1

CheckForConversionProblems... aborted.
Longword conversion error detected at value: 16777217
vSingle: 1.67772160000000E+0007
vLongword: 16777217

D2007: Typecasting 32 bit integer/longword to 32 bit single floating point not possible ?!?

Post by MitchAlsu » Thu, 07 Jan 2010 03:32:31

There are floating point implementations that do not keep the value
stored in FP registers in "memory format". That is, when a value is
loaded from memory, the bit pattern in the register is not the same as
the bit pattern in memory. However, when stored back into memory the
original bit pattern can be reconstructed and stored.

So, if you were to load a 64-bit integer bit pattern from memory into
say 64-bit Double Precision, and then store the result back in a 32-
bit format, you would not get the expected result if {the bit pattern
overflowed, needed to be rounded, or otherwise manipulated to be
forced into a 32-bit form}. Thus, these kinds of typecasts are at best
machine specific--not architecture specific, and thereby, unportable.
Over on the integer side, this kind of typecasting would carry no
"surprise" to the result.