Repository: apache/arrow
File: cpp/src/arrow/testing/math.cc (C++)

Code before:
  // https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
  static UIntType UlpDistnace(Float left, Float right) {
    auto unsigned_left = BitCast<UIntType>(left);
    auto unsigned_right = BitCast<UIntType>(right);
    auto biased_left = ConvertSignAndMagnitudeToBiased(unsigned_left);
    auto biased_right = ConvertSignAndMagnitudeToBiased(unsigned_right);
    if (biased_left > biased_right) {
      std::swap(biased_left, biased_right);
    }
    return biased_right - biased_left;
  }

 private:
  template <typename To, typename From>
  union BitCastUnion {
    explicit BitCastUnion(From from) : from(from) {}
    From from;
    To to;
  };

  template <typename To, typename From>
  static UIntType BitCast(From value) {
    assert(sizeof(To) == sizeof(From));
    BitCastUnion<To, From> bit_cast(value);
    return bit_cast.to;
  }

  // Source reference (GoogleTest):
  // https://github.com/google/googletest/blob/085af2cc08600bdb13827ca40261abcbe5048bb5/googletest/include/gtest/internal/gtest-internal.h#L336-L342
  static UIntType ConvertSignAndMagnitudeToBiased(UIntType value) {
    if (value & kSignMask) {
      return ~value + 1;
    } else {
      return value | kSignMask;
    }
  }
};

template <typename Float>
bool WithinUlpGeneric(Float left, Float right, int n_ulps) {
  if constexpr (std::is_same_v<Float, util::Float16>) {
    if (left.is_nan() || right.is_nan()) {
      return left.is_nan() && right.is_nan();
    } else if (left.is_infinity() || right.is_infinity()) {
      return left == right;
    }
  } else {
    if (std::isnan(left) || std::isnan(right)) {
      return std::isnan(left) == std::isnan(right);
    }
    if (!std::isfinite(left) || !std::isfinite(right)) {

Review comment (suggestion):
You don't need this, you can just use `SafeCopy<UIntType>` (see `arrow/util/ubsan.h`)

Code after:
  // This implementation is inspired by:
  // https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
  static UIntType UlpDistance(Float left, Float right) {
    auto unsigned_left = util::SafeCopy<UIntType>(left);
    auto unsigned_right = util::SafeCopy<UIntType>(right);
    auto biased_left = ConvertSignAndMagnitudeToBiased(unsigned_left);
    auto biased_right = ConvertSignAndMagnitudeToBiased(unsigned_right);
    if (biased_left > biased_right) {
      std::swap(biased_left, biased_right);
    }
    return biased_right - biased_left;
  }

 private:
  // Source reference (GoogleTest):
  // https://github.com/google/googletest/blob/1b96fa13f549387b7549cc89e1a785cf143a1a50/googletest/include/gtest/internal/gtest-internal.h#L345-L368
  static UIntType ConvertSignAndMagnitudeToBiased(UIntType value) {
    if (value & kSignMask) {
      return ~value + 1;
    } else {
      return value | kSignMask;
    }
  }
};

template <typename Float>
bool WithinUlpGeneric(Float left, Float right, int n_ulps) {
  if constexpr (std::is_same_v<Float, util::Float16>) {
    if (left.is_nan() || right.is_nan()) {
      return left.is_nan() == right.is_nan();
    } else if (left.is_infinity() || right.is_infinity()) {
      return left == right;
    }
  } else {
    if (std::isnan(left) || std::isnan(right)) {
      return std::isnan(left) == std::isnan(right);
    }
    if (!std::isfinite(left) || !std::isfinite(right)) {
      return left == right;
    }
  }

  if (n_ulps == 0) {
    return left == right;
  }

  DCHECK_GE(n_ulps, 1);
  return UlpDistanceUtil<Float>::UlpDistance(left, right) <=
         static_cast<uint64_t>(n_ulps);
}