-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
add til::math, use it for float conversions to point, size #5150
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
#pragma once | ||
|
||
namespace til | ||
{ | ||
// The til::math namespace contains TIL math guidance casts; | ||
// they are intended to be used as the first argument to | ||
// floating-point universal converters elswehere in the til namespace. | ||
namespace math | ||
{ | ||
namespace details | ||
{ | ||
struct ceiling_t | ||
{ | ||
template<typename O, typename T> | ||
static O cast(T val) | ||
{ | ||
if constexpr (std::is_floating_point_v<T>) | ||
{ | ||
THROW_HR_IF(E_ABORT, ::std::isnan(val)); | ||
return ::base::saturated_cast<O>(::std::ceil(val)); | ||
} | ||
else | ||
{ | ||
return ::base::saturated_cast<O>(val); | ||
} | ||
} | ||
}; | ||
|
||
struct flooring_t | ||
{ | ||
template<typename O, typename T> | ||
static O cast(T val) | ||
{ | ||
if constexpr (std::is_floating_point_v<T>) | ||
{ | ||
THROW_HR_IF(E_ABORT, ::std::isnan(val)); | ||
return ::base::saturated_cast<O>(::std::floor(val)); | ||
} | ||
else | ||
{ | ||
return ::base::saturated_cast<O>(val); | ||
} | ||
} | ||
}; | ||
|
||
struct rounding_t | ||
{ | ||
template<typename O, typename T> | ||
static O cast(T val) | ||
{ | ||
if constexpr (std::is_floating_point_v<T>) | ||
{ | ||
THROW_HR_IF(E_ABORT, ::std::isnan(val)); | ||
return ::base::saturated_cast<O>(::std::round(val)); | ||
} | ||
else | ||
{ | ||
return ::base::saturated_cast<O>(val); | ||
} | ||
} | ||
}; | ||
|
||
struct truncating_t | ||
{ | ||
template<typename O, typename T> | ||
static O cast(T val) | ||
{ | ||
if constexpr (std::is_floating_point_v<T>) | ||
{ | ||
THROW_HR_IF(E_ABORT, ::std::isnan(val)); | ||
} | ||
return ::base::saturated_cast<O>(val); | ||
} | ||
}; | ||
} | ||
|
||
static constexpr details::ceiling_t ceiling; // positives become more positive, negatives become less negative | ||
static constexpr details::flooring_t flooring; // positives become less positive, negatives become more negative | ||
static constexpr details::rounding_t rounding; // it's rounding, from math class | ||
static constexpr details::truncating_t truncating; // drop the decimal point, regardless of how close it is to the next value | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
#include "precomp.h" | ||
|
||
#include "til/math.h" | ||
|
||
using namespace WEX::Common; | ||
using namespace WEX::Logging; | ||
using namespace WEX::TestExecution; | ||
|
||
class MathTests | ||
{ | ||
TEST_CLASS(MathTests); | ||
|
||
template<typename TG, typename TX = ptrdiff_t> | ||
struct TestCase | ||
{ | ||
TG given; | ||
TX expected; | ||
}; | ||
|
||
template<class TilMath, typename TG, typename TX, int N> | ||
static void _RunCases(TilMath, const std::array<TestCase<TG, TX>, N>& cases) | ||
{ | ||
for (const auto& tc : cases) | ||
{ | ||
VERIFY_ARE_EQUAL(tc.expected, TilMath::template cast<decltype(tc.expected)>(tc.given)); | ||
} | ||
} | ||
|
||
TEST_METHOD(Truncating) | ||
{ | ||
std::array<TestCase<long double, ptrdiff_t>, 8> cases{ | ||
TestCase<long double, ptrdiff_t>{ 1., 1 }, | ||
{ 1.9, 1 }, | ||
{ -7.1, -7 }, | ||
{ -8.5, -8 }, | ||
{ PTRDIFF_MAX + 0.5, PTRDIFF_MAX }, | ||
{ PTRDIFF_MIN - 0.5, PTRDIFF_MIN }, | ||
{ INFINITY, PTRDIFF_MAX }, | ||
{ -INFINITY, PTRDIFF_MIN }, | ||
}; | ||
miniksa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
_RunCases(til::math::truncating, cases); | ||
|
||
const auto fn = []() { | ||
const auto v = til::math::details::truncating_t::cast<ptrdiff_t>(NAN); | ||
}; | ||
VERIFY_THROWS_SPECIFIC(fn(), wil::ResultException, [](wil::ResultException& e) { return e.GetErrorCode() == E_ABORT; }); | ||
} | ||
|
||
TEST_METHOD(Ceiling) | ||
{ | ||
std::array<TestCase<long double, ptrdiff_t>, 8> cases{ | ||
TestCase<long double, ptrdiff_t>{ 1., 1 }, | ||
{ 1.9, 2 }, | ||
{ -7.1, -7 }, | ||
{ -8.5, -8 }, | ||
{ PTRDIFF_MAX + 0.5, PTRDIFF_MAX }, | ||
{ PTRDIFF_MIN - 0.5, PTRDIFF_MIN }, | ||
{ INFINITY, PTRDIFF_MAX }, | ||
{ -INFINITY, PTRDIFF_MIN }, | ||
}; | ||
|
||
_RunCases(til::math::ceiling, cases); | ||
|
||
const auto fn = []() { | ||
const auto v = til::math::details::ceiling_t::cast<ptrdiff_t>(NAN); | ||
}; | ||
VERIFY_THROWS_SPECIFIC(fn(), wil::ResultException, [](wil::ResultException& e) { return e.GetErrorCode() == E_ABORT; }); | ||
} | ||
|
||
TEST_METHOD(Flooring) | ||
{ | ||
std::array<TestCase<long double, ptrdiff_t>, 8> cases{ | ||
TestCase<long double, ptrdiff_t>{ 1., 1 }, | ||
{ 1.9, 1 }, | ||
{ -7.1, -8 }, | ||
{ -8.5, -9 }, | ||
{ PTRDIFF_MAX + 0.5, PTRDIFF_MAX }, | ||
{ PTRDIFF_MIN - 0.5, PTRDIFF_MIN }, | ||
{ INFINITY, PTRDIFF_MAX }, | ||
{ -INFINITY, PTRDIFF_MIN }, | ||
}; | ||
|
||
_RunCases(til::math::flooring, cases); | ||
|
||
const auto fn = []() { | ||
const auto v = til::math::details::flooring_t::cast<ptrdiff_t>(NAN); | ||
}; | ||
VERIFY_THROWS_SPECIFIC(fn(), wil::ResultException, [](wil::ResultException& e) { return e.GetErrorCode() == E_ABORT; }); | ||
} | ||
|
||
TEST_METHOD(Rounding) | ||
{ | ||
std::array<TestCase<long double, ptrdiff_t>, 8> cases{ | ||
TestCase<long double, ptrdiff_t>{ 1., 1 }, | ||
{ 1.9, 2 }, | ||
{ -7.1, -7 }, | ||
{ -8.5, -9 }, | ||
{ PTRDIFF_MAX + 0.5, PTRDIFF_MAX }, | ||
{ PTRDIFF_MIN - 0.5, PTRDIFF_MIN }, | ||
{ INFINITY, PTRDIFF_MAX }, | ||
{ -INFINITY, PTRDIFF_MIN }, | ||
}; | ||
|
||
_RunCases(til::math::rounding, cases); | ||
|
||
const auto fn = []() { | ||
const auto v = til::math::details::rounding_t::cast<ptrdiff_t>(NAN); | ||
}; | ||
VERIFY_THROWS_SPECIFIC(fn(), wil::ResultException, [](wil::ResultException& e) { return e.GetErrorCode() == E_ABORT; }); | ||
} | ||
|
||
TEST_METHOD(NormalIntegers) | ||
{ | ||
std::array<TestCase<ptrdiff_t, int>, 4> cases{ | ||
TestCase<ptrdiff_t, int>{ 1, 1 }, | ||
{ -1, -1 }, | ||
{ PTRDIFF_MAX, INT_MAX }, | ||
{ PTRDIFF_MIN, INT_MIN }, | ||
}; | ||
|
||
_RunCases(til::math::rounding, cases); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we sure we want the x/y version for
size
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Size already has an X/Y version, this just expands it. @miniksa?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the only thing I was expressly avoiding is implicit from COORD to both point and size because it's been a source of confusion. I can see the need to explicitly put one into the other one.