Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libc][math][c23] adds nanf128 #85201

Merged
merged 7 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
libc.src.math.lrintf128
libc.src.math.lroundf128
libc.src.math.modff128
libc.src.math.nanf128
Flandini marked this conversation as resolved.
Show resolved Hide resolved
libc.src.math.nextafterf128
libc.src.math.rintf128
libc.src.math.roundf128
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
libc.src.math.lrintf128
libc.src.math.lroundf128
libc.src.math.modff128
libc.src.math.nanf128
libc.src.math.nextafterf128
libc.src.math.rintf128
libc.src.math.roundf128
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
libc.src.math.lrintf128
libc.src.math.lroundf128
libc.src.math.modff128
libc.src.math.nanf128
libc.src.math.nextafterf128
libc.src.math.rintf128
libc.src.math.roundf128
Expand Down
2 changes: 2 additions & 0 deletions libc/docs/math/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ Basic Operations
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| nanl | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| nanf128 | |check| | |check| | | |check| | | | | | | | | |
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| nearbyint | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| nearbyintf | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |
Expand Down
1 change: 1 addition & 0 deletions libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"nanf", RetValSpec<FloatType>, [ArgSpec<ConstCharPtr>]>,
FunctionSpec<"nan", RetValSpec<DoubleType>, [ArgSpec<ConstCharPtr>]>,
FunctionSpec<"nanl", RetValSpec<LongDoubleType>, [ArgSpec<ConstCharPtr>]>,
GuardedFunctionSpec<"nanf128", RetValSpec<Float128Type>, [ArgSpec<ConstCharPtr>], "LIBC_TYPES_HAS_FLOAT128">,
]
>;

Expand Down
62 changes: 62 additions & 0 deletions libc/src/__support/UInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,68 @@ struct is_big_int<BigInt<Bits, Signed, T>> : cpp::true_type {};
template <class T>
LIBC_INLINE_VAR constexpr bool is_big_int_v = is_big_int<T>::value;

// extensions of type traits to include BigInt

// is_integral_or_big_int
template <typename T>
struct is_integral_or_big_int
: cpp::bool_constant<(cpp::is_integral_v<T> || is_big_int_v<T>)> {};

template <typename T>
LIBC_INLINE_VAR constexpr bool is_integral_or_big_int_v =
is_integral_or_big_int<T>::value;

// make_big_int_unsigned
template <typename T> struct make_big_int_unsigned;

template <size_t Bits, bool Signed, typename T>
struct make_big_int_unsigned<BigInt<Bits, Signed, T>>
: cpp::type_identity<BigInt<Bits, false, T>> {};

template <typename T>
using make_big_int_unsigned_t = typename make_big_int_unsigned<T>::type;

// make_big_int_signed
template <typename T> struct make_big_int_signed;

template <size_t Bits, bool Signed, typename T>
struct make_big_int_signed<BigInt<Bits, Signed, T>>
: cpp::type_identity<BigInt<Bits, true, T>> {};

template <typename T>
using make_big_int_signed_t = typename make_big_int_signed<T>::type;

// make_integral_or_big_int_unsigned
template <typename T, class = void> struct make_integral_or_big_int_unsigned;

template <typename T>
struct make_integral_or_big_int_unsigned<
T, cpp::enable_if_t<cpp::is_integral_v<T>>> : cpp::make_unsigned<T> {};

template <typename T>
struct make_integral_or_big_int_unsigned<T, cpp::enable_if_t<is_big_int_v<T>>>
: make_big_int_unsigned<T> {};

template <typename T>
using make_integral_or_big_int_unsigned_t =
typename make_integral_or_big_int_unsigned<T>::type;

// make_integral_or_big_int_signed
template <typename T, class = void> struct make_integral_or_big_int_signed;

template <typename T>
struct make_integral_or_big_int_signed<T,
cpp::enable_if_t<cpp::is_integral_v<T>>>
: cpp::make_signed<T> {};

template <typename T>
struct make_integral_or_big_int_signed<T, cpp::enable_if_t<is_big_int_v<T>>>
: make_big_int_signed<T> {};

template <typename T>
using make_integral_or_big_int_signed_t =
typename make_integral_or_big_int_signed<T>::type;

