| 1 | // Copyright 2021 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "Common/FPURoundMode.h" |
| 5 | |
| 6 | #ifdef _MSC_VER |
| 7 | #include <intrin.h> |
| 8 | #endif |
| 9 | |
| 10 | #include "Common/CPUDetect.h" |
| 11 | #include "Common/CommonTypes.h" |
| 12 | #include "Common/Logging/Log.h" |
| 13 | |
| 14 | namespace Common::FPU |
| 15 | { |
| 16 | static u64 GetFPCR() |
| 17 | { |
| 18 | #ifdef _MSC_VER |
| 19 | return _ReadStatusReg(ARM64_FPCR); |
| 20 | #else |
| 21 | u64 fpcr; |
| 22 | __asm__ __volatile__("mrs %0, fpcr" : "=r"(fpcr)); |
| 23 | return fpcr; |
| 24 | #endif |
| 25 | } |
| 26 | |
| 27 | static void SetFPCR(u64 fpcr) |
| 28 | { |
| 29 | #ifdef _MSC_VER |
| 30 | _WriteStatusReg(ARM64_FPCR, fpcr); |
| 31 | #else |
| 32 | __asm__ __volatile__("msr fpcr, %0" : : "ri"(fpcr)); |
| 33 | #endif |
| 34 | } |
| 35 | |
| 36 | static const u64 default_fpcr = GetFPCR(); |
| 37 | static u64 saved_fpcr = default_fpcr; |
| 38 | |
| 39 | void SetSIMDMode(RoundMode rounding_mode, bool non_ieee_mode) |
| 40 | { |
| 41 | // When AH is disabled, FZ controls flush-to-zero for both inputs and outputs. When AH is enabled, |
| 42 | // FZ controls flush-to-zero for outputs, and FIZ controls flush-to-zero for inputs. |
| 43 | constexpr u32 FZ = 1 << 24; |
| 44 | constexpr u32 AH = 1 << 1; |
| 45 | constexpr u32 FIZ = 1 << 0; |
| 46 | constexpr u32 flush_to_zero_mask = FZ | AH | FIZ; |
| 47 | |
| 48 | // On CPUs with FEAT_AFP support, setting AH = 1, FZ = 1, FIZ = 0 emulates the GC/Wii CPU's |
| 49 | // "non-IEEE mode". Unfortunately, FEAT_AFP didn't exist until 2020, so we can't count on setting |
| 50 | // AH actually doing anything. But flushing both inputs and outputs seems to cause less problems |
| 51 | // than flushing nothing, so let's just set FZ and AH and roll with whatever behavior we get. |
| 52 | const u32 flush_to_zero_bits = (non_ieee_mode ? FZ | AH : 0); |
| 53 | static bool afp_warning_shown = false; |
| 54 | if (!afp_warning_shown && !cpu_info.bAFP && non_ieee_mode) |
| 55 | { |
| 56 | afp_warning_shown = true; |
| 57 | WARN_LOG_FMT(POWERPC, |
| 58 | "Non-IEEE mode was requested, but host CPU is not known to support FEAT_AFP"); |
| 59 | } |
| 60 | |
| 61 | // lookup table for FPSCR.RN-to-FPCR.RMode translation |
| 62 | constexpr u32 rounding_mode_table[] = { |
| 63 | (0 << 22), // nearest |
| 64 | (3 << 22), // zero |
| 65 | (1 << 22), // +inf |
| 66 | (2 << 22), // -inf |
| 67 | }; |
| 68 | constexpr u32 rounding_mode_mask = 3 << 22; |
| 69 | const u32 rounding_mode_bits = rounding_mode_table[rounding_mode]; |
| 70 | |
| 71 | const u64 base = default_fpcr & ~(flush_to_zero_mask | rounding_mode_mask); |
| 72 | SetFPCR(base | rounding_mode_bits | flush_to_zero_bits); |
| 73 | } |
| 74 | |
| 75 | void SaveSIMDState() |
| 76 | { |
| 77 | saved_fpcr = GetFPCR(); |
| 78 | } |
| 79 | |
| 80 | void LoadSIMDState() |
| 81 | { |
| 82 | SetFPCR(saved_fpcr); |
| 83 | } |
| 84 | |
| 85 | void LoadDefaultSIMDState() |
| 86 | { |
| 87 | SetFPCR(default_fpcr); |
| 88 | } |
| 89 | |
| 90 | } // namespace Common::FPU |