Skip to content

Commit e53edba

Browse files
committed
resolved reviews
1 parent 0052e16 commit e53edba

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

quaddtype/numpy_quaddtype/src/dragon4.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
This code was extracted from NumPy and the original author was Allan Haldane(@ahaldane)
3+
Modifications are specific to support the SLEEF_QUAD
4+
*/
5+
16
#include <numpy/npy_common.h>
27
#include <math.h>
38
#include <stdio.h>

quaddtype/numpy_quaddtype/src/dtype.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,15 @@ ensure_canonical(QuadPrecDTypeObject *self)
7979
static QuadPrecDTypeObject *
8080
common_instance(QuadPrecDTypeObject *dtype1, QuadPrecDTypeObject *dtype2)
8181
{
82+
// if backend mismatch then return SLEEF one (safe to cast ld to quad)
8283
if (dtype1->backend != dtype2->backend) {
83-
PyErr_SetString(PyExc_TypeError,
84-
"Cannot find common instance for QuadPrecDTypes with different backends");
85-
return NULL;
84+
if (dtype1->backend == BACKEND_SLEEF) {
85+
Py_INCREF(dtype1);
86+
return dtype1;
87+
}
88+
89+
Py_INCREF(dtype2);
90+
return dtype2;
8691
}
8792
Py_INCREF(dtype1);
8893
return dtype1;

quaddtype/numpy_quaddtype/src/scalar.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,39 +76,42 @@ QuadPrecision_from_object(PyObject *value, QuadBackendType backend)
7676
self->value.longdouble_value = (long double)val;
7777
}
7878
}
79-
else if (Py_TYPE(value) == &QuadPrecision_Type)
80-
{
81-
// todo: not working for ld backend, getting garbage value not sure why?
79+
else if (Py_TYPE(value) == &QuadPrecision_Type) {
80+
Py_DECREF(self); // discard the default one
8281
QuadPrecisionObject *quad_obj = (QuadPrecisionObject *)value;
83-
// printf("%d %d\n", quad_obj->backend, backend);
84-
// printf("%Lf\n", quad_obj->value.longdouble_value);
82+
83+
// create a new one with the same backend
84+
QuadPrecisionObject *self = QuadPrecision_raw_new(quad_obj->backend);
8585
if (quad_obj->backend == BACKEND_SLEEF) {
8686
self->value.sleef_value = quad_obj->value.sleef_value;
8787
}
8888
else {
8989
self->value.longdouble_value = quad_obj->value.longdouble_value;
9090
}
91+
92+
return self;
9193
}
9294
else {
9395
PyObject *type_str = PyObject_Str((PyObject *)Py_TYPE(value));
9496
if (type_str != NULL) {
9597
const char *type_cstr = PyUnicode_AsUTF8(type_str);
9698
if (type_cstr != NULL) {
97-
PyErr_Format(
98-
PyExc_TypeError,
99-
"QuadPrecision value must be a float, int or string, but got %s instead",
100-
type_cstr);
99+
PyErr_Format(PyExc_TypeError,
100+
"QuadPrecision value must be a quad, float, int or string, but got %s "
101+
"instead",
102+
type_cstr);
101103
}
102104
else {
103-
PyErr_SetString(PyExc_TypeError,
104-
"QuadPrecision value must be a float, int or string, but got an "
105-
"unknown type instead");
105+
PyErr_SetString(
106+
PyExc_TypeError,
107+
"QuadPrecision value must be a quad, float, int or string, but got an "
108+
"unknown type instead");
106109
}
107110
Py_DECREF(type_str);
108111
}
109112
else {
110113
PyErr_SetString(PyExc_TypeError,
111-
"QuadPrecision value must be a float, int or string, but got an "
114+
"QuadPrecision value must be a quad, float, int or string, but got an "
112115
"unknown type instead");
113116
}
114117
Py_DECREF(self);

0 commit comments

Comments
 (0)