Skip to content

Commit 499163d

Browse files
committed
works now
1 parent 1b4c800 commit 499163d

File tree

1 file changed

+52
-0
lines changed
  • quaddtype/numpy_quaddtype/src

1 file changed

+52
-0
lines changed

quaddtype/numpy_quaddtype/src/dtype.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ common_instance(QuadPrecDTypeObject *dtype1, QuadPrecDTypeObject *dtype2)
9797
static PyArray_DTypeMeta *
9898
common_dtype(PyArray_DTypeMeta *cls, PyArray_DTypeMeta *other)
9999
{
100+
// Handle Python abstract dtypes (PyLongDType, PyFloatDType)
101+
// These have type_num = -1
102+
if (other == &PyArray_PyLongDType || other == &PyArray_PyFloatDType) {
103+
Py_INCREF(cls);
104+
return cls;
105+
}
106+
100107
// Promote integer and floating-point types to QuadPrecDType
101108
if (other->type_num >= 0 &&
102109
(PyTypeNum_ISINTEGER(other->type_num) || PyTypeNum_ISFLOAT(other->type_num))) {
@@ -261,6 +268,50 @@ quadprec_get_constant(PyArray_Descr *descr, int constant_id, void *ptr)
261268
return 1;
262269
}
263270

271+
/*
272+
* Fill function.
273+
* The buffer already has the first two elements set:
274+
* buffer[0] = start
275+
* buffer[1] = start + step
276+
* We need to fill buffer[2..length-1] with the arithmetic progression.
277+
*/
278+
static int
279+
quadprec_fill(void *buffer, npy_intp length, void *arr_)
280+
{
281+
PyArrayObject *arr = (PyArrayObject *)arr_;
282+
QuadPrecDTypeObject *descr = (QuadPrecDTypeObject *)PyArray_DESCR(arr);
283+
QuadBackendType backend = descr->backend;
284+
npy_intp i;
285+
286+
if (length < 2) {
287+
return 0; // Nothing to fill
288+
}
289+
290+
if (backend == BACKEND_SLEEF) {
291+
Sleef_quad *buf = (Sleef_quad *)buffer;
292+
Sleef_quad start = buf[0];
293+
Sleef_quad delta = Sleef_subq1_u05(buf[1], start); // delta = buf[1] - start
294+
295+
for (i = 2; i < length; ++i) {
296+
// buf[i] = start + i * delta
297+
Sleef_quad i_quad = Sleef_cast_from_doubleq1(i);
298+
Sleef_quad i_delta = Sleef_mulq1_u05(i_quad, delta);
299+
buf[i] = Sleef_addq1_u05(start, i_delta);
300+
}
301+
}
302+
else {
303+
long double *buf = (long double *)buffer;
304+
long double start = buf[0];
305+
long double delta = buf[1] - start;
306+
307+
for (i = 2; i < length; ++i) {
308+
buf[i] = start + i * delta;
309+
}
310+
}
311+
312+
return 0;
313+
}
314+
264315
static PyType_Slot QuadPrecDType_Slots[] = {
265316
{NPY_DT_ensure_canonical, &ensure_canonical},
266317
{NPY_DT_common_instance, &common_instance},
@@ -270,6 +321,7 @@ static PyType_Slot QuadPrecDType_Slots[] = {
270321
{NPY_DT_getitem, &quadprec_getitem},
271322
{NPY_DT_default_descr, &quadprec_default_descr},
272323
{NPY_DT_get_constant, &quadprec_get_constant},
324+
{NPY_DT_PyArray_ArrFuncs_fill, &quadprec_fill},
273325
{0, NULL}};
274326

275327
static PyObject *

0 commit comments

Comments
 (0)