Page 1 of 1
File Library: add endianess flags
Posted: Sat Feb 15, 2020 4:34 pm
by Tristano
It would be really useful to have an optional flag to deal with endianess in file read/write commands that deal with multi-byte data types.
Examples:
Code: Select all
ReadDouble(#File, #big_endian)
ReadLong(#File, #big_endian)
WriteLong(#File, Number, #big_endian)
etc., where the read values are adjusted to little endianess at read time.
Because of the strong ties between PureBasic and the Amiga world, and since many vintage file formats are stored using big endianess (e.g. Amiga IFF), it would be nice to see endianess handling added to PB natively. Hopefully, adding this to the PB language shouldn't be a huge work, and it would provide a much more efficient solution than custom workarounds, besides keeping the source code slimmer.
Since little-endian is the default, it probably makes sense to only add the big-endian flag (unless also adding #little_endian makes sense for completeness sake, even though it wouldn't change the current behavior).
Re: File Library: add endianess flags
Posted: Mon Feb 17, 2020 6:29 am
by kenmo
+1
It would be convenient to have this functionality built-in.
I think endianness only applies to integers though.
For floats and doubles, if I'm not mistaken, the bit format is well-defined by IEEE standards, and there's no such thing as a Little Endian or Big Endian float.
Re: File Library: add endianess flags
Posted: Mon Feb 17, 2020 1:37 pm
by hoerbie
+1
And if this is added, it would be nice to be added to PeekW/U/L/I/Q and PokeW/U/L/I/Q the same way.
Re: File Library: add endianess flags
Posted: Mon Feb 17, 2020 10:28 pm
by Kurzer
+1000
the "wrong" bit order was one of the hardest brainfuck for me while switching to the "windows world" after years of assembler coding on the Amiga.
Sent via mobile phone
Re: File Library: add endianess flags
Posted: Tue Feb 18, 2020 5:29 pm
by kenmo
Thinking more, I'd rather see these as separate Big Endian functions, not flags, like SDL does it:
https://wiki.libsdl.org/CategoryIO
Because:
1. Reading/writing files and peeking/poking memory can be performance critical! If you're processing thousands or millions of integers that's a million added flag checks.
2. 99% of the time PB uses Little Endian anyway (it's on LE operating systems, with LE native data types).
3. Calling a new function is ~equal amount of code change as calling the existing function with a new parameter.
Re: File Library: add endianess flags
Posted: Tue Feb 18, 2020 6:57 pm
by Josh
kenmo wrote:1. Reading/writing files and peeking/poking memory can be performance critical! If you're processing thousands or millions of integers that's a million added flag checks.
An additional flag does not necessarily lead to more computing work. A compiler has more possibilities with a function than we have with a procedure.
Re: File Library: add endianess flags
Posted: Tue Feb 18, 2020 7:04 pm
by mk-soft
Write own short functions...
X64
Code: Select all
Procedure.f ReadFloatBigEndian(File)
Protected r1.f, value.l
value = ReadLong(File)
!mov eax, dword[p.v_value]
!bswap eax
!mov dword[p.v_r1], eax
ProcedureReturn r1
EndProcedure
Procedure.d ReadDoubleBigEndian(File)
Protected r1.d, value.q
value = ReadQuad(File)
!mov rax, qword[p.v_value]
!bswap rax
!mov qword[p.v_r1], rax
ProcedureReturn r1
EndProcedure
Procedure.l ReadLongBigEndian(File)
Protected value.l
value = ReadLong(File)
!mov eax, dword[p.v_value]
!bswap eax
ProcedureReturn
EndProcedure
Procedure.q ReadQuadBigEndian(File)
Protected value.q
value = ReadQuad(File)
!mov rax, qword[p.v_value]
!bswap rax
ProcedureReturn
EndProcedure
fltVal.f = ReadFloatBigEndian(File)
dblVal.d = ReadDoubleBigEndian(File)
Re: File Library: add endianess flags
Posted: Sun Apr 18, 2021 5:41 pm
by NicTheQuick
+1