SDL: emscripten: Fixes for data addresses above 2gb

From 02434cd29335c2032a0efeca0a1f1058fa534702 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Tue, 8 Oct 2024 17:41:18 -0400
Subject: [PATCH] emscripten: Fixes for data addresses above 2gb

This includes both wasm64 and wasm32 when addressing more than 2gb of memory.

Fixes: #9052

(Manually cherry-picked from 3deb07ea395373204462130c1e062bc1f71fe060.)
---
 include/SDL3/SDL_stdinc.h                        | 4 ++--
 src/audio/emscripten/SDL_emscriptenaudio.c       | 9 ++++++++-
 src/video/emscripten/SDL_emscriptenframebuffer.c | 2 +-
 src/video/emscripten/SDL_emscriptenmouse.c       | 2 +-
 4 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/include/SDL3/SDL_stdinc.h b/include/SDL3/SDL_stdinc.h
index c369917c5039f..b443101369ec0 100644
--- a/include/SDL3/SDL_stdinc.h
+++ b/include/SDL3/SDL_stdinc.h
@@ -428,7 +428,7 @@ typedef Sint64 SDL_Time;
 #define SDL_PRIs64 "I64d"
 #elif defined(PRIs64)
 #define SDL_PRIs64 PRIs64
-#elif defined(__LP64__) && !defined(SDL_PLATFORM_APPLE)
+#elif defined(__LP64__) && !defined(SDL_PLATFORM_APPLE) && !defined(__EMSCRIPTEN__)
 #define SDL_PRIs64 "ld"
 #else
 #define SDL_PRIs64 "lld"
@@ -439,7 +439,7 @@ typedef Sint64 SDL_Time;
 #define SDL_PRIu64 "I64u"
 #elif defined(PRIu64)
 #define SDL_PRIu64 PRIu64
-#elif defined(__LP64__) && !defined(SDL_PLATFORM_APPLE)
+#elif defined(__LP64__) && !defined(SDL_PLATFORM_APPLE) && !defined(__EMSCRIPTEN__)
 #define SDL_PRIu64 "lu"
 #else
 #define SDL_PRIu64 "llu"
diff --git a/src/audio/emscripten/SDL_emscriptenaudio.c b/src/audio/emscripten/SDL_emscriptenaudio.c
index 89ddbcd236687..00ae39a69e20b 100644
--- a/src/audio/emscripten/SDL_emscriptenaudio.c
+++ b/src/audio/emscripten/SDL_emscriptenaudio.c
@@ -40,6 +40,13 @@ static bool EMSCRIPTENAUDIO_PlayDevice(SDL_AudioDevice *device, const Uint8 *buf
 {
     const int framelen = SDL_AUDIO_FRAMESIZE(device->spec);
     MAIN_THREAD_EM_ASM({
+        /* Convert incoming buf pointer to a HEAPF32 offset. */
+        #ifdef __wasm64__
+        var buf = $0 / 4;
+        #else
+        var buf = $0 >>> 2;
+        #endif
+
         var SDL3 = Module['SDL3'];
         var numChannels = SDL3.audio_playback.currentPlaybackBuffer['numberOfChannels'];
         for (var c = 0; c < numChannels; ++c) {
@@ -49,7 +56,7 @@ static bool EMSCRIPTENAUDIO_PlayDevice(SDL_AudioDevice *device, const Uint8 *buf
             }
 
             for (var j = 0; j < $1; ++j) {
-                channelData[j] = HEAPF32[$0 + ((j*numChannels + c) << 2) >> 2];  // !!! FIXME: why are these shifts here?
+                channelData[j] = HEAPF32[buf + (j*numChannels + c)];
             }
         }
     }, buffer, buffer_size / framelen);
diff --git a/src/video/emscripten/SDL_emscriptenframebuffer.c b/src/video/emscripten/SDL_emscriptenframebuffer.c
index 50e945bc5f061..f300555e41901 100644
--- a/src/video/emscripten/SDL_emscriptenframebuffer.c
+++ b/src/video/emscripten/SDL_emscriptenframebuffer.c
@@ -88,7 +88,7 @@ bool Emscripten_UpdateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *wind
             SDL3.imageCtx = SDL3.ctx;
         }
         var data = SDL3.image.data;
-        var src = pixels >> 2;
+        var src = pixels / 4;
         var dst = 0;
         var num;
 
diff --git a/src/video/emscripten/SDL_emscriptenmouse.c b/src/video/emscripten/SDL_emscriptenmouse.c
index 8ded04d10f638..44ca4dd773c9f 100644
--- a/src/video/emscripten/SDL_emscriptenmouse.c
+++ b/src/video/emscripten/SDL_emscriptenmouse.c
@@ -94,7 +94,7 @@ static SDL_Cursor *Emscripten_CreateCursor(SDL_Surface *surface, int hot_x, int
 
         var image = ctx.createImageData(w, h);
         var data = image.data;
-        var src = pixels >> 2;
+        var src = pixels / 4;
 
         var data32 = new Int32Array(data.buffer);
         data32.set(HEAP32.subarray(src, src + data32.length));