This is an archive of the discontinued LLVM Phabricator instance.

[libc][NFC] Use cpp::optional for checking exceptional values of math functions.
ClosedPublic

Authored by lntue on Sep 1 2022, 11:54 AM.

Details

Summary

Update the utility functions for checking exceptional values of math
functions to use cpp::optional return values.

Diff Detail

Event Timeline

lntue created this revision.Sep 1 2022, 11:54 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptSep 1 2022, 11:54 AM
lntue requested review of this revision.Sep 1 2022, 11:54 AM
sivachandra added inline comments.Sep 1 2022, 12:36 PM
libc/src/__support/FPUtil/except_value_utils.h
43

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.
  }
};
libc/src/math/generic/cosf.cpp
110

Patterns like this should get simplified to:

if (auto r = COSF_EXCEPTS.lookup(x_abs); unlikely(r))
  return r;
lntue updated this revision to Diff 457384.Sep 1 2022, 1:49 PM

Address comments.

lntue marked an inline comment as done.Sep 1 2022, 1:51 PM
lntue added inline comments.
libc/src/math/generic/cosf.cpp
110

I tried it but our current implementation of cpp::optional has no implicit conversion to T and the conversion to bool is explicit.

sivachandra accepted this revision.Sep 1 2022, 2:11 PM
sivachandra added inline comments.
libc/src/math/generic/cosf.cpp
110

Ah yes! We should do *r but thats a nitty nit at this point.

This revision is now accepted and ready to land.Sep 1 2022, 2:11 PM
This revision was landed with ongoing or failed builds.Sep 1 2022, 2:39 PM
This revision was automatically updated to reflect the committed changes.