[libcu++] Fix cuda::std::ilogb and logb for subnormal arguments #11799
<!-- This is an auto-generated comment: summarize by coderabbit.ai -->
<!-- review_stack_entry_start -->
<a href="https://app.coderabbit.ai/change-stack/NVIDIA/cccl/pull/11799?scope=ghh_OusAJGA4HG8ljoOV0wJvVx8NmnZIjc2DdjmxOu0xZWA&cs_source=review_comment"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui-dark.svg?v=2" alt="Review in Change Stack →" width="220" height="32"></a>
<!-- review_stack_entry_end -->
<!-- recent_review_start -->
No actionable comments were generated in the recent review. 🎉
<details>
<summary>ℹ️ Recent review info</summary>
<details>
<summary>⚙️ Run configuration</summary>
af30f5a1-dc51-45ff-94d9-f3dc889e506e<hr>
</details>
<details>
<summary>📥 Commits</summary>
Reviewing files that changed from the base of the PR and between 0577044fc3b0b475eabcb80645fb2320e7017b29 and 4fcdfdde2e950bc01ce1745a1f1015296a303ac3.
<hr>
</details>
<details>
<summary>📒 Files selected for processing (3)</summary>
libcudacxx/include/cuda/std/__cmath/logarithms.h
libcudacxx/include/cuda/std/__floating_point/decompose.h
libcudacxx/test/libcudacxx/libcxx/numerics/floating.point/decompose/get_exp.pass.cpp
<hr>
</details>
Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review.
<hr>
</details>
---
<!-- recent_review_end -->
<!-- walkthrough_start -->
<details>
<summary>📝 Summary</summary>
<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
Summary by CodeRabbit
Bug Fixes
Corrected ilogb and logb results for subnormal floating-point values, including the smallest positive and negative values and values near the normal range. Smallest normal values retain their expected exponents.
Corrected exponent results for subnormal values in floating-point decomposition.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Walkthrough
ilogb and logb now calculate exponents for subnormal values. Tests cover selected positive and negative subnormals and the smallest normal across supported floating-point types.
Changes
Subnormal exponent handling
|Layer / File(s)|Summary|
|:---|:---|
|Normalize subnormal exponents <br> libcudacxx/include/cuda/std/__floating_point/decompose.h, libcudacxx/test/libcudacxx/libcxx/numerics/floating.point/decompose/get_exp.pass.cpp|__fp_get_exp derives a subnormal’s exponent from its highest set mantissa bit. The denormal exponent test now expects that value.|
|Handle subnormal logarithm inputs <br> libcudacxx/include/cuda/std/__cmath/logarithms.h, libcudacxx/test/libcudacxx/std/numerics/c.math/logb_subnormal.pass.cpp|ilogb returns extracted exponents below the format’s minimum exponent. The native logb fallback uses a signed exponent counter and scales values below 1. Tests check selected subnormal values and the smallest normal at runtime, and at compile time when constexpr bit-cast support is available.|
Suggested reviewers: miscco
<hr>
</details>
<!-- walkthrough_end -->
<!-- change_assessment_start -->
Priority: ⬇️ Low
<!-- change_assessment_commit:"4fcdfdde2e950bc01ce1745a1f1015296a303ac3" -->
Change: Bug fix
<!-- change_assessment_end -->
<!-- final_review_risk_start -->
Merge Risk: ⚪ Minimal · up to 4fcdf
<!-- final_review_risk_coverage:{"sourceCommitId":"4fcdfdde2e950bc01ce1745a1f1015296a303ac3","coveredCommitId":"4fcdfdde2e950bc01ce1745a1f1015296a303ac3","kind":"reviewed"} -->
No unresolved defect is established by the reviewed changes; the PR is mergeable after normal checks.
<!-- final_review_risk_end -->
<!-- autopilot:start -->
Autopilot is currently an internal CodeRabbit preview.
<!-- autopilot:end -->
<!-- tips_start -->
---
<sub>Comment @coderabbitai help to get the list of available commands.</sub>
<!-- tips_end -->
no need for doxygen comments on internal functions
inline is redundant here
[[nodiscard]] _CCCL_API constexpr int __fp_get_exp_normalized(const _Tp __x) noexcept
int64_t __exp = 0;
@davebayer would make sense to move this utility in the __fp functionalities?
Does not hurt, especially for nontrivial math things
Definitely
Thanks a lot, this is a great PR.
I would like this to be close to __fp_get_exp, and would like to think hard whether we need to just fix __fp_get_exp
I would like this to be close to__fp_get_exp, and would like to think hard whether we need to just fix__fp_get_exp
Actually there are no other uses of
__fp_get_exp so we should just fix itI meant do not use doxygen comment, a standard comment is fine :)
Fixed __fp_get_exp itself as suggested. A subnormal now gets its true exponent from the highest set mantissa bit, and zero, infinity and NaN still return the raw exponent field minus the bias. ilogb and logb call it directly and __fp_get_exp_normalized is gone, which settles the remarks on its doxygen comment, inline and signature. The int64_t suggestion is applied in the logb fallback.
The description is out of date, since __fp_get_exp isn't unchanged anymore and there's no new helper. get_exp.pass.cpp now expects denorm_min() to report exp_min - digits + 1, logb_subnormal.pass.cpp is as before, and the conflict with main in logarithms.h is gone. CI needs a /ok to test for the new commit.
ilogb and logb of denorm_min() give -149, -1074, -24 and -133 for float, double, __half and __nv_bfloat16 on the host and on an RTX 5090, and the 63 tests under libcxx/numerics/floating.point and std/numerics/c.math pass with nvcc 13.2 for sm_120.
fbusato
Arthur031221
Description
cuda::std::ilogbandcuda::std::logbreturn the wrong exponent for subnormal arguments, so code that uses them to rescale or normalise denormals gets wrong results:Built with
g++ -std=c++20 -Ilibcudacxx/includeon the parent commit this prints:and with this change:
Both functions read the exponent from the biased exponent field through
__fp_get_exp. That field is zero for every subnormal, so the result is the exponent of the smallest normal number minus one, andilogbthen mapped it toINT_MINthrough the__exp < __fp_exp_min_vbranch. The new helper__fp_get_exp_normalizedtakes the exponent from the highest set mantissa bit when the biased exponent is zero and the mantissa is not, asexp_min + bit_width(mantissa) - digits. This also covers the x87long doubleformat, where the integer bit is explicit.__fp_get_expitself is unchanged, so its existing test is unaffected.The fallback in
logbfor compilers without a constexprbit_castlooped only while|x| >= 2, so it returned 0 for every value below 1, not only for subnormals. It now scales up as well.Testing
New test
logb_subnormal.pass.cppchecksdenorm_min,3 * denorm_min, the largest subnormal andmin()forfloat,doubleandlong double, with both signs, at run time and in astatic_assert.Run locally with gcc 13.3 (host only,
-std=c++20 -Ilibcudacxx/include -Ilibcudacxx/test/support): the new test passes,get_exp.pass.cpppasses, andlogarithms.pass.cpppasses with its__global__kernel removed. It also passes with_CCCL_HAS_CONSTEXPR_BIT_CAST()forced to 0, which exercises the fallback. With the headers from the parent commit the new test fails atilogb(smallest) == min_exp. Not run: nvcc, clang, MSVC,__half,__nv_bfloat16and the fp8/fp6/fp4 formats (the helper is generic over the format description and does not special-case them).Checklist