Skip to content

Instantly share code, notes, and snippets.

@Rinzii
Last active May 19, 2024 23:33
Show Gist options
  • Save Rinzii/00371543a453a3af84d3457a2ab6cec8 to your computer and use it in GitHub Desktop.
Save Rinzii/00371543a453a3af84d3457a2ab6cec8 to your computer and use it in GitHub Desktop.
Find the current rounding mode both during compile time and run time in C++20
/*
* MIT License
*
* Copyright (c) 2024 Ian Pike
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the “Software”), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#pragma once
#include <cfenv>
#include <limits>
#include <type_traits>
namespace internal
{
inline bool rt_rounding_mode_is_round_up()
{
volatile constexpr float x = 0x1.0p-25F;
return (1.0F + x != 1.0F);
}
inline bool rt_rounding_mode_is_round_down()
{
volatile constexpr float x = 0x1.0p-25F;
return (-1.0F - x != -1.0F);
}
inline bool rt_rounding_mode_is_round_to_nearest()
{
static volatile constexpr float x = 0x1.0p-24F;
float y = x;
return (1.5F + y == 1.5F - y);
}
inline bool rt_rounding_mode_is_round_to_zero()
{
static volatile float x = 0x1.0p-24F;
const float y = x;
return ((0x1.000002p0F + y) + (-1.0F - y) == 0x1.0p-23F);
}
inline int rt_get_rounding_mode()
{
static volatile float x = 0x1.0p-24F;
float y = x;
float z = (0x1.000002p0F + y) + (-1.0F - y);
if (z == 0.0F) { return FE_DOWNWARD; }
if (z == 0x1.0p-23F) { return FE_TOWARDZERO; }
return (2.0F + y == 2.0F) ? FE_TONEAREST : FE_UPWARD;
}
} // namespace internal
constexpr bool rounding_mode_is_round_up()
{
if constexpr (std::is_constant_evaluated()) { return std::numeric_limits<float>::round_style == std::float_round_style::round_toward_infinity; }
else { return internal::rt_rounding_mode_is_round_up(); }
}
constexpr bool rounding_mode_is_round_down()
{
if constexpr (std::is_constant_evaluated()) { return std::numeric_limits<float>::round_style == std::float_round_style::round_toward_neg_infinity; }
else { return internal::rt_rounding_mode_is_round_down(); }
}
constexpr bool rounding_mode_is_round_to_nearest()
{
if constexpr (std::is_constant_evaluated()) { return std::numeric_limits<float>::round_style == std::float_round_style::round_to_nearest; }
else { return internal::rt_rounding_mode_is_round_to_nearest(); }
}
constexpr bool rounding_mode_is_round_to_zero()
{
if constexpr (std::is_constant_evaluated()) { return std::numeric_limits<float>::round_style == std::float_round_style::round_toward_zero; }
else { return internal::rt_rounding_mode_is_round_to_zero(); }
}
constexpr int get_rounding_mode()
{
if constexpr (std::is_constant_evaluated())
{
switch (std::numeric_limits<float>::round_style)
{
case std::float_round_style::round_toward_neg_infinity: return FE_DOWNWARD;
case std::float_round_style::round_toward_infinity: return FE_UPWARD;
case std::float_round_style::round_toward_zero: return FE_TOWARDZERO;
default: return FE_TONEAREST;
}
}
else { return internal::rt_get_rounding_mode(); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment