[Cfp-interest] functions: another try at fromf/ufromf

David Hough CFP pcfp at oakapple.net
Mon Aug 9 12:44:11 PDT 2010


Based somewhat on our discussions today:




5. convertToInteger functions, with and without inexact signal, 
for five rounding modes


Each implementation shall define integral rounding directions:

	#define FP_CEIL ...
	#define FP_FLOOR ...
	#define FP_TRUNC ...
	#define FP_ROUND ...
	#define FP_ROUNDEVEN ...

corresponding to the various sorts of conversions to integral values in
floating-point formats.

Each implementation shall also define four functions for each 
supported floating-point type:

 intmax_t  fromfN   ( _FloatN x, int round, int bits );
 intmax_t  fromxfN  ( _FloatN x, int round, int bits );
uintmax_t ufromfN   ( _FloatN x, int round, int bits );
uintmax_t ufromxfN  ( _FloatN x, int round, int bits );

 intmax_t  fromfNx  ( _FloatNx x, int round, int bits );
 intmax_t  fromxfNx ( _FloatNx x, int round, int bits );
uintmax_t ufromfNx  ( _FloatNx x, int round, int bits );
uintmax_t ufromxfNx ( _FloatNx x, int round, int bits );

 intmax_t  fromdN   ( _DecimalN x, int round, int bits );
 intmax_t  fromxdN  ( _DecimalN x, int round, int bits );
unitmax_t ufromdN   ( _DecimalN x, int round, int bits );
unitmax_t ufromxdN  ( _DecimalN x, int round, int bits );

 intmax_t  fromfNx  ( _DecimalNx x, int round, int bits );
 intmax_t  fromxfNx ( _DecimalNx x, int round, int bits );
uintmax_t ufromfNx  ( _DecimalNx x, int round, int bits );
uintmax_t ufromxfNx ( _DecimalNx x, int round, int bits );

These functions return intmax_t or uintmax_t,
the maximum length signed or unsigned
integer types in an implementation.

The round parameter expects one of the FP_ direction macros above.
The bits parameter expects the number of bits in the integer result,
and so accommodates bit fields as well as hardware lengths.

The fromf and fromd functions do not signal the inexact exception.
The fromxf and fromxd functions do signal the inexact exception 
when an otherwise valid result differs in value from the operand x.

Note that many common cases of the fromf and fromd functions can be
implemented simply by rounding to integral and then casting to the
target integer type, e.g.  the conversion of __Float128 x to uint64_t 
using upward rounding is implemented by

	(uint64_t)ceilf128(x)

Optimizing compilers should recognize common cases that can be expressed
in short machine instruction sequences.




More information about the Cfp-interest mailing list