[Numpy-discussion] Problem with NA_IeeeSpecial64

Todd Miller jmiller at stsci.edu
Thu Dec 4 02:22:04 EST 2003


On Wed, 2003-12-03 at 19:23, Edward C. Jones wrote:
> /*
>   * 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? 

Did you "import_libnumarray();" in the module where you are trying to
use NA_IeeeSpecial64?  If not, then the numarray C-API jump table
pointer (for that module) is uninitialized and leads to a segfault on
the first call through it.

Regards,
Todd

> 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);
> }
> 
> 
> 
> -------------------------------------------------------
> This SF.net email is sponsored by: SF.net Giveback Program.
> Does SourceForge.net help you be more productive?  Does it
> help you create better code?  SHARE THE LOVE, and help us help
> YOU!  Click Here: http://sourceforge.net/donate/
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion
-- 
Todd Miller <jmiller at stsci.edu>





More information about the NumPy-Discussion mailing list