-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
488 additions
and
0 deletions.
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 }, | ||
}; | ||
|
||
_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.
138c6b9
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.
New misspellings found, please review:
To accept these changes, run the following commands