DPC++ Runtime
Runtime libraries for oneAPI DPC++
half_type.cpp
Go to the documentation of this file.
1 //==-------------- half_type.cpp --- SYCL half type ------------------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include <CL/sycl/half_type.hpp>
11 // This is included to enable __builtin_expect()
12 #include <detail/platform_util.hpp>
13 
14 #include <cstring>
15 
17 namespace sycl {
18 namespace detail {
19 
20 namespace host_half_impl {
21 
22 half::half(const float &RHS) : Buf(float2Half(RHS)) {}
23 
24 half &half::operator+=(const half &RHS) {
25  *this = operator float() + static_cast<float>(RHS);
26  return *this;
27 }
28 
29 half &half::operator-=(const half &RHS) {
30  *this = operator float() - static_cast<float>(RHS);
31  return *this;
32 }
33 
34 half &half::operator*=(const half &RHS) {
35  *this = operator float() * static_cast<float>(RHS);
36  return *this;
37 }
38 
39 half &half::operator/=(const half &RHS) {
40  *this = operator float() / static_cast<float>(RHS);
41  return *this;
42 }
43 
44 half::operator float() const { return half2Float(Buf); }
45 
46 // Operator +, -, *, /
47 half operator+(half LHS, const half &RHS) {
48  LHS += RHS;
49  return LHS;
50 }
51 
52 half operator-(half LHS, const half &RHS) {
53  LHS -= RHS;
54  return LHS;
55 }
56 
57 half operator*(half LHS, const half &RHS) {
58  LHS *= RHS;
59  return LHS;
60 }
61 
62 half operator/(half LHS, const half &RHS) {
63  LHS /= RHS;
64  return LHS;
65 }
66 
67 // Operator <, >, <=, >=
68 bool operator<(const half &LHS, const half &RHS) {
69  return static_cast<float>(LHS) < static_cast<float>(RHS);
70 }
71 
72 bool operator>(const half &LHS, const half &RHS) { return RHS < LHS; }
73 
74 bool operator<=(const half &LHS, const half &RHS) { return !(LHS > RHS); }
75 
76 bool operator>=(const half &LHS, const half &RHS) { return !(LHS < RHS); }
77 
78 // Operator ==, !=
79 bool operator==(const half &LHS, const half &RHS) {
80  return static_cast<float>(LHS) == static_cast<float>(RHS);
81 }
82 
83 bool operator!=(const half &LHS, const half &RHS) { return !(LHS == RHS); }
84 } // namespace host_half_impl
85 
86 } // namespace detail
87 } // namespace sycl
88 } // __SYCL_INLINE_NAMESPACE(cl)
cl::sycl::detail::host_half_impl::operator*
half operator*(half LHS, const half &RHS)
Definition: half_type.cpp:57
cl::sycl::detail::host_half_impl::half
Definition: half_type.hpp:137
cl::sycl::detail::host_half_impl::operator/
half operator/(half LHS, const half &RHS)
Definition: half_type.cpp:62
cl::sycl::detail::host_half_impl::half::operator/=
half & operator/=(const half &rhs)
Definition: half_type.cpp:39
helpers.hpp
sycl
Definition: invoke_simd.hpp:68
cl::sycl::detail::host_half_impl::operator+
half operator+(half LHS, const half &RHS)
Definition: half_type.cpp:47
cl::sycl::half
cl::sycl::detail::half_impl::half half
Definition: aliases.hpp:77
cl::sycl::detail::host_half_impl::half::operator+=
half & operator+=(const half &rhs)
Definition: half_type.cpp:24
cl::sycl::detail::host_half_impl::operator-
half operator-(half LHS, const half &RHS)
Definition: half_type.cpp:52
cl::sycl::detail::host_half_impl::half::operator-=
half & operator-=(const half &rhs)
Definition: half_type.cpp:29
cl::sycl::detail::host_half_impl::operator<
bool operator<(const half &LHS, const half &RHS)
Definition: half_type.cpp:68
cl
We provide new interfaces for matrix muliply in this patch:
Definition: access.hpp:13
cl::sycl::detail::host_half_impl::half::operator*=
half & operator*=(const half &rhs)
Definition: half_type.cpp:34
cl::sycl::detail::host_half_impl::operator==
bool operator==(const half &LHS, const half &RHS)
Definition: half_type.cpp:79
platform_util.hpp
cl::sycl::detail::float2Half
__SYCL_CONSTEXPR_HALF uint16_t float2Half(const float &Val)
Definition: half_type.hpp:48
cl::sycl::detail::half2Float
__SYCL_CONSTEXPR_HALF float half2Float(const uint16_t &Val)
Definition: half_type.hpp:94
half_type.hpp
cl::sycl::detail::host_half_impl::operator!=
bool operator!=(const half &LHS, const half &RHS)
Definition: half_type.cpp:83
cl::sycl::detail::host_half_impl::operator<=
bool operator<=(const half &LHS, const half &RHS)
Definition: half_type.cpp:74
cl::sycl::detail::host_half_impl::operator>=
bool operator>=(const half &LHS, const half &RHS)
Definition: half_type.cpp:76
cl::sycl::detail::host_half_impl::operator>
bool operator>(const half &LHS, const half &RHS)
Definition: half_type.cpp:72
__SYCL_INLINE_NAMESPACE
#define __SYCL_INLINE_NAMESPACE(X)
Definition: defines_elementary.hpp:12