[libcxx-commits] [libcxx] [libc++][math] Fix undue overflowing of `std::hypot(x, y, z)` (PR #93350)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Thu Jul 4 12:14:49 PDT 2024


================
@@ -41,6 +46,89 @@ inline _LIBCPP_HIDE_FROM_ABI typename __promote<_A1, _A2>::type hypot(_A1 __x, _
   return __math::hypot((__result_type)__x, (__result_type)__y);
 }
 
+#if _LIBCPP_STD_VER >= 17
+// Factors needed to determine if over-/underflow might happen for `std::hypot(x,y,z)`.
+template <class _Real>
+struct __hypot_factors {
+  _Real __threshold;
+  _Real __scale_xyz;
+  _Real __scale_M;
+};
+
+// Computes `__hypot_factors` needed to determine if over-/underflow might happen for `std::hypot(x,y,z)`.
+// Returns: [underflow_factors, overflow_factors]
+template <class _Real>
+_LIBCPP_HIDE_FROM_ABI array<__math::__hypot_factors<_Real>, 2> __create_factors() {
----------------
ldionne wrote:

This could also be moved to a static member function like

```c++
template <class _Real>
 struct __hypot_factors {
   _Real __threshold;
   _Real __scale_xyz;
   _Real __scale_M;

  _LIBCPP_HIDE_FROM_ABI static array<__hypot_factors<_Real>, 2> __create() { ... }
 };
```

That way there's no ambiguity if we ever introduce another function called `__math::__create_factors`, which I could imagine since this is a pretty generic name. Non-blocking, this is just a suggestion.

https://github.com/llvm/llvm-project/pull/93350


More information about the libcxx-commits mailing list