[Numpy-discussion] Problem with NA_IeeeSpecial64
Edward C. Jones
edcjones at erols.com
Wed Dec 3 16:27:05 CST 2003
/*
* When I use NA_IeeeSpecial64 in this little module, I get a
* segfault. If I use MY_IeeeSpecial64, the program appears to
* work. Anyone know what the problem is? Here is the Python code.
*
* #! /usr/bin/env python
* import ieee
* mask = ieee.POS_QUIET_NAN
* print ieee.IeeeSpecial64(1.0, mask)
*/
#include </usr/local/include/python2.3/Python.h>
#include </usr/local/include/python2.3/numarray/numarray.h>
#include </usr/local/include/python2.3/numarray/libnumarray.h>
/* From libnumarraymodule.c or ieeespecial.ch */
#define WITHIN64(v, f) (((v) >= f##_MIN64) && ((v) <= f##_MAX64))
Bool MY_IeeeSpecial64( Float64 *f, Int32 *mask)
{
Int32 category;
UInt64 *f1 = (UInt64 *) f;
UInt64 v = *f1;
if (v & BIT(63)) {
if (WITHIN64(v, NEG_NORMALIZED)) {
category = MSK_NEG_NOR;
} else if (WITHIN64(v, NEG_DENORMALIZED)) {
category = MSK_NEG_DEN;
} else if (WITHIN64(v, NEG_SIGNAL_NAN)) {
category = MSK_NEG_SNAN;
} else if (WITHIN64(v, NEG_QUIET_NAN)) {
category = MSK_NEG_QNAN;
} else if (v == NEG_INFINITY_MIN64) {
category = MSK_NEG_INF;
} else if (v == NEG_ZERO_MIN64) {
category = MSK_NEG_ZERO;
} else if (v == INDETERMINATE_MIN64) {
category = MSK_INDETERM;
} else {
category = MSK_BUG;
}
} else {
if (WITHIN64(v, POS_NORMALIZED)) {
category = MSK_POS_NOR;
} else if (WITHIN64(v, POS_DENORMALIZED)) {
category = MSK_POS_DEN;
} else if (WITHIN64(v, POS_SIGNAL_NAN)) {
category = MSK_POS_SNAN;
} else if (WITHIN64(v, POS_QUIET_NAN)) {
category = MSK_POS_QNAN;
} else if (v == POS_INFINITY_MIN64) {
category = MSK_POS_INF;
} else if (v == POS_ZERO_MIN64) {
category = MSK_POS_ZERO;
} else {
category = MSK_BUG;
}
}
return (category & *mask) != 0;
}
static PyObject* IeeeSpecial64(PyObject* obj, PyObject *args)
{
Int32 mask;
Bool b;
Float64 value;
PyObject* result;
if (!PyArg_ParseTuple(args, "di:IeeeSpecial64", &value, &mask))
return NULL;
printf("%f %d\n", value, mask);
/* Seems to work. */
b = MY_IeeeSpecial64(&value, &mask);
printf("L75 %d\n", b);
/* The next line causes a segfault. */
b = NA_IeeeSpecial64(&value, &mask);
printf("L78 %d\n", b);
if (b)
result = Py_True;
else
result = Py_False;
Py_INCREF(result);
return result;
}
static PyMethodDef ieee_Methods[] = {
{"IeeeSpecial64", IeeeSpecial64, METH_VARARGS, ""},
{NULL, NULL, 0, NULL} /* sentinel */
};
PyMODINIT_FUNC initieee(void)
{
PyObject* m;
m = Py_InitModule("ieee", ieee_Methods);
PyModule_AddIntConstant(m, "POS_QUIET_NAN", (int) MSK_POS_QNAN);
}
More information about the Numpy-discussion
mailing list