179 changes: 0 additions & 179 deletions clang/test/OpenMP/nvptx_target_printf_codegen.c

This file was deleted.

6 changes: 6 additions & 0 deletions clang/test/Parser/namelookup-anonymous-struct.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// RUN: %clang_cc1 -std=c11 -verify %s

struct GH31295 {
struct { int x; };
int arr[sizeof(x)]; // expected-error{{use of undeclared identifier 'x'}}
};
4 changes: 2 additions & 2 deletions clang/test/Preprocessor/predefined-macros.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
// RUN: | FileCheck -match-full-lines %s --check-prefix=CHECK-NO-MATH-ERRNO
// CHECK-NO-MATH-ERRNO: #define __NO_MATH_ERRNO__ 1
//
// RUN: %clang_cc1 %s -E -dM -ffinite-math-only -o - \
// RUN: %clang_cc1 %s -E -dM -menable-no-nans -menable-no-infs -o - \
// RUN: | FileCheck -match-full-lines %s --check-prefix=CHECK-FINITE-MATH-ONLY
// CHECK-FINITE-MATH-ONLY: #define __FINITE_MATH_ONLY__ 1
//
Expand Down Expand Up @@ -316,4 +316,4 @@
// RUN: -triple amdgcn-amd-amdhsa -fcuda-is-device | FileCheck -match-full-lines \
// RUN: %s --check-prefix=CHECK-HIPSTDPAR-INTERPOSE-DEV-NEG
// CHECK-HIPSTDPAR-INTERPOSE-DEV-NEG: #define __HIPSTDPAR__ 1
// CHECK-HIPSTDPAR-INTERPOSE-DEV-NEG-NOT: #define __HIPSTDPAR_INTERPOSE_ALLOC__ 1
// CHECK-HIPSTDPAR-INTERPOSE-DEV-NEG-NOT: #define __HIPSTDPAR_INTERPOSE_ALLOC__ 1
46 changes: 46 additions & 0 deletions clang/test/Sema/aarch64-fmv-streaming.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// RUN: %clang_cc1 -triple aarch64-none-linux-gnu -target-feature +sme -Waarch64-sme-attributes -fsyntax-only -verify %s
// RUN: %clang_cc1 -triple aarch64-none-linux-gnu -target-feature +sme -Waarch64-sme-attributes -fsyntax-only -verify=expected-cpp -x c++ %s

__attribute__((target_clones("sve", "simd"))) void ok_arm_streaming(void) __arm_streaming {}
__arm_locally_streaming __attribute__((target_version("sme2"))) void ok_arm_streaming(void) __arm_streaming {}
__attribute__((target_version("default"))) void ok_arm_streaming(void) __arm_streaming {}

__attribute__((target_clones("sve", "simd"))) void ok_arm_streaming_compatible(void) __arm_streaming_compatible {}
__arm_locally_streaming __attribute__((target_version("sme2"))) void ok_arm_streaming_compatible(void) __arm_streaming_compatible {}
__attribute__((target_version("default"))) void ok_arm_streaming_compatible(void) __arm_streaming_compatible {}

__arm_locally_streaming __attribute__((target_clones("sve", "simd"))) void ok_no_streaming(void) {}
__attribute__((target_version("sme2"))) void ok_no_streaming(void) {}
__attribute__((target_version("default"))) void ok_no_streaming(void) {}

__attribute__((target_clones("sve", "simd"))) void bad_mixed_streaming(void) {}
// expected-cpp-error@+2 {{multiversioned function declaration has a different calling convention}}
// expected-error@+1 {{multiversioned function declaration has a different calling convention}}
__attribute__((target_version("sme2"))) void bad_mixed_streaming(void) __arm_streaming {}
// expected-cpp-error@+2 {{multiversioned function declaration has a different calling convention}}
// expected-error@+1 {{multiversioned function declaration has a different calling convention}}
__attribute__((target_version("default"))) void bad_mixed_streaming(void) __arm_streaming_compatible {}
// expected-cpp-error@+2 {{multiversioned function declaration has a different calling convention}}
// expected-error@+1 {{multiversioned function declaration has a different calling convention}}
__arm_locally_streaming __attribute__((target_version("dotprod"))) void bad_mixed_streaming(void) __arm_streaming {}

void n_caller(void) {
ok_arm_streaming();
ok_arm_streaming_compatible();
ok_no_streaming();
bad_mixed_streaming();
}

void s_caller(void) __arm_streaming {
ok_arm_streaming();
ok_arm_streaming_compatible();
ok_no_streaming();
bad_mixed_streaming();
}

void sc_caller(void) __arm_streaming_compatible {
ok_arm_streaming();
ok_arm_streaming_compatible();
ok_no_streaming();
bad_mixed_streaming();
}
42 changes: 0 additions & 42 deletions clang/test/Sema/aarch64-sme-func-attrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,48 +455,6 @@ void unimplemented_spill_fill_za(void (*share_zt0_only)(void) __arm_inout("zt0")
share_zt0_only();
}

// expected-cpp-error@+2 {{streaming function cannot be multi-versioned}}
// expected-error@+1 {{streaming function cannot be multi-versioned}}
__attribute__((target_version("sme2")))
void cannot_work_version(void) __arm_streaming {}
// expected-cpp-error@+5 {{function declared 'void ()' was previously declared 'void () __arm_streaming', which has different SME function attributes}}
// expected-cpp-note@-2 {{previous declaration is here}}
// expected-error@+3 {{function declared 'void (void)' was previously declared 'void (void) __arm_streaming', which has different SME function attributes}}
// expected-note@-4 {{previous declaration is here}}
__attribute__((target_version("default")))
void cannot_work_version(void) {}


// expected-cpp-error@+2 {{streaming function cannot be multi-versioned}}
// expected-error@+1 {{streaming function cannot be multi-versioned}}
__attribute__((target_clones("sme2")))
void cannot_work_clones(void) __arm_streaming {}


__attribute__((target("sme2")))
void just_fine_streaming(void) __arm_streaming {}
__attribute__((target_version("sme2")))
void just_fine(void) { just_fine_streaming(); }
__attribute__((target_version("default")))
void just_fine(void) {}


__arm_locally_streaming
__attribute__((target_version("sme2")))
void incompatible_locally_streaming(void) {}
// expected-error@-1 {{attribute 'target_version' multiversioning cannot be combined with attribute '__arm_locally_streaming'}}
// expected-cpp-error@-2 {{attribute 'target_version' multiversioning cannot be combined with attribute '__arm_locally_streaming'}}
__attribute__((target_version("default")))
void incompatible_locally_streaming(void) {}


void fmv_caller() {
cannot_work_version();
cannot_work_clones();
just_fine();
incompatible_locally_streaming();
}

void sme_streaming_with_vl_arg(__SVInt8_t a) __arm_streaming { }

__SVInt8_t sme_streaming_returns_vl(void) __arm_streaming { __SVInt8_t r; return r; }
Expand Down
9 changes: 5 additions & 4 deletions clang/test/Sema/warn-infinity-nan-disabled-lnx.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// RUN: %clang_cc1 -x c++ -verify=no-inf-no-nan \
// RUN: -triple powerpc64le-unknown-unknown %s -menable-no-infs \
// RUN: -menable-no-nans -std=c++23
// RUN: -triple powerpc64le-unknown-unknown %s \
// RUN: -menable-no-infs -menable-no-nans -std=c++23

// RUN: %clang_cc1 -x c++ -verify=no-inf-no-nan \
// RUN: -triple powerpc64le-unknown-unknown %s -menable-no-infs \
// RUN: -menable-no-nans -funsafe-math-optimizations -std=c++23
// RUN: -triple powerpc64le-unknown-unknown %s \
// RUN: -menable-no-infs -menable-no-nans -funsafe-math-optimizations \
// RUN: -std=c++23

// RUN: %clang_cc1 -x c++ -verify=no-fast -triple powerpc64le-unknown-unknown \
// RUN: %s -std=c++23
Expand Down
5 changes: 3 additions & 2 deletions clang/test/Sema/warn-infinity-nan-disabled-win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
// RUN: -menable-no-nans -std=c++23

// RUN: %clang_cc1 -x c++ -verify=no-inf-no-nan \
// RUN: -triple powerpc64le-unknown-unknown %s -menable-no-infs \
// RUN: -menable-no-nans -funsafe-math-optimizations -std=c++23
// RUN: -triple powerpc64le-unknown-unknown %s \
// RUN: -menable-no-infs -menable-no-nans -funsafe-math-optimizations \
// RUN: -std=c++23

// RUN: %clang_cc1 -x c++ -verify=no-fast -triple powerpc64le-unknown-unknown \
// RUN: %s -std=c++23
Expand Down
17 changes: 17 additions & 0 deletions clang/test/SemaCXX/pr100095.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %clang_cc1 -fsyntax-only -std=c++11 %s
// XFAIL: asserts

template <class> struct Pair;
template <class...> struct Tuple {
template <class _Up> Tuple(_Up);
};
template <typename> struct StatusOr;
template <int> using ElementType = int;
template <int... fields>
using Key = Tuple<ElementType<fields>...>;
template <int... fields>
StatusOr<Pair<Key<fields...>>> Parser();
struct Helper { Helper(Tuple<>, Tuple<>, int, int); };
struct D : Helper {
D(Key<> f, int n, int e) : Helper(f, Parser<>, n, e) {}
};
74 changes: 72 additions & 2 deletions clang/test/SemaCXX/type-traits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2402,11 +2402,11 @@ template<typename T> struct DerivedB : BaseA<T> { };
template<typename T> struct CrazyDerived : T { };


class class_forward; // expected-note 2 {{forward declaration of 'class_forward'}}
class class_forward; // expected-note 4 {{forward declaration of 'class_forward'}}

template <class T> class DerivedTemp : Base {};
template <class T> class NonderivedTemp {};
template <class T> class UndefinedTemp; // expected-note {{declared here}}
template <class T> class UndefinedTemp; // expected-note 2 {{declared here}}

