From 9f2a77f78801da760df582900ec862fff6438a09 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Wed, 21 Nov 2018 09:07:02 -0800 Subject: Use RAII when handling the mixer's FPU state --- Alc/helpers.cpp | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'Alc/helpers.cpp') diff --git a/Alc/helpers.cpp b/Alc/helpers.cpp index a78e3d83..658c7d09 100644 --- a/Alc/helpers.cpp +++ b/Alc/helpers.cpp @@ -260,13 +260,13 @@ void FillCPUCaps(int capfilter) } -void SetMixerFPUMode(FPUCtl *ctl) +FPUCtl::FPUCtl() noexcept { #if defined(__GNUC__) && defined(HAVE_SSE) if((CPUCapFlags&CPU_CAP_SSE)) { - __asm__ __volatile__("stmxcsr %0" : "=m" (*&ctl->sse_state)); - unsigned int sseState = ctl->sse_state; + __asm__ __volatile__("stmxcsr %0" : "=m" (*&this->sse_state)); + unsigned int sseState = this->sse_state; sseState |= 0x8000; /* set flush-to-zero */ if((CPUCapFlags&CPU_CAP_SSE2)) sseState |= 0x0040; /* set denormals-are-zero */ @@ -275,32 +275,37 @@ void SetMixerFPUMode(FPUCtl *ctl) #elif defined(HAVE___CONTROL87_2) - __control87_2(0, 0, &ctl->state, &ctl->sse_state); + __control87_2(0, 0, &this->state, &this->sse_state); _control87(_DN_FLUSH, _MCW_DN); #elif defined(HAVE__CONTROLFP) - ctl->state = _controlfp(0, 0); + this->state = _controlfp(0, 0); _controlfp(_DN_FLUSH, _MCW_DN); #endif + + this->in_mode = true; } -void RestoreFPUMode(const FPUCtl *ctl) +void FPUCtl::leave() noexcept { + if(!this->in_mode) return; + #if defined(__GNUC__) && defined(HAVE_SSE) if((CPUCapFlags&CPU_CAP_SSE)) - __asm__ __volatile__("ldmxcsr %0" : : "m" (*&ctl->sse_state)); + __asm__ __volatile__("ldmxcsr %0" : : "m" (*&this->sse_state)); #elif defined(HAVE___CONTROL87_2) unsigned int mode; - __control87_2(ctl->state, _MCW_DN, &mode, nullptr); - __control87_2(ctl->sse_state, _MCW_DN, nullptr, &mode); + __control87_2(this->state, _MCW_DN, &mode, nullptr); + __control87_2(this->sse_state, _MCW_DN, nullptr, &mode); #elif defined(HAVE__CONTROLFP) - _controlfp(ctl->state, _MCW_DN); + _controlfp(this->state, _MCW_DN); #endif + this->in_mode = false; } -- cgit v1.2.3