?

Log in

piggu

faerie_gift in td_project

Are you little or big (endian)?

I transferred the code onto to our PowerPC based Mac Mini last night and it compiled first time... it was however a shame that it didn't actually work.

Welcome to the world of endianess... where depending on whether or not you are putting some intel employees kids though college or not, or IBM kids, your lovely Pascal code might compile, but totally fail to do what it was supposed to.

The problem was, that I developed this on a PC, and moved the code to PowerPC based mac. Pascal is a wonderfully optimized language, down to the point where it uses the byte ordering of the CPU to represent words, integers and longwords. This is fine, except when you write code that is intended to be cross-platform. In that case, you need to ensure that when it compiles, it reads and writes the data to and from files in the correct way...

A LongWord is 4 bytes. On a Big Endian system, the most significant byte (MSB) is first in memory, and on a little endian system (ie. Intel based), the LSB (least significant byte) is written first. Now, if I ask the system to read a longword from a file stream, you see that this might present problems.

The solution, at least on FreePascal are some handy defines the compiler gives you...

Either ENDIAN_LITTLE, or ENDIAN_BIG will be defined, depending on the target architecture you are compiling for. My solution was to create some functions that would compile differently based on what the target system was. I created a unit called "Endian" which provides four functions...

function endianLittleLong( l: longword ): longword;
function endianLittleWord( w: word ): word;
function endianBigLong( l: longword ): longword;
function endianBigWord( w: word ): word;

So, if a value we read from a file is supposed to be a Little Endian LongWord value, we would wrap the access in endianLittleLong(). On a little endian system, this does nothing except return the value as is, but thanks to the wonders of conditional compilation, this same function will reverse the byte ordering of the LongWord on a BigEndian system.

After doing this, everything worked on PowerPC. Theoretically now, the same code should compile on Windows as well, and act correctly there as well.

Long story short (no pun intended), I have tested and run tapdancer on a PowerPC Mac and it runs fine. A little quicker than my Windows 2000 machine (but that is a Pentium II).

April.

Comments

c2n

March 2008

S M T W T F S
      1
2345678
9101112131415
16171819202122
23242526272829
3031     
Powered by LiveJournal.com