Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable fast floating point model and fast debug linking #11466

Merged
8 commits merged into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/actions/spelling/allow/math.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ LStep
powf
RSub
sqrtf
ULP
5 changes: 5 additions & 0 deletions src/cascadia/ut_app/ColorHelperTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include "../TerminalSettingsModel/Profile.h"
#include "../TerminalApp/ColorHelper.h"

// Import some templates to compare floats using approximate matching.
#include <consoletaeftemplates.hpp>

using namespace Microsoft::Console;
using namespace winrt::TerminalApp;
using namespace WEX::Logging;
Expand All @@ -27,6 +30,8 @@ namespace TerminalAppUnitTests

void ColorHelperTests::ConvertHslToRgb()
{
//DebugBreak();
lhecker marked this conversation as resolved.
Show resolved Hide resolved

auto red = winrt::Windows::UI::Colors::Red();
auto redHsl = ColorHelper::RgbToHsl(red);
VERIFY_ARE_EQUAL(0.f, redHsl.H);
Expand Down
5 changes: 5 additions & 0 deletions src/common.build.pre.props
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
<LanguageStandard>stdcpp17</LanguageStandard>
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
<ControlFlowGuard>Guard</ControlFlowGuard>
<FloatingPointModel>Fast</FloatingPointModel>
</ClCompile>
<ResourceCompile>
<PreprocessorDefinitions>EXTERNAL_BUILD;_UNICODE;UNICODE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
Expand All @@ -145,6 +146,10 @@
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_DEBUG;DBG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SetChecksum>false</SetChecksum>
<GenerateDebugInformation>DebugFastLink</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>

<!-- For Release ONLY -->
Expand Down
39 changes: 39 additions & 0 deletions src/inc/consoletaeftemplates.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,45 @@ Revision History:

namespace WEX::TestExecution
{
// Compare two floats using a ULP (unit last place) tolerance of up to 4.
// Allows you to compare two floats that are almost equal.
// Think of: 0.200000000000000 vs. 0.200000000000001.
template<typename T, typename U>
bool CompareFloats(T a, T b) noexcept
{
if (std::isnan(a))
{
return std::isnan(b);
}

if (a == b)
{
return true;
}

const auto nDiff = static_cast<std::make_signed_t<U>>(*reinterpret_cast<U*>(&a) - *reinterpret_cast<U*>(&b));
const auto uDiff = static_cast<U>(nDiff < 0 ? -nDiff : nDiff);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What the heck? This is legal??

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And not UB?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No this is 100% undefined behavior. But it works because MSVC doesn't do strict aliasing. In C++20 you can use std::bit_cast.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the question about legality of reading floats like that: Yeah that's legal, because the bit patterns of IEEE floats are clearly defined.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added til::bit_cast and replaced the reinterpret_cast hack.

return uDiff <= 4;
}

template<>
struct VerifyCompareTraits<float, float>
{
static bool AreEqual(float a, float b) noexcept
{
return CompareFloats<float, uint32_t>(a, b);
}
};

template<>
struct VerifyCompareTraits<double, double>
{
static bool AreEqual(double a, double b) noexcept
{
return CompareFloats<double, uint64_t>(a, b);
}
};

template<>
class VerifyOutputTraits<SMALL_RECT>
{
Expand Down
2 changes: 1 addition & 1 deletion src/project.inc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ USE_NATIVE_EH = 1
# -------------------------------------

MSC_WARNING_LEVEL = /W4 /WX
USER_C_FLAGS = $(USER_C_FLAGS) /utf-8
USER_C_FLAGS = $(USER_C_FLAGS) /fp:fast /utf-8

# -------------------------------------
# Common Console Includes and Libraries
Expand Down