File Library: add endianess flags

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Tristano
Enthusiast
Enthusiast
Posts: 190
Joined: Thu Nov 26, 2015 6:52 pm
Location: Italy
Contact:

File Library: add endianess flags

Post 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).
The PureBASIC Archives: FOSS Resources:
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: File Library: add endianess flags

Post 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.
hoerbie
Enthusiast
Enthusiast
Posts: 119
Joined: Fri Dec 06, 2013 11:57 am
Location: DE/BY/MUC

Re: File Library: add endianess flags

Post 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.
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 664
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

Re: File Library: add endianess flags

Post 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
PB 6.02 x64, OS: Win 7 Pro x64 & Win 11 x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age in 2023: 56y
"Happiness is a pet." | "Never run a changing system!"
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: File Library: add endianess flags

Post 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.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: File Library: add endianess flags

Post 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.
sorry for my bad english
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: File Library: add endianess flags

Post 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)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: File Library: add endianess flags

Post by NicTheQuick »

+1
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Post Reply