-
Notifications
You must be signed in to change notification settings - Fork 797
[SYCL][NFCI] Move abs and div to cstdlib header #19671
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e7467a8
[SYCL][NFCI] Move abs and div to cstdlib header
bader ed3ba14
Change attribute style from GNU to C++11.
bader 352cf9b
Make __sycl_cmath_wrapper_impl.hpp self-contained
bader b21a882
Fix typo.
bader 8507f9e
Merge remote-tracking branch 'origin/sycl' into cstdlib
bader 0b26f2b
Apply code review.
bader 74c4e95
Add one more test.
bader de78451
Extend the test and disable checks for global name space overloads on…
bader File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//==- cstdlib --------------------------------------------------------------==// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#pragma once | ||
|
||
// Include real STL <cstdlib> header - the next one from the include search | ||
// directories. | ||
#if defined(__has_include_next) | ||
// GCC/clang support go through this path. | ||
#include_next <cstdlib> | ||
#else | ||
// MSVC doesn't support "#include_next", so we have to be creative. | ||
// Our header is located in "stl_wrappers/cstdlib" so it won't be picked by the | ||
// following include. MSVC's installation, on the other hand, has the layout | ||
// where the following would result in the <cstdlib> we want. This is obviously | ||
// hacky, but the best we can do... | ||
#include <../include/cstdlib> | ||
#endif | ||
|
||
#ifdef __SYCL_DEVICE_ONLY__ | ||
extern "C" { | ||
[[clang::sycl_device_only, clang::always_inline]] div_t div(int x, int y) { return {x / y, x % y}; } | ||
[[clang::sycl_device_only, clang::always_inline]] ldiv_t ldiv(long x, long y) { return {x / y, x % y}; } | ||
[[clang::sycl_device_only, clang::always_inline]] lldiv_t lldiv(long long x, long long y) { return {x / y, x % y}; } | ||
bader marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
[[clang::sycl_device_only, clang::always_inline]] int abs(int n) { return n < 0 ? -n : n; } | ||
[[clang::sycl_device_only, clang::always_inline]] long labs(long n) { return n < 0 ? -n : n; } | ||
[[clang::sycl_device_only, clang::always_inline]] long long llabs(long long n) { return n < 0 ? -n : n; } | ||
} | ||
|
||
#ifdef _LIBCPP_BEGIN_NAMESPACE_STD | ||
_LIBCPP_BEGIN_NAMESPACE_STD | ||
#else | ||
namespace std { | ||
#ifdef _GLIBCXX_BEGIN_NAMESPACE_VERSION | ||
_GLIBCXX_BEGIN_NAMESPACE_VERSION | ||
#endif | ||
#endif | ||
|
||
using ::div; | ||
[[clang::sycl_device_only, clang::always_inline]] ldiv_t div(long x, long y) { return {x / y, x % y}; } | ||
[[clang::sycl_device_only, clang::always_inline]] lldiv_t div(long long x, long long y) { return {x / y, x % y}; } | ||
using ::ldiv; | ||
using ::lldiv; | ||
|
||
using ::abs; | ||
[[clang::sycl_device_only, clang::always_inline]] long abs(long n) { return n < 0 ? -n : n; } | ||
[[clang::sycl_device_only, clang::always_inline]] long long abs(long long n) { return n < 0 ? -n : n; } | ||
using ::labs; | ||
using ::llabs; | ||
|
||
#ifdef _LIBCPP_END_NAMESPACE_STD | ||
_LIBCPP_END_NAMESPACE_STD | ||
#else | ||
#ifdef _GLIBCXX_BEGIN_NAMESPACE_VERSION | ||
_GLIBCXX_END_NAMESPACE_VERSION | ||
#endif | ||
} // namespace std | ||
#endif | ||
#endif // __SYCL_DEVICE_ONLY__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// RUN: %clangxx -fsycl -fsyntax-only %s | ||
|
||
#include <type_traits> | ||
|
||
#include <cstdlib> | ||
|
||
int i = 1; | ||
long l = 1; | ||
long long ll = 1; | ||
|
||
// Unqualified calls should resolve to the `int` overloads | ||
static_assert(std::is_same_v<decltype(abs(i)), int>); | ||
static_assert(std::is_same_v<decltype(div(i, i)), div_t>); | ||
// NOTE: Windows Universal C Runtime defines C++ overloads for `long` and | ||
// `long long` types in global name space. See | ||
// https://github.com/huangqinjin/ucrt/blob/d6e817a4cc90f6f1fe54f8a0aa4af4fff0bb647d/include/stdlib.h#L360-L383. | ||
#ifndef _WIN32 | ||
static_assert(std::is_same_v<decltype(abs(l)), int>); | ||
static_assert(std::is_same_v<decltype(abs(ll)), int>); | ||
static_assert(std::is_same_v<decltype(div(i, l)), div_t>); | ||
static_assert(std::is_same_v<decltype(div(i, ll)), div_t>); | ||
static_assert(std::is_same_v<decltype(div(l, i)), div_t>); | ||
static_assert(std::is_same_v<decltype(div(l, l)), div_t>); | ||
static_assert(std::is_same_v<decltype(div(l, ll)), div_t>); | ||
static_assert(std::is_same_v<decltype(div(ll, i)), div_t>); | ||
static_assert(std::is_same_v<decltype(div(ll, l)), div_t>); | ||
static_assert(std::is_same_v<decltype(div(ll, ll)), div_t>); | ||
#endif // _WIN32 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.