[Scummvm-devel] Wii & Gamecube (resp. PowerPC) vs. unaligned memory access

Max Horn max at quendi.de
Fri May 6 13:27:30 CEST 2011


So, in summary, unaligned access seems to be OK on x86, and on *some* PowerPC systems. And is not in general known to be a good idea anywhere else.

Anyway, I would appreciate some input on the whole "unaligned access" business. Should we even bother running a test for it, and instead just assume "unaligned access is OK" on x86, but nowhere else? 

You see, we currently have some test code in configure which disables signal handlers, and then performs an unaligned access, to check whether (a) it works at all without crashing, and (b) whether it returns correct results (remember, some nasty archs silently return bogus results upon unaligned memory access).

But I am not sure how well that works. In particular, it does attempt to detect whether unaligned access is possible without a (super slow) fault handler. But maybe on some archs, the fault handler will not be deactivated by the signal() calls. So it might say "yes" on a system where unaligned access is supported but super-slow. Moreover, it performs the test on an 8 byte block. On some systems, unaligned access may works in 8 byte blocks, but not when crossing an 8 byte boundary. Granted, the test could be adjusted for that, but what if the same problem occurs with say 16 byte boundaries?

Lastly, how bad is our "slow" code path for systems that do not support unaligned access anyway? I imagine that on desktop machines, the difference is irrelevant and not noticeable; on the other hand, for small devices we mostly disable unaligned access *anyway*.
Plus, for GCC, we already use slightly "optimized" C code that tells the compiler that unaligned access might happens. For MSVC 2005+, we could add something similar using the "__unaligned" keyword.

So, given all this, maybe we should drop the test for unaligned access? Opinions?


In summary, I see the following three choices:

1) When compiling for x86, enable unaligned access. Otherwise disable it.
2) When compiling for x86, enable unaligned access. Otherwise, if cross compiling, disable it. Otherwise, run test code.
3) If cross compiling, enable unaligned access for x86, disable it otherwise. If not cross compiling, run test code.

Note thatnd my branch <https://github.com/fingolfin/scummvm/compare/scummvm:master...fingolfin:configure/cleanup> currently implements this variant:

2b) If compiling for x86, enable unaligned access; if compiling for a known "bad" arch, disable it. Otherwise, if cross compiling, disable it. Otherwise, run test code.



Bye,
Max



More information about the Scummvm-devel mailing list