Skip to content

Commit

Permalink
Common: Add GSVector
Browse files Browse the repository at this point in the history
Mostly based on PCSX2.
  • Loading branch information
stenzek committed Jul 2, 2024
1 parent baab966 commit 0ae6ddc
Show file tree
Hide file tree
Showing 12 changed files with 4,779 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CMakeModules/DuckStationUtils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ function(detect_architecture)
if("x86_64" IN_LIST CMAKE_OSX_ARCHITECTURES)
message(STATUS "Building x86_64 MacOS binaries.")
set(CPU_ARCH_X64 TRUE PARENT_SCOPE)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Xarch_x86_64 -msse4.1" PARENT_SCOPE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Xarch_864_64 -msse4.1" PARENT_SCOPE)
endif()
if("arm64" IN_LIST CMAKE_OSX_ARCHITECTURES)
message(STATUS "Building ARM64 MacOS binaries.")
Expand All @@ -67,6 +69,8 @@ function(detect_architecture)
CMAKE_SIZEOF_VOID_P EQUAL 8)
message(STATUS "Building x86_64 binaries.")
set(CPU_ARCH_X64 TRUE PARENT_SCOPE)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse4.1" PARENT_SCOPE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse4.1" PARENT_SCOPE)
elseif(("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch64" OR "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "arm64") AND
CMAKE_SIZEOF_VOID_P EQUAL 8) # Might have an A64 kernel, e.g. Raspbian.
message(STATUS "Building ARM64 binaries.")
Expand Down
1 change: 1 addition & 0 deletions dep/msvc/vsprops/Base.props
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<PreprocessorDefinitions>_HAS_EXCEPTIONS=0;_CRT_INTERNAL_NONSTDC_NAMES;_CRT_NONSTDC_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;WIN32;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(DepsIncludeDir)</AdditionalIncludeDirectories>
<AdditionalOptions Condition="!$(Configuration.Contains(Clang))">/Zc:__cplusplus /Zo /utf-8 %(AdditionalOptions)</AdditionalOptions>
<AdditionalOptions Condition="$(Configuration.Contains(Clang)) And '$(Platform)'=='x64'"> -msse4.1 %(AdditionalOptions)</AdditionalOptions>
<!-- Force ThinLTO for Release builds, MSVC doesn't seem to do it otherwise. -->
<AdditionalOptions Condition="$(Configuration.Contains(Clang)) And $(Configuration.Contains(ReleaseLTCG))"> -flto=thin %(AdditionalOptions)</AdditionalOptions>
<ExceptionHandling>false</ExceptionHandling>
Expand Down
5 changes: 5 additions & 0 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ add_library(common
fifo_queue.h
file_system.cpp
file_system.h
gsvector.h
gsvector_formatter.h
gsvector_neon.h
gsvector_nosimd.h
gsvector_sse.h
intrin.h
hash_combine.h
heap_array.h
Expand Down
6 changes: 6 additions & 0 deletions src/common/common.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
<ClInclude Include="fastjmp.h" />
<ClInclude Include="fifo_queue.h" />
<ClInclude Include="file_system.h" />
<ClInclude Include="gsvector.h" />
<ClInclude Include="gsvector_formatter.h" />
<ClInclude Include="gsvector_neon.h" />
<ClInclude Include="gsvector_nosimd.h" />
<ClInclude Include="gsvector_sse.h" />
<ClInclude Include="hash_combine.h" />
<ClInclude Include="heap_array.h" />
<ClInclude Include="intrin.h" />
Expand Down Expand Up @@ -70,6 +75,7 @@
</ItemGroup>
<ItemGroup>
<Natvis Include="bitfield.natvis" />
<Natvis Include="gsvector.natvis" />
<Natvis Include="thirdparty\llvm.natvis" />
</ItemGroup>
<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions src/common/common.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
</ClInclude>
<ClInclude Include="dynamic_library.h" />
<ClInclude Include="binary_span_reader_writer.h" />
<ClInclude Include="gsvector_sse.h" />
<ClInclude Include="gsvector_neon.h" />
<ClInclude Include="gsvector.h" />
<ClInclude Include="gsvector_formatter.h" />
<ClInclude Include="gsvector_nosimd.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="small_string.cpp" />
Expand Down Expand Up @@ -80,6 +85,7 @@
<Natvis Include="thirdparty\llvm.natvis">
<Filter>thirdparty</Filter>
</Natvis>
<Natvis Include="gsvector.natvis" />
</ItemGroup>
<ItemGroup>
<Filter Include="thirdparty">
Expand Down
65 changes: 65 additions & 0 deletions src/common/gsvector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)

#pragma once

#include "common/intrin.h"

#include <cstring>

enum Align_Mode
{
Align_Outside,
Align_Inside,
Align_NegInf,
Align_PosInf
};

enum Round_Mode
{
Round_NearestInt = 8,
Round_NegInf = 9,
Round_PosInf = 10,
Round_Truncate = 11
};

template<class T>
class GSVector2T
{
public:
union
{
struct
{
T x, y;
};
struct
{
T r, g;
};
struct
{
T v[2];
};
};

GSVector2T() = default;

ALWAYS_INLINE constexpr GSVector2T(T x) : x(x), y(x) {}
ALWAYS_INLINE constexpr GSVector2T(T x, T y) : x(x), y(y) {}
ALWAYS_INLINE constexpr bool operator==(const GSVector2T& v) const { return std::memcmp(this, &v, sizeof(*this)) == 0; }
ALWAYS_INLINE constexpr bool operator!=(const GSVector2T& v) const { return std::memcmp(this, &v, sizeof(*this)) != 0; }
ALWAYS_INLINE constexpr GSVector2T operator*(const GSVector2T& v) const { return {x * v.x, y * v.y}; }
ALWAYS_INLINE constexpr GSVector2T operator/(const GSVector2T& v) const { return {x / v.x, y / v.y}; }
};

using GSVector2 = GSVector2T<float>;
using GSVector2i = GSVector2T<s32>;

#if defined(CPU_ARCH_SSE)
#include "common/gsvector_sse.h"
#elif defined(CPU_ARCH_NEON)
#include "common/gsvector_neon.h"
#else
#include "common/gsvector_nosimd.h"
#endif
10 changes: 10 additions & 0 deletions src/common/gsvector.natvis
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
<Type Name="GSVector2T&lt;*&gt;">
<DisplayString>{{ {x}, {y} }}</DisplayString>
</Type>

<Type Name="GSVector4i">
<DisplayString>{{ {I32[0]}, {I32[1]}, {I32[2]}, {I32[3]} }}</DisplayString>
</Type>
</AutoVisualizer>
21 changes: 21 additions & 0 deletions src/common/gsvector_formatter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-FileCopyrightText: 2024 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)

#pragma once

#include "gsvector.h"
#include "small_string.h"

#include "fmt/format.h"

template<>
struct fmt::formatter<GSVector4i> : formatter<std::string_view>
{
auto format(const GSVector4i& rc, format_context& ctx) const
{
const TinyString str =
TinyString::from_format("{},{} => {},{} ({}x{})", rc.left, rc.top, rc.right, rc.bottom, rc.width(), rc.height());

return fmt::formatter<std::string_view>::format(str.view(), ctx);
}
};
Loading

0 comments on commit 0ae6ddc

Please sign in to comment.