MeshLib C++ Docs
Loading...
Searching...
No Matches
MRRound.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRBitCast.h"
4#include <cmath>
5#include <cstdint>
6
7namespace MR
8{
11
12
15#if ( defined( _MSC_VER ) && !defined( __clang__ ) ) || ( defined( __x86_64__ ) && !( defined( __clang__ ) && defined( __SSE4_1__ ) ) ) || defined( __wasm__ )
16#define MR_ROUND_TO_INT_NO_LIBRARY_CALL
17#endif
18
20[[nodiscard]] inline int roundToInt( double x ) noexcept
21{
22#ifdef MR_ROUND_TO_INT_NO_LIBRARY_CALL
24 const double half = bit_cast<double>( bit_cast<std::uint64_t>( 0.49999999999999994 ) | ( bit_cast<std::uint64_t>( x ) & 0x8000000000000000ull ) );
25 return int( x + half );
26#else
27 return int( std::round( x ) );
28#endif
29}
30
32[[nodiscard]] inline int roundToInt( float x ) noexcept
33{
34#ifdef MR_ROUND_TO_INT_NO_LIBRARY_CALL
36 const float half = bit_cast<float>( bit_cast<std::uint32_t>( 0.49999997f ) | ( bit_cast<std::uint32_t>( x ) & 0x80000000u ) );
37 return int( x + half );
38#else
39 return int( std::round( x ) );
40#endif
41}
42
43}
int roundToInt(double x) noexcept
the same as (int)std::round( x ) for any x in int range (halves are rounded away from zero),...
Definition MRRound.h:20
constexpr To bit_cast(const From &from) noexcept
std::bit_cast wrapper with the compiler's intrinsic fallback
Definition MRBitCast.h:17
Only strictly necessary for the bindings (to make MR::MeshIntersectionResult a complete type).
Definition MRCameraOrientationPlugin.h:8