void is_base_of() {
static_assert(__is_base_of(Base, Derived));
Expand Down Expand Up @@ -2457,6 +2457,76 @@ void is_base_of() {
static_assert(!__is_base_of(DerivedB<int>, BaseA<int>));
}

struct DerivedTransitiveViaNonVirtual : Derived3 {};
struct DerivedTransitiveViaVirtual : virtual Derived3 {};

template <typename T>
struct CrazyDerivedVirtual : virtual T {};

struct DerivedPrivate : private virtual Base {};
struct DerivedProtected : protected virtual Base {};
struct DerivedPrivatePrivate : private DerivedPrivate {};
struct DerivedPrivateProtected : private DerivedProtected {};
struct DerivedProtectedPrivate : protected DerivedProtected {};
struct DerivedProtectedProtected : protected DerivedProtected {};

void is_virtual_base_of(int n) {
static_assert(!__builtin_is_virtual_base_of(Base, Derived));
static_assert(!__builtin_is_virtual_base_of(const Base, Derived));
static_assert(!__builtin_is_virtual_base_of(Derived, Base));
static_assert(!__builtin_is_virtual_base_of(Derived, int));
static_assert(!__builtin_is_virtual_base_of(Base, Base));
static_assert(!__builtin_is_virtual_base_of(Base, Derived3));
static_assert(!__builtin_is_virtual_base_of(Derived, Derived3));
static_assert(__builtin_is_virtual_base_of(Derived2b, Derived3));
static_assert(__builtin_is_virtual_base_of(Derived2a, Derived3));
static_assert(!__builtin_is_virtual_base_of(BaseA<int>, DerivedB<int>));
static_assert(!__builtin_is_virtual_base_of(DerivedB<int>, BaseA<int>));
static_assert(!__builtin_is_virtual_base_of(Union, Union));
static_assert(!__builtin_is_virtual_base_of(Empty, Empty));
static_assert(!__builtin_is_virtual_base_of(class_forward, class_forward)); // expected-error {{incomplete type 'class_forward' where a complete type is required}}
static_assert(!__builtin_is_virtual_base_of(Empty, class_forward)); // expected-error {{incomplete type 'class_forward' where a complete type is required}}
static_assert(!__builtin_is_virtual_base_of(class_forward, Empty));
static_assert(!__builtin_is_virtual_base_of(Base&, Derived&));
static_assert(!__builtin_is_virtual_base_of(Base[10], Derived[10]));
static_assert(!__builtin_is_virtual_base_of(Base[n], Derived[n])); // expected-error 2 {{variable length arrays are not supported in '__builtin_is_virtual_base_of'}}
static_assert(!__builtin_is_virtual_base_of(int, int));
static_assert(!__builtin_is_virtual_base_of(int[], int[]));
static_assert(!__builtin_is_virtual_base_of(long, int));
static_assert(!__builtin_is_virtual_base_of(Base, DerivedTemp<int>));
static_assert(!__builtin_is_virtual_base_of(Base, NonderivedTemp<int>));
static_assert(!__builtin_is_virtual_base_of(Base, UndefinedTemp<int>)); // expected-error {{implicit instantiation of undefined template 'UndefinedTemp<int>'}}
static_assert(__builtin_is_virtual_base_of(Base, DerivedPrivate));
static_assert(__builtin_is_virtual_base_of(Base, DerivedProtected));
static_assert(__builtin_is_virtual_base_of(Base, DerivedPrivatePrivate));
static_assert(__builtin_is_virtual_base_of(Base, DerivedPrivateProtected));
static_assert(__builtin_is_virtual_base_of(Base, DerivedProtectedPrivate));
static_assert(__builtin_is_virtual_base_of(Base, DerivedProtectedProtected));
static_assert(__builtin_is_virtual_base_of(Derived2a, DerivedTransitiveViaNonVirtual));
static_assert(__builtin_is_virtual_base_of(Derived2b, DerivedTransitiveViaNonVirtual));
static_assert(__builtin_is_virtual_base_of(Derived2a, DerivedTransitiveViaVirtual));
static_assert(__builtin_is_virtual_base_of(Derived2b, DerivedTransitiveViaVirtual));
static_assert(!__builtin_is_virtual_base_of(Base, CrazyDerived<Base>));
static_assert(!__builtin_is_virtual_base_of(CrazyDerived<Base>, Base));
static_assert(__builtin_is_virtual_base_of(Base, CrazyDerivedVirtual<Base>));
static_assert(!__builtin_is_virtual_base_of(CrazyDerivedVirtual<Base>, Base));

static_assert(!__builtin_is_virtual_base_of(IncompleteUnion, IncompleteUnion));
static_assert(!__builtin_is_virtual_base_of(Union, IncompleteUnion));
static_assert(!__builtin_is_virtual_base_of(IncompleteUnion, Union));
static_assert(!__builtin_is_virtual_base_of(IncompleteStruct, IncompleteUnion));
static_assert(!__builtin_is_virtual_base_of(IncompleteUnion, IncompleteStruct));
static_assert(!__builtin_is_virtual_base_of(Empty, IncompleteUnion));
static_assert(!__builtin_is_virtual_base_of(IncompleteUnion, Empty));
static_assert(!__builtin_is_virtual_base_of(int, IncompleteUnion));
static_assert(!__builtin_is_virtual_base_of(IncompleteUnion, int));
static_assert(!__builtin_is_virtual_base_of(Empty, Union));
static_assert(!__builtin_is_virtual_base_of(Union, Empty));
static_assert(!__builtin_is_virtual_base_of(int, Empty));
static_assert(!__builtin_is_virtual_base_of(Union, int));
static_assert(!__builtin_is_virtual_base_of(IncompleteStruct, IncompleteStruct[n])); // expected-error {{variable length arrays are not supported in '__builtin_is_virtual_base_of'}}
}

template<class T, class U>
class TemplateClass {};

Expand Down
7 changes: 4 additions & 3 deletions clang/tools/clang-nvlink-wrapper/ClangNVLinkWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,10 +598,11 @@ Expected<SmallVector<StringRef>> getInput(const ArgList &Args) {
Res.Prevailing = !Sym.isUndefined() && ObjSym.File == *BitcodeFile;

// We need LTO to preseve the following global symbols:
// 1) Symbols used in regular objects.
// 2) Prevailing symbols that are needed visible to the gpu runtime.
// 1) All symbols during a relocatable link.
// 2) Symbols used in regular objects.
// 3) Prevailing symbols that are needed visible to the gpu runtime.
Res.VisibleToRegularObj =
ObjSym.UsedInRegularObj ||
Args.hasArg(OPT_relocatable) || ObjSym.UsedInRegularObj ||
(Res.Prevailing &&
(Sym.getVisibility() != GlobalValue::HiddenVisibility &&
!Sym.canBeOmittedFromSymbolTable()));
Expand Down
7 changes: 5 additions & 2 deletions clang/tools/clang-nvlink-wrapper/NVLinkOpts.td
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ def plugin_opt : Joined<["--", "-"], "plugin-opt=">, Flags<[WrapperOnlyOption]>,
def save_temps : Flag<["--", "-"], "save-temps">,
Flags<[WrapperOnlyOption]>, HelpText<"Save intermediate results">;

def relocatable : Flag<["--", "-"], "relocatable">,
Flags<[WrapperOnlyOption]>, HelpText<"Perform a relocatable link (LTO only)">;
def r : Flag<["-"], "r">, Flags<[WrapperOnlyOption]>, Alias<relocatable>;

def whole_archive : Flag<["--", "-"], "whole-archive">,
Flags<[WrapperOnlyOption, HelpHidden]>;
def no_whole_archive : Flag<["--", "-"], "no-whole-archive">,
Expand All @@ -83,8 +87,7 @@ def mllvm : Separate<["-"], "mllvm">, Flags<[WrapperOnlyOption]>,
HelpText<"Arguments passed to LLVM, including Clang invocations, for which "
"the '-mllvm' prefix is preserved. Use '-mllvm --help' for a list "
"of options.">;
def mllvm_EQ : Joined<["-"], "mllvm=">, Flags<[HelpHidden]>,
Alias<mllvm>;
def mllvm_EQ : Joined<["-"], "mllvm=">, Flags<[HelpHidden]>, Alias<mllvm>;

def dry_run : Flag<["--", "-"], "dry-run">, Flags<[WrapperOnlyOption]>,
HelpText<"Print generated commands without running.">;
49 changes: 49 additions & 0 deletions clang/unittests/Analysis/FlowSensitive/DataflowEnvironmentTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,55 @@ TEST_F(EnvironmentTest, CXXDefaultInitExprResultObjIsWrappedExprResultObj) {
&Env.getResultObjectLocation(*DefaultInit->getExpr()));
}

// This test verifies the behavior of `getResultObjectLocation()` in
// scenarios involving inherited constructors.
// Since the specific AST node of interest `CXXConstructorDecl` is implicitly
// generated, we cannot annotate any statements inside of it as we do in tests
// within TransferTest. Thus, the only way to get the right `Environment` is by
// explicitly initializing it as we do in tests within EnvironmentTest.
// This is why this test is not inside TransferTest, where most of the tests for
// `getResultObjectLocation()` are located.
TEST_F(EnvironmentTest, ResultObjectLocationForInheritedCtorInitExpr) {
using namespace ast_matchers;

std::string Code = R"(
struct Base {
Base(int b) {}
};
struct Derived : Base {
using Base::Base;
};
Derived d = Derived(0);
)";

auto Unit =
tooling::buildASTFromCodeWithArgs(Code, {"-fsyntax-only", "-std=c++20"});
auto &Context = Unit->getASTContext();

ASSERT_EQ(Context.getDiagnostics().getClient()->getNumErrors(), 0U);

auto Results =
match(cxxConstructorDecl(
hasAnyConstructorInitializer(cxxCtorInitializer(
withInitializer(expr().bind("inherited_ctor_init_expr")))))
.bind("ctor"),
Context);
const auto *Constructor = selectFirst<CXXConstructorDecl>("ctor", Results);
const auto *InheritedCtorInit = selectFirst<CXXInheritedCtorInitExpr>(
"inherited_ctor_init_expr", Results);

EXPECT_EQ(InheritedCtorInit->child_begin(), InheritedCtorInit->child_end());

Environment Env(DAContext, *Constructor);
Env.initialize();

RecordStorageLocation &Loc = Env.getResultObjectLocation(*InheritedCtorInit);
EXPECT_NE(&Loc, nullptr);

EXPECT_EQ(&Loc, Env.getThisPointeeStorageLocation());
}

