microsoft / microsoft/STL

cmath intrinsic functions missing for double

Open
#4,998 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

performance UCRT
Dominant language
C++
Stars
11.1k
Forks
1.7k
Avg merge
4d 15h
Merged PRs (30d)
22

Description

I'm using std::round from cmath header file. I want the __round intrinsic function to be used, but I could not find any function supporting just double parameter, there is only long double version which casts the parameter to double anyways.

_NODISCARD _Check_return_ inline long double round(_In_ long double _Xx) noexcept /* strengthened */ {
#if _HAS_CMATH_INTRINSICS
    return __round(static_cast<double>(_Xx));
#elif defined(__clang__)
    return __builtin_roundl(_Xx);
#else // ^^^ defined(__clang__) / intrinsics unavailable vvv
    return _CSTD roundl(_Xx);
#endif // ^^^ intrinsics unavailable ^^^
}

To use the version with __round intrinsic function I have to cast the parameter to long double otherwise the old C version from math.h (corecrt_math.h) header file is used, which is much slower.

Is there any reason for that or am I doing anything wrong?

const double number{ -0.493 };

const auto result{ std::round(number) }; // uses C function from math.h
const auto result{ std::round(static_cast<long double>(number)) }; // uses std::round from cmath with __round intrinsic function

I found this issue Use ceil/floor/round/etc. intrinsics so I don't know if this is still in progress, but it's been 4 years since the issue was opened..

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in the cmath overloads and compare them with corecrt_math.h; the issue identifies the long-double path and the C fallback for double. Check the existing discussion in issue #1234, then verify that std::round(double) uses the __round intrinsic rather than the slower C implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
62/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.