namespace cpp {

// Specialization of cpp::bit_cast ('bit.h') from T to BigInt.
Expand Down
16 changes: 2 additions & 14 deletions libc/src/__support/integer_to_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
#include "src/__support/CPP/span.h"
#include "src/__support/CPP/string_view.h"
#include "src/__support/CPP/type_traits.h"
#include "src/__support/UInt.h" // is_big_int
#include "src/__support/UInt.h" // make_integral_or_big_int_unsigned_t
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {
Expand Down Expand Up @@ -150,18 +150,6 @@ template <bool forward> class StringBufferWriterImpl {
using StringBufferWriter = StringBufferWriterImpl<true>;
using BackwardStringBufferWriter = StringBufferWriterImpl<false>;

template <typename T, class = void> struct IntegerWriterUnsigned {};

template <typename T>
struct IntegerWriterUnsigned<T, cpp::enable_if_t<cpp::is_integral_v<T>>> {
using type = cpp::make_unsigned_t<T>;
};

template <typename T>
struct IntegerWriterUnsigned<T, cpp::enable_if_t<is_big_int_v<T>>> {
using type = typename T::unsigned_type;
};

} // namespace details

namespace radix {
Expand Down Expand Up @@ -222,7 +210,7 @@ template <typename T, typename Fmt = radix::Dec> class IntegerToString {
// An internal stateless structure that handles the number formatting logic.
struct IntegerWriter {
static_assert(cpp::is_integral_v<T> || is_big_int_v<T>);
using UNSIGNED_T = typename details::IntegerWriterUnsigned<T>::type;
using UNSIGNED_T = make_integral_or_big_int_unsigned_t<T>;

LIBC_INLINE static char digit_char(uint8_t digit) {
if (digit < 10)
Expand Down
26 changes: 11 additions & 15 deletions libc/src/__support/str_to_float.h
Original file line number Diff line number Diff line change
Expand Up @@ -1056,17 +1056,17 @@ hexadecimal_string_to_float(const char *__restrict src,
return output;
}

LIBC_INLINE uint64_t
template <class T>
LIBC_INLINE typename fputil::FPBits<T>::StorageType
nan_mantissa_from_ncharseq(const cpp::string_view ncharseq) {
uint64_t nan_mantissa = 0;
using FPBits = typename fputil::FPBits<T>;
using StorageType = typename FPBits::StorageType;

StorageType nan_mantissa = 0;

if (ncharseq.data() != nullptr && isdigit(ncharseq[0])) {
// This is to prevent errors when StorageType is larger than 64
// bits, since strtointeger only supports up to 64 bits. This is
// actually more than is required by the specification, which says
// for the input type "NAN(n-char-sequence)" that "the meaning of
// the n-char sequence is implementation-defined."
auto strtoint_result = strtointeger<uint64_t>(ncharseq.data(), 0);
StrToNumResult<StorageType> strtoint_result =
strtointeger<StorageType>(ncharseq.data(), 0);
if (!strtoint_result.has_error())
nan_mantissa = strtoint_result.value;

Expand Down Expand Up @@ -1172,9 +1172,8 @@ LIBC_INLINE StrToNumResult<T> strtofloatingpoint(const char *__restrict src) {
++index;
if (src[index] == ')') {
++index;
auto nan_mantissa_result = nan_mantissa_from_ncharseq(
nan_mantissa = nan_mantissa_from_ncharseq<T>(
cpp::string_view(src + (left_paren + 1), index - left_paren - 2));
nan_mantissa = static_cast<StorageType>(nan_mantissa_result);
} else {
index = left_paren;
}
Expand Down Expand Up @@ -1221,11 +1220,8 @@ template <class T> LIBC_INLINE StrToNumResult<T> strtonan(const char *arg) {
while (isalnum(arg[index]) || arg[index] == '_')
++index;

if (arg[index] == '\0') {
auto nan_mantissa_result =
nan_mantissa_from_ncharseq(cpp::string_view(arg, index));
nan_mantissa = static_cast<StorageType>(nan_mantissa_result);
}
if (arg[index] == '\0')
nan_mantissa = nan_mantissa_from_ncharseq<T>(cpp::string_view(arg, index));

result = FPBits::quiet_nan(fputil::Sign::POS, nan_mantissa);
return {result.get_val(), 0, error};
Expand Down
34 changes: 21 additions & 13 deletions libc/src/__support/str_to_integer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "src/__support/CPP/limits.h"
#include "src/__support/CPP/type_traits.h"
#include "src/__support/UInt128.h"
#include "src/__support/common.h"
#include "src/__support/ctype_utils.h"
#include "src/__support/str_to_num_result.h"
Expand Down Expand Up @@ -75,8 +76,12 @@ template <class T>
LIBC_INLINE StrToNumResult<T>
strtointeger(const char *__restrict src, int base,
const size_t src_len = cpp::numeric_limits<size_t>::max()) {
// TODO: Rewrite to support numbers longer than long long
unsigned long long result = 0;
using ResultType = typename cpp::conditional_t<(cpp::is_same_v<T, UInt128> ||
cpp::is_same_v<T, Int128>),
UInt128, unsigned long long>;

ResultType result = 0;

bool is_number = false;
size_t src_cur = 0;
int error_val = 0;
Expand All @@ -101,15 +106,16 @@ strtointeger(const char *__restrict src, int base,
if (base == 16 && is_hex_start(src + src_cur, src_len - src_cur))
src_cur = src_cur + 2;

constexpr bool IS_UNSIGNED = (cpp::numeric_limits<T>::min() == 0);
constexpr bool IS_UNSIGNED = cpp::is_unsigned_v<T>;
const bool is_positive = (result_sign == '+');
unsigned long long constexpr NEGATIVE_MAX =
!IS_UNSIGNED
? static_cast<unsigned long long>(cpp::numeric_limits<T>::max()) + 1
: cpp::numeric_limits<T>::max();
unsigned long long const abs_max =

ResultType constexpr NEGATIVE_MAX =
!IS_UNSIGNED ? static_cast<ResultType>(cpp::numeric_limits<T>::max()) + 1
: cpp::numeric_limits<T>::max();
ResultType const abs_max =
(is_positive ? cpp::numeric_limits<T>::max() : NEGATIVE_MAX);
unsigned long long const abs_max_div_by_base = abs_max / base;
ResultType const abs_max_div_by_base = abs_max / base;
lntue marked this conversation as resolved.
Show resolved Hide resolved

while (src_cur < src_len && isalnum(src[src_cur])) {
int cur_digit = b36_char_to_int(src[src_cur]);
if (cur_digit >= base)
Expand Down Expand Up @@ -149,10 +155,12 @@ strtointeger(const char *__restrict src, int base,
return {cpp::numeric_limits<T>::min(), str_len, error_val};
}

return {is_positive
? static_cast<T>(result)
: static_cast<T>(-static_cast<cpp::make_unsigned_t<T>>(result)),
str_len, error_val};
return {
is_positive
? static_cast<T>(result)
: static_cast<T>(
-static_cast<make_integral_or_big_int_unsigned_t<T>>(result)),
str_len, error_val};
}

} // namespace internal
Expand Down
1 change: 1 addition & 0 deletions libc/src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ add_math_entrypoint_object(modff128)
add_math_entrypoint_object(nan)
add_math_entrypoint_object(nanf)
add_math_entrypoint_object(nanl)
add_math_entrypoint_object(nanf128)

add_math_entrypoint_object(nearbyint)
add_math_entrypoint_object(nearbyintf)
Expand Down
13 changes: 13 additions & 0 deletions libc/src/math/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1780,6 +1780,19 @@ add_entrypoint_object(
-O3
)

add_entrypoint_object(
nanf128
SRCS
nanf128.cpp
HDRS
../nanf128.h
DEPENDS
libc.src.__support.str_to_float
libc.src.errno.errno
COMPILE_OPTIONS
-O3
)

add_entrypoint_object(
nextafter
SRCS
Expand Down
23 changes: 23 additions & 0 deletions libc/src/math/generic/nanf128.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===-- Implementation of nanf128 function --------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "src/math/nanf128.h"
#include "src/__support/common.h"
#include "src/__support/str_to_float.h"
#include "src/errno/libc_errno.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(float128, nanf128, (const char *arg)) {
auto result = internal::strtonan<float128>(arg);
if (result.has_error())
libc_errno = result.error;
return result.value;
}

} // namespace LIBC_NAMESPACE
20 changes: 20 additions & 0 deletions libc/src/math/nanf128.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for nanf128 -----------------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_MATH_NANF128_H
#define LLVM_LIBC_SRC_MATH_NANF128_H

#include "src/__support/macros/properties/types.h"

namespace LIBC_NAMESPACE {

float128 nanf128(const char *arg);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_MATH_NANF128_H
16 changes: 16 additions & 0 deletions libc/test/src/math/smoke/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1506,6 +1506,22 @@ add_fp_unittest(
UNIT_TEST_ONLY
)

add_fp_unittest(
nanf128_test
SUITE
libc-math-smoke-tests
SRCS
nanf128_test.cpp
DEPENDS
libc.include.math
libc.include.signal
libc.src.math.nanf128
libc.src.__support.FPUtil.fp_bits
# FIXME: The nan tests currently have death tests, which aren't supported for
# hermetic tests.
UNIT_TEST_ONLY
)

add_fp_unittest(
nextafter_test
SUITE
Expand Down
Loading
Loading