TEST_F(EnvironmentTest, Stmt) {
using namespace ast_matchers;

Expand Down
18 changes: 0 additions & 18 deletions compiler-rt/test/builtins/Unit/ppc/test

This file was deleted.

63 changes: 0 additions & 63 deletions compiler-rt/test/builtins/Unit/test

This file was deleted.

49 changes: 23 additions & 26 deletions flang/include/flang/Evaluate/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -1243,22 +1243,30 @@ bool CheckForCoindexedObject(parser::ContextualMessages &,
const std::optional<ActualArgument> &, const std::string &procName,
const std::string &argName);

// Get the number of distinct symbols with CUDA attribute in the expression.
inline bool IsCUDADeviceSymbol(const Symbol &sym) {
if (const auto *details =
sym.GetUltimate().detailsIf<semantics::ObjectEntityDetails>()) {
if (details->cudaDataAttr() &&
*details->cudaDataAttr() != common::CUDADataAttr::Pinned) {
return true;
}
}
return false;
}

// Get the number of distinct symbols with CUDA device
// attribute in the expression.
template <typename A> inline int GetNbOfCUDADeviceSymbols(const A &expr) {
semantics::UnorderedSymbolSet symbols;
for (const Symbol &sym : CollectCudaSymbols(expr)) {
if (const auto *details =
sym.GetUltimate().detailsIf<semantics::ObjectEntityDetails>()) {
if (details->cudaDataAttr() &&
*details->cudaDataAttr() != common::CUDADataAttr::Pinned) {
symbols.insert(sym);
}
if (IsCUDADeviceSymbol(sym)) {
symbols.insert(sym);
}
}
return symbols.size();
}

// Check if any of the symbols part of the expression has a CUDA data
// Check if any of the symbols part of the expression has a CUDA device
// attribute.
template <typename A> inline bool HasCUDADeviceAttrs(const A &expr) {
return GetNbOfCUDADeviceSymbols(expr) > 0;
Expand All @@ -1270,26 +1278,15 @@ inline bool HasCUDAImplicitTransfer(const Expr<SomeType> &expr) {
unsigned hostSymbols{0};
unsigned deviceSymbols{0};
for (const Symbol &sym : CollectCudaSymbols(expr)) {
if (const auto *details =
sym.GetUltimate().detailsIf<semantics::ObjectEntityDetails>()) {
if (details->cudaDataAttr() &&
*details->cudaDataAttr() != common::CUDADataAttr::Pinned) {
++deviceSymbols;
} else {
if (sym.owner().IsDerivedType()) {
if (const auto *details =
sym.owner()
.GetSymbol()
->GetUltimate()
.detailsIf<semantics::ObjectEntityDetails>()) {
if (details->cudaDataAttr() &&
*details->cudaDataAttr() != common::CUDADataAttr::Pinned) {
++deviceSymbols;
}
}
if (IsCUDADeviceSymbol(sym)) {
++deviceSymbols;
} else {
if (sym.owner().IsDerivedType()) {
if (IsCUDADeviceSymbol(sym.owner().GetSymbol()->GetUltimate())) {
++deviceSymbols;
}
++hostSymbols;
}
++hostSymbols;
}
}
return hostSymbols > 0 && deviceSymbols > 0;
Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,7 @@ static bool parseFloatingPointArgs(CompilerInvocation &invoc,
opts.setFPContractMode(fpContractMode);
}

if (args.getLastArg(clang::driver::options::OPT_menable_no_infinities)) {
if (args.getLastArg(clang::driver::options::OPT_menable_no_infs)) {
opts.NoHonorInfs = true;
}

Expand Down
5 changes: 5 additions & 0 deletions flang/lib/Optimizer/Builder/FIRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ mlir::Value fir::FirOpBuilder::allocateLocal(

/// Get the block for adding Allocas.
mlir::Block *fir::FirOpBuilder::getAllocaBlock() {
if (auto accComputeRegionIface =
getRegion().getParentOfType<mlir::acc::ComputeRegionOpInterface>()) {
return accComputeRegionIface.getAllocaBlock();
}

if (auto ompOutlineableIface =
getRegion()
.getParentOfType<mlir::omp::OutlineableOpenMPOpInterface>()) {
Expand Down
32 changes: 32 additions & 0 deletions flang/lib/Semantics/check-cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,25 @@ template <bool IsCUFKernelDo> class DeviceContextChecker {
WarnOnIoStmt(source);
}
}
template <typename A>
void ErrorIfHostSymbol(const A &expr, const parser::CharBlock &source) {
for (const Symbol &sym : CollectCudaSymbols(expr)) {
if (const auto *details =
sym.GetUltimate().detailsIf<semantics::ObjectEntityDetails>()) {
if (details->IsArray() &&
(!details->cudaDataAttr() ||
(details->cudaDataAttr() &&
*details->cudaDataAttr() != common::CUDADataAttr::Device &&
*details->cudaDataAttr() != common::CUDADataAttr::Managed &&
*details->cudaDataAttr() !=
common::CUDADataAttr::Unified))) {
context_.Say(source,
"Host array '%s' cannot be present in CUF kernel"_err_en_US,
sym.name());
}
}
}
}
void Check(const parser::ActionStmt &stmt, const parser::CharBlock &source) {
common::visit(
common::visitors{
Expand Down Expand Up @@ -349,6 +368,19 @@ template <bool IsCUFKernelDo> class DeviceContextChecker {
[&](const common::Indirection<parser::IfStmt> &x) {
Check(x.value());
},
[&](const common::Indirection<parser::AssignmentStmt> &x) {
if (IsCUFKernelDo) {
const evaluate::Assignment *assign{
semantics::GetAssignment(x.value())};
if (assign) {
ErrorIfHostSymbol(assign->lhs, source);
ErrorIfHostSymbol(assign->rhs, source);
}
}
if (auto msg{ActionStmtChecker<IsCUFKernelDo>::WhyNotOk(x)}) {
context_.Say(source, std::move(*msg));
}
},
[&](const auto &x) {
if (auto msg{ActionStmtChecker<IsCUFKernelDo>::WhyNotOk(x)}) {
context_.Say(source, std::move(*msg));
Expand Down
5 changes: 3 additions & 2 deletions flang/runtime/transformational.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,8 @@ void RTDEF(CshiftVector)(Descriptor &result, const Descriptor &source,
SubscriptValue lb{sourceDim.LowerBound()};
for (SubscriptValue j{0}; j < extent; ++j) {
SubscriptValue resultAt{1 + j};
SubscriptValue sourceAt{lb + (j + shift) % extent};
SubscriptValue sourceAt{
lb + static_cast<SubscriptValue>(j + shift) % extent};
if (sourceAt < lb) {
sourceAt += extent;
}
Expand Down Expand Up @@ -619,7 +620,7 @@ void RTDEF(EoshiftVector)(Descriptor &result, const Descriptor &source,
}
SubscriptValue lb{source.GetDimension(0).LowerBound()};
for (SubscriptValue j{1}; j <= extent; ++j) {
SubscriptValue sourceAt{lb + j - 1 + shift};
SubscriptValue sourceAt{lb + j - 1 + static_cast<SubscriptValue>(shift)};
if (sourceAt >= lb && sourceAt < lb + extent) {
CopyElement(result, &j, source, &sourceAt, terminator);
} else if (boundary) {
Expand Down
2 changes: 1 addition & 1 deletion flang/test/Lower/CUDA/cuda-data-transfer.cuf
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ subroutine sub4()
integer, parameter :: n = 10
real, device :: adev(n)
real :: ahost(n)
real :: b
real, managed :: b
integer :: i

adev = ahost
Expand Down
4 changes: 2 additions & 2 deletions flang/test/Lower/CUDA/cuda-kernel-loop-directive.cuf
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ subroutine sub1()
integer :: i, j
integer, parameter :: n = 100
integer(8) :: istream
real :: a(n), b(n)
real :: c(n,n), d(n,n)
real, device :: a(n), b(n)
real, device :: c(n,n), d(n,n)

! CHECK-LABEL: func.func @_QPsub1()
! CHECK: %[[IV:.*]]:2 = hlfir.declare %{{.*}} {uniq_name = "_QFsub1Ei"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
Expand Down
2 changes: 1 addition & 1 deletion flang/test/Lower/OpenACC/acc-loop.f90
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,10 @@ subroutine sub1(i, j, k)
end subroutine

! CHECK: func.func @_QPsub1
! CHECK: acc.parallel
! CHECK: %[[DC_K:.*]] = fir.alloca i32 {bindc_name = "k"}
! CHECK: %[[DC_J:.*]] = fir.alloca i32 {bindc_name = "j"}
! CHECK: %[[DC_I:.*]] = fir.alloca i32 {bindc_name = "i"}
! CHECK: acc.parallel
! CHECK: %[[P_I:.*]] = acc.private varPtr(%[[DC_I]] : !fir.ref<i32>) -> !fir.ref<i32> {implicit = true, name = ""}
! CHECK: %[[P_J:.*]] = acc.private varPtr(%[[DC_J]] : !fir.ref<i32>) -> !fir.ref<i32> {implicit = true, name = ""}
! CHECK: %[[P_K:.*]] = acc.private varPtr(%[[DC_K]] : !fir.ref<i32>) -> !fir.ref<i32> {implicit = true, name = ""}
Expand Down
3 changes: 2 additions & 1 deletion flang/test/Parser/cuf-sanity-common
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ module m
end subroutine
subroutine test
logical isPinned
real a(10), x, y, z
real, device :: a(10)
real :: x, y, z
!$cuf kernel do(1) <<<*, *, stream = 1>>>
do j = 1, 10
end do
Expand Down
10 changes: 10 additions & 0 deletions flang/test/Semantics/cuf09.cuf
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ module m
end

program main
integer, device :: a_d(10 ,10)
integer :: b(10, 10)
!$cuf kernel do <<< *, * >>> ! ok
do j = 1, 0
end do
Expand Down Expand Up @@ -90,4 +92,12 @@ program main
else if (ifunc() /= 1) then
end if
end do

!$cuf kernel do (2) <<<*, *>>>
do j = 1, 10
do i = 1, 10
!ERROR: Host array 'b' cannot be present in CUF kernel
a_d(i,j) = b(i,j)
enddo
enddo
end
6 changes: 3 additions & 3 deletions flang/test/Semantics/reduce.cuf
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
! RUN: %python %S/test_errors.py %s %flang_fc1
subroutine s(n,m,a,l)
integer, intent(in) :: n
integer, intent(in) :: m(n)
real, intent(in) :: a(n)
logical, intent(in) :: l(n)
integer, device, intent(in) :: m(n)
real, device, intent(in) :: a(n)
logical, device, intent(in) :: l(n)
integer j, mr
real ar
logical lr
Expand Down
2 changes: 2 additions & 0 deletions libc/benchmarks/gpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ function(add_benchmark benchmark_name)
libc.src.stdio.printf
${BENCHMARK_DEPENDS}
${BENCHMARK_UNPARSED_ARGUMENTS}
COMPILE_OPTIONS
-flto
)
get_fq_target_name(${benchmark_name} fq_target_name)
set(fq_build_target_name ${fq_target_name}.__build__)
Expand Down
1 change: 1 addition & 0 deletions libc/benchmarks/gpu/LibcGpuBenchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ BenchmarkResult benchmark(const BenchmarkOptions &options,
if (samples >= options.max_samples || iterations >= options.max_iterations)
break;
if (total_time >= options.min_duration && samples >= options.min_samples &&
total_iterations >= options.min_iterations &&
change_ratio < options.epsilon)
break;

Expand Down
5 changes: 3 additions & 2 deletions libc/benchmarks/gpu/LibcGpuBenchmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ namespace benchmarks {

struct BenchmarkOptions {
uint32_t initial_iterations = 1;
uint32_t min_iterations = 50;
uint32_t max_iterations = 10000000;
uint32_t min_samples = 4;
uint32_t max_samples = 1000;
int64_t min_duration = 0; // in nanoseconds (ns)
int64_t min_duration = 500 * 1000; // 500 * 1000 nanoseconds = 500 us
int64_t max_duration = 1000 * 1000 * 1000; // 1e9 nanoseconds = 1 second
double epsilon = 0.01;
double epsilon = 0.0001;
double scaling_factor = 1.4;
};

Expand Down
16 changes: 3 additions & 13 deletions libc/cmake/modules/LLVMLibCLibraryRules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,9 @@ function(add_bitcode_entrypoint_library target_name base_target_name)
list(APPEND objects ${object})
endforeach()

set(output ${CMAKE_CURRENT_BINARY_DIR}/${target_name}.bc)
add_custom_command(
OUTPUT ${output}
COMMAND ${LIBC_LLVM_LINK} ${objects} -o ${output}
DEPENDS ${all_deps} ${base_target_name}
COMMENT "Linking LLVM-IR bitcode for ${base_target_name}"
COMMAND_EXPAND_LISTS
)
add_custom_target(${target_name} DEPENDS ${output} ${all_deps})
set_target_properties(${target_name} PROPERTIES TARGET_OBJECT ${output})
if(TARGET llvm-link)
add_dependencies(${target_name} llvm-link)
endif()
add_executable(${target_name} ${objects})
target_link_options(${target_name} PRIVATE
"-r" "-nostdlib" "-flto" "-Wl,--lto-emit-llvm" "-march= ")
endfunction(add_bitcode_entrypoint_library)

# A rule to build a library from a collection of entrypoint objects.
Expand Down
4 changes: 4 additions & 0 deletions libc/cmake/modules/LLVMLibCTestRules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,8 @@ function(add_integration_test test_name)
target_link_options(${fq_build_target_name} PRIVATE
${LIBC_COMPILE_OPTIONS_DEFAULT} -Wno-multi-gpu
"-Wl,--suppress-stack-size-warning"
"-Wl,-mllvm,-nvptx-lower-global-ctor-dtor=1"
"-Wl,-mllvm,-nvptx-emit-init-fini-kernel"
-march=${LIBC_GPU_TARGET_ARCHITECTURE} -nostdlib -static
"--cuda-path=${LIBC_CUDA_ROOT}")
elseif(LIBC_CC_SUPPORTS_NOSTDLIBPP)
Expand Down Expand Up @@ -657,6 +659,8 @@ function(add_libc_hermetic test_name)
target_link_options(${fq_build_target_name} PRIVATE
${LIBC_COMPILE_OPTIONS_DEFAULT} -Wno-multi-gpu
"-Wl,--suppress-stack-size-warning"
"-Wl,-mllvm,-nvptx-lower-global-ctor-dtor=1"
"-Wl,-mllvm,-nvptx-emit-init-fini-kernel"
-march=${LIBC_GPU_TARGET_ARCHITECTURE} -nostdlib -static
"--cuda-path=${LIBC_CUDA_ROOT}")
elseif(LIBC_CC_SUPPORTS_NOSTDLIBPP)
Expand Down
30 changes: 2 additions & 28 deletions libc/cmake/modules/prepare_libc_gpu_build.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,10 @@ if(LIBC_TARGET_TRIPLE)
set(CMAKE_REQUIRED_FLAGS "--target=${LIBC_TARGET_TRIPLE}")
endif()
if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nogpulib")
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nogpulib -nostdlib")
elseif(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
set(CMAKE_REQUIRED_FLAGS
"${CMAKE_REQUIRED_FLAGS} -flto -c -Wno-unused-command-line-argument")
endif()

# Identify the program used to package multiple images into a single binary.
get_filename_component(compiler_path ${CMAKE_CXX_COMPILER} DIRECTORY)
if(TARGET clang-offload-packager)
get_target_property(LIBC_CLANG_OFFLOAD_PACKAGER clang-offload-packager LOCATION)
else()
find_program(LIBC_CLANG_OFFLOAD_PACKAGER
NAMES clang-offload-packager NO_DEFAULT_PATH
PATHS ${LLVM_BINARY_DIR}/bin ${compiler_path})
if(NOT LIBC_CLANG_OFFLOAD_PACKAGER)
message(FATAL_ERROR "Cannot find the 'clang-offload-packager' for the GPU "
"build")
endif()
endif()

# Identify llvm-link program so we can merge the output IR into a single blob.
if(TARGET llvm-link)
get_target_property(LIBC_LLVM_LINK llvm-link LOCATION)
else()
find_program(LIBC_LLVM_LINK
NAMES llvm-link NO_DEFAULT_PATH
PATHS ${LLVM_BINARY_DIR}/bin ${compiler_path})
if(NOT LIBC_LLVM_LINK)
message(FATAL_ERROR "Cannot find 'llvm-link' for the GPU build")
endif()
"${CMAKE_REQUIRED_FLAGS} -flto -c -Wno-unused-command-line-argument -nostdlib")
endif()

# Optionally set up a job pool to limit the number of GPU tests run in parallel.
Expand Down
1 change: 1 addition & 0 deletions libc/config/darwin/arm/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.acoshf
libc.src.math.asinf
libc.src.math.asinhf
libc.src.math.atan2
libc.src.math.atan2f
libc.src.math.atanf
libc.src.math.atanhf
Expand Down
1 change: 0 additions & 1 deletion libc/config/gpu/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ set(TARGET_LIBC_ENTRYPOINTS

# gpu/rpc.h entrypoints
libc.src.gpu.rpc_host_call
libc.src.gpu.rpc_fprintf
)

set(TARGET_LIBM_ENTRYPOINTS
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.acoshf
libc.src.math.asinf
libc.src.math.asinhf
libc.src.math.atan2
libc.src.math.atan2f
libc.src.math.atanf
libc.src.math.atanhf
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/arm/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.acoshf
libc.src.math.asinf
libc.src.math.asinhf
libc.src.math.atan2
libc.src.math.atan2f
libc.src.math.atanf
libc.src.math.atanhf
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 @@ -365,6 +365,7 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.acoshf
libc.src.math.asinf
libc.src.math.asinhf
libc.src.math.atan2
libc.src.math.atan2f
libc.src.math.atanf
libc.src.math.atanhf
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 @@ -365,6 +365,7 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.acoshf
libc.src.math.asinf
libc.src.math.asinhf
libc.src.math.atan2
libc.src.math.atan2f
libc.src.math.atanf
libc.src.math.atanhf
Expand Down
1 change: 1 addition & 0 deletions libc/config/windows/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.acoshf
libc.src.math.asinf
libc.src.math.asinhf
libc.src.math.atan2
libc.src.math.atan2f
libc.src.math.atanf
libc.src.math.atanhf
Expand Down
2 changes: 1 addition & 1 deletion libc/docs/dev/header_generation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Instructions
------------

Required Versions:
- Python Version: 3.6
- Python Version: 3.8
- PyYAML Version: 5.1

1. Keep full-build mode on when building, otherwise headers will not be
Expand Down
2 changes: 1 addition & 1 deletion libc/docs/math/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ Higher Math Functions
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| atan | |check| | | | | | 7.12.4.3 | F.10.1.3 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| atan2 | |check| | | | | | 7.12.4.4 | F.10.1.4 |
| atan2 | |check| | 1 ULP | | | | 7.12.4.4 | F.10.1.4 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| atan2pi | | | | | | 7.12.4.11 | F.10.1.11 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
Expand Down
9 changes: 9 additions & 0 deletions libc/hdr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ add_proxy_header_library(
libc.include.math
)

add_proxy_header_library(
math_function_macros
HDRS
math_function_macros.h
FULL_BUILD_DEPENDS
libc.include.llvm-libc-macros.math_function_macros
libc.include.math
)

add_proxy_header_library(
errno_macros
HDRS
Expand Down
27 changes: 27 additions & 0 deletions libc/hdr/math_function_macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===-- Definition of macros from math.h ----------------------------------===//
//
// 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_HDR_MATH_FUNCTION_MACROS_H
#define LLVM_LIBC_HDR_MATH_FUNCTION_MACROS_H

#ifdef LIBC_FULL_BUILD

#include "include/llvm-libc-macros/math-function-macros.h"

#else // Overlay mode

// GCC will include CXX headers when __cplusplus is defined. This behavior
// can be suppressed by defining _GLIBCXX_INCLUDE_NEXT_C_HEADERS.
#if defined(__GNUC__) && !defined(__clang__)
#define _GLIBCXX_INCLUDE_NEXT_C_HEADERS
#endif
#include <math.h>

#endif // LLVM_LIBC_FULL_BUILD

#endif // LLVM_LIBC_HDR_MATH_MACROS_H
1 change: 0 additions & 1 deletion libc/hdr/math_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#ifdef LIBC_FULL_BUILD

#include "include/llvm-libc-macros/math-function-macros.h"
#include "include/llvm-libc-macros/math-macros.h"

#else // Overlay mode
Expand Down
27 changes: 9 additions & 18 deletions libc/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ foreach(archive IN ZIP_LISTS
PROPERTIES
OUTPUT_NAME ${archive_1}.bc
)
list(APPEND added_gpu_bitcode_targets ${archive_1}bitcode)
list(APPEND added_bitcode_targets ${archive_1}bitcode)
endif()
endforeach()

Expand All @@ -61,24 +61,13 @@ install(
COMPONENT libc
)

if(LIBC_TARGET_OS_IS_GPU)
set(gpu_install_dir lib${LLVM_LIBDIR_SUFFIX})
if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR)
set(gpu_install_dir lib${LLVM_LIBDIR_SUFFIX}/${LLVM_HOST_TRIPLE})
endif()
install(
TARGETS ${added_gpu_archive_targets}
ARCHIVE DESTINATION ${gpu_install_dir}
COMPONENT libc
foreach(file ${added_bitcode_targets})
install(FILES $<TARGET_FILE:${file}>
DESTINATION ${LIBC_INSTALL_LIBRARY_DIR}
RENAME $<TARGET_PROPERTY:${file},OUTPUT_NAME>
COMPONENT libc
)
foreach(file ${added_gpu_bitcode_targets})
install(FILES $<TARGET_PROPERTY:${file},TARGET_OBJECT>
DESTINATION ${LIBC_INSTALL_LIBRARY_DIR}
RENAME $<TARGET_PROPERTY:${file},OUTPUT_NAME>
COMPONENT libc
)
endforeach()
endif()
endforeach()

if(NOT LIBC_TARGET_OS_IS_BAREMETAL)
# For now we will disable libc-startup installation for baremetal. The
Expand All @@ -93,13 +82,15 @@ endif()

add_custom_target(install-libc
DEPENDS ${added_archive_targets}
${added_bitcode_targets}
${startup_target}
${header_install_target}
COMMAND "${CMAKE_COMMAND}"
-DCMAKE_INSTALL_COMPONENT=libc
-P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
add_custom_target(install-libc-stripped
DEPENDS ${added_archive_targets}
${added_bitcode_targets}
${startup_target}
${header_install_target}
COMMAND "${CMAKE_COMMAND}"
Expand Down
8 changes: 0 additions & 8 deletions libc/spec/gpu_ext.td
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@ def GPUExtensions : StandardSpec<"GPUExtensions"> {
RetValSpec<VoidType>,
[ArgSpec<VoidPtr>, ArgSpec<VoidPtr>, ArgSpec<SizeTType>]
>,
FunctionSpec<
"rpc_fprintf",
RetValSpec<IntType>,
[ArgSpec<FILERestrictedPtr>,
ArgSpec<ConstCharRestrictedPtr>,
ArgSpec<VoidPtr>,
ArgSpec<SizeTType>]
>,
]
>;
let Headers = [
Expand Down
1 change: 1 addition & 0 deletions libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,7 @@ def StdC : StandardSpec<"stdc"> {

FunctionSpec<"atanf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,

FunctionSpec<"atan2", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
FunctionSpec<"atan2f", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,

FunctionSpec<"acoshf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/threads/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ endif()

set(tid_dep)
if (LLVM_LIBC_FULL_BUILD)
list(APPEND tid_dep libc.src.__support.thread)
list(APPEND tid_dep libc.src.__support.threads.thread)
else()
list(APPEND tid_dep libc.src.__support.OSUtil.osutil)
list(APPEND tid_dep libc.include.sys_syscall)
Expand Down
12 changes: 0 additions & 12 deletions libc/src/gpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,3 @@ add_entrypoint_object(
libc.src.__support.RPC.rpc_client
libc.src.__support.GPU.utils
)

add_entrypoint_object(
rpc_fprintf
SRCS
rpc_fprintf.cpp
HDRS
rpc_fprintf.h
DEPENDS
libc.src.stdio.gpu.gpu_file
libc.src.__support.RPC.rpc_client
libc.src.__support.GPU.utils
)
75 changes: 0 additions & 75 deletions libc/src/gpu/rpc_fprintf.cpp

This file was deleted.

23 changes: 0 additions & 23 deletions libc/src/gpu/rpc_fprintf.h

This file was deleted.

20 changes: 20 additions & 0 deletions libc/src/math/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3848,6 +3848,26 @@ add_entrypoint_object(
libc.src.__support.macros.optimization
)

add_entrypoint_object(
atan2
SRCS
atan2.cpp
HDRS
../atan2.h
COMPILE_OPTIONS
-O3
DEPENDS
.inv_trigf_utils
libc.src.__support.FPUtil.double_double
libc.src.__support.FPUtil.dyadic_float
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.multiply_add
libc.src.__support.FPUtil.nearest_integer
libc.src.__support.FPUtil.polyeval
libc.src.__support.FPUtil.rounding_mode
libc.src.__support.macros.optimization
)

add_entrypoint_object(
scalblnf16
SRCS
Expand Down
313 changes: 313 additions & 0 deletions libc/src/math/generic/atan2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,313 @@
//===-- Double-precision atan2 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/atan2.h"
#include "inv_trigf_utils.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/PolyEval.h"
#include "src/__support/FPUtil/double_double.h"
#include "src/__support/FPUtil/multiply_add.h"
#include "src/__support/FPUtil/nearest_integer.h"
#include "src/__support/FPUtil/rounding_mode.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY

namespace LIBC_NAMESPACE_DECL {

namespace {

using DoubleDouble = fputil::DoubleDouble;

// atan(i/64) with i = 0..64, generated by Sollya with:
// > for i from 0 to 64 do {
// a = round(atan(i/64), D, RN);
// b = round(atan(i/64) - a, D, RN);
// print("{", b, ",", a, "},");
// };
constexpr fputil::DoubleDouble ATAN_I[65] = {
{0.0, 0.0},
{-0x1.220c39d4dff5p-61, 0x1.fff555bbb729bp-7},
{-0x1.5ec431444912cp-60, 0x1.ffd55bba97625p-6},
{-0x1.86ef8f794f105p-63, 0x1.7fb818430da2ap-5},
{-0x1.c934d86d23f1dp-60, 0x1.ff55bb72cfdeap-5},
{0x1.ac4ce285df847p-58, 0x1.3f59f0e7c559dp-4},
{-0x1.cfb654c0c3d98p-58, 0x1.7ee182602f10fp-4},
{0x1.f7b8f29a05987p-58, 0x1.be39ebe6f07c3p-4},
{-0x1.cd37686760c17p-59, 0x1.fd5ba9aac2f6ep-4},
{-0x1.b485914dacf8cp-59, 0x1.1e1fafb043727p-3},
{0x1.61a3b0ce9281bp-57, 0x1.3d6eee8c6626cp-3},
{-0x1.054ab2c010f3dp-58, 0x1.5c9811e3ec26ap-3},
{0x1.347b0b4f881cap-58, 0x1.7b97b4bce5b02p-3},
{0x1.cf601e7b4348ep-59, 0x1.9a6a8e96c8626p-3},
{0x1.17b10d2e0e5abp-61, 0x1.b90d7529260a2p-3},
{0x1.c648d1534597ep-57, 0x1.d77d5df205736p-3},
{0x1.8ab6e3cf7afbdp-57, 0x1.f5b75f92c80ddp-3},
{0x1.62e47390cb865p-56, 0x1.09dc597d86362p-2},
{0x1.30ca4748b1bf9p-57, 0x1.18bf5a30bf178p-2},
{-0x1.077cdd36dfc81p-56, 0x1.278372057ef46p-2},
{-0x1.963a544b672d8p-57, 0x1.362773707ebccp-2},
{-0x1.5d5e43c55b3bap-56, 0x1.44aa436c2af0ap-2},
{-0x1.2566480884082p-57, 0x1.530ad9951cd4ap-2},
{-0x1.a725715711fp-56, 0x1.614840309cfe2p-2},
{-0x1.c63aae6f6e918p-56, 0x1.6f61941e4def1p-2},
{0x1.69c885c2b249ap-56, 0x1.7d5604b63b3f7p-2},
{0x1.b6d0ba3748fa8p-56, 0x1.8b24d394a1b25p-2},
{0x1.9e6c988fd0a77p-56, 0x1.98cd5454d6b18p-2},
{-0x1.24dec1b50b7ffp-56, 0x1.a64eec3cc23fdp-2},
{0x1.ae187b1ca504p-56, 0x1.b3a911da65c6cp-2},
{-0x1.cc1ce70934c34p-56, 0x1.c0db4c94ec9fp-2},
{-0x1.a2cfa4418f1adp-56, 0x1.cde53432c1351p-2},
{0x1.a2b7f222f65e2p-56, 0x1.dac670561bb4fp-2},
{0x1.0e53dc1bf3435p-56, 0x1.e77eb7f175a34p-2},
{-0x1.a3992dc382a23p-57, 0x1.f40dd0b541418p-2},
{-0x1.b32c949c9d593p-55, 0x1.0039c73c1a40cp-1},
{-0x1.d5b495f6349e6p-56, 0x1.0657e94db30dp-1},
{0x1.974fa13b5404fp-58, 0x1.0c6145b5b43dap-1},
{-0x1.2bdaee1c0ee35p-58, 0x1.1255d9bfbd2a9p-1},
{0x1.c621cec00c301p-55, 0x1.1835a88be7c13p-1},
{-0x1.928df287a668fp-58, 0x1.1e00babdefeb4p-1},
{0x1.c421c9f38224ep-57, 0x1.23b71e2cc9e6ap-1},
{-0x1.09e73b0c6c087p-56, 0x1.2958e59308e31p-1},
{0x1.c5d5e9ff0cf8dp-55, 0x1.2ee628406cbcap-1},
{0x1.1021137c71102p-55, 0x1.345f01cce37bbp-1},
{-0x1.2304331d8bf46p-55, 0x1.39c391cd4171ap-1},
{0x1.ecf8b492644fp-56, 0x1.3f13fb89e96f4p-1},
{-0x1.f76d0163f79c8p-56, 0x1.445065b795b56p-1},
{0x1.2419a87f2a458p-56, 0x1.4978fa3269ee1p-1},
{0x1.4a33dbeb3796cp-55, 0x1.4e8de5bb6ec04p-1},
{-0x1.1bb74abda520cp-55, 0x1.538f57b89061fp-1},
{-0x1.5e5c9d8c5a95p-56, 0x1.587d81f732fbbp-1},
{0x1.0028e4bc5e7cap-57, 0x1.5d58987169b18p-1},
{-0x1.2b785350ee8c1p-57, 0x1.6220d115d7b8ep-1},
{-0x1.6ea6febe8bbbap-56, 0x1.66d663923e087p-1},
{-0x1.a80386188c50ep-55, 0x1.6b798920b3d99p-1},
{-0x1.8c34d25aadef6p-56, 0x1.700a7c5784634p-1},
{0x1.7b2a6165884a1p-59, 0x1.748978fba8e0fp-1},
{0x1.406a08980374p-55, 0x1.78f6bbd5d315ep-1},
{0x1.560821e2f3aa9p-55, 0x1.7d528289fa093p-1},
{-0x1.bf76229d3b917p-56, 0x1.819d0b7158a4dp-1},
{0x1.6b66e7fc8b8c3p-57, 0x1.85d69576cc2c5p-1},
{-0x1.55b9a5e177a1bp-55, 0x1.89ff5ff57f1f8p-1},
{-0x1.ec182ab042f61p-56, 0x1.8e17aa99cc05ep-1},
{0x1.1a62633145c07p-55, 0x1.921fb54442d18p-1},
};

// Approximate atan(x) for |x| <= 2^-7.
// Using degree-9 Taylor polynomial:
// P = x - x^3/3 + x^5/5 -x^7/7 + x^9/9;
// Then the absolute error is bounded by:
// |atan(x) - P(x)| < |x|^11/11 < 2^(-7*11) / 11 < 2^-80.
// And the relative error is bounded by:
// |(atan(x) - P(x))/atan(x)| < |x|^10 / 10 < 2^-73.
// For x = x_hi + x_lo, fully expand the polynomial and drop any terms less than
// ulp(x_hi^3 / 3) gives us:
// P(x) ~ x_hi - x_hi^3/3 + x_hi^5/5 - x_hi^7/7 + x_hi^9/9 +
// + x_lo * (1 - x_hi^2 + x_hi^4)
DoubleDouble atan_eval(const DoubleDouble &x) {
DoubleDouble p;
p.hi = x.hi;
double x_hi_sq = x.hi * x.hi;
// c0 ~ x_hi^2 * 1/5 - 1/3
double c0 = fputil::multiply_add(x_hi_sq, 0x1.999999999999ap-3,
-0x1.5555555555555p-2);
// c1 ~ x_hi^2 * 1/9 - 1/7
double c1 = fputil::multiply_add(x_hi_sq, 0x1.c71c71c71c71cp-4,
-0x1.2492492492492p-3);
// x_hi^3
double x_hi_3 = x_hi_sq * x.hi;
// x_hi^4
double x_hi_4 = x_hi_sq * x_hi_sq;
// d0 ~ 1/3 - x_hi^2 / 5 + x_hi^4 / 7 - x_hi^6 / 9
double d0 = fputil::multiply_add(x_hi_4, c1, c0);
// x_lo - x_lo * x_hi^2 + x_lo * x_hi^4
double d1 = fputil::multiply_add(x_hi_4 - x_hi_sq, x.lo, x.lo);
// p.lo ~ -x_hi^3/3 + x_hi^5/5 - x_hi^7/7 + x_hi^9/9 +
// + x_lo * (1 - x_hi^2 + x_hi^4)
p.lo = fputil::multiply_add(x_hi_3, d0, d1);
return p;
}

} // anonymous namespace

// There are several range reduction steps we can take for atan2(y, x) as
// follow:

// * Range reduction 1: signness
// atan2(y, x) will return a number between -PI and PI representing the angle
// forming by the 0x axis and the vector (x, y) on the 0xy-plane.
// In particular, we have that:
// atan2(y, x) = atan( y/x ) if x >= 0 and y >= 0 (I-quadrant)
// = pi + atan( y/x ) if x < 0 and y >= 0 (II-quadrant)
// = -pi + atan( y/x ) if x < 0 and y < 0 (III-quadrant)
// = atan( y/x ) if x >= 0 and y < 0 (IV-quadrant)
// Since atan function is odd, we can use the formula:
// atan(-u) = -atan(u)
// to adjust the above conditions a bit further:
// atan2(y, x) = atan( |y|/|x| ) if x >= 0 and y >= 0 (I-quadrant)
// = pi - atan( |y|/|x| ) if x < 0 and y >= 0 (II-quadrant)
// = -pi + atan( |y|/|x| ) if x < 0 and y < 0 (III-quadrant)
// = -atan( |y|/|x| ) if x >= 0 and y < 0 (IV-quadrant)
// Which can be simplified to:
// atan2(y, x) = sign(y) * atan( |y|/|x| ) if x >= 0
// = sign(y) * (pi - atan( |y|/|x| )) if x < 0

// * Range reduction 2: reciprocal
// Now that the argument inside atan is positive, we can use the formula:
// atan(1/x) = pi/2 - atan(x)
// to make the argument inside atan <= 1 as follow:
// atan2(y, x) = sign(y) * atan( |y|/|x|) if 0 <= |y| <= x
// = sign(y) * (pi/2 - atan( |x|/|y| ) if 0 <= x < |y|
// = sign(y) * (pi - atan( |y|/|x| )) if 0 <= |y| <= -x
// = sign(y) * (pi/2 + atan( |x|/|y| )) if 0 <= -x < |y|

// * Range reduction 3: look up table.
// After the previous two range reduction steps, we reduce the problem to
// compute atan(u) with 0 <= u <= 1, or to be precise:
// atan( n / d ) where n = min(|x|, |y|) and d = max(|x|, |y|).
// An accurate polynomial approximation for the whole [0, 1] input range will
// require a very large degree. To make it more efficient, we reduce the input
// range further by finding an integer idx such that:
// | n/d - idx/64 | <= 1/128.
// In particular,
// idx := round(2^6 * n/d)
// Then for the fast pass, we find a polynomial approximation for:
// atan( n/d ) ~ atan( idx/64 ) + (n/d - idx/64) * Q(n/d - idx/64)
// For the accurate pass, we use the addition formula:
// atan( n/d ) - atan( idx/64 ) = atan( (n/d - idx/64)/(1 + (n*idx)/(64*d)) )
// = atan( (n - d*(idx/64))/(d + n*(idx/64)) )
// And for the fast pass, we use degree-9 Taylor polynomial to compute the RHS:
// atan(u) ~ P(u) = u - u^3/3 + u^5/5 - u^7/7 + u^9/9
// with absolute errors bounded by:
// |atan(u) - P(u)| < |u|^11 / 11 < 2^-80
// and relative errors bounded by:
// |(atan(u) - P(u)) / P(u)| < u^10 / 11 < 2^-73.

LLVM_LIBC_FUNCTION(double, atan2, (double y, double x)) {
using FPBits = fputil::FPBits<double>;

constexpr double IS_NEG[2] = {1.0, -1.0};
constexpr DoubleDouble ZERO = {0.0, 0.0};
constexpr DoubleDouble MZERO = {-0.0, -0.0};
constexpr DoubleDouble PI = {0x1.1a62633145c07p-53, 0x1.921fb54442d18p+1};
constexpr DoubleDouble MPI = {-0x1.1a62633145c07p-53, -0x1.921fb54442d18p+1};
constexpr DoubleDouble PI_OVER_2 = {0x1.1a62633145c07p-54,
0x1.921fb54442d18p0};
constexpr DoubleDouble MPI_OVER_2 = {-0x1.1a62633145c07p-54,
-0x1.921fb54442d18p0};
constexpr DoubleDouble PI_OVER_4 = {0x1.1a62633145c07p-55,
0x1.921fb54442d18p-1};
constexpr DoubleDouble THREE_PI_OVER_4 = {0x1.a79394c9e8a0ap-54,
0x1.2d97c7f3321d2p+1};
// Adjustment for constant term:
// CONST_ADJ[x_sign][y_sign][recip]
constexpr DoubleDouble CONST_ADJ[2][2][2] = {
{{ZERO, MPI_OVER_2}, {MZERO, MPI_OVER_2}},
{{MPI, PI_OVER_2}, {MPI, PI_OVER_2}}};

FPBits x_bits(x), y_bits(y);
bool x_sign = x_bits.sign().is_neg();
bool y_sign = y_bits.sign().is_neg();
x_bits = x_bits.abs();
y_bits = y_bits.abs();
uint64_t x_abs = x_bits.uintval();
uint64_t y_abs = y_bits.uintval();
bool recip = x_abs < y_abs;
uint64_t min_abs = recip ? x_abs : y_abs;
uint64_t max_abs = !recip ? x_abs : y_abs;
unsigned min_exp = static_cast<unsigned>(min_abs >> FPBits::FRACTION_LEN);
unsigned max_exp = static_cast<unsigned>(max_abs >> FPBits::FRACTION_LEN);

double num = FPBits(min_abs).get_val();
double den = FPBits(max_abs).get_val();

// Check for exceptional cases, whether inputs are 0, inf, nan, or close to
// overflow, or close to underflow.
if (LIBC_UNLIKELY(max_exp > 0x7ffU - 128U || min_exp < 128U)) {
if (x_bits.is_nan() || y_bits.is_nan())
return FPBits::quiet_nan().get_val();
unsigned x_except = x_abs == 0 ? 0 : (FPBits(x_abs).is_inf() ? 2 : 1);
unsigned y_except = y_abs == 0 ? 0 : (FPBits(y_abs).is_inf() ? 2 : 1);

// Exceptional cases:
// EXCEPT[y_except][x_except][x_is_neg]
// with x_except & y_except:
// 0: zero
// 1: finite, non-zero
// 2: infinity
constexpr DoubleDouble EXCEPTS[3][3][2] = {
{{ZERO, PI}, {ZERO, PI}, {ZERO, PI}},
{{PI_OVER_2, PI_OVER_2}, {ZERO, ZERO}, {ZERO, PI}},
{{PI_OVER_2, PI_OVER_2},
{PI_OVER_2, PI_OVER_2},
{PI_OVER_4, THREE_PI_OVER_4}},
};

if ((x_except != 1) || (y_except != 1)) {
DoubleDouble r = EXCEPTS[y_except][x_except][x_sign];
return fputil::multiply_add(IS_NEG[y_sign], r.hi, IS_NEG[y_sign] * r.lo);
}
bool scale_up = min_exp < 128U;
bool scale_down = max_exp > 0x7ffU - 128U;
// At least one input is denormal, multiply both numerator and denominator
// by some large enough power of 2 to normalize denormal inputs.
if (scale_up) {
num *= 0x1.0p64;
if (!scale_down)
den *= 0x1.0p64;
} else if (scale_down) {
den *= 0x1.0p-64;
if (!scale_up)
num *= 0x1.0p-64;
}

min_abs = FPBits(num).uintval();
max_abs = FPBits(den).uintval();
min_exp = static_cast<unsigned>(min_abs >> FPBits::FRACTION_LEN);
max_exp = static_cast<unsigned>(max_abs >> FPBits::FRACTION_LEN);
}

double final_sign = IS_NEG[(x_sign != y_sign) != recip];
DoubleDouble const_term = CONST_ADJ[x_sign][y_sign][recip];
unsigned exp_diff = max_exp - min_exp;
// We have the following bound for normalized n and d:
// 2^(-exp_diff - 1) < n/d < 2^(-exp_diff + 1).
if (LIBC_UNLIKELY(exp_diff > 54)) {
return fputil::multiply_add(final_sign, const_term.hi,
final_sign * (const_term.lo + num / den));
}

double k = fputil::nearest_integer(64.0 * num / den);
unsigned idx = static_cast<unsigned>(k);
// k = idx / 64
k *= 0x1.0p-6;

// Range reduction:
// atan(n/d) - atan(k/64) = atan((n/d - k/64) / (1 + (n/d) * (k/64)))
// = atan((n - d * k/64)) / (d + n * k/64))
DoubleDouble num_k = fputil::exact_mult(num, k);
DoubleDouble den_k = fputil::exact_mult(den, k);

// num_dd = n - d * k
DoubleDouble num_dd = fputil::exact_add(num - den_k.hi, -den_k.lo);
// den_dd = d + n * k
DoubleDouble den_dd = fputil::exact_add(den, num_k.hi);
den_dd.lo += num_k.lo;

// q = (n - d * k) / (d + n * k)
DoubleDouble q = fputil::div(num_dd, den_dd);
// p ~ atan(q)
DoubleDouble p = atan_eval(q);

DoubleDouble r = fputil::add(const_term, fputil::add(ATAN_I[idx], p));
r.hi *= final_sign;
r.lo *= final_sign;

return r.hi + r.lo;
}

} // namespace LIBC_NAMESPACE_DECL
10 changes: 10 additions & 0 deletions libc/startup/gpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ function(add_startup_object name)
PROPERTIES
OUTPUT_NAME ${name}.o
)

# Make an executable target of relocatable bitcode for clang if needed.
if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR)
add_executable(${fq_target_name}.exe $<TARGET_OBJECTS:${fq_target_name}>)
set_target_properties(${fq_target_name}.exe PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${LIBC_LIBRARY_DIR}
RUNTIME_OUTPUT_NAME ${name}.o)
target_link_options(${fq_target_name}.exe PRIVATE
"-nostdlib" "-flto" "-Wl,--lto-emit-llvm" "-march= ")
endif()
endfunction()

if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_ARCHITECTURE})
Expand Down
12 changes: 12 additions & 0 deletions libc/test/src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2044,6 +2044,18 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)

add_fp_unittest(
atan2_test
NEED_MPFR
SUITE
libc-math-unittests
SRCS
atan2_test.cpp
DEPENDS
libc.src.math.atan2
libc.src.__support.FPUtil.fp_bits
)

add_fp_unittest(
f16add_test
NEED_MPFR
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/CopySignTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class CopySignTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = FPBits(v).get_val();
if (isnan(x) || isinf(x))
if (FPBits(v).is_nan() || FPBits(v).is_inf())
continue;

double res1 = func(x, -x);
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/FAbsTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class FAbsTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = FPBits(v).get_val();
if (isnan(x) || isinf(x))
if (FPBits(v).is_nan() || FPBits(v).is_inf())
continue;
ASSERT_MPFR_MATCH(mpfr::Operation::Abs, x, func(x), 0.0);
}
Expand Down
4 changes: 2 additions & 2 deletions libc/test/src/math/FDimTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ class FDimTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT;
++i, v += STEP, w -= STEP) {
T x = FPBits(v).get_val(), y = FPBits(w).get_val();
if (isnan(x) || isinf(x))
if (FPBits(v).is_nan() || FPBits(v).is_inf())
continue;
if (isnan(y) || isinf(y))
if (FPBits(w).is_nan() || FPBits(w).is_inf())
continue;

if (x > y) {
Expand Down
4 changes: 2 additions & 2 deletions libc/test/src/math/FMaxTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ class FMaxTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT;
++i, v += STEP, w -= STEP) {
T x = FPBits(v).get_val(), y = FPBits(w).get_val();
if (isnan(x) || isinf(x))
if (FPBits(v).is_nan() || FPBits(v).is_inf())
continue;
if (isnan(y) || isinf(y))
if (FPBits(w).is_nan() || FPBits(w).is_inf())
continue;
if ((x == 0) && (y == 0))
continue;
Expand Down
5 changes: 3 additions & 2 deletions libc/test/src/math/FMinTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_TEST_SRC_MATH_FMINTEST_H
#define LLVM_LIBC_TEST_SRC_MATH_FMINTEST_H

#include "src/__support/FPUtil/FPBits.h"
#include "test/UnitTest/FEnvSafeTest.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
Expand Down Expand Up @@ -65,9 +66,9 @@ class FMinTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT;
++i, v += STEP, w -= STEP) {
T x = FPBits(v).get_val(), y = FPBits(w).get_val();
if (isnan(x) || isinf(x))
if (FPBits(v).is_nan() || FPBits(v).is_inf())
continue;
if (isnan(y) || isinf(y))
if (FPBits(w).is_nan() || FPBits(w).is_inf())
continue;
if ((x == 0) && (y == 0))
continue;
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/FrexpTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class FrexpTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = FPBits(v).get_val();
if (isnan(x) || isinf(x) || x == 0.0l)
if (FPBits(v).is_nan() || FPBits(v).is_inf() || x == 0.0l)
continue;

mpfr::BinaryOutput<T> result;
Expand Down
4 changes: 2 additions & 2 deletions libc/test/src/math/ILogbTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class LlvmLibcILogbTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
constexpr StorageType STEP = (MAX_SUBNORMAL - MIN_SUBNORMAL) / COUNT;
for (StorageType v = MIN_SUBNORMAL; v <= MAX_SUBNORMAL; v += STEP) {
T x = FPBits(v).get_val();
if (isnan(x) || isinf(x) || x == 0.0)
if (FPBits(v).is_nan() || FPBits(v).is_inf() || x == 0.0)
continue;

int exponent;
Expand All @@ -101,7 +101,7 @@ class LlvmLibcILogbTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
constexpr StorageType STEP = (MAX_NORMAL - MIN_NORMAL) / COUNT;
for (StorageType v = MIN_NORMAL; v <= MAX_NORMAL; v += STEP) {
T x = FPBits(v).get_val();
if (isnan(x) || isinf(x) || x == 0.0)
if (FPBits(v).is_nan() || FPBits(v).is_inf() || x == 0.0)
continue;

int exponent;
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/LogbTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class LogbTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = FPBits(v).get_val();
if (isnan(x) || isinf(x) || x == 0.0l)
if (FPBits(v).is_nan() || FPBits(v).is_inf() || x == 0.0l)
continue;

int exponent;
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/ModfTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class ModfTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = FPBits(v).get_val();
if (isnan(x) || isinf(x) || x == T(0.0))
if (FPBits(v).is_nan() || FPBits(v).is_inf() || x == T(0.0))
continue;

T integral;
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/RemQuoTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class RemQuoTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {

// In normal range on x86 platforms, the long double implicit 1 bit can be
// zero making the numbers NaN. Hence we test for them separately.
if (isnan(x) || isnan(y)) {
if (FPBits(v).is_nan() || FPBits(w).is_nan()) {
ASSERT_FP_EQ(result.f, nan);
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/acosf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ TEST_F(LlvmLibcAcosfTest, InFloatRange) {
constexpr uint32_t STEP = UINT32_MAX / COUNT;
for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
float x = FPBits(v).get_val();
if (isnan(x) || isinf(x))
if (FPBits(v).is_nan() || FPBits(v).is_inf())
continue;
ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Acos, x,
LIBC_NAMESPACE::acosf(x), 0.5);
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/acoshf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ TEST_F(LlvmLibcAcoshfTest, InFloatRange) {
constexpr uint32_t STEP = UINT32_MAX / COUNT;
for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
float x = FPBits(v).get_val();
if (isnan(x) || isinf(x))
if (FPBits(v).is_nan() || FPBits(v).is_inf())
continue;
ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Acosh, x,
LIBC_NAMESPACE::acoshf(x), 0.5);
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/asinf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ TEST_F(LlvmLibcAsinfTest, InFloatRange) {
constexpr uint32_t STEP = UINT32_MAX / COUNT;
for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
float x = FPBits(v).get_val();
if (isnan(x) || isinf(x))
if (FPBits(v).is_nan() || FPBits(v).is_inf())
continue;
ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Asin, x,
LIBC_NAMESPACE::asinf(x), 0.5);
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/asinhf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ TEST_F(LlvmLibcAsinhfTest, InFloatRange) {
constexpr uint32_t STEP = UINT32_MAX / COUNT;
for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
float x = FPBits(v).get_val();
if (isnan(x) || isinf(x))
if (FPBits(v).is_nan() || FPBits(v).is_inf())
continue;
ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Asinh, x,
LIBC_NAMESPACE::asinhf(x), 0.5);
Expand Down
125 changes: 125 additions & 0 deletions libc/test/src/math/atan2_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
//===-- Unittests for atan2 -----------------------------------------------===//
//
// 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/__support/FPUtil/FPBits.h"
#include "src/math/atan2.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
#include "utils/MPFRWrapper/MPFRUtils.h"

using LlvmLibcAtan2Test = LIBC_NAMESPACE::testing::FPTest<double>;
using LIBC_NAMESPACE::testing::tlog;

namespace mpfr = LIBC_NAMESPACE::testing::mpfr;

TEST_F(LlvmLibcAtan2Test, TrickyInputs) {
mpfr::BinaryInput<double> inputs[] = {
{0x1.0853408534085p-2, 0x1.e7b54166c6126p-2},
{FPBits::inf().get_val(), 0x0.0000000000001p-1022},
};

for (mpfr::BinaryInput<double> &input : inputs) {
double x = input.x;
double y = input.y;
mpfr::RoundingMode rm = mpfr::RoundingMode::Downward;
mpfr::ForceRoundingMode rr(rm);
ASSERT_MPFR_MATCH(mpfr::Operation::Atan2, input,
LIBC_NAMESPACE::atan2(x, y), 0.5, rm);
input.x = -input.x;
ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Atan2, input,
LIBC_NAMESPACE::atan2(-x, y), 0.5);
input.y = -input.y;
ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Atan2, input,
LIBC_NAMESPACE::atan2(-x, -y), 0.5);
input.x = -input.x;
ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Atan2, input,
LIBC_NAMESPACE::atan2(x, -y), 0.5);
}
}

TEST_F(LlvmLibcAtan2Test, InDoubleRange) {
constexpr uint64_t X_COUNT = 123;
constexpr uint64_t X_START = FPBits(0.25).uintval();
constexpr uint64_t X_STOP = FPBits(4.0).uintval();
constexpr uint64_t X_STEP = (X_STOP - X_START) / X_COUNT;

constexpr uint64_t Y_COUNT = 137;
constexpr uint64_t Y_START = FPBits(0.25).uintval();
constexpr uint64_t Y_STOP = FPBits(4.0).uintval();
constexpr uint64_t Y_STEP = (Y_STOP - Y_START) / Y_COUNT;

auto test = [&](mpfr::RoundingMode rounding_mode) {
mpfr::ForceRoundingMode __r(rounding_mode);
if (!__r.success)
return;

uint64_t fails = 0;
uint64_t finite_count = 0;
uint64_t total_count = 0;
double failed_x = 0.0, failed_y = 0.0, failed_r = 0.0;
double tol = 0.5;

for (uint64_t i = 0, v = X_START; i <= X_COUNT; ++i, v += X_STEP) {
double x = FPBits(v).get_val();
if (FPBits(x).is_inf_or_nan() || x < 0.0)
continue;

for (uint64_t j = 0, w = Y_START; j <= Y_COUNT; ++j, w += Y_STEP) {
double y = FPBits(w).get_val();
if (FPBits(y).is_inf_or_nan())
continue;

double result = LIBC_NAMESPACE::atan2(x, y);
++total_count;
if (FPBits(result).is_inf_or_nan())
continue;

++finite_count;
mpfr::BinaryInput<double> inputs{x, y};

if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Atan2, inputs,
result, 0.5, rounding_mode)) {
++fails;
while (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(
mpfr::Operation::Atan2, inputs, result, tol, rounding_mode)) {
failed_x = x;
failed_y = y;
failed_r = result;

if (tol > 1000.0)
break;

tol *= 2.0;
}
}
}
}
if (fails || (finite_count < total_count)) {
tlog << " Atan2 failed: " << fails << "/" << finite_count << "/"
<< total_count << " tests.\n"
<< " Max ULPs is at most: " << static_cast<uint64_t>(tol) << ".\n";
}
if (fails) {
mpfr::BinaryInput<double> inputs{failed_x, failed_y};
EXPECT_MPFR_MATCH(mpfr::Operation::Atan2, inputs, failed_r, 0.5,
rounding_mode);
}
};

tlog << " Test Rounding To Nearest...\n";
test(mpfr::RoundingMode::Nearest);

tlog << " Test Rounding Downward...\n";
test(mpfr::RoundingMode::Downward);

tlog << " Test Rounding Upward...\n";
test(mpfr::RoundingMode::Upward);

tlog << " Test Rounding Toward Zero...\n";
test(mpfr::RoundingMode::TowardZero);
}
6 changes: 3 additions & 3 deletions libc/test/src/math/atan2f_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,18 @@ TEST_F(LlvmLibcAtan2fTest, InFloatRange) {

for (uint32_t i = 0, v = X_START; i <= X_COUNT; ++i, v += X_STEP) {
float x = FPBits(v).get_val();
if (isnan(x) || isinf(x) || x < 0.0)
if (FPBits(v).is_nan() || FPBits(v).is_inf() || x < 0.0)
continue;

for (uint32_t j = 0, w = Y_START; j <= Y_COUNT; ++j, w += Y_STEP) {
float y = FPBits(w).get_val();
if (isnan(y) || isinf(y))
if (FPBits(w).is_nan() || FPBits(w).is_inf())
continue;

LIBC_NAMESPACE::libc_errno = 0;
float result = LIBC_NAMESPACE::atan2f(x, y);
++total_count;
if (isnan(result) || isinf(result))
if (FPBits(result).is_nan() || FPBits(result).is_inf())
continue;

++finite_count;
Expand Down
4 changes: 2 additions & 2 deletions libc/test/src/math/cos_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ TEST_F(LlvmLibcCosTest, InDoubleRange) {

for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) {
double x = FPBits(v).get_val();
if (isnan(x) || isinf(x))
if (FPBits(v).is_nan() || FPBits(v).is_inf())
continue;

double result = LIBC_NAMESPACE::cos(x);
++total;
if (isnan(result) || isinf(result))
if (FPBits(result).is_nan() || FPBits(result).is_inf())
continue;

++tested;
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/cosf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ TEST_F(LlvmLibcCosfTest, InFloatRange) {
constexpr uint32_t STEP = UINT32_MAX / COUNT;
for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
float x = FPBits(v).get_val();
if (isnan(x) || isinf(x))
if (FPBits(v).is_nan() || FPBits(v).is_inf())
continue;
ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Cos, x,
LIBC_NAMESPACE::cosf(x), 0.5);
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/coshf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ TEST_F(LlvmLibcCoshfTest, InFloatRange) {
constexpr uint32_t STEP = UINT32_MAX / COUNT;
for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
float x = FPBits(v).get_val();
if (isnan(x) || isinf(x))
if (FPBits(v).is_nan() || FPBits(v).is_inf())
continue;
ASSERT_MPFR_MATCH(mpfr::Operation::Cosh, x, LIBC_NAMESPACE::coshf(x), 0.5);
}
Expand Down
4 changes: 2 additions & 2 deletions libc/test/src/math/erff_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ TEST_F(LlvmLibcErffTest, InFloatRange) {

for (uint32_t i = 0, v = START; i <= COUNT; ++i, v += STEP) {
float x = FPBits(v).get_val();
if (isnan(x))
if (FPBits(v).is_nan())
continue;

float result = LIBC_NAMESPACE::erff(x);
++cc;
if (isnan(result))
if (FPBits(result).is_nan())
continue;

++count;
Expand Down
4 changes: 2 additions & 2 deletions libc/test/src/math/exp10_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ TEST_F(LlvmLibcExp10Test, InDoubleRange) {

for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) {
double x = FPBits(v).get_val();
if (isnan(x) || isinf(x) || x < 0.0)
if (FPBits(v).is_nan() || FPBits(v).is_inf() || x < 0.0)
continue;
LIBC_NAMESPACE::libc_errno = 0;
double result = LIBC_NAMESPACE::exp10(x);
++cc;
if (isnan(result) || isinf(result))
if (FPBits(result).is_nan() || FPBits(result).is_inf())
continue;

++count;
Expand Down
5 changes: 3 additions & 2 deletions libc/test/src/math/exp10f_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ TEST_F(LlvmLibcExp10fTest, InFloatRange) {
constexpr uint32_t STEP = UINT32_MAX / COUNT;
for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
float x = FPBits(v).get_val();
if (isnan(x) || isinf(x))
if (FPBits(v).is_nan() || FPBits(v).is_inf())
continue;
LIBC_NAMESPACE::libc_errno = 0;
float result = LIBC_NAMESPACE::exp10f(x);
Expand All @@ -120,7 +120,8 @@ TEST_F(LlvmLibcExp10fTest, InFloatRange) {
// in the single-precision floating point range, then ignore comparing with
// MPFR result as MPFR can still produce valid results because of its
// wider precision.
if (isnan(result) || isinf(result) || LIBC_NAMESPACE::libc_errno != 0)
if (FPBits(result).is_nan() || FPBits(result).is_inf() ||
LIBC_NAMESPACE::libc_errno != 0)
continue;
ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp10, x,
LIBC_NAMESPACE::exp10f(x), 0.5);
Expand Down
4 changes: 2 additions & 2 deletions libc/test/src/math/exp2_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ TEST_F(LlvmLibcExp2Test, InDoubleRange) {

for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) {
double x = FPBits(v).get_val();
if (isnan(x) || isinf(x) || x < 0.0)
if (FPBits(v).is_nan() || FPBits(v).is_inf() || x < 0.0)
continue;
LIBC_NAMESPACE::libc_errno = 0;
double result = LIBC_NAMESPACE::exp2(x);
++cc;
if (isnan(result) || isinf(result))
if (FPBits(result).is_nan() || FPBits(result).is_inf())
continue;

++count;
Expand Down
5 changes: 3 additions & 2 deletions libc/test/src/math/exp2f_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ TEST_F(LlvmLibcExp2fTest, InFloatRange) {
constexpr uint32_t STEP = UINT32_MAX / COUNT;
for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
float x = FPBits(v).get_val();
if (isnan(x) || isinf(x))
if (FPBits(v).is_nan() || FPBits(v).is_inf())
continue;
LIBC_NAMESPACE::libc_errno = 0;
float result = LIBC_NAMESPACE::exp2f(x);
Expand All @@ -116,7 +116,8 @@ TEST_F(LlvmLibcExp2fTest, InFloatRange) {
// in the single-precision floating point range, then ignore comparing with
// MPFR result as MPFR can still produce valid results because of its
// wider precision.
if (isnan(result) || isinf(result) || LIBC_NAMESPACE::libc_errno != 0)
if (FPBits(result).is_nan() || FPBits(result).is_inf() ||
LIBC_NAMESPACE::libc_errno != 0)
continue;
ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp2, x,
LIBC_NAMESPACE::exp2f(x), 0.5);
Expand Down
5 changes: 3 additions & 2 deletions libc/test/src/math/exp2m1f_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ TEST_F(LlvmLibcExp2m1fTest, InFloatRange) {
constexpr uint32_t STEP = UINT32_MAX / COUNT;
for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
float x = FPBits(v).get_val();
if (isnan(x) || isinf(x))
if (FPBits(v).is_nan() || FPBits(v).is_inf())
continue;
LIBC_NAMESPACE::libc_errno = 0;
float result = LIBC_NAMESPACE::exp2m1f(x);
Expand All @@ -58,7 +58,8 @@ TEST_F(LlvmLibcExp2m1fTest, InFloatRange) {
// in the single-precision floating point range, then ignore comparing with
// MPFR result as MPFR can still produce valid results because of its
// wider precision.
if (isnan(result) || isinf(result) || LIBC_NAMESPACE::libc_errno != 0)
if (FPBits(result).is_nan() || FPBits(result).is_inf() ||
LIBC_NAMESPACE::libc_errno != 0)
continue;
ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp2m1, x,
LIBC_NAMESPACE::exp2m1f(x), 0.5);
Expand Down
4 changes: 2 additions & 2 deletions libc/test/src/math/exp_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ TEST_F(LlvmLibcExpTest, InDoubleRange) {

for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) {
double x = FPBits(v).get_val();
if (isnan(x) || isinf(x) || x < 0.0)
if (FPBits(v).is_nan() || FPBits(v).is_inf() || x < 0.0)
continue;
LIBC_NAMESPACE::libc_errno = 0;
double result = LIBC_NAMESPACE::exp(x);
++cc;
if (isnan(result) || isinf(result))
if (FPBits(result).is_nan() || FPBits(result).is_inf())
continue;

++count;
Expand Down
5 changes: 3 additions & 2 deletions libc/test/src/math/expf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ TEST_F(LlvmLibcExpfTest, InFloatRange) {
constexpr uint32_t STEP = UINT32_MAX / COUNT;
for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
float x = FPBits(v).get_val();
if (isnan(x) || isinf(x))
if (FPBits(v).is_nan() || FPBits(v).is_inf())
continue;
LIBC_NAMESPACE::libc_errno = 0;
float result = LIBC_NAMESPACE::expf(x);
Expand All @@ -117,7 +117,8 @@ TEST_F(LlvmLibcExpfTest, InFloatRange) {
// in the single-precision floating point range, then ignore comparing with
// MPFR result as MPFR can still produce valid results because of its
// wider precision.
if (isnan(result) || isinf(result) || LIBC_NAMESPACE::libc_errno != 0)
if (FPBits(result).is_nan() || FPBits(result).is_inf() ||
LIBC_NAMESPACE::libc_errno != 0)
continue;
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp, x,
LIBC_NAMESPACE::expf(x), 0.5);
Expand Down
6 changes: 4 additions & 2 deletions libc/test/src/math/explogxf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
#include "utils/MPFRWrapper/MPFRUtils.h"

using LlvmLibcExplogfTest = LIBC_NAMESPACE::testing::FPTest<float>;
using FPBits = LIBC_NAMESPACE::fputil::FPBits<float>;

namespace mpfr = LIBC_NAMESPACE::testing::mpfr;

constexpr int def_count = 100003;
constexpr float def_prec = 0.500001f;

auto f_normal = [](float x) -> bool {
return !(isnan(x) || isinf(x) || LIBC_NAMESPACE::fabs(x) < 2E-38);
return !(FPBits(x).is_nan() || FPBits(x).is_inf() ||
LIBC_NAMESPACE::fabs(x) < 2E-38);
};

TEST_F(LlvmLibcExplogfTest, ExpInFloatRange) {
Expand All @@ -34,7 +36,7 @@ TEST_F(LlvmLibcExplogfTest, ExpInFloatRange) {
return static_cast<float>(result.mh * r);
};
auto f_check = [](float x) -> bool {
return !((isnan(x) || isinf(x) || x < -70 || x > 70 ||
return !((FPBits(x).is_nan() || FPBits(x).is_inf() || x < -70 || x > 70 ||
LIBC_NAMESPACE::fabsf(x) < 0x1.0p-10));
};
CHECK_DATA(0.0f, neg_inf, mpfr::Operation::Exp, fx, f_check, def_count,
Expand Down
4 changes: 2 additions & 2 deletions libc/test/src/math/expm1_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ TEST_F(LlvmLibcExpm1Test, InDoubleRange) {

for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) {
double x = FPBits(v).get_val();
if (isnan(x) || isinf(x) || x < 0.0)
if (FPBits(v).is_nan() || FPBits(v).is_inf() || x < 0.0)
continue;
LIBC_NAMESPACE::libc_errno = 0;
double result = LIBC_NAMESPACE::expm1(x);
++cc;
if (isnan(result) || isinf(result))
if (FPBits(result).is_nan() || FPBits(result).is_inf())
continue;

++count;
Expand Down
5 changes: 3 additions & 2 deletions libc/test/src/math/expm1f_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ TEST_F(LlvmLibcExpm1fTest, InFloatRange) {
constexpr uint32_t STEP = UINT32_MAX / COUNT;
for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
float x = FPBits(v).get_val();
if (isnan(x) || isinf(x))
if (FPBits(v).is_nan() || FPBits(v).is_inf())
continue;
LIBC_NAMESPACE::libc_errno = 0;
float result = LIBC_NAMESPACE::expm1f(x);
Expand All @@ -126,7 +126,8 @@ TEST_F(LlvmLibcExpm1fTest, InFloatRange) {
// in the single-precision floating point range, then ignore comparing with
// MPFR result as MPFR can still produce valid results because of its
// wider precision.
if (isnan(result) || isinf(result) || LIBC_NAMESPACE::libc_errno != 0)
if (FPBits(result).is_nan() || FPBits(result).is_inf() ||
LIBC_NAMESPACE::libc_errno != 0)
continue;
ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Expm1, x,
LIBC_NAMESPACE::expm1f(x), 0.5);
Expand Down
4 changes: 2 additions & 2 deletions libc/test/src/math/log10_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ TEST_F(LlvmLibcLog10Test, InDoubleRange) {

for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) {
double x = FPBits(v).get_val();
if (isnan(x) || isinf(x) || x < 0.0)
if (FPBits(v).is_nan() || FPBits(v).is_inf() || x < 0.0)
continue;
LIBC_NAMESPACE::libc_errno = 0;
double result = LIBC_NAMESPACE::log10(x);
++cc;
if (isnan(result) || isinf(result))
if (FPBits(result).is_nan() || FPBits(result).is_inf())
continue;

++count;
Expand Down
4 changes: 2 additions & 2 deletions libc/test/src/math/log1p_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ TEST_F(LlvmLibcLog1pTest, InDoubleRange) {

for (uint64_t i = 0, v = start; i <= COUNT; ++i, v += step) {
double x = FPBits(v).get_val();
if (isnan(x) || isinf(x) || x < 0.0)
if (FPBits(v).is_nan() || FPBits(v).is_inf() || x < 0.0)
continue;
LIBC_NAMESPACE::libc_errno = 0;
double result = LIBC_NAMESPACE::log1p(x);
++cc;
if (isnan(result) || isinf(result))
if (FPBits(result).is_nan() || FPBits(result).is_inf())
continue;

++count;
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/log1pf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ TEST_F(LlvmLibcLog1pfTest, InFloatRange) {
constexpr uint32_t STEP = UINT32_MAX / COUNT;
for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
float x = FPBits(v).get_val();
if (isnan(x) || isinf(x))
if (FPBits(v).is_nan() || FPBits(v).is_inf())
continue;
LIBC_NAMESPACE::libc_errno = 0;
ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log1p, x,
Expand Down
4 changes: 2 additions & 2 deletions libc/test/src/math/log2_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ TEST_F(LlvmLibcLog2Test, InDoubleRange) {

for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) {
double x = FPBits(v).get_val();
if (isnan(x) || isinf(x) || x < 0.0)
if (FPBits(v).is_nan() || FPBits(v).is_inf() || x < 0.0)
continue;
LIBC_NAMESPACE::libc_errno = 0;
double result = LIBC_NAMESPACE::log2(x);
++cc;
if (isnan(result) || isinf(result))
if (FPBits(result).is_nan() || FPBits(result).is_inf())
continue;

++count;
Expand Down
5 changes: 3 additions & 2 deletions libc/test/src/math/log2f_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,16 @@ TEST_F(LlvmLibcLog2fTest, InFloatRange) {
constexpr uint32_t STEP = UINT32_MAX / COUNT;
for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
float x = FPBits(v).get_val();
if (isnan(x) || isinf(x))
if (FPBits(v).is_nan() || FPBits(v).is_inf())
continue;
LIBC_NAMESPACE::libc_errno = 0;
float result = LIBC_NAMESPACE::log2f(x);
// If the computation resulted in an error or did not produce valid result
// in the single-precision floating point range, then ignore comparing with
// MPFR result as MPFR can still produce valid results because of its
// wider precision.
if (isnan(result) || isinf(result) || LIBC_NAMESPACE::libc_errno != 0)
if (FPBits(result).is_nan() || FPBits(result).is_inf() ||
LIBC_NAMESPACE::libc_errno != 0)
continue;
ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log2, x,
LIBC_NAMESPACE::log2f(x), 0.5);
Expand Down
4 changes: 2 additions & 2 deletions libc/test/src/math/log_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ TEST_F(LlvmLibcLogTest, InDoubleRange) {

for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) {
double x = FPBits(v).get_val();
if (isnan(x) || isinf(x) || x < 0.0)
if (FPBits(v).is_nan() || FPBits(v).is_inf() || x < 0.0)
continue;
LIBC_NAMESPACE::libc_errno = 0;
double result = LIBC_NAMESPACE::log(x);
++cc;
if (isnan(result) || isinf(result))
if (FPBits(result).is_nan() || FPBits(result).is_inf())
continue;

++count;
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/logf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ TEST_F(LlvmLibcLogfTest, InFloatRange) {
constexpr uint32_t STEP = UINT32_MAX / COUNT;
for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
float x = FPBits(v).get_val();
if (isnan(x) || isinf(x))
if (FPBits(v).is_nan() || FPBits(v).is_inf())
continue;
ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log, x,
LIBC_NAMESPACE::logf(x), 0.5);
Expand Down
6 changes: 3 additions & 3 deletions libc/test/src/math/powf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,18 @@ TEST_F(LlvmLibcPowfTest, InFloatRange) {

for (uint32_t i = 0, v = X_START; i <= X_COUNT; ++i, v += X_STEP) {
float x = FPBits(v).get_val();
if (isnan(x) || isinf(x) || x < 0.0)
if (FPBits(v).is_nan() || FPBits(v).is_inf() || x < 0.0)
continue;

for (uint32_t j = 0, w = Y_START; j <= Y_COUNT; ++j, w += Y_STEP) {
float y = FPBits(w).get_val();
if (isnan(y) || isinf(y))
if (FPBits(w).is_nan() || FPBits(w).is_inf())
continue;

LIBC_NAMESPACE::libc_errno = 0;
float result = LIBC_NAMESPACE::powf(x, y);
++cc;
if (isnan(result) || isinf(result))
if (FPBits(result).is_nan() || FPBits(result).is_inf())
continue;

++count;
Expand Down
4 changes: 2 additions & 2 deletions libc/test/src/math/sin_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ TEST_F(LlvmLibcSinTest, InDoubleRange) {

for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) {
double x = FPBits(v).get_val();
if (isnan(x) || isinf(x))
if (FPBits(v).is_nan() || FPBits(v).is_inf())
continue;
LIBC_NAMESPACE::libc_errno = 0;
double result = LIBC_NAMESPACE::sin(x);
++cc;
if (isnan(result) || isinf(result))
if (FPBits(result).is_nan() || FPBits(result).is_inf())
continue;

++count;
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/sincos_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ TEST_F(LlvmLibcSincosTest, InDoubleRange) {

for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) {
double x = FPBits(v).get_val();
if (isnan(x) || isinf(x))
if (FPBits(v).is_nan() || FPBits(v).is_inf())
continue;

ASSERT_SINCOS_MATCH_ALL_ROUNDING(x);
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/sincosf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ TEST_F(LlvmLibcSinCosfTest, InFloatRange) {
constexpr uint32_t STEP = UINT32_MAX / COUNT;
for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
float x = FPBits(v).get_val();
if (isnan(x) || isinf(x))
if (FPBits(v).is_nan() || FPBits(v).is_inf())
continue;

EXPECT_SINCOS_MATCH_ALL_ROUNDING(x);
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/sinf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ TEST_F(LlvmLibcSinfTest, InFloatRange) {
constexpr uint32_t STEP = UINT32_MAX / COUNT;
for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
float x = FPBits(v).get_val();
if (isnan(x) || isinf(x))
if (FPBits(v).is_nan() || FPBits(v).is_inf())
continue;
ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sin, x,
LIBC_NAMESPACE::sinf(x), 0.5);
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/sinhf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ TEST_F(LlvmLibcSinhfTest, InFloatRange) {
constexpr uint32_t STEP = UINT32_MAX / COUNT;
for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
float x = FPBits(v).get_val();
if (isnan(x) || isinf(x))
if (FPBits(v).is_nan() || FPBits(v).is_inf())
continue;
ASSERT_MPFR_MATCH(mpfr::Operation::Sinh, x, LIBC_NAMESPACE::sinhf(x), 0.5);
}
Expand Down
10 changes: 10 additions & 0 deletions libc/test/src/math/smoke/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3531,6 +3531,16 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)

add_fp_unittest(
atan2_test
SUITE
libc-math-smoke-tests
SRCS
atan2_test.cpp
DEPENDS
libc.src.math.atan2
)

add_fp_unittest(
scalblnf16_test
SUITE
Expand Down
22 changes: 22 additions & 0 deletions libc/test/src/math/smoke/atan2_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Unittests for atan2 -----------------------------------------------===//
//
// 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/atan2.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"

using LlvmLibcAtan2Test = LIBC_NAMESPACE::testing::FPTest<double>;

TEST_F(LlvmLibcAtan2Test, SpecialNumbers) {
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::atan2(aNaN, zero));
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::atan2(1.0, aNaN));
EXPECT_FP_EQ_ALL_ROUNDING(0.0, LIBC_NAMESPACE::atan2(zero, zero));
EXPECT_FP_EQ_ALL_ROUNDING(-0.0, LIBC_NAMESPACE::atan2(-0.0, zero));
EXPECT_FP_EQ_ALL_ROUNDING(0.0, LIBC_NAMESPACE::atan2(1.0, inf));
EXPECT_FP_EQ_ALL_ROUNDING(-0.0, LIBC_NAMESPACE::atan2(-1.0, inf));
}
4 changes: 2 additions & 2 deletions libc/test/src/math/tan_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ TEST_F(LlvmLibcTanTest, InDoubleRange) {

for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) {
double x = FPBits(v).get_val();
if (isnan(x) || isinf(x))
if (FPBits(v).is_nan() || FPBits(v).is_inf())
continue;

double result = LIBC_NAMESPACE::tan(x);
++total;
if (isnan(result) || isinf(result))
if (FPBits(result).is_nan() || FPBits(result).is_inf())
continue;

++tested;
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/tanf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ TEST_F(LlvmLibcTanfTest, InFloatRange) {
constexpr uint32_t STEP = UINT32_MAX / COUNT;
for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
float x = FPBits(v).get_val();
if (isnan(x) || isinf(x))
if (FPBits(v).is_nan() || FPBits(v).is_inf())
continue;
ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tan, x,
LIBC_NAMESPACE::tanf(x), 0.5);
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/tanhf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ TEST_F(LlvmLibcTanhfTest, InFloatRange) {
constexpr uint32_t STEP = UINT32_MAX / COUNT;
for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
float x = FPBits(v).get_val();
if (isnan(x) || isinf(x))
if (FPBits(v).is_nan() || FPBits(v).is_inf())
continue;
ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tanh, x,
LIBC_NAMESPACE::tanhf(x), 0.5);
Expand Down
Loading