Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
41 changes: 40 additions & 1 deletion quaddtype/numpy_quaddtype/src/casts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extern "C" {
#include "casts.h"
#include "dtype.h"

#define NUM_CASTS 33 // 16 to_casts + 16 from_casts + 1 quad_to_quad
#define NUM_CASTS 34 // 16 to_casts + 16 from_casts + 1 quad_to_quad + 1 void_to_quad

static NPY_CASTING
quad_to_quad_resolve_descriptors(PyObject *NPY_UNUSED(self),
Expand Down Expand Up @@ -151,6 +151,27 @@ quad_to_quad_strided_loop_aligned(PyArrayMethod_Context *context, char *const da
return 0;
}


static NPY_CASTING
void_to_quad_resolve_descriptors(PyObject *NPY_UNUSED(self), PyArray_DTypeMeta *dtypes[2],
PyArray_Descr *given_descrs[2], PyArray_Descr *loop_descrs[2],
npy_intp *view_offset)
{
PyErr_SetString(PyExc_TypeError,
"Void to QuadPrecision cast is not implemented");
return (NPY_CASTING)-1;
}

static int
void_to_quad_strided_loop(PyArrayMethod_Context *context, char *const data[],
npy_intp const dimensions[], npy_intp const strides[],
void *NPY_UNUSED(auxdata))
{
PyErr_SetString(PyExc_RuntimeError, "void_to_quad_strided_loop should not be called");
return -1;
Copy link
Member

Choose a reason for hiding this comment

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

We could probably also do something similar in NumPy, but this is fine for now.

}


// Tag dispatching to ensure npy_bool/npy_ubyte and npy_half/npy_ushort do not alias in templates
// see e.g. https://stackoverflow.com/q/32522279
struct spec_npy_bool {};
Expand Down Expand Up @@ -805,6 +826,24 @@ init_casts_internal(void)

add_spec(quad2quad_spec);

PyArray_DTypeMeta **void_dtypes = new PyArray_DTypeMeta *[2]{&PyArray_VoidDType, &QuadPrecDType};
PyType_Slot *void_slots = new PyType_Slot[]{
{NPY_METH_resolve_descriptors, (void *)&void_to_quad_resolve_descriptors},
{NPY_METH_strided_loop, (void *)&void_to_quad_strided_loop},
{NPY_METH_unaligned_strided_loop, (void *)&void_to_quad_strided_loop},
{0, nullptr}};

PyArrayMethod_Spec *void_spec = new PyArrayMethod_Spec{
.name = "cast_Void_to_QuadPrec_ERROR",
.nin = 1,
.nout = 1,
.casting = NPY_UNSAFE_CASTING,
.flags = NPY_METH_SUPPORTS_UNALIGNED,
.dtypes = void_dtypes,
.slots = void_slots,
};
add_spec(void_spec);

add_cast_to<spec_npy_bool>(&PyArray_BoolDType);
add_cast_to<npy_byte>(&PyArray_ByteDType);
add_cast_to<npy_ubyte>(&PyArray_UByteDType);
Expand Down
3 changes: 2 additions & 1 deletion quaddtype/reinstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ if [ -d "build/" ]; then
rm -rf subprojects/sleef
fi


export CFLAGS="-g -O0"
export CXXFLAGS="-g -O0"
python -m pip uninstall -y numpy_quaddtype
python -m pip install . -v
13 changes: 7 additions & 6 deletions quaddtype/tests/test_quaddtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ def test_supported_astype(dtype):
@pytest.mark.parametrize("dtype", ["S10", "U10", "T", "V10", "datetime64[ms]", "timedelta64[ms]"])
def test_unsupported_astype(dtype):
if dtype == "V10":
pytest.xfail("casts to and from V10 segfault")

with pytest.raises(TypeError, match="cast"):
np.array(1, dtype=dtype).astype(QuadPrecDType, casting="unsafe")
with pytest.raises(TypeError, match="cast"):
np.ones((3, 3), dtype="V10").astype(QuadPrecDType, casting="unsafe")
else:
with pytest.raises(TypeError, match="cast"):
np.array(1, dtype=dtype).astype(QuadPrecDType, casting="unsafe")

with pytest.raises(TypeError, match="cast"):
np.array(QuadPrecision(1)).astype(dtype, casting="unsafe")
with pytest.raises(TypeError, match="cast"):
np.array(QuadPrecision(1)).astype(dtype, casting="unsafe")


def test_basic_equality():
Expand Down
Loading