Skip to content

Commit ccfc64a

Browse files
committed
initial commit, adding Float128 dtype
1 parent 70626c3 commit ccfc64a

File tree

7 files changed

+205
-0
lines changed

7 files changed

+205
-0
lines changed

quaddtype2/README.md

Whitespace-only changes.

quaddtype2/pyproject.toml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[build-system]
2+
requires = [
3+
"meson>=1.3.2",
4+
"meson-python",
5+
"patchelf",
6+
"wheel",
7+
"numpy"
8+
]
9+
build-backend = "mesonpy"
10+
11+
[project]
12+
name = "quaddtype"
13+
description = "Quad (128-bit) float dtype for numpy"
14+
version = "0.0.1"
15+
readme = 'README.md'
16+
author = "Swayam Singh"
17+
requires-python = ">=3.9.0"
18+
dependencies = [
19+
"numpy"
20+
]
21+
22+
[project.optional-dependencies]
23+
test = [
24+
"pytest",
25+
]

quaddtype2/quaddtype/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .scalar import QuadScalar
2+
from ._quaddtype_main import QuadDType
3+
4+
__all__ = ["QuadScalar", "QuadDType"]

quaddtype2/quaddtype/scalar.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""Quad scalar floating point type for numpy."""
2+
3+
4+
class QuadScalar:
5+
"""Quad scalar floating point type."""
6+
7+
def __init__(self, value):
8+
self.value = value
9+
10+
def __repr__(self):
11+
return f"{self.value}"

quaddtype2/quaddtype/src/dtype.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include <Python.h>
2+
3+
#define PY_ARRAY_UNIQUE_SYMBOL quaddtype_ARRAY_API
4+
#define PY_UFUNC_UNIQUE_SYMBOL quaddtype_UFUNC_API
5+
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
6+
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
7+
#define NO_IMPORT_ARRAY
8+
#define NO_IMPORT_UFUNC
9+
#include "numpy/ndarraytypes.h"
10+
#include "numpy/arrayobject.h"
11+
#include "numpy/dtype_api.h"
12+
13+
#include "dtype.h"
14+
15+
16+
PyTypeObject *QuadScalar_Type = NULL;
17+
18+
QuadDTypeObject *new_quaddtype_instance(void)
19+
{
20+
QuadDTypeObject *new =
21+
(QuadDTypeObject *)PyArrayDescr_Type.tp_new((PyTypeObject *)&QuadDType, NULL, NULL);
22+
if (new == NULL) {
23+
return NULL;
24+
}
25+
26+
new->base.elsize = sizeof(__float128);
27+
new->base.alignment = _Alignof(__float128);
28+
return new;
29+
}
30+
31+
static PyObject *quaddtype_new(PyTypeObject *NPY_UNUSED(cls), PyObject *args, PyObject *kwargs)
32+
{
33+
return (PyObject *)new_quaddtype_instance();
34+
}
35+
36+
static void quaddtype_dealloc(QuadDTypeObject *self)
37+
{
38+
PyArrayDescr_Type.tp_dealloc((PyObject *)self);
39+
}
40+
41+
static PyObject *quaddtype_repr(QuadDTypeObject *self)
42+
{
43+
PyObject *res = PyUnicode_FromString("This is a quad (128-bit float) dtype.");
44+
return res;
45+
}
46+
47+
PyArray_DTypeMeta QuadDType = {
48+
{{
49+
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "quaddtype.QuadDType",
50+
.tp_basicsize = sizeof(QuadDTypeObject),
51+
.tp_new = quaddtype_new,
52+
.tp_dealloc = (destructor)quaddtype_dealloc,
53+
.tp_repr = (reprfunc)quaddtype_repr,
54+
.tp_str = (reprfunc)quaddtype_repr,
55+
}},
56+
};
57+
58+
int init_quad_dtype(void)
59+
{
60+
PyArrayMethod_Spec *casts[] = {
61+
NULL,
62+
};
63+
64+
PyArrayDTypeMeta_Spec QuadDType_DTypeSpec = {
65+
.flags = NPY_DT_NUMERIC,
66+
.casts = casts,
67+
.typeobj = QuadScalar_Type,
68+
.slots = NULL,
69+
};
70+
71+
((PyObject *)&QuadDType)->ob_type = &PyArrayDTypeMeta_Type;
72+
((PyTypeObject *)&QuadDType)->tp_base = &PyArrayDescr_Type;
73+
if (PyType_Ready((PyTypeObject *)&QuadDType) < 0) {
74+
return -1;
75+
}
76+
77+
if (PyArrayInitDTypeMeta_FromSpec(&QuadDType, &QuadDType_DTypeSpec) < 0) {
78+
return -1;
79+
}
80+
81+
QuadDType.singleton = PyArray_GetDefaultDescr(&QuadDType);
82+
return 0;
83+
}
84+
85+
86+
87+

quaddtype2/quaddtype/src/dtype.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef _NPY_DTYPE_H
2+
#define _NPY_DTYPE_H
3+
4+
#include<Python.h>
5+
#include<numpy/ndarraytypes.h>
6+
#include<numpy/dtype_api.h>
7+
8+
typedef struct
9+
{
10+
PyArray_Descr base;
11+
12+
} QuadDTypeObject;
13+
14+
extern PyArray_DTypeMeta QuadDType;
15+
extern PyTypeObject *QuadScalar_Type;
16+
17+
QuadDTypeObject * new_quaddtype_instance(void);
18+
19+
int init_quad_dtype(void);
20+
21+
#endif
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <Python.h>
2+
3+
#define PY_ARRAY_UNIQUE_SYMBOL quaddtype_ARRAY_API
4+
#define PY_UFUNC_UNIQUE_SYMBOL quaddtype_UFUNC_API
5+
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
6+
#include "numpy/arrayobject.h"
7+
#include "numpy/dtype_api.h"
8+
9+
#include "dtype.h"
10+
11+
static struct PyModuleDef moduledef = {
12+
PyModuleDef_HEAD_INIT,
13+
.m_name = "quaddtype_main",
14+
.m_doc = "Quad (128-bit) floating point Data Type for Numpy",
15+
.m_size = -1,
16+
};
17+
18+
PyMODINIT_FUNC
19+
PyInit__quaddtype_main(void)
20+
{
21+
import_array();
22+
23+
PyObject *m = PyModule_Create(&moduledef);
24+
if (m == NULL) {
25+
PyErr_SetString(PyExc_ImportError, "Unable to create the quaddtype_main module.");
26+
return NULL;
27+
}
28+
29+
PyObject *mod = PyImport_ImportModule("quaddtype");
30+
if (mod == NULL) {
31+
PyErr_SetString(PyExc_ImportError, "Unable to import the quaddtype module.");
32+
goto error;
33+
}
34+
QuadScalar_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "QuadScalar");
35+
Py_DECREF(mod);
36+
if (QuadScalar_Type == NULL) {
37+
PyErr_SetString(PyExc_AttributeError,
38+
"Unable to find QuadScalar attribute in the "
39+
"quaddtype_main module.");
40+
goto error;
41+
}
42+
if (init_quad_dtype() < 0) {
43+
PyErr_SetString(PyExc_AttributeError, "QuadDType failed to initialize.");
44+
goto error;
45+
}
46+
if (PyModule_AddObject(m, "QuadDType", (PyObject *)&QuadDType) < 0) {
47+
PyErr_SetString(PyExc_TypeError, "Failed to add QuadDType to the quaddtype_main module.");
48+
goto error;
49+
}
50+
51+
52+
return m;
53+
54+
error:
55+
Py_DECREF(m);
56+
return NULL;
57+
}

0 commit comments

Comments
 (0)