From 161edbf2f6e5691352bbd775cf6524ba85376314 Mon Sep 17 00:00:00 2001 From: Megamouse Date: Wed, 7 Aug 2024 23:18:32 +0200 Subject: [PATCH] cellMic: make bit_resolution and channel_size constexpr This allows us to optimize and inline variable_byteswap --- rpcs3/Emu/Cell/Modules/cellMic.cpp | 21 ++++++++++++++------- rpcs3/Emu/Cell/Modules/cellMic.h | 7 ++++--- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/rpcs3/Emu/Cell/Modules/cellMic.cpp b/rpcs3/Emu/Cell/Modules/cellMic.cpp index 03f0d4c994..946c34633a 100644 --- a/rpcs3/Emu/Cell/Modules/cellMic.cpp +++ b/rpcs3/Emu/Cell/Modules/cellMic.cpp @@ -241,13 +241,20 @@ bool mic_context::check_device(u32 dev_num) // Static functions -void microphone_device::variable_byteswap(const void* src, void* dst, const u32 bytesize) +template +inline void microphone_device::variable_byteswap(const void* src, void* dst) { - switch (bytesize) + if constexpr (bytesize == 4) { - case 4: *static_cast(dst) = *static_cast*>(src); break; - case 2: *static_cast(dst) = *static_cast*>(src); break; - default: break; + *static_cast(dst) = *static_cast*>(src); + } + else if constexpr (bytesize == 2) + { + *static_cast(dst) = *static_cast*>(src); + } + else + { + fmt::throw_exception("variable_byteswap with bytesize %d unimplemented", bytesize); } } @@ -588,7 +595,7 @@ void microphone_device::get_data(const u32 num_samples) case microphone_handler::standard: case microphone_handler::rocksmith: { - const u8 channel_size = bit_resolution / 8; + constexpr u8 channel_size = bit_resolution / 8; const usz bufsize = num_samples * sample_size; const std::vector& buf = ::at32(devices, 0).buf; ensure(bufsize <= buf.size()); @@ -602,7 +609,7 @@ void microphone_device::get_data(const u32 num_samples) for (u32 indchan = 0; indchan < num_channels; indchan++) { const u32 curindex = sample_pos + indchan * channel_size; - microphone_device::variable_byteswap(buf.data() + curindex, tmp_ptr + curindex, channel_size); + microphone_device::variable_byteswap(buf.data() + curindex, tmp_ptr + curindex); } } break; diff --git a/rpcs3/Emu/Cell/Modules/cellMic.h b/rpcs3/Emu/Cell/Modules/cellMic.h index aec7dd49b3..b1d4982523 100644 --- a/rpcs3/Emu/Cell/Modules/cellMic.h +++ b/rpcs3/Emu/Cell/Modules/cellMic.h @@ -294,7 +294,7 @@ public: bool is_opened() const { return mic_opened; } bool is_started() const { return mic_started; } u8 get_signal_types() const { return signal_types; } - u8 get_bit_resolution() const { return bit_resolution; } + constexpr u8 get_bit_resolution() const { return bit_resolution; } u32 get_raw_samplingrate() const { return raw_samplingrate; } u8 get_num_channels() const { return num_channels; } u8 get_datatype() const @@ -321,7 +321,8 @@ public: u32 attr_dsptype = 0; private: - static void variable_byteswap(const void* src, void* dst, const u32 bytesize); + template + static inline void variable_byteswap(const void* src, void* dst); u32 capture_audio(); @@ -349,13 +350,13 @@ private: u32 raw_samplingrate = 48000; u32 dsp_samplingrate = 48000; u32 aux_samplingrate = 48000; - u8 bit_resolution = 16; u8 num_channels = 2; u8 signal_types = CELLMIC_SIGTYPE_NULL; u32 sample_size = 0; // Determined at opening for internal use + static constexpr u8 bit_resolution = 16; static constexpr usz inbuf_size = 400000; // Default value unknown simple_ringbuf rbuf_raw;