This repository was archived by the owner on Jan 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPyTypes.hpp
More file actions
116 lines (103 loc) · 2.99 KB
/
PyTypes.hpp
File metadata and controls
116 lines (103 loc) · 2.99 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
// SPDX-License-Identifier: BSD-3-Clause
#pragma once
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
namespace py = pybind11;
#include "CppTypes.hpp"
#include "p2c_ids.hpp"
namespace SHARPY {
template <> inline bool SharedBaseObject<py::object>::needGIL() const {
return true;
}
template <> inline bool SharedBaseObject<py::handle>::needGIL() const {
return true;
}
template <typename T> py::object get_impl_dtype() {
return get_impl_dtype(DTYPE<T>::value);
};
inline PyScalar mk_scalar(const py::object &b, DTypeId dtype) {
PyScalar s;
if (b.is_none()) {
s._float = std::numeric_limits<double>::quiet_NaN();
return s;
}
switch (dtype) {
case FLOAT64:
case FLOAT32:
s._float = b.cast<double>();
return s;
case INT64:
case INT32:
case INT16:
case INT8:
case UINT64:
case UINT32:
case UINT16:
case UINT8:
s._int = b.cast<int64_t>();
return s;
/* FIXME
case BOOL:
return TypeDispatch2<OpDispatch>(std::forward<Ts>(args)...,
_downcast<bool>(a_ptr));
*/
default:
throw std::invalid_argument("unknown dtype");
}
}
template <typename T, typename SZ, typename EL>
py::tuple _make_tuple(const T &v, const SZ &sz, const EL &el) {
const auto n = sz(v);
switch (n) {
case 0:
return py::tuple();
case 1:
return py::make_tuple(el(v, 0));
case 2:
return py::make_tuple(el(v, 0), el(v, 1));
case 3:
return py::make_tuple(el(v, 0), el(v, 1), el(v, 2));
case 4:
return py::make_tuple(el(v, 0), el(v, 1), el(v, 2), el(v, 3));
case 5:
return py::make_tuple(el(v, 0), el(v, 1), el(v, 2), el(v, 3), el(v, 4));
case 6:
return py::make_tuple(el(v, 0), el(v, 1), el(v, 2), el(v, 3), el(v, 4),
el(v, 5));
case 7:
return py::make_tuple(el(v, 0), el(v, 1), el(v, 2), el(v, 3), el(v, 4),
el(v, 5), el(v, 6));
default:
auto tpl = py::make_tuple(el(v, 0), el(v, 1), el(v, 2), el(v, 3), el(v, 4),
el(v, 5), el(v, 6), el(v, 7));
for (auto i = 8ul; i < n; ++i) {
tpl += py::make_tuple(el(v, i));
}
return tpl;
}
}
template <typename T> py::tuple _make_tuple(const std::vector<T> &v) {
using V = std::vector<T>;
return _make_tuple(
v, [](const V &v) { return v.size(); },
[](const V &v, int i) { return v[i]; });
}
template <typename T> py::tuple _make_tuple(const T ptr, size_t n) {
return _make_tuple(
ptr, [n](const T &) { return n; },
[](const T &v, int i) { return v[i]; });
}
template <typename T> T to_native(const py::object &o) { return o.cast<T>(); }
inline void compute_slice(const py::slice &slc, uint64_t length,
int64_t &offset, int64_t &size, int64_t &stride) {
int64_t stop = 0;
slc.compute(length, &offset, &stop, &stride, &size);
}
#if 0
inline py::tuple _make_tuple(const NDSlice & v)
{
using V = NDSlice;
return _make_tuple(v, [](const V & v){return v.ndims();}, [](const V & v, int i){return v.dim(i).pyslice();});
}
#endif
} // namespace SHARPY