Repository navigation
Expand file tree
/
Copy pathtype_utils.hpp
More file actions
158 lines (147 loc) · 5.22 KB
/
Copy pathtype_utils.hpp
File metadata and controls
158 lines (147 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//===------ type_utils.hpp - Implementation of types utils ----*-C++-*/===//
//
// Data Parallel Control (dpctl)
//
// Copyright 2020-2025 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file defines functions for value casting.
//===----------------------------------------------------------------------===//
#pragma once
#include <complex>
#include <cstddef>
#include <cstdint>
#include <stdexcept>
#include <sycl/sycl.hpp>
#include <type_traits>
#include <utility>
namespace dpctl
{
namespace tensor
{
namespace type_utils
{
template <typename T, typename = void>
struct is_complex : public std::false_type
{
};
template <typename T>
struct is_complex<
T,
std::enable_if_t<std::is_same_v<std::remove_cv_t<T>, std::complex<float>> ||
std::is_same_v<std::remove_cv_t<T>, std::complex<double>>>>
: public std::true_type
{
};
template <typename T> inline constexpr bool is_complex_v = is_complex<T>::value;
template <typename dstTy, typename srcTy> dstTy convert_impl(const srcTy &v)
{
if constexpr (std::is_same_v<dstTy, srcTy>) {
return v;
}
else if constexpr (std::is_same_v<dstTy, bool>) {
if constexpr (is_complex_v<srcTy>) {
// bool(complex_v) ==
// (complex_v.real() != 0) && (complex_v.imag() !=0)
return (convert_impl<bool, typename srcTy::value_type>(v.real()) ||
convert_impl<bool, typename srcTy::value_type>(v.imag()));
}
else {
return static_cast<dstTy>(v != srcTy{0});
}
}
else if constexpr (std::is_same_v<srcTy, bool>) {
// C++ interprets a byte of storage behind bool by only
// testing is least significant bit, leading to both
// 0x00 and 0x02 interpreted as False, while 0x01 and 0xFF
// interpreted as True. NumPy's interpretation of underlying
// storage is different: any bit set is interpreted as True,
// no bits set as False, see gh-2121
const std::uint8_t &u = sycl::bit_cast<std::uint8_t>(v);
if constexpr (is_complex_v<dstTy>) {
return (u == 0) ? dstTy{} : dstTy{1, 0};
}
else {
return (u == 0) ? dstTy{} : dstTy{1};
}
}
else if constexpr (is_complex_v<srcTy> && !is_complex_v<dstTy>) {
// real_t(complex_v) == real_t(complex_v.real())
return convert_impl<dstTy, typename srcTy::value_type>(v.real());
}
else if constexpr (!std::is_integral_v<srcTy> &&
!std::is_same_v<dstTy, bool> &&
std::is_integral_v<dstTy> && std::is_unsigned_v<dstTy>)
{
// first cast to signed variant, the cast to unsigned one
using signedT = typename std::make_signed_t<dstTy>;
return static_cast<dstTy>(convert_impl<signedT, srcTy>(v));
}
else {
return static_cast<dstTy>(v);
}
}
template <typename T> void validate_type_for_device(const sycl::device &d)
{
if constexpr (std::is_same_v<T, double>) {
if (!d.has(sycl::aspect::fp64)) {
throw std::runtime_error("Device " +
d.get_info<sycl::info::device::name>() +
" does not support type 'float64'");
}
}
else if constexpr (std::is_same_v<T, std::complex<double>>) {
if (!d.has(sycl::aspect::fp64)) {
throw std::runtime_error("Device " +
d.get_info<sycl::info::device::name>() +
" does not support type 'complex128'");
}
}
else if constexpr (std::is_same_v<T, sycl::half>) {
if (!d.has(sycl::aspect::fp16)) {
throw std::runtime_error("Device " +
d.get_info<sycl::info::device::name>() +
" does not support type 'float16'");
}
}
}
template <typename T> void validate_type_for_device(const sycl::queue &q)
{
validate_type_for_device<T>(q.get_device());
}
template <typename Op, typename Vec, std::size_t... I>
auto vec_cast_impl(const Vec &v, std::index_sequence<I...>)
{
return Op{v[I]...};
}
template <typename dstT,
typename srcT,
std::size_t N,
typename Indices = std::make_index_sequence<N>>
auto vec_cast(const sycl::vec<srcT, N> &s)
{
if constexpr (std::is_same_v<srcT, dstT>) {
return s;
}
else {
return vec_cast_impl<sycl::vec<dstT, N>, sycl::vec<srcT, N>>(s,
Indices{});
}
}
} // namespace type_utils
} // namespace tensor
} // namespace dpctl