Files
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

148 lines
4.9 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file contains the definition of numeric utilities, including functions
/// to compute ULP distance and traits for floating-point types.
///
//===----------------------------------------------------------------------===//
#ifndef MATHTEST_NUMERICS_HPP
#define MATHTEST_NUMERICS_HPP
#include "mathtest/Support.hpp"
#include "mathtest/TypeExtras.hpp"
// These headers are in the shared LLVM-libc header library
#include "shared/fp_bits.h"
#include "shared/sign.h"
#include <climits>
#include <cstdint>
#include <limits>
#include <math.h>
#include <type_traits>
namespace mathtest {
template <typename FloatType>
using FPBits = LIBC_NAMESPACE::shared::FPBits<FloatType>;
using Sign = LIBC_NAMESPACE::shared::Sign;
//===----------------------------------------------------------------------===//
// Type Traits
//===----------------------------------------------------------------------===//
template <typename T> struct IsFloatingPoint : std::is_floating_point<T> {};
template <> struct IsFloatingPoint<float16> : std::true_type {};
template <typename T>
inline constexpr bool IsFloatingPoint_v // NOLINT(readability-identifier-naming)
= IsFloatingPoint<T>::value;
template <typename T> struct StorageTypeOf {
private:
static constexpr auto getStorageType() noexcept {
if constexpr (IsFloatingPoint_v<T>)
return TypeIdentityOf<typename FPBits<T>::StorageType>{};
else if constexpr (std::is_unsigned_v<T>)
return TypeIdentityOf<T>{};
else if constexpr (std::is_signed_v<T>)
return TypeIdentityOf<std::make_unsigned_t<T>>{};
else
static_assert(!std::is_same_v<T, T>, "Unsupported type");
}
public:
using type = typename decltype(getStorageType())::type;
};
template <typename T> using StorageTypeOf_t = typename StorageTypeOf<T>::type;
//===----------------------------------------------------------------------===//
// Numeric Functions
//===----------------------------------------------------------------------===//
template <typename T> [[nodiscard]] constexpr T getMinOrNegInf() noexcept {
if constexpr (IsFloatingPoint_v<T>) {
// All currently supported floating-point types have infinity
return FPBits<T>::inf(Sign::NEG).get_val();
} else {
static_assert(std::is_integral_v<T>,
"Type T must be an integral or floating-point type");
return std::numeric_limits<T>::lowest();
}
}
template <typename T> [[nodiscard]] constexpr T getMaxOrInf() noexcept {
if constexpr (IsFloatingPoint_v<T>) {
// All currently supported floating-point types have infinity
return FPBits<T>::inf(Sign::POS).get_val();
} else {
static_assert(std::is_integral_v<T>,
"Type T must be an integral or floating-point type");
return std::numeric_limits<T>::max();
}
}
template <typename FloatType>
[[nodiscard]] uint64_t computeUlpDistance(FloatType X, FloatType Y) noexcept {
static_assert(IsFloatingPoint_v<FloatType>,
"FloatType must be a floating-point type");
using FPBits = FPBits<FloatType>;
using StorageType = typename FPBits::StorageType;
const FPBits XBits(X);
const FPBits YBits(Y);
if (X == Y) {
if (XBits.sign() != YBits.sign()) [[unlikely]] {
// When X == Y, different sign bits imply that X and Y are +0.0 and -0.0
// (in any order). Since we want to treat them as unequal in the context
// of accuracy testing of mathematical functions, we return the smallest
// non-zero value.
return 1;
}
return 0;
}
const bool XIsNaN = XBits.is_nan();
const bool YIsNaN = YBits.is_nan();
if (XIsNaN && YIsNaN)
return 0;
if (XIsNaN || YIsNaN)
return std::numeric_limits<uint64_t>::max();
constexpr StorageType SignMask = FPBits::SIGN_MASK;
// Linearise FloatType values into an ordered unsigned space. Let a and b
// be bits(x), bits(y), respectively, where x and y are FloatType values.
// * The mapping is monotonic: x >= y if, and only if, map(a) >= map(b).
// * The difference |map(a) − map(b)| equals the number of std::nextafter
// steps between a and b within the same type.
auto MapToOrderedUnsigned = [](FPBits Bits) {
const StorageType Unsigned = Bits.uintval();
return (Unsigned & SignMask) ? SignMask - (Unsigned - SignMask)
: SignMask + Unsigned;
};
const StorageType MappedX = MapToOrderedUnsigned(XBits);
const StorageType MappedY = MapToOrderedUnsigned(YBits);
return static_cast<uint64_t>(MappedX > MappedY ? MappedX - MappedY
: MappedY - MappedX);
}
} // namespace mathtest
#endif // MATHTEST_NUMERICS_HPP