Skip to content

[libc][math] Implement nan(f|l) functions #76690

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

Merged
merged 9 commits into from
Jan 5, 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
3 changes: 3 additions & 0 deletions libc/config/darwin/arm/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.modf
libc.src.math.modff
libc.src.math.modfl
libc.src.math.nan
libc.src.math.nanf
libc.src.math.nanl
libc.src.math.nearbyint
libc.src.math.nearbyintf
libc.src.math.nearbyintl
Expand Down
3 changes: 3 additions & 0 deletions libc/config/darwin/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ set(TARGET_LIBM_ENTRYPOINTS
#libc.src.math.modf
#libc.src.math.modff
#libc.src.math.modfl
#libc.src.math.nan
#libc.src.math.nanf
#libc.src.math.nanl
#libc.src.math.nearbyint
#libc.src.math.nearbyintf
#libc.src.math.nearbyintl
Expand Down
2 changes: 2 additions & 0 deletions libc/config/gpu/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.lroundf
libc.src.math.modf
libc.src.math.modff
libc.src.math.nan
libc.src.math.nanf
libc.src.math.nearbyint
libc.src.math.nearbyintf
libc.src.math.nextafter
Expand Down
3 changes: 3 additions & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.modf
libc.src.math.modff
libc.src.math.modfl
libc.src.math.nan
libc.src.math.nanf
libc.src.math.nanl
libc.src.math.nearbyint
libc.src.math.nearbyintf
libc.src.math.nearbyintl
Expand Down
3 changes: 3 additions & 0 deletions libc/config/linux/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,9 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.modf
libc.src.math.modff
libc.src.math.modfl
libc.src.math.nan
libc.src.math.nanf
libc.src.math.nanl
libc.src.math.nearbyint
libc.src.math.nearbyintf
libc.src.math.nearbyintl
Expand Down
3 changes: 3 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,9 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.modf
libc.src.math.modff
libc.src.math.modfl
libc.src.math.nan
libc.src.math.nanf
libc.src.math.nanl
libc.src.math.nearbyint
libc.src.math.nearbyintf
libc.src.math.nearbyintl
Expand Down
3 changes: 3 additions & 0 deletions libc/config/windows/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.modf
libc.src.math.modff
libc.src.math.modfl
libc.src.math.nan
libc.src.math.nanf
libc.src.math.nanl
libc.src.math.nearbyint
libc.src.math.nearbyintf
libc.src.math.nearbyintl
Expand Down
6 changes: 3 additions & 3 deletions libc/docs/math/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,11 @@ Basic Operations
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| modfl | |check| | |check| | | |check| | |check| | | | |check| | | | | |
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| nan | | | | | | | | | | | | |
| nan | |check| | |check| | | |check| | |check| | | | |check| | | | | |
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| nanf | | | | | | | | | | | | |
| nanf | |check| | |check| | | |check| | |check| | | | |check| | | | | |
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| nanl | | | | | | | | | | | | |
| nanl | |check| | |check| | | |check| | |check| | | | |check| | | | | |
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| nearbyint | |check| | |check| | | |check| | |check| | | | |check| | | | | |
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
Expand Down
4 changes: 4 additions & 0 deletions libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,10 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"scalbn", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<IntType>]>,
FunctionSpec<"scalbnf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<IntType>]>,
FunctionSpec<"scalbnl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<IntType>]>,

FunctionSpec<"nanf", RetValSpec<FloatType>, [ArgSpec<ConstCharPtr>]>,
FunctionSpec<"nan", RetValSpec<DoubleType>, [ArgSpec<ConstCharPtr>]>,
FunctionSpec<"nanl", RetValSpec<LongDoubleType>, [ArgSpec<ConstCharPtr>]>,
]
>;

Expand Down
65 changes: 48 additions & 17 deletions libc/src/__support/str_to_float.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "src/__support/CPP/bit.h"
#include "src/__support/CPP/limits.h"
#include "src/__support/CPP/optional.h"
#include "src/__support/CPP/string_view.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/dyadic_float.h"
Expand Down Expand Up @@ -1055,6 +1056,27 @@ hexadecimal_string_to_float(const char *__restrict src,
return output;
}

