Skip to content

armgit / nota

Public Imported from GitHub
0

Nota — a free DAW for macOS, Windows and Linux. No subscription, no account — just open it and make music.

(fix) Windows arm64 build: MSVC intrinsics for FPCR and the spin pause

Egor Khindikaynenb98d262ca597
diff --git a/src/native/nota.engine/src/dsp/core/RtAttributes.h b/src/native/nota.engine/src/dsp/core/RtAttributes.h
index c06a0f74..0583b2ec 100644
--- a/src/native/nota.engine/src/dsp/core/RtAttributes.h
+++ b/src/native/nota.engine/src/dsp/core/RtAttributes.h
@@ -54,6 +54,9 @@ inline constexpr std::size_t kCacheLineSize = 64;
#if !defined(NOTA_CPU_PAUSE)
#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
#define NOTA_CPU_PAUSE() _mm_pause()
+#elif defined(_M_ARM64) && defined(_MSC_VER) && !defined(__clang__)
+#include <intrin.h>
+#define NOTA_CPU_PAUSE() __isb(_ARM64_BARRIER_SY)
#elif defined(__aarch64__) || defined(_M_ARM64)
#define NOTA_CPU_PAUSE() __asm__ volatile("isb sy" : : : "memory")
#else
diff --git a/src/native/nota.engine/src/dsp/core/ScopedNoDenormals.h b/src/native/nota.engine/src/dsp/core/ScopedNoDenormals.h
index 01daaa80..f2e319f6 100644
--- a/src/native/nota.engine/src/dsp/core/ScopedNoDenormals.h
+++ b/src/native/nota.engine/src/dsp/core/ScopedNoDenormals.h
@@ -9,6 +9,8 @@
#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
#include <xmmintrin.h>
+#elif defined(_M_ARM64) && defined(_MSC_VER) && !defined(__clang__)
+#include <intrin.h>
#endif
#include <cstdint>
@@ -27,13 +29,26 @@ class ScopedNoDenormals {
#if defined(__aarch64__) || defined(_M_ARM64)
uint64_t saved_ = 0;
+ // FPCR.FZ bit 24: flush-to-zero. ARM64 has no DAZ bit; FZ covers it.
void enter() noexcept {
- __asm__ volatile("mrs %0, fpcr" : "=r"(saved_));
- // FPCR.FZ bit 24: flush-to-zero. ARM64 has no DAZ bit; FZ covers it.
- const uint64_t fz = saved_ | (1ULL << 24);
- __asm__ volatile("msr fpcr, %0" : : "r"(fz));
+ saved_ = readFpcr();
+ writeFpcr(saved_ | (1ULL << 24));
}
- void leave() noexcept { __asm__ volatile("msr fpcr, %0" : : "r"(saved_)); }
+ void leave() noexcept { writeFpcr(saved_); }
+
+#if defined(_MSC_VER) && !defined(__clang__)
+ // MSVC ARM64 has no GCC-style inline asm; FPCR = ARM64_SYSREG(3, 3, 4, 4, 0).
+ static constexpr int kFpcr = 0x5A20;
+ static uint64_t readFpcr() noexcept { return static_cast<uint64_t>(_ReadStatusReg(kFpcr)); }
+ static void writeFpcr(uint64_t v) noexcept { _WriteStatusReg(kFpcr, static_cast<__int64>(v)); }
+#else
+ static uint64_t readFpcr() noexcept {
+ uint64_t v;
+ __asm__ volatile("mrs %0, fpcr" : "=r"(v));
+ return v;
+ }
+ static void writeFpcr(uint64_t v) noexcept { __asm__ volatile("msr fpcr, %0" : : "r"(v)); }
+#endif
#elif defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
unsigned saved_ = 0;
diff --git a/src/native/nota.engine/src/dsp/math/Simd.h b/src/native/nota.engine/src/dsp/math/Simd.h
index c7d6a068..2bfa6627 100644
--- a/src/native/nota.engine/src/dsp/math/Simd.h
+++ b/src/native/nota.engine/src/dsp/math/Simd.h
@@ -44,6 +44,9 @@
#if defined(NOTA_ARCH_X86)
#define NOTA_CPU_PAUSE() _mm_pause()
+#elif defined(_M_ARM64) && defined(_MSC_VER) && !defined(__clang__)
+#include <intrin.h>
+#define NOTA_CPU_PAUSE() __isb(_ARM64_BARRIER_SY)
#elif defined(NOTA_ARCH_ARM)
#define NOTA_CPU_PAUSE() __asm__ volatile("isb sy" : : : "memory")
#else