Skip to content

Commit 01e9b7e

Browse files
committed
post-cleaning
1 parent e8cba6a commit 01e9b7e

File tree

7 files changed

+43
-312
lines changed

7 files changed

+43
-312
lines changed

quaddtype/meson.build

-56
This file was deleted.

quaddtype/numpy_quaddtype/__init__.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from ._quaddtype_main import (
22
QuadPrecision,
3-
QuadPrecDType
3+
QuadPrecDType,
4+
is_longdouble_128
45
)
56

67
__all__ = ['QuadPrecision', 'QuadPrecDType', 'SleefQuadPrecision', 'LongDoubleQuadPrecision',
7-
'SleefQuadPrecDType', 'LongDoubleQuadPrecDType']
8+
'SleefQuadPrecDType', 'LongDoubleQuadPrecDType', 'is_longdouble_128']
89

910

1011
def SleefQuadPrecision(value):

quaddtype/numpy_quaddtype/src/casts.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -272,15 +272,15 @@ numpy_to_quad_resolve_descriptors(PyObject *NPY_UNUSED(self), PyArray_DTypeMeta
272272
PyArray_Descr *given_descrs[2], PyArray_Descr *loop_descrs[2],
273273
npy_intp *view_offset)
274274
{
275-
printf("cast.cpp: numpy_to_quad_resolve_descriptors is called\n");
275+
276276
if (given_descrs[1] == NULL) {
277277
loop_descrs[1] = (PyArray_Descr *)new_quaddtype_instance(BACKEND_SLEEF);
278278
if (loop_descrs[1] == nullptr) {
279279
return (NPY_CASTING)-1;
280280
}
281281
}
282282
else {
283-
printf("cast.cpp: numpy_to_quad_resolve_descriptors, I am in ELSE condition\n");
283+
284284
Py_INCREF(given_descrs[1]);
285285
loop_descrs[1] = given_descrs[1];
286286
}
@@ -299,12 +299,8 @@ numpy_to_quad_strided_loop(PyArrayMethod_Context *context, char *const data[],
299299
char *in_ptr = data[0];
300300
char *out_ptr = data[1];
301301

302-
QuadPrecDTypeObject *descr_out1 = (QuadPrecDTypeObject *)context->descriptors[0];
303-
printf("The type of context->descriptor[0] is: %s\n", Py_TYPE(descr_out1)->tp_name);
304302
QuadPrecDTypeObject *descr_out = (QuadPrecDTypeObject *)context->descriptors[1];
305-
printf("The type of context->descriptor[1] is: %s\n", Py_TYPE(descr_out)->tp_name);
306303
QuadBackendType backend = descr_out->backend;
307-
printf("cast.cpp: numpy_to_quad_strided_loop with backend: %d\n", backend);
308304
size_t elem_size = (backend == BACKEND_SLEEF) ? sizeof(Sleef_quad) : sizeof(long double);
309305

310306
while (N--) {

quaddtype/numpy_quaddtype/src/dtype.c

+12-15
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ quad_store(char *data_ptr, void *x, QuadBackendType backend)
4949
QuadPrecDTypeObject *
5050
new_quaddtype_instance(QuadBackendType backend)
5151
{
52-
// if (backend != BACKEND_SLEEF && backend != BACKEND_LONGDOUBLE)
53-
// {
54-
// PyErr_SetString(PyExc_TypeError,
55-
// "Backend must be sleef or longdouble");
56-
// return NULL;
57-
// }
58-
printf("New Quandtype instance is created with backend: %d\n", backend);
52+
if (backend != BACKEND_SLEEF && backend != BACKEND_LONGDOUBLE)
53+
{
54+
PyErr_SetString(PyExc_TypeError,
55+
"Backend must be sleef or longdouble");
56+
return NULL;
57+
}
58+
5959
QuadPrecDTypeObject *new = (QuadPrecDTypeObject *)PyArrayDescr_Type.tp_new(
6060
(PyTypeObject *)&QuadPrecDType, NULL, NULL);
6161
if (new == NULL) {
@@ -70,15 +70,15 @@ new_quaddtype_instance(QuadBackendType backend)
7070
static QuadPrecDTypeObject *
7171
ensure_canonical(QuadPrecDTypeObject *self)
7272
{
73-
printf("Ensure Canonical is called\n");
73+
7474
Py_INCREF(self);
7575
return self;
7676
}
7777

7878
static QuadPrecDTypeObject *
7979
common_instance(QuadPrecDTypeObject *dtype1, QuadPrecDTypeObject *dtype2)
8080
{
81-
printf("Common Instance is called\n");
81+
8282
if (dtype1->backend != dtype2->backend) {
8383
PyErr_SetString(PyExc_TypeError,
8484
"Cannot find common instance for QuadPrecDTypes with different backends");
@@ -91,7 +91,7 @@ common_instance(QuadPrecDTypeObject *dtype1, QuadPrecDTypeObject *dtype2)
9191
static PyArray_DTypeMeta *
9292
common_dtype(PyArray_DTypeMeta *cls, PyArray_DTypeMeta *other)
9393
{
94-
printf("Common dtype is called\n");
94+
9595
// Promote integer and floating-point types to QuadPrecDType
9696
if (other->type_num >= 0 &&
9797
(PyTypeNum_ISINTEGER(other->type_num) || PyTypeNum_ISFLOAT(other->type_num))) {
@@ -117,7 +117,7 @@ quadprec_discover_descriptor_from_pyobject(PyArray_DTypeMeta *NPY_UNUSED(cls), P
117117
}
118118

119119
QuadPrecisionObject *quad_obj = (QuadPrecisionObject *)obj;
120-
printf("dtype.c: quadprec_discover_descriptor_from_pyobject is called with backend %d\n", quad_obj->backend);
120+
121121
return (PyArray_Descr *)new_quaddtype_instance(quad_obj->backend);
122122
}
123123

@@ -168,10 +168,7 @@ quadprec_getitem(QuadPrecDTypeObject *descr, char *dataptr)
168168
static PyArray_Descr *
169169
quadprec_default_descr(PyArray_DTypeMeta *cls)
170170
{
171-
QuadPrecDTypeObject * a = (QuadPrecDTypeObject *)cls;
172-
printf("Default descriptor called with backend: %d\n", a->backend);
173-
QuadPrecDTypeObject * temp = new_quaddtype_instance(a->backend);
174-
printf("Default descriptor made backend: %d\n", temp->backend);
171+
QuadPrecDTypeObject * temp = new_quaddtype_instance(BACKEND_SLEEF);
175172
return (PyArray_Descr *)temp;
176173
}
177174

quaddtype/numpy_quaddtype/src/quaddtype_main.c

+17
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,29 @@
1313
#include "dtype.h"
1414
#include "umath.h"
1515
#include "quad_common.h"
16+
#include "float.h"
17+
18+
static PyObject* py_is_longdouble_128(PyObject* self, PyObject* args) {
19+
if(sizeof(long double) == 16 &&
20+
LDBL_MANT_DIG == 113 &&
21+
LDBL_MAX_EXP == 16384) {
22+
Py_RETURN_TRUE;
23+
} else {
24+
Py_RETURN_FALSE;
25+
}
26+
}
27+
28+
static PyMethodDef module_methods[] = {
29+
{"is_longdouble_128", py_is_longdouble_128, METH_NOARGS, "Check if long double is 128-bit"},
30+
{NULL, NULL, 0, NULL}
31+
};
1632

1733
static struct PyModuleDef moduledef = {
1834
PyModuleDef_HEAD_INIT,
1935
.m_name = "_quaddtype_main",
2036
.m_doc = "Quad (128-bit) floating point Data Type for NumPy with multiple backends",
2137
.m_size = -1,
38+
.m_methods = module_methods
2239
};
2340

2441
PyMODINIT_FUNC

quaddtype/numpy_quaddtype/src/umath.cpp

+9-26
Original file line numberDiff line numberDiff line change
@@ -227,26 +227,21 @@ quad_binary_op_resolve_descriptors(PyObject *self, PyArray_DTypeMeta *const dtyp
227227
PyArray_Descr *const given_descrs[],
228228
PyArray_Descr *loop_descrs[], npy_intp *NPY_UNUSED(view_offset))
229229
{
230-
printf("Descriptor Resolver is called\n");
231230

232231
QuadPrecDTypeObject *descr_in1 = (QuadPrecDTypeObject *)given_descrs[0];
233232
QuadPrecDTypeObject *descr_in2 = (QuadPrecDTypeObject *)given_descrs[1];
234233
QuadBackendType target_backend;
235234

236235
const char *s1 = (descr_in1->backend == BACKEND_SLEEF) ? "SLEEF" : "LONGDOUBLE";
237236
const char *s2 = (descr_in2->backend == BACKEND_SLEEF) ? "SLEEF" : "LONGDOUBLE";
238-
printf("1: %s %d %s\n", s1, descr_in1->backend, Py_TYPE(given_descrs[0])->tp_name);
239-
printf("2: %s %d %s\n", s2, descr_in2->backend, Py_TYPE(given_descrs[1])->tp_name);
240237

241238
// Determine target backend and if casting is needed
242239
NPY_CASTING casting = NPY_NO_CASTING;
243240
if (descr_in1->backend != descr_in2->backend) {
244241
target_backend = BACKEND_LONGDOUBLE;
245242
casting = NPY_SAFE_CASTING;
246-
printf("Different backends detected. Casting to LONGDOUBLE.\n");
247243
} else {
248244
target_backend = descr_in1->backend;
249-
printf("Unified backend: %s\n", (target_backend == BACKEND_SLEEF) ? "SLEEF" : "LONGDOUBLE");
250245
}
251246

252247
// Set up input descriptors, casting if necessary
@@ -280,8 +275,6 @@ quad_binary_op_resolve_descriptors(PyObject *self, PyArray_DTypeMeta *const dtyp
280275
loop_descrs[2] = given_descrs[2];
281276
}
282277
}
283-
284-
printf("Casting result: %d\n", casting);
285278
return casting;
286279
}
287280

@@ -291,7 +284,6 @@ quad_generic_binop_strided_loop(PyArrayMethod_Context *context, char *const data
291284
npy_intp const dimensions[], npy_intp const strides[],
292285
NpyAuxData *auxdata)
293286
{
294-
printf("Umath: Generic Strided loop is calledn\n");
295287
npy_intp N = dimensions[0];
296288
char *in1_ptr = data[0], *in2_ptr = data[1];
297289
char *out_ptr = data[2];
@@ -326,10 +318,6 @@ static int
326318
quad_ufunc_promoter(PyUFuncObject *ufunc, PyArray_DTypeMeta *op_dtypes[],
327319
PyArray_DTypeMeta *signature[], PyArray_DTypeMeta *new_op_dtypes[])
328320
{
329-
printf("quad_ufunc_promoter called for ufunc: %s\n", ufunc->name);
330-
printf("Entering quad_ufunc_promoter\n");
331-
printf("Ufunc name: %s\n", ufunc->name);
332-
printf("nin: %d, nargs: %d\n", ufunc->nin, ufunc->nargs);
333321

334322
int nin = ufunc->nin;
335323
int nargs = ufunc->nargs;
@@ -343,52 +331,51 @@ quad_ufunc_promoter(PyUFuncObject *ufunc, PyArray_DTypeMeta *op_dtypes[],
343331
for (int i = 0; i < 3; i++) {
344332
Py_INCREF(op_dtypes[1]);
345333
new_op_dtypes[i] = op_dtypes[1];
346-
printf("new_op_dtypes[%d] set to %s\n", i, get_dtype_name(new_op_dtypes[i]));
334+
347335
}
348336
return 0;
349337
}
350338

351339
// Check if any input or signature is QuadPrecision
352340
for (int i = 0; i < nin; i++) {
353-
printf("iterating on dtype : %s\n", get_dtype_name(op_dtypes[i]));
341+
354342
if (op_dtypes[i] == &QuadPrecDType) {
355343
has_quad = true;
356-
printf("QuadPrecision detected in input %d\n", i);
344+
357345
}
358346
}
359347

360348
if (has_quad) {
361349
common = &QuadPrecDType;
362-
printf("Using QuadPrecDType as common type\n");
350+
363351
}
364352
else {
365353
for (int i = nin; i < nargs; i++) {
366354
if (signature[i] != NULL) {
367355
if (common == NULL) {
368356
Py_INCREF(signature[i]);
369357
common = signature[i];
370-
printf("Common type set to %s from signature\n", get_dtype_name(common));
358+
371359
}
372360
else if (common != signature[i]) {
373361
Py_CLEAR(common); // Not homogeneous, unset common
374-
printf("Output signature not homogeneous, cleared common type\n");
362+
375363
break;
376364
}
377365
}
378366
}
379367
}
380368
// If no common output dtype, use standard promotion for inputs
381369
if (common == NULL) {
382-
printf("Using standard promotion for inputs\n");
383370
common = PyArray_PromoteDTypeSequence(nin, op_dtypes);
384371
if (common == NULL) {
385372
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
386373
PyErr_Clear(); // Do not propagate normal promotion errors
387374
}
388-
printf("Exiting quad_ufunc_promoter (promotion failed)\n");
375+
389376
return -1;
390377
}
391-
printf("Common type after promotion: %s\n", get_dtype_name(common));
378+
392379
}
393380

394381
// Set all new_op_dtypes to the common dtype
@@ -397,20 +384,16 @@ quad_ufunc_promoter(PyUFuncObject *ufunc, PyArray_DTypeMeta *op_dtypes[],
397384
// If signature is specified for this argument, use it
398385
Py_INCREF(signature[i]);
399386
new_op_dtypes[i] = signature[i];
400-
printf("new_op_dtypes[%d] set to %s (from signature)\n", i,
401-
get_dtype_name(new_op_dtypes[i]));
402387
}
403388
else {
404389
// Otherwise, use the common dtype
405390
Py_INCREF(common);
406391
new_op_dtypes[i] = common;
407-
printf("new_op_dtypes[%d] set to %s (from common)\n", i,
408-
get_dtype_name(new_op_dtypes[i]));
409392
}
410393
}
411394

412395
Py_XDECREF(common);
413-
printf("Exiting quad_ufunc_promoter\n");
396+
414397
return 0;
415398
}
416399

0 commit comments

Comments
 (0)