Re: poke8 problems : 64-bit numbers in a 53-bit language

new topic     » goto parent     » topic index » view thread      » older message » newer message

Obviously an error of some sort is unavoidable, though 0 is a bit extreme, and as you say what it really needs is documenting properly. Here is what I wrote in the documentation of peek for Phix:

The peek8s and peek8u routines are not intended for use on 32-bit; for more details see below.

...

While somewhat flakey versions of peek8s and peek8u now exist in 32-bit Phix they are not formally supported; any code that is required to run on both 32 and 64 bit should stick to using peek4s/u and below. Atoms in 64-bit Phix use 80-bit floats that have a maximum precision of 64 bits, perfect for peek8s/u. However, atoms in 32-bit Phix use 64-bit floats that have a maximum precision of 53 bits, hence loss of data is avoided by making these routines return a two-element pair of unsigned dword-sized blocks when necessary, as the following example (which assumes little endian) shows. When peek8s or peek8u is used in a 32-bit application (not recommended and not officially supported), it checks for precision loss:

poke(addr,{0,0,0,0,0,0,0,#80,1,0,0,0,0,0,0,#80}) 
result = peek8u({addr,2}) 

On 64-bit result gets set to {#8000000000000000,#8000000000000001}
On 32-bit result gets set to {#8000000000000000,{#80000000,#00000001}}.

...

and obviously it would all be far easier if you just did it all using peek4s/u and poke4s/u, as I recommended in the first place.

...

In truth, there are two reasons why peek8s/u exist at all on 32-bit: peek8u is used (lightly and in a non-critical manner) when cross-compiling, ie when a 32-bit Phix is asked, via a format directive, to create a 64-bit executable, and secondly so that code such as

if machine_bits()=32 then 
    r = peek4u(k) 
else -- machine_bits()=64 
    r = peek8u(k) 
end if 

compiles cleanly rather than complains that peek8u does not exist, even though you are not going to call it because you understand and accept that it can be a bit flakey on 32-bit.


The other thing is that you cannot catch anything in the backend/poke8: there is no way it could tell that you've dropped some precision somewhere and the number you passed it isn't quite what you meant. Unless you meant there are some numbers that should work but don't, in which case a) I wouldn't mind testing them on Phix, and b) surely it would be just as easy to fix it so it works right as trap some "error".

Pete

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu