lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> On mixed-endian architectures, packfloat reads:
> 
>     may not work correctly for non-native endianess
> 
> and packint reads:
> 
>     may not work correctly for native endianess
> 
> Is there a mistake for one of the two or are these really different? I
> don't think I ever used a mixed-endian computer, but for reliability
> I'd rather have all cases be handled properly (my priority is on
> "non-native" for binary files and protocols, but I'm also using my
> serialization library for a few "native" use cases).

They are different. packint does all work by hand, packfloat just
copies the internal representation for a number. So, packint does
not have the code to serialize data in a mixed-endian way, while
packfloat does not have the code to "correct" a float that is
already represented in a mixed-endian way.


> The packint function says it supports any size value from 1 to 8. Is
> that any power-of-two value or any integer value? I.e. would it work
> with 3 or 7?

Yes, it would.


> Regarding negative integers, can you specify in the manual whether you
> use some defined representation (two's complement) or whether you rely
> on the C compiler format (which AFAIK may not be two's complement).
> I.e. can I reliably expect that I can pack an integer from -8 388 608
> to +8 388 607 in a size 3 (24bits) integer?

In fact it uses two complement, but it also assumes two complement.
It probably will not work correctly in a non-two complement machine.
(These functions have to do some bit manipulations to adapt numbers
between different sizes.)


> A similar question holds
> for floats: do you use the C compiler format or a clearly defined IEEE
> binary32 and binary64?

For floats we use the native format.


> Finally two suggestions. I'd very much enjoy a packuint, since
> unsigned integers are as common as signed ones in binary data. I
> realize it would be easy to implement on top of packint, but packint
> and packfloat can also be implemented on top of string.char.

Both packint and unpackint should work fine for unsigned integers (as
long as these unsigned integers can be represented in lua_Integer).
When packing, you do not have to do anything. When unpacking, all you
have to do is unpackint(...) & (2^n - 1) to clear occasional extra
signal bits.


-- Roberto