Skip to content

Commit f2a4bec

Browse files
authored
Merge pull request #232 from SwayamInSync/hash
FEAT: Adding pickle support to quaddtype
2 parents 345b596 + 8bc56bf commit f2a4bec

File tree

5 files changed

+490
-6
lines changed

5 files changed

+490
-6
lines changed

quaddtype/numpy_quaddtype/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import enum
2+
13
from ._quaddtype_main import (
24
QuadPrecision,
35
QuadPrecDType,
@@ -10,8 +12,16 @@
1012

1113
__version__ = "0.2.0"
1214

15+
16+
class QuadBackend(enum.IntEnum):
17+
"""Backend type for QuadPrecision computations."""
18+
SLEEF = 0
19+
LONGDOUBLE = 1
20+
21+
1322
__all__ = [
14-
'QuadPrecision', 'QuadPrecDType', 'SleefQuadPrecision', 'LongDoubleQuadPrecision',
23+
'QuadPrecision', 'QuadPrecDType', 'QuadBackend',
24+
'SleefQuadPrecision', 'LongDoubleQuadPrecision',
1525
'SleefQuadPrecDType', 'LongDoubleQuadPrecDType', 'is_longdouble_128',
1626
# Constants
1727
'pi', 'e', 'log2e', 'log10e', 'ln2', 'ln10', 'max_value', 'epsilon',

quaddtype/numpy_quaddtype/__init__.pyi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import Final
2+
import enum
23

34
from ._quaddtype_main import (
45
QuadPrecDType,
@@ -10,9 +11,14 @@ from ._quaddtype_main import (
1011
set_num_threads,
1112
)
1213

14+
class QuadBackend(enum.IntEnum):
15+
SLEEF = 0
16+
LONGDOUBLE = 1
17+
1318
__all__ = [
1419
"QuadPrecision",
1520
"QuadPrecDType",
21+
"QuadBackend",
1622
"SleefQuadPrecision",
1723
"LongDoubleQuadPrecision",
1824
"SleefQuadPrecDType",

quaddtype/numpy_quaddtype/_quaddtype_main.pyi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import numpy as np
44
from numpy._typing import _128Bit # pyright: ignore[reportPrivateUsage]
55
from typing_extensions import Never, Self, override
66

7+
from numpy_quaddtype import QuadBackend
8+
79
_Backend: TypeAlias = Literal["sleef", "longdouble"]
810
_IntoQuad: TypeAlias = (
911
QuadPrecision
@@ -20,6 +22,10 @@ _ScalarItemArg: TypeAlias = Literal[0, -1] | tuple[Literal[0, -1]] | tuple[()]
2022
class QuadPrecDType(np.dtype[QuadPrecision]): # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
2123
def __new__(cls, /, backend: _Backend = "sleef") -> Self: ...
2224

25+
# QuadPrecDType specific attributes
26+
@property
27+
def backend(self) -> QuadBackend: ...
28+
2329
# `numpy.dtype` overrides
2430
names: None # pyright: ignore[reportIncompatibleVariableOverride]
2531
@property

quaddtype/numpy_quaddtype/src/dtype.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <Python.h>
2+
#include <structmember.h>
23
#include <sleef.h>
34
#include <sleefquad.h>
45
#include <ctype.h>
@@ -439,13 +440,45 @@ QuadPrecDType_str(QuadPrecDTypeObject *self)
439440
return PyUnicode_FromFormat("QuadPrecDType(backend='%s')", backend_str);
440441
}
441442

443+
444+
static PyObject *
445+
quaddtype__reduce__(QuadPrecDTypeObject *self, PyObject *NPY_UNUSED(args))
446+
{
447+
const char *backend_str = (self->backend == BACKEND_SLEEF) ? "sleef" : "longdouble";
448+
449+
/* Return (type(self), (backend_str,))
450+
* This will call type(self).__new__(type(self), backend_str) followed by __init__
451+
*/
452+
PyObject *result = Py_BuildValue("O(s)", Py_TYPE(self), backend_str);
453+
454+
return result;
455+
}
456+
457+
static PyMethodDef QuadPrecDType_methods[] = {
458+
{
459+
"__reduce__",
460+
(PyCFunction)quaddtype__reduce__,
461+
METH_NOARGS,
462+
"Reduction method for a QuadPrecDType object",
463+
},
464+
{NULL, NULL, 0, NULL},
465+
};
466+
467+
static PyMemberDef QuadPrecDType_members[] = {
468+
{"backend", T_INT, offsetof(QuadPrecDTypeObject, backend), READONLY,
469+
"The backend used for quad precision (0=sleef, 1=longdouble)"},
470+
{NULL, 0, 0, 0, NULL},
471+
};
472+
442473
PyArray_DTypeMeta QuadPrecDType = {
443474
{{
444475
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "numpy_quaddtype.QuadPrecDType",
445476
.tp_basicsize = sizeof(QuadPrecDTypeObject),
446477
.tp_new = QuadPrecDType_new,
447478
.tp_repr = (reprfunc)QuadPrecDType_repr,
448479
.tp_str = (reprfunc)QuadPrecDType_str,
480+
.tp_methods = QuadPrecDType_methods,
481+
.tp_members = QuadPrecDType_members,
449482
}},
450483
};
451484

0 commit comments

Comments
 (0)