nepx / halfix

x86 PC emulator that runs both natively and in the browser, via WebAssembly
https://nepx.github.io/halfix-demo/
GNU General Public License v3.0
681 stars 87 forks source link

What does `BIG_ENDIAN` do in fpu.h #4

Closed DCNick3 closed 4 years ago

DCNick3 commented 4 years ago

I'm not sure what does ifdef with a BIG_ENDIAN does. Is it an attemt to support arbitrary endianness hosts? Well, if it is, then it does not work. Even if standard C had something like that, at my perfectly little endian x86_64 system it is defined somewhere in depths of glibc...

nepx commented 4 years ago

Hello,

This emulator uses the SoftFloat package from Bochs, which handles big-endian data better than I do:

/*----------------------------------------------------------------------------
| Software IEC/IEEE floating-point types.
*----------------------------------------------------------------------------*/

#ifdef BX_BIG_ENDIAN
typedef struct {    // leave alignment to compiler
    uint16_t exp;
    uint64_t fraction;
} floatx80;
#else
typedef struct {
    uint64_t fraction;
    uint16_t exp;
} floatx80;
#endif

Aliasing the MMX registers to the FP stack was a hack involving a union (but it works, so there's that!), and I had to make sure that both registers were laid out the same way (replacing fraction for r8. r16, etc.). Rather than BX_BIG_ENDIAN, I went with the simpler BIG_ENDIAN.

The fact that BIG_ENDIAN is defined on x86-64 systems is a good point (and a slightly strange one, too, considering that x86-64 is a little-endian system) and something that definitely needs to be fixed, but in practice it's never caused me any trouble.

Concerning endianness: big endian hosts might work, but I doubt they do. Reworking the emulator to handle guest loads/stores would be a massive multi-month project, but sadly I don't have any big-endian systems to debug on. At the very least, all those crazy arith_rmw macros in opcodes.c will need to be reworked.