Fix channel swapping logic for BigEndian systems (#37)

* Fix channel swapping logic for BigEndian systems
This commit is contained in:
bsmiles32 2021-02-20 05:59:12 +01:00 committed by GitHub
parent f352528e43
commit af6af5b1fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -375,7 +375,18 @@ void sdl_push_samples(struct sdl_backend* sdl_backend, const void* src, size_t s
unsigned char* dst = cbuff_head(&sdl_backend->primary_buffer, &available);
if (size <= available)
{
if (sdl_backend->swap_channels) {
/* Confusing logic but, for LittleEndian host using memcpy will result in swapped channels,
* whereas the other branch will result in non-swapped channels.
* For BigEndian host this logic is inverted, memcpy will result in non swapped channels
* and the other branch will result in swapped channels.
*
* This is due to the fact that the core stores 32bit words in native order in RDRAM.
* For instance N64 bytes "Lh Ll Rh Rl" will be stored as "Rl Rh Ll Lh" on LittleEndian host
* and therefore should the non-memcpy path to get non swapped channels,
* whereas on BigEndian host the bytes will be stored as "Lh Ll Rh Rl" and therefore
* memcpy path results in the non-swapped channels outcome.
*/
if (sdl_backend->swap_channels ^ (SDL_BYTEORDER == SDL_BIG_ENDIAN)) {
memcpy(dst, src, size);
}
else {