Post by олÑга кÑÑжановÑкаÑCERN has requested the ability to create a nan value with payload in
our shell interpreter, and extract it later. I think the point is to
make error detection possible, without littering the code with a lot
of if() branches in the hot areas.
How to tell a Quiet NaN from a Signaling NaN is not well defined in
the older IEEE-754-1985 (which is what almost all chips are designed to).
The C committee is working on a binding between C11 and IEEE-754-2008.
We have come up with some functions for working with payloads.
14.10 NaN functions
IEC 60559 defines the payload of a NaN to be a certain part of the NaN's significand interpreted as an integer.
The payload is intended to provide implementation-defined diagnostic information about the NaN, such as
where or how the NaN was created. The following suggested changes to C11 provide functions to get and set
the NaN payloads defined in IEC 60559.
Suggested change to C11:
F.10.13 Payload functions
F.10.13.1 The getpayload functions
Synopsis
[1]
#define __STDC_WANT_IEC_18661_EXT1__
#include <math.h>
double getpayload(const double *x );
float getpayloadf(const float *x );
long double getpayloadl(const long double *x );
Description
[2] The getpayload functions extract the integer value of the payload of a NaN input and return the
integer as a floating-point value. The sign of the returned integer is positive. If *x is not a NaN, the
return result is unspecified. These functions raise no floating-point exceptions, even if *x is a
signaling NaN.
Returns
[3] The functions return a floating-point representation of the integer value of the payload of the NaN
input.
F.10.13.2 The setpayload functions
Synopsis
[1]
#define __STDC_WANT_IEC_18661_EXT1__
#include <math.h>
int setpayload(double *res, double pl);
int setpayloadf(float *res, float pl);
int setpayloadl(long double *res, long double pl);
Description
[2] The setpayload functions create a quiet NaN with the payload specified by pl and a zero sign
bit and store that NaN into the object pointed to by *res. If pl is not a positive floating-point integer
representing a valid payload, *res is set to positive zero.
Returns
[3] If the functions stored the specified NaN, the functions return a zero value, otherwise a non-zero
value (and *res is set to zero).
F.10.13.3 The setpayloadsig functions
Synopsis
[1]
#define __STDC_WANT_IEC_18661_EXT1__
#include <math.h>
int setpayloadsig(double *res, double pl);
int setpayloadsigf(float *res, float pl);
int setpayloadsigl(long double *res, long double pl);
Description
[2] The setpayloadsig functions create a signaling NaN with the payload specified by pl and a
zero sign bit and store that NaN into the object pointed to by *res. If pl is not a positive floating-
point integer representing a valid payload, *res is set to positive zero.
Returns
[3] If the functions stored the specified NaN, the functions return a zero value, otherwise a non-zero
value (and *res is set to zero).
---
Fred J. Tydeman Tydeman Consulting
***@tybor.com Testing, numerics, programming
+1 (775) 287-5904 Vice-chair of PL22.11 (ANSI "C")
Sample C99+FPCE tests: http://www.tybor.com
Savers sleep well, investors eat well, spenders work forever.