LIBC_INLINE uint64_t
nan_mantissa_from_ncharseq(const cpp::string_view ncharseq) {
uint64_t 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);
if (!strtoint_result.has_error())
nan_mantissa = strtoint_result.value;

if (strtoint_result.parsed_len != static_cast<ptrdiff_t>(ncharseq.size()))
nan_mantissa = 0;
}

return nan_mantissa;
}

// Takes a pointer to a string and a pointer to a string pointer. This function
// is used as the backend for all of the string to float functions.
template <class T>
Expand Down Expand Up @@ -1147,31 +1169,18 @@ LIBC_INLINE StrToNumResult<T> strtofloatingpoint(const char *__restrict src) {
++index;
if (src[index] == ')') {
++index;
if (isdigit(src[left_paren + 1])) {
// 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>(src + (left_paren + 1), 0);
if (strtoint_result.has_error()) {
error = strtoint_result.error;
}
nan_mantissa = static_cast<StorageType>(strtoint_result.value);
if (src[left_paren + 1 + strtoint_result.parsed_len] != ')')
nan_mantissa = 0;
}
auto nan_mantissa_result = nan_mantissa_from_ncharseq(
cpp::string_view(src + (left_paren + 1), index - left_paren - 2));
nan_mantissa = static_cast<StorageType>(nan_mantissa_result);
} else {
index = left_paren;
}
}

if (result.get_sign()) {
result = FPBits(result.build_quiet_nan(nan_mantissa));
result.set_sign(true);
} else {
result.set_sign(false);
result = FPBits(result.build_quiet_nan(nan_mantissa));
}
}
Expand Down Expand Up @@ -1206,6 +1215,28 @@ LIBC_INLINE StrToNumResult<T> strtofloatingpoint(const char *__restrict src) {
return {T(result), index, error};
}

template <class T> LIBC_INLINE StrToNumResult<T> strtonan(const char *arg) {
using FPBits = typename fputil::FPBits<T>;
using StorageType = typename FPBits::StorageType;

FPBits result;
int error = 0;
StorageType nan_mantissa = 0;

ptrdiff_t index = 0;
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);
}

result = FPBits(result.build_quiet_nan(nan_mantissa));
return {T(result), 0, error};
}

} // namespace internal
} // namespace LIBC_NAMESPACE

Expand Down
4 changes: 4 additions & 0 deletions libc/src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ add_math_entrypoint_object(modf)
add_math_entrypoint_object(modff)
add_math_entrypoint_object(modfl)

add_math_entrypoint_object(nan)
add_math_entrypoint_object(nanf)
add_math_entrypoint_object(nanl)

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

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

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

add_entrypoint_object(
nanl
SRCS
nanl.cpp
HDRS
../nanl.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/nan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===-- Implementation of nan 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/nan.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(double, nan, (const char *arg)) {
auto result = internal::strtonan<double>(arg);
if (result.has_error())
libc_errno = result.error;
return result.value;
}

} // namespace LIBC_NAMESPACE
23 changes: 23 additions & 0 deletions libc/src/math/generic/nanf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===-- Implementation of nanf 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/nanf.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(float, nanf, (const char *arg)) {
auto result = internal::strtonan<float>(arg);
if (result.has_error())
libc_errno = result.error;
return result.value;
}

} // namespace LIBC_NAMESPACE
23 changes: 23 additions & 0 deletions libc/src/math/generic/nanl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===-- Implementation of nanl 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/nanl.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(long double, nanl, (const char *arg)) {
auto result = internal::strtonan<long double>(arg);
if (result.has_error())
libc_errno = result.error;
return result.value;
}

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

namespace LIBC_NAMESPACE {

double nan(const char *arg);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_MATH_NAN_H
18 changes: 18 additions & 0 deletions libc/src/math/nanf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header for nanf --------------------------*- 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_NANF_H
#define LLVM_LIBC_SRC_MATH_NANF_H

namespace LIBC_NAMESPACE {

float nanf(const char *arg);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_MATH_NANF_H
18 changes: 18 additions & 0 deletions libc/src/math/nanl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header for nanl --------------------------*- 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_NANL_H
#define LLVM_LIBC_SRC_MATH_NANL_H

namespace LIBC_NAMESPACE {

long double nanl(const char *arg);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_MATH_NANL_H
Loading