Skip to content
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion quaddtype/numpy_quaddtype/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
get_quadblas_version
)

from dataclasses import dataclass

__all__ = [
'QuadPrecision', 'QuadPrecDType', 'SleefQuadPrecision', 'LongDoubleQuadPrecision',
'SleefQuadPrecDType', 'LongDoubleQuadPrecDType', 'is_longdouble_128',
# Constants
'pi', 'e', 'log2e', 'log10e', 'ln2', 'ln10', 'max_value', 'epsilon',
'smallest_normal', 'smallest_subnormal', 'bits', 'precision', 'resolution',
# QuadBLAS related functions
'set_num_threads', 'get_num_threads', 'get_quadblas_version'
'set_num_threads', 'get_num_threads', 'get_quadblas_version',
# finfo class
'QuadPrecFinfo'
]

def SleefQuadPrecision(value):
Expand Down Expand Up @@ -43,3 +47,39 @@ def LongDoubleQuadPrecDType():
bits = get_sleef_constant("bits")
precision = get_sleef_constant("precision")
resolution = get_sleef_constant("resolution")

@dataclass
class QuadPrecFinfo:
"""Floating-point information for quadruple precision dtype.

This class provides information about the floating-point representation
used by the QuadPrecDType, similar to numpy.finfo but customized for
quad precision arithmetic.
"""
bits: int = int(bits)
eps: float = float(epsilon)
epsneg: float = float(epsilon)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong - it should 1 - nextafter(1, 0) [similar to how exp is nextafter(1, 2) - 1], we can probably compute this in the C++ source and then expose it as a new constant

iexp: int = int(precision)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iexp should be 15, I think?

machar: object = None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this property?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is just an object finfo uses to fetch the finfo properties for dtypes, since we are defining them in our package hence marking it as None

machep: float = float(epsilon)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

machep should be int(log2(epsilon))?

max: float = float(max_value)
maxexp: float = float(max_value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would probably be int(ceil(max_value))?

min: float = float(smallest_normal)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

min = -max_value

minexp: float = float(smallest_normal)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure how to compute this one

negep: float = float(epsilon)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int(log2(epsneg))

nexp: int = int(bits) - int(precision) - 1
Copy link
Contributor

@juntyr juntyr Sep 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe 15 + 1? not sure

nmant: int = int(precision)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

113 or 112

precision: int = int(precision)
resolution: float = float(resolution)
tiny: float = float(smallest_normal)
smallest_normal: float = float(smallest_normal)
smallest_subnormal: float = float(smallest_subnormal)

def get(self, attr):
return getattr(self, attr, None)

def __str__(self):
return f"QuadPrecFinfo(precision={self.precision}, resolution={self.resolution})"

def __repr__(self):
return f"QuadPrecFinfo(max={self.max}, min={self.min}, eps={self.eps}, bits={self.bits})"
29 changes: 29 additions & 0 deletions quaddtype/numpy_quaddtype/src/dtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,42 @@ QuadPrecDType_str(QuadPrecDTypeObject *self)
return PyUnicode_FromFormat("QuadPrecDType(backend='%s')", backend_str);
}

static PyObject *
QuadPrecDType_finfo(QuadPrecDTypeObject *self, PyObject *args)
{
PyObject *numpy_quaddtype_module = PyImport_ImportModule("numpy_quaddtype");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like doing this as well, but for now it works so

if (numpy_quaddtype_module == NULL) {
return NULL;
}

PyObject *finfo_class = PyObject_GetAttrString(numpy_quaddtype_module, "QuadPrecFinfo");
Py_DECREF(numpy_quaddtype_module);

if (finfo_class == NULL) {
PyErr_SetString(PyExc_AttributeError, "Could not find QuadPrecFinfo class in numpy_quaddtype module.");
return NULL;
}

PyObject *finfo_instance = PyObject_CallNoArgs(finfo_class);
Py_DECREF(finfo_class);

return finfo_instance;
}

static PyMethodDef QuadPrecDType_methods[] = {
{"finfo", (PyCFunction)QuadPrecDType_finfo, METH_NOARGS,
"Return floating-point information for this QuadPrecDType"},
{NULL, NULL, 0, NULL}
};

PyArray_DTypeMeta QuadPrecDType = {
{{
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "numpy_quaddtype.QuadPrecDType",
.tp_basicsize = sizeof(QuadPrecDTypeObject),
.tp_new = QuadPrecDType_new,
.tp_repr = (reprfunc)QuadPrecDType_repr,
.tp_str = (reprfunc)QuadPrecDType_str,
.tp_methods = QuadPrecDType_methods,
}},
};

Expand Down
3 changes: 2 additions & 1 deletion quaddtype/numpy_quaddtype/src/scalar.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ QuadPrecision_dealloc(QuadPrecisionObject *self)
}

PyTypeObject QuadPrecision_Type = {
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "numpy_quaddtype.QuadPrecision",
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "numpy_quaddtype.QuadPrecDType",
.tp_basicsize = sizeof(QuadPrecisionObject),
.tp_itemsize = 0,
.tp_new = QuadPrecision_new,
Expand All @@ -253,5 +253,6 @@ PyTypeObject QuadPrecision_Type = {
int
init_quadprecision_scalar(void)
{
QuadPrecision_Type.tp_base = &PyFloatingArrType_Type;
return PyType_Ready(&QuadPrecision_Type);
}
6 changes: 3 additions & 3 deletions quaddtype/reinstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if [ -d "build/" ]; then
rm -rf subprojects/sleef
fi

export CFLAGS="-g -O0"
export CXXFLAGS="-g -O0"
# export CFLAGS="-g -O0"
# export CXXFLAGS="-g -O0"
python -m pip uninstall -y numpy_quaddtype
python -m pip install . -v
python -m pip install . --no-build-isolation -v 2>&1 | tee build_log.txt
Loading