]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/cpp/src/arrow/python/numpy_interop.h
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / cpp / src / arrow / python / numpy_interop.h
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17
18 #pragma once
19
20 #include "arrow/python/platform.h" // IWYU pragma: export
21
22 #include <numpy/numpyconfig.h> // IWYU pragma: export
23
24 // Don't use the deprecated Numpy functions
25 #ifdef NPY_1_7_API_VERSION
26 #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
27 #else
28 #define NPY_ARRAY_NOTSWAPPED NPY_NOTSWAPPED
29 #define NPY_ARRAY_ALIGNED NPY_ALIGNED
30 #define NPY_ARRAY_WRITEABLE NPY_WRITEABLE
31 #define NPY_ARRAY_UPDATEIFCOPY NPY_UPDATEIFCOPY
32 #endif
33
34 // This is required to be able to access the NumPy C API properly in C++ files
35 // other than init.cc.
36 #define PY_ARRAY_UNIQUE_SYMBOL arrow_ARRAY_API
37 #ifndef NUMPY_IMPORT_ARRAY
38 #define NO_IMPORT_ARRAY
39 #endif
40
41 #include <numpy/arrayobject.h> // IWYU pragma: export
42 #include <numpy/arrayscalars.h> // IWYU pragma: export
43 #include <numpy/ufuncobject.h> // IWYU pragma: export
44
45 // A bit subtle. Numpy has 5 canonical integer types:
46 // (or, rather, type pairs: signed and unsigned)
47 // NPY_BYTE, NPY_SHORT, NPY_INT, NPY_LONG, NPY_LONGLONG
48 // It also has 4 fixed-width integer aliases.
49 // When mapping Arrow integer types to these 4 fixed-width aliases,
50 // we always miss one of the canonical types (even though it may
51 // have the same width as one of the aliases).
52 // Which one depends on the platform...
53 // On a LP64 system, NPY_INT64 maps to NPY_LONG and
54 // NPY_LONGLONG needs to be handled separately.
55 // On a LLP64 system, NPY_INT32 maps to NPY_LONG and
56 // NPY_INT needs to be handled separately.
57
58 #if NPY_BITSOF_LONG == 32 && NPY_BITSOF_LONGLONG == 64
59 #define NPY_INT64_IS_LONG_LONG 1
60 #else
61 #define NPY_INT64_IS_LONG_LONG 0
62 #endif
63
64 #if NPY_BITSOF_INT == 32 && NPY_BITSOF_LONG == 64
65 #define NPY_INT32_IS_INT 1
66 #else
67 #define NPY_INT32_IS_INT 0
68 #endif
69
70 namespace arrow {
71 namespace py {
72
73 inline int import_numpy() {
74 #ifdef NUMPY_IMPORT_ARRAY
75 import_array1(-1);
76 import_umath1(-1);
77 #endif
78
79 return 0;
80 }
81
82 // See above about the missing Numpy integer type numbers
83 inline int fix_numpy_type_num(int type_num) {
84 #if !NPY_INT32_IS_INT && NPY_BITSOF_INT == 32
85 if (type_num == NPY_INT) return NPY_INT32;
86 if (type_num == NPY_UINT) return NPY_UINT32;
87 #endif
88 #if !NPY_INT64_IS_LONG_LONG && NPY_BITSOF_LONGLONG == 64
89 if (type_num == NPY_LONGLONG) return NPY_INT64;
90 if (type_num == NPY_ULONGLONG) return NPY_UINT64;
91 #endif
92 return type_num;
93 }
94
95 } // namespace py
96 } // namespace arrow