mackron / dr_libs

Audio decoding libraries for C/C++, each in a single source file.
Other
1.24k stars 205 forks source link

Make it compile on Cortex M0 (Raspberry Pico) #259

Closed mrdudz closed 9 months ago

mrdudz commented 11 months ago

The M0 cores do not have some instructions that the decoders use, this patch will still make it compile:

diff --git a/dr_mp3.h b/dr_mp3.h
index 660dd71..e155e54 100644
--- a/firmware/dr_mp3.h
+++ b/firmware/dr_mp3.h
@@ -713,7 +713,7 @@ static int drmp3_have_simd(void)

 #endif

-#if defined(__ARM_ARCH) && (__ARM_ARCH >= 6) && !defined(__aarch64__) && !defined(_M_ARM64)
+#if defined(__ARM_ARCH) && (__ARM_ARCH >= 6) && !defined(__aarch64__) && !defined(_M_ARM64) && !defined(__ARM_ARCH_6M__)
 #define DRMP3_HAVE_ARMV6 1
 static __inline__ __attribute__((always_inline)) drmp3_int32 drmp3_clip_int16_arm(drmp3_int32 a)
 {

and:

diff --git a/dr_flac.h b/dr_flac.h
index 1ede99f..e48aa07 100644
--- a/firmware/dr_flac.h
+++ b/firmware/dr_flac.h
@@ -1851,7 +1851,7 @@ static DRFLAC_INLINE drflac_uint32 drflac__swap_endian_uint32(drflac_uint32 n)
     #if defined(_MSC_VER) && !defined(__clang__)
         return _byteswap_ulong(n);
     #elif defined(__GNUC__) || defined(__clang__)
-        #if defined(DRFLAC_ARM) && (defined(__ARM_ARCH) && __ARM_ARCH >= 6) && !defined(DRFLAC_64BIT)   /* <-- 64-bit inline assembly has not been tested, so disabling for now. */
+        #if defined(DRFLAC_ARM) && (defined(__ARM_ARCH) && __ARM_ARCH >= 6) && !defined(__ARM_ARCH_6M__) && !defined(DRFLAC_64BIT)   /* <-- 64-bit inline assembly has not been tested, so disabling for now. */
             /* Inline assembly optimized implementation for ARM. In my testing, GCC does not generate optimized code with __builtin_bswap32(). */
             drflac_uint32 r;
             __asm__ __volatile__ (
@@ -2815,7 +2815,7 @@ static DRFLAC_INLINE drflac_uint32 drflac__clz_lzcnt(drflac_cache_t x)

                 return r;
             }
-        #elif defined(DRFLAC_ARM) && (defined(__ARM_ARCH) && __ARM_ARCH >= 5) && !defined(DRFLAC_64BIT)   /* <-- I haven't tested 64-bit inline assembly, so only enabling this for the 32-bit build for now. */
+        #elif defined(DRFLAC_ARM) && (defined(__ARM_ARCH) && __ARM_ARCH >= 5) && !defined(__ARM_ARCH_6M__) && !defined(DRFLAC_64BIT)   /* <-- I haven't tested 64-bit inline assembly, so only enabling this for the 32-bit build for now. */
             {
                 unsigned int r;
                 __asm__ __volatile__ (
mackron commented 11 months ago

Thanks for the report. Just for my own reference, do by chance know the exact instructions that aren't supported?

mrdudz commented 11 months ago

I don't know, sorry ... i just disabled them where it complained :) (There must be a list somewhere)

mrdudz commented 11 months ago

i found this "RP2040 is based on the Cortex-M0+ core design. The M0+ uses the ARMv6-M Thumb instruction set" - so it does not supports the instructions that only exist in 32bit mode.

mackron commented 11 months ago

I've gone ahead and updated dr_mp3 and dr_flac in the dev branch with your proposed fixes. Before I get that released to the master branch, would you be able to give that a try when you get a chance just to verify?

mrdudz commented 11 months ago

I will report back if it actually works when i have the hardware with actual audio output... so far i can only confirm it builds and doesn't crash - no idea if the decoded stream is OK :)

mackron commented 11 months ago

It should be fine. That particular code was just an optimized path. It'll just fall back to the generic C implementation instead. I'll get this released shortly. Thanks for checking that it compiles.