-
Notifications
You must be signed in to change notification settings - Fork 796
[SYCL] Add sycl::detail::bit_cast implementation #1762
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
bader
merged 9 commits into
intel:sycl
from
dm-vodopyanov:private/dvodopya/implement_sycl_bit_cast_extension
Jun 3, 2020
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b459f18
[SYCL] Implement sycl::bit_cast extension
dm-vodopyanov 658d25a
[SYCL] Fix clang-format and fix comparison of floating-point values
dm-vodopyanov 7c42d64
[SYCL] Fix PR comments
dm-vodopyanov 4dbf1f7
[SYCL] Remove unnecessary copyright notice from sycl::bit_cast test
dm-vodopyanov 316543c
[SYCL] Fix PR comments
dm-vodopyanov 56c1143
[SYCL] Fix missing sycl::intel::bit_cast mentions in SYCL_INTEL_bitca…
dm-vodopyanov eb92988
[SYCL] Leave only sycl::detail::bit_cast implementation
dm-vodopyanov 81a7294
[SYCL] Add TODO comment for sycl::bit_cast
dm-vodopyanov 2e77361
[SYCL] make bit_cast test more device-agnostic
dm-vodopyanov 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out | ||
| // RUN: env SYCL_DEVICE_TYPE=HOST %t.out | ||
| // RUN: %CPU_RUN_PLACEHOLDER %t.out | ||
| // RUN: %GPU_RUN_PLACEHOLDER %t.out | ||
| // RUN: %ACC_RUN_PLACEHOLDER %t.out | ||
dm-vodopyanov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| #include <CL/sycl.hpp> | ||
|
|
||
| #include <iostream> | ||
| #include <math.h> | ||
| #include <type_traits> | ||
|
|
||
| constexpr cl::sycl::access::mode sycl_write = cl::sycl::access::mode::write; | ||
|
|
||
| template <typename To, typename From> | ||
| class BitCastKernel; | ||
|
|
||
| template <typename To, typename From> | ||
| To doBitCast(const From &ValueToConvert) { | ||
| std::vector<To> Vec(1); | ||
| { | ||
| sycl::buffer<To, 1> Buf(Vec.data(), 1); | ||
| sycl::queue Queue; | ||
| Queue.submit([&](sycl::handler &cgh) { | ||
| auto acc = Buf.template get_access<sycl_write>(cgh); | ||
| cgh.single_task<class BitCastKernel<To, From>>([=]() { | ||
| // TODO: change to sycl::bit_cast in the future | ||
| acc[0] = sycl::detail::bit_cast<To>(ValueToConvert); | ||
| }); | ||
| }); | ||
| } | ||
| return Vec[0]; | ||
| } | ||
|
|
||
| template <typename To, typename From> | ||
| int test(const From &Value) { | ||
| auto ValueConvertedTwoTimes = doBitCast<From>(doBitCast<To>(Value)); | ||
| bool isOriginalValueEqualsToConvertedTwoTimes = false; | ||
| if (std::is_integral<From>::value) { | ||
| isOriginalValueEqualsToConvertedTwoTimes = Value == ValueConvertedTwoTimes; | ||
| } else if ((std::is_floating_point<From>::value) || std::is_same<From, cl::sycl::half>::value) { | ||
| static const float Epsilon = 0.0000001f; | ||
| isOriginalValueEqualsToConvertedTwoTimes = fabs(Value - ValueConvertedTwoTimes) < Epsilon; | ||
dm-vodopyanov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } else { | ||
| std::cerr << "Type " << typeid(From).name() << " neither integral nor floating point nor cl::sycl::half\n"; | ||
| return 1; | ||
| } | ||
| if (!isOriginalValueEqualsToConvertedTwoTimes) { | ||
| std::cerr << "FAIL: Original value which is " << Value << " != value converted two times which is " << ValueConvertedTwoTimes << "\n"; | ||
| return 1; | ||
| } | ||
| std::cout << "PASS\n"; | ||
| return 0; | ||
| } | ||
|
|
||
| int main() { | ||
| int ReturnCode = 0; | ||
|
|
||
| std::cout << "cl::sycl::half to unsigned short ...\n"; | ||
| ReturnCode += test<unsigned short>(cl::sycl::half(1.0f)); | ||
|
|
||
| std::cout << "unsigned short to cl::sycl::half ...\n"; | ||
| ReturnCode += test<cl::sycl::half>(static_cast<unsigned short>(16384)); | ||
|
|
||
| std::cout << "cl::sycl::half to short ...\n"; | ||
| ReturnCode += test<short>(cl::sycl::half(1.0f)); | ||
|
|
||
| std::cout << "short to cl::sycl::half ...\n"; | ||
| ReturnCode += test<cl::sycl::half>(static_cast<short>(16384)); | ||
|
|
||
| std::cout << "int to float ...\n"; | ||
| ReturnCode += test<float>(static_cast<int>(2)); | ||
|
|
||
| std::cout << "float to int ...\n"; | ||
| ReturnCode += test<int>(static_cast<float>(-2.4f)); | ||
dm-vodopyanov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| std::cout << "unsigned int to float ...\n"; | ||
| ReturnCode += test<float>(static_cast<unsigned int>(6)); | ||
|
|
||
| std::cout << "float to unsigned int ...\n"; | ||
| ReturnCode += test<unsigned int>(static_cast<float>(-2.4f)); | ||
dm-vodopyanov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return ReturnCode; | ||
| } | ||
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.