From 6a1057cd7233223313a77e3698768f420636c6f0 Mon Sep 17 00:00:00 2001 From: SwayamInSync Date: Tue, 9 Sep 2025 14:35:03 +0000 Subject: [PATCH 1/2] void->quad gives error than segfault --- quaddtype/numpy_quaddtype/src/casts.cpp | 40 ++++++++++++++++++++++++- quaddtype/reinstall.sh | 3 +- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/quaddtype/numpy_quaddtype/src/casts.cpp b/quaddtype/numpy_quaddtype/src/casts.cpp index c662b4d5..8ae6e153 100644 --- a/quaddtype/numpy_quaddtype/src/casts.cpp +++ b/quaddtype/numpy_quaddtype/src/casts.cpp @@ -20,7 +20,7 @@ extern "C" { #include "casts.h" #include "dtype.h" -#define NUM_CASTS 29 // 14 to_casts + 14 from_casts + 1 quad_to_quad +#define NUM_CASTS 30 // 14 to_casts + 14 from_casts + 1 quad_to_quad + 1 void_to_quad static NPY_CASTING quad_to_quad_resolve_descriptors(PyObject *NPY_UNUSED(self), @@ -717,6 +717,26 @@ add_cast_to(PyArray_DTypeMeta *from) add_spec(spec); } +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, + "Cannot cast void dtype to QuadPrecDType. " + "Void arrays contain arbitrary binary data that cannot be meaningfully converted to quad-precision floats."); + 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; +} + PyArrayMethod_Spec ** init_casts_internal(void) { @@ -739,6 +759,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(&PyArray_BoolDType); add_cast_to(&PyArray_ByteDType); add_cast_to(&PyArray_ShortDType); diff --git a/quaddtype/reinstall.sh b/quaddtype/reinstall.sh index b5e04876..5a028f1c 100755 --- a/quaddtype/reinstall.sh +++ b/quaddtype/reinstall.sh @@ -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 \ No newline at end of file From 9131f140411d47517ab9e94752d148d81700194b Mon Sep 17 00:00:00 2001 From: SwayamInSync Date: Tue, 9 Sep 2025 17:03:32 +0000 Subject: [PATCH 2/2] updating branch --- quaddtype/numpy_quaddtype/src/casts.cpp | 42 ++++++++++++------------- quaddtype/tests/test_quaddtype.py | 13 ++++---- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/quaddtype/numpy_quaddtype/src/casts.cpp b/quaddtype/numpy_quaddtype/src/casts.cpp index fa84cb75..bfd42966 100644 --- a/quaddtype/numpy_quaddtype/src/casts.cpp +++ b/quaddtype/numpy_quaddtype/src/casts.cpp @@ -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; +} + + // 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 {}; @@ -783,26 +804,6 @@ add_cast_to(PyArray_DTypeMeta *from) add_spec(spec); } -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, - "Cannot cast void dtype to QuadPrecDType. " - "Void arrays contain arbitrary binary data that cannot be meaningfully converted to quad-precision floats."); - 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; -} - PyArrayMethod_Spec ** init_casts_internal(void) { @@ -843,7 +844,6 @@ init_casts_internal(void) }; add_spec(void_spec); - add_cast_to(&PyArray_BoolDType); add_cast_to(&PyArray_BoolDType); add_cast_to(&PyArray_ByteDType); add_cast_to(&PyArray_UByteDType); diff --git a/quaddtype/tests/test_quaddtype.py b/quaddtype/tests/test_quaddtype.py index 1319ac86..a761df14 100644 --- a/quaddtype/tests/test_quaddtype.py +++ b/quaddtype/tests/test_quaddtype.py @@ -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():