Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing C++ code for endian-independence

How can I test or check C++ code for endian-independence? It's already implemented, I would just like to verify that it works on both little- and big-endian platforms.

I could write unit tests and run them on the target platforms, but I don't have the hardware. Perhaps emulators?

Are there compile time checks that can be done?

like image 705
sourcenouveau Avatar asked Jun 20 '11 14:06

sourcenouveau


People also ask

How do I test my endian machine?

We can write a small tool to test Whether a Machine is Big Endian or Little Endian in C/C++. First, we declare a 16-bit integer (short int), which has the value 0x0001, then we gets its pointer and deference it. If the MSB is stored at lower address (e.g. the value that pointer points to), then it is little endian.

How do you know if you have endianness?

If it is little-endian, it would be stored as “01 00 00 00”. The program checks the first byte by dereferencing the cptr pointer. If it equals to 0, it means the processor is big-endian(“00 00 00 01”), If it equals to 1, it means the processor is little-endian (“01 00 00 00”).

What is endian in C language?

Endianness. The attribute of a system that indicates whether integers are represented with the most significant byte stored at the lowest address (big endian) or at the highest address (little endian). Each address stores one element of the memory array.

What is endianness and its type and program on it to check endianness?

Little and big endian are two ways of storing multibyte data-types ( int, float, etc). In little endian machines, last byte of binary representation of the multibyte data-type is stored first. On the other hand, in big endian machines, first byte of binary representation of the multibyte data-type is stored first.


2 Answers

If you have access to an x86-based Mac then you can take advantage of the fact that Mac OS X has PowerPC emulation built in as well as developer tool support for both x86 (little endian) and PowerPC (big endian). This enables you to compile and run a big and little endian executable on the same platform, e.g.

$ gcc -arch i386 foo.c -o foo_x86 # build little endian x86 executable $ gcc -arch ppc foo.c -o foo_ppc  # build big endian PowerPC executable 

Having built both big endian and little endian executables you can then run whatever unit tests you have available on both, which will catch some classes of endianness-related problems, and you can also compare any data generated by the executables (files, network packets, whatever) - this should obviously match.

like image 92
Paul R Avatar answered Oct 18 '22 22:10

Paul R


You can set up an execution environment in the opposite endianness using qemu. For example if you have access to little-endian amd64 or i386 hardware, you can set up qemu to emulate a PowerPC Linux platform, run your code there.

like image 38
Ken Bloom Avatar answered Oct 18 '22 22:10

Ken Bloom