Skip to content

Commit

Permalink
Common/CommonFuncs: Remove now-unneccessary ArraySize function
Browse files Browse the repository at this point in the history
Since C++17, non-member std::size() is present in the standard library
which also operates on regular C arrays. Given that, we can just replace
usages of ArraySize with that where applicable.

In many cases, we can just change the actual C array ArraySize() was
called on into a std::array and just use its .size() member function
instead.

In some other cases, we can collapse the loops they were used in, into a
ranged-for loop, eliminating the need for en explicit bounds query.
  • Loading branch information
lioncash committed Jun 1, 2019
1 parent a4837a5 commit a966366
Show file tree
Hide file tree
Showing 26 changed files with 281 additions and 230 deletions.
8 changes: 0 additions & 8 deletions Source/Core/Common/CommonFuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,9 @@

#pragma once

#include <cstddef>
#include <string>
#include "Common/CommonTypes.h"

// Will fail to compile on a non-array:
template <typename T, size_t N>
constexpr size_t ArraySize(T (&arr)[N])
{
return N;
}

#ifndef _WIN32

// go to debugger mode
Expand Down
18 changes: 10 additions & 8 deletions Source/Core/Core/Boot/Boot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,21 +254,23 @@ bool CBoot::FindMapFile(std::string* existing_map_file, std::string* writable_ma
if (writable_map_file)
*writable_map_file = File::GetUserPath(D_MAPS_IDX) + game_id + ".map";

bool found = false;
static const std::string maps_directories[] = {File::GetUserPath(D_MAPS_IDX),
File::GetSysDirectory() + MAPS_DIR DIR_SEP};
for (size_t i = 0; !found && i < ArraySize(maps_directories); ++i)
static const std::array<std::string, 2> maps_directories{
File::GetUserPath(D_MAPS_IDX),
File::GetSysDirectory() + MAPS_DIR DIR_SEP,
};
for (const auto& directory : maps_directories)
{
std::string path = maps_directories[i] + game_id + ".map";
std::string path = directory + game_id + ".map";
if (File::Exists(path))
{
found = true;
if (existing_map_file)
*existing_map_file = path;
*existing_map_file = std::move(path);

return true;
}
}

return found;
return false;
}

bool CBoot::LoadMapFromFilename()
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/Core/DSP/DSPCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ bool DSPCore_Init(const DSPInitOptions& opts)

std::fill(std::begin(g_dsp.reg_stack_ptr), std::end(g_dsp.reg_stack_ptr), 0);

for (size_t i = 0; i < ArraySize(g_dsp.reg_stack); i++)
std::fill(std::begin(g_dsp.reg_stack[i]), std::end(g_dsp.reg_stack[i]), 0);
for (auto& stack : g_dsp.reg_stack)
std::fill(std::begin(stack), std::end(stack), 0);

// Fill IRAM with HALT opcodes.
std::fill(g_dsp.iram, g_dsp.iram + DSP_IRAM_SIZE, 0x0021);
Expand Down
23 changes: 12 additions & 11 deletions Source/Core/Core/HLE/HLE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Core/HLE/HLE.h"

#include <algorithm>
#include <array>
#include <map>

#include "Common/CommonTypes.h"
Expand Down Expand Up @@ -41,7 +42,7 @@ struct SPatch
};

// clang-format off
static const SPatch OSPatches[] = {
constexpr std::array<SPatch, 21> OSPatches{{
// Placeholder, OSPatches[0] is the "non-existent function" index
{"FAKE_TO_SKIP_0", HLE_Misc::UnimplementedFunction, HookType::Replace, HookFlag::Generic},

Expand Down Expand Up @@ -72,16 +73,16 @@ static const SPatch OSPatches[] = {
{"GeckoCodehandler", HLE_Misc::GeckoCodeHandlerICacheFlush, HookType::Start, HookFlag::Fixed},
{"GeckoHandlerReturnTrampoline", HLE_Misc::GeckoReturnTrampoline, HookType::Replace, HookFlag::Fixed},
{"AppLoaderReport", HLE_OS::HLE_GeneralDebugPrint, HookType::Replace, HookFlag::Fixed} // apploader needs OSReport-like function
};
}};

static const SPatch OSBreakPoints[] = {
constexpr std::array<SPatch, 1> OSBreakPoints{{
{"FAKE_TO_SKIP_0", HLE_Misc::UnimplementedFunction, HookType::Start, HookFlag::Generic},
};
}};
// clang-format on

void Patch(u32 addr, const char* hle_func_name)
{
for (u32 i = 1; i < ArraySize(OSPatches); ++i)
for (u32 i = 1; i < OSPatches.size(); ++i)
{
if (!strcmp(OSPatches[i].m_szPatchName, hle_func_name))
{
Expand Down Expand Up @@ -126,7 +127,7 @@ void PatchFunctions()
}
}

for (u32 i = 1; i < ArraySize(OSPatches); ++i)
for (u32 i = 1; i < OSPatches.size(); ++i)
{
// Fixed hooks don't map to symbols
if (OSPatches[i].flags == HookFlag::Fixed)
Expand All @@ -145,7 +146,7 @@ void PatchFunctions()

if (SConfig::GetInstance().bEnableDebugging)
{
for (size_t i = 1; i < ArraySize(OSBreakPoints); ++i)
for (size_t i = 1; i < OSBreakPoints.size(); ++i)
{
for (const auto& symbol : g_symbolDB.GetSymbolsFromName(OSBreakPoints[i].m_szPatchName))
{
Expand Down Expand Up @@ -173,7 +174,7 @@ void Reload()
void Execute(u32 _CurrentPC, u32 _Instruction)
{
unsigned int FunctionIndex = _Instruction & 0xFFFFF;
if (FunctionIndex > 0 && FunctionIndex < ArraySize(OSPatches))
if (FunctionIndex > 0 && FunctionIndex < OSPatches.size())
{
OSPatches[FunctionIndex].PatchFunction();
}
Expand Down Expand Up @@ -216,14 +217,14 @@ bool IsEnabled(HookFlag flag)

u32 UnPatch(const std::string& patch_name)
{
auto* patch = std::find_if(std::begin(OSPatches), std::end(OSPatches),
[&](const SPatch& p) { return patch_name == p.m_szPatchName; });
const auto patch = std::find_if(std::begin(OSPatches), std::end(OSPatches),
[&](const SPatch& p) { return patch_name == p.m_szPatchName; });
if (patch == std::end(OSPatches))
return 0;

if (patch->flags == HookFlag::Fixed)
{
u32 patch_idx = static_cast<u32>(patch - OSPatches);
const u32 patch_idx = static_cast<u32>(std::distance(OSPatches.begin(), patch));
u32 addr = 0;
// Reverse search by OSPatch key instead of address
for (auto i = s_original_instructions.begin(); i != s_original_instructions.end();)
Expand Down
50 changes: 35 additions & 15 deletions Source/Core/Core/HW/DSPHLE/UCodes/AX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include <algorithm>

#include "Core/HW/DSPHLE/UCodes/AX.h"

#include <algorithm>
#include <array>
#include <iterator>

#include "Common/ChunkFile.h"
#include "Common/CommonTypes.h"
#include "Common/File.h"
Expand Down Expand Up @@ -45,12 +47,14 @@ void AXUCode::LoadResamplingCoefficients()
{
m_coeffs_available = false;

std::string filenames[] = {File::GetUserPath(D_GCUSER_IDX) + "dsp_coef.bin",
File::GetSysDirectory() + "/GC/dsp_coef.bin"};
const std::array<std::string, 2> filenames{
File::GetUserPath(D_GCUSER_IDX) + "dsp_coef.bin",
File::GetSysDirectory() + "/GC/dsp_coef.bin",
};

size_t fidx;
std::string filename;
for (fidx = 0; fidx < ArraySize(filenames); ++fidx)
for (fidx = 0; fidx < filenames.size(); ++fidx)
{
filename = filenames[fidx];
if (File::GetSize(filename) != 0x1000)
Expand All @@ -59,7 +63,7 @@ void AXUCode::LoadResamplingCoefficients()
break;
}

if (fidx >= ArraySize(filenames))
if (fidx >= filenames.size())
return;

INFO_LOG(DSPHLE, "Loading polyphase resampling coeffs from %s", filename.c_str());
Expand Down Expand Up @@ -449,8 +453,8 @@ void AXUCode::ProcessPBList(u32 pb_addr)
m_coeffs_available ? m_coeffs : nullptr);

// Forward the buffers
for (size_t i = 0; i < ArraySize(buffers.ptrs); ++i)
buffers.ptrs[i] += spms;
for (auto& ptr : buffers.ptrs)
ptr += spms;
}

WritePB(pb_addr, pb, m_crc);
Expand Down Expand Up @@ -587,27 +591,43 @@ void AXUCode::SendAUXAndMix(u32 main_auxa_up, u32 auxb_s_up, u32 main_l_dl, u32
u32 auxb_l_dl, u32 auxb_r_dl)
{
// Buffers to upload first
int* up_buffers[] = {m_samples_auxA_left, m_samples_auxA_right, m_samples_auxA_surround};
const std::array<const int*, 3> up_buffers{
m_samples_auxA_left,
m_samples_auxA_right,
m_samples_auxA_surround,
};

// Upload AUXA LRS
int* ptr = (int*)HLEMemory_Get_Pointer(main_auxa_up);
for (auto& up_buffer : up_buffers)
for (const auto& up_buffer : up_buffers)
{
for (u32 j = 0; j < 32 * 5; ++j)
*ptr++ = Common::swap32(up_buffer[j]);
}

// Upload AUXB S
ptr = (int*)HLEMemory_Get_Pointer(auxb_s_up);
for (auto& sample : m_samples_auxB_surround)
*ptr++ = Common::swap32(sample);

// Download buffers and addresses
int* dl_buffers[] = {m_samples_left, m_samples_right, m_samples_auxB_left, m_samples_auxB_right};
u32 dl_addrs[] = {main_l_dl, main_r_dl, auxb_l_dl, auxb_r_dl};
const std::array<int*, 4> dl_buffers{
m_samples_left,
m_samples_right,
m_samples_auxB_left,
m_samples_auxB_right,
};
const std::array<u32, 4> dl_addrs{
main_l_dl,
main_r_dl,
auxb_l_dl,
auxb_r_dl,
};

// Download and mix
for (size_t i = 0; i < ArraySize(dl_buffers); ++i)
for (size_t i = 0; i < dl_buffers.size(); ++i)
{
int* dl_src = (int*)HLEMemory_Get_Pointer(dl_addrs[i]);
const int* dl_src = (int*)HLEMemory_Get_Pointer(dl_addrs[i]);
for (size_t j = 0; j < 32 * 5; ++j)
dl_buffers[i][j] += (int)Common::swap32(*dl_src++);
}
Expand Down Expand Up @@ -666,7 +686,7 @@ void AXUCode::HandleMail(u32 mail)

void AXUCode::CopyCmdList(u32 addr, u16 size)
{
if (size >= ArraySize(m_cmdlist))
if (size >= std::size(m_cmdlist))
{
ERROR_LOG(DSPHLE, "Command list at %08x is too large: size=%d", addr, size);
return;
Expand Down
Loading

0 comments on commit a966366

Please sign in to comment.