[libc-commits] [PATCH] D133134: [libc][NFC] Use cpp::optional for checking exceptional values of math functions.

Siva Chandra via Phabricator via libc-commits libc-commits at lists.llvm.org
Thu Sep 1 12:36:44 PDT 2022


sivachandra added inline comments.


================
Comment at: libc/src/__support/FPUtil/except_value_utils.h:44
+
+template <typename T, int N> struct ExceptValues {
   static constexpr int SIZE = N;
----------------
I want to suggest a rearrangement:

```
template <typename T, size_t N> struct ExceptValues {
  static_assert(cpp::is_floating_point_v<T>, "...");
  using UIntType = typename FPBits<T>::UIntType;
  struct Mapping { // This is the same as ExceptValueOutput from above with an additional field
    UIntType input;
    UIntType rnd_towardzero_result;
    ...
  };

  Mapping values[N];

  // You can choose to prevent implicit type promotions and convertions using enable_if
  constexpr cpp::optional<T> lookup(UIntType bits) const {
    ...
  }
  constexpr cpp::optional<T> lookup(T x) const {
    // Implement this method if required
  }
  constexpr cpp::optional<T> lookup_odd(UIntType bits) const {
    ...
  }
  constexpr cpp::optional<T> lookup_odd(T x) const (
    // Implement this method if required.
  }
};
```


================
Comment at: libc/src/math/generic/cosf.cpp:114
+  if (auto r =
+          fputil::check_except_values<float, N_EXCEPTS>(COSF_EXCEPTS, x_abs);
+      unlikely(r.has_value()))
----------------
Patterns like this should get simplified to:

```
if (auto r = COSF_EXCEPTS.lookup(x_abs); unlikely(r))
  return r;
```


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D133134/new/

https://reviews.llvm.org/D133134



More information about the libc-commits mailing list