forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoubleBitMask_ext.I
More file actions
81 lines (74 loc) · 2.31 KB
/
doubleBitMask_ext.I
File metadata and controls
81 lines (74 loc) · 2.31 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
/**
* PANDA 3D SOFTWARE
* Copyright (c) Carnegie Mellon University. All rights reserved.
*
* All use of this software is subject to the terms of the revised BSD
* license. You should have received a copy of this license along
* with this source code in a file named "LICENSE."
*
* @file doubleBitMask_ext.I
* @author rdb
* @date 2020-04-01
*/
/**
* Initializes a DoubleBitMask from a Python long integer.
*/
template<class BMType>
INLINE void Extension<DoubleBitMask<BMType> >::
__init__(PyObject *init_value) {
if (!PyLong_Check(init_value) || Py_SIZE(init_value) < 0) {
PyErr_SetString(PyExc_ValueError, "DoubleBitMask constructor requires a positive integer");
return;
}
int n = _PyLong_NumBits(init_value);
if (n > DoubleBitMask<BMType>::num_bits) {
PyErr_SetString(PyExc_OverflowError, "value out of range for DoubleBitMask");
return;
}
if (n > 0) {
size_t num_bytes = (n + 7) / 8;
unsigned char *bytes = (unsigned char *)alloca(num_bytes);
_PyLong_AsByteArray((PyLongObject *)init_value, bytes, num_bytes, 1, 0);
for (size_t i = 0; i < num_bytes; ++i) {
this->_this->store(bytes[i], i * 8, 8);
}
}
}
/**
* Returns true if the value is not zero.
*/
template<class BMType>
INLINE bool Extension<DoubleBitMask<BMType> >::
__bool__() const {
return !this->_this->is_zero();
}
/**
* Returns the value as an integer.
*/
template<class BMType>
INLINE PyObject *Extension<DoubleBitMask<BMType> >::
__int__() const {
PyObject *result = invoke_extension(&this->_this->_lo).__int__();
if (!this->_this->_hi.is_zero()) {
PyObject *lo = result;
PyObject *hi = invoke_extension(&this->_this->_hi).__int__();
PyObject *shifted = _PyLong_Lshift(hi, DoubleBitMask<BMType>::half_bits);
Py_DECREF(hi);
result = PyNumber_Or(shifted, lo);
Py_DECREF(shifted);
Py_DECREF(lo);
}
return result;
}
/**
* This special Python method is implemented to provide support for the pickle
* module.
*/
template<class BMType>
INLINE PyObject *Extension<DoubleBitMask<BMType> >::
__reduce__(PyObject *self) const {
// We should return at least a 2-tuple, (Class, (args)): the necessary class
// object whose constructor we should call (e.g. this), and the arguments
// necessary to reconstruct this object.
return Py_BuildValue("(O(N))", Py_TYPE(self), __int__());
}