blob: 6eb616a521467eb577321945068d7526b54e3ea5 [file] [log] [blame]
Bjorn Reese45029602001-08-21 09:23:53 +00001/*************************************************************************
2 *
3 * $Id$
4 *
5 * Copyright (C) 2001 Bjorn Reese <breese@users.sourceforge.net>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
12 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
13 * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
14 * CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
15 *
16 ************************************************************************
17 *
18 * Functions to handle special quantities in floating-point numbers
19 * (that is, NaNs and infinity). They provide the capability to detect
20 * and fabricate special quantities.
21 *
22 * Although written to be as portable as possible, it can never be
23 * guaranteed to work on all platforms, as not all hardware supports
24 * special quantities.
25 *
26 * The approach used here (approximately) is to:
27 *
28 * 1. Use C99 functionality when available.
29 * 2. Use IEEE 754 bit-patterns if possible.
30 * 3. Use platform-specific techniques.
31 *
Bjorn Reese45029602001-08-21 09:23:53 +000032 ************************************************************************/
33
Daniel Veillardb7c29c32002-09-25 22:44:43 +000034/*
35 * TODO:
36 * o Put all the magic into trio_fpclassify_and_signbit(), and use this from
37 * trio_isnan() etc.
38 */
Bjorn Reese45029602001-08-21 09:23:53 +000039
Bjorn Reese45029602001-08-21 09:23:53 +000040/*************************************************************************
41 * Include files
42 */
43#include "triodef.h"
44#include "trionan.h"
45
46#include <math.h>
47#include <string.h>
48#include <limits.h>
49#include <float.h>
50#if defined(TRIO_PLATFORM_UNIX)
51# include <signal.h>
52#endif
Bjorn Reese026d29f2002-01-19 15:40:18 +000053#if defined(TRIO_COMPILER_DECC)
William M. Brack476cd962003-08-13 11:09:42 +000054# if defined(__linux__)
55# include <cpml.h>
56# else
57# include <fp_class.h>
58# endif
Bjorn Reese026d29f2002-01-19 15:40:18 +000059#endif
Bjorn Reese45029602001-08-21 09:23:53 +000060#include <assert.h>
61
Bjorn Reese026d29f2002-01-19 15:40:18 +000062#if defined(TRIO_DOCUMENTATION)
63# include "doc/doc_nan.h"
Bjorn Reese45029602001-08-21 09:23:53 +000064#endif
Bjorn Reese026d29f2002-01-19 15:40:18 +000065/** @addtogroup SpecialQuantities
66 @{
67*/
Bjorn Reese45029602001-08-21 09:23:53 +000068
69/*************************************************************************
70 * Definitions
71 */
72
Daniel Veillard21458c82002-03-27 16:12:22 +000073#define TRIO_TRUE (1 == 1)
74#define TRIO_FALSE (0 == 1)
75
Daniel Veillarda48ed3d2003-04-03 15:28:28 +000076/*
77 * We must enable IEEE floating-point on Alpha
78 */
Bjorn Reese45029602001-08-21 09:23:53 +000079#if defined(__alpha) && !defined(_IEEE_FP)
80# if defined(TRIO_COMPILER_DECC)
Bjorn Reese026d29f2002-01-19 15:40:18 +000081# if defined(TRIO_PLATFORM_VMS)
82# error "Must be compiled with option /IEEE_MODE=UNDERFLOW_TO_ZERO/FLOAT=IEEE"
83# else
Daniel Veillardb7c29c32002-09-25 22:44:43 +000084# if !defined(_CFE)
85# error "Must be compiled with option -ieee"
86# endif
Bjorn Reese026d29f2002-01-19 15:40:18 +000087# endif
Bjorn Reese45029602001-08-21 09:23:53 +000088# elif defined(TRIO_COMPILER_GCC) && (defined(__osf__) || defined(__linux__))
89# error "Must be compiled with option -mieee"
90# endif
91#endif /* __alpha && ! _IEEE_FP */
92
93/*
94 * In ANSI/IEEE 754-1985 64-bits double format numbers have the
95 * following properties (amoungst others)
96 *
97 * o FLT_RADIX == 2: binary encoding
98 * o DBL_MAX_EXP == 1024: 11 bits exponent, where one bit is used
99 * to indicate special numbers (e.g. NaN and Infinity), so the
100 * maximum exponent is 10 bits wide (2^10 == 1024).
101 * o DBL_MANT_DIG == 53: The mantissa is 52 bits wide, but because
102 * numbers are normalized the initial binary 1 is represented
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000103 * implicitly (the so-called "hidden bit"), which leaves us with
Bjorn Reese45029602001-08-21 09:23:53 +0000104 * the ability to represent 53 bits wide mantissa.
105 */
106#if (FLT_RADIX == 2) && (DBL_MAX_EXP == 1024) && (DBL_MANT_DIG == 53)
107# define USE_IEEE_754
108#endif
109
110
111/*************************************************************************
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000112 * Constants
Bjorn Reese45029602001-08-21 09:23:53 +0000113 */
114
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000115static TRIO_CONST char rcsid[] = "@(#)$Id$";
116
Bjorn Reese45029602001-08-21 09:23:53 +0000117#if defined(USE_IEEE_754)
118
119/*
120 * Endian-agnostic indexing macro.
121 *
122 * The value of internalEndianMagic, when converted into a 64-bit
Bjorn Reese026d29f2002-01-19 15:40:18 +0000123 * integer, becomes 0x0706050403020100 (we could have used a 64-bit
Bjorn Reese45029602001-08-21 09:23:53 +0000124 * integer value instead of a double, but not all platforms supports
125 * that type). The value is automatically encoded with the correct
126 * endianess by the compiler, which means that we can support any
127 * kind of endianess. The individual bytes are then used as an index
128 * for the IEEE 754 bit-patterns and masks.
129 */
Bjorn Reese026d29f2002-01-19 15:40:18 +0000130#define TRIO_DOUBLE_INDEX(x) (((unsigned char *)&internalEndianMagic)[7-(x)])
Bjorn Reese45029602001-08-21 09:23:53 +0000131
Bjorn Reese026d29f2002-01-19 15:40:18 +0000132static TRIO_CONST double internalEndianMagic = 7.949928895127363e-275;
Bjorn Reese45029602001-08-21 09:23:53 +0000133
134/* Mask for the exponent */
Bjorn Reese026d29f2002-01-19 15:40:18 +0000135static TRIO_CONST unsigned char ieee_754_exponent_mask[] = {
Bjorn Reese45029602001-08-21 09:23:53 +0000136 0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
137};
138
139/* Mask for the mantissa */
Bjorn Reese026d29f2002-01-19 15:40:18 +0000140static TRIO_CONST unsigned char ieee_754_mantissa_mask[] = {
Bjorn Reese45029602001-08-21 09:23:53 +0000141 0x00, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
142};
143
Daniel Veillard21458c82002-03-27 16:12:22 +0000144/* Mask for the sign bit */
145static TRIO_CONST unsigned char ieee_754_sign_mask[] = {
146 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
147};
148
Daniel Veillard5fc1f082002-03-27 09:05:40 +0000149/* Bit-pattern for negative zero */
150static TRIO_CONST unsigned char ieee_754_negzero_array[] = {
151 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
152};
153
Bjorn Reese45029602001-08-21 09:23:53 +0000154/* Bit-pattern for infinity */
Bjorn Reese026d29f2002-01-19 15:40:18 +0000155static TRIO_CONST unsigned char ieee_754_infinity_array[] = {
Bjorn Reese45029602001-08-21 09:23:53 +0000156 0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
157};
158
159/* Bit-pattern for quiet NaN */
Bjorn Reese026d29f2002-01-19 15:40:18 +0000160static TRIO_CONST unsigned char ieee_754_qnan_array[] = {
Bjorn Reese45029602001-08-21 09:23:53 +0000161 0x7F, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
162};
163
164
165/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +0000166 * Functions
167 */
168
169/*
Bjorn Reese45029602001-08-21 09:23:53 +0000170 * trio_make_double
171 */
Bjorn Reese026d29f2002-01-19 15:40:18 +0000172TRIO_PRIVATE double
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000173trio_make_double
174TRIO_ARGS1((values),
175 TRIO_CONST unsigned char *values)
Bjorn Reese45029602001-08-21 09:23:53 +0000176{
Bjorn Reese026d29f2002-01-19 15:40:18 +0000177 TRIO_VOLATILE double result;
Bjorn Reese45029602001-08-21 09:23:53 +0000178 int i;
179
180 for (i = 0; i < (int)sizeof(double); i++) {
Bjorn Reese026d29f2002-01-19 15:40:18 +0000181 ((TRIO_VOLATILE unsigned char *)&result)[TRIO_DOUBLE_INDEX(i)] = values[i];
Bjorn Reese45029602001-08-21 09:23:53 +0000182 }
183 return result;
184}
185
Bjorn Reese026d29f2002-01-19 15:40:18 +0000186/*
Daniel Veillard21458c82002-03-27 16:12:22 +0000187 * trio_is_special_quantity
Bjorn Reese45029602001-08-21 09:23:53 +0000188 */
Bjorn Reese026d29f2002-01-19 15:40:18 +0000189TRIO_PRIVATE int
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000190trio_is_special_quantity
191TRIO_ARGS2((number, has_mantissa),
192 double number,
193 int *has_mantissa)
Bjorn Reese45029602001-08-21 09:23:53 +0000194{
195 unsigned int i;
196 unsigned char current;
Daniel Veillard21458c82002-03-27 16:12:22 +0000197 int is_special_quantity = TRIO_TRUE;
Bjorn Reese45029602001-08-21 09:23:53 +0000198
199 *has_mantissa = 0;
200
201 for (i = 0; i < (unsigned int)sizeof(double); i++) {
202 current = ((unsigned char *)&number)[TRIO_DOUBLE_INDEX(i)];
203 is_special_quantity
204 &= ((current & ieee_754_exponent_mask[i]) == ieee_754_exponent_mask[i]);
205 *has_mantissa |= (current & ieee_754_mantissa_mask[i]);
206 }
207 return is_special_quantity;
208}
209
Daniel Veillard21458c82002-03-27 16:12:22 +0000210/*
211 * trio_is_negative
212 */
213TRIO_PRIVATE int
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000214trio_is_negative
215TRIO_ARGS1((number),
216 double number)
Daniel Veillard5fc1f082002-03-27 09:05:40 +0000217{
218 unsigned int i;
Daniel Veillard21458c82002-03-27 16:12:22 +0000219 int is_negative = TRIO_FALSE;
Daniel Veillard5fc1f082002-03-27 09:05:40 +0000220
221 for (i = 0; i < (unsigned int)sizeof(double); i++) {
Daniel Veillard21458c82002-03-27 16:12:22 +0000222 is_negative |= (((unsigned char *)&number)[TRIO_DOUBLE_INDEX(i)]
223 & ieee_754_sign_mask[i]);
Daniel Veillard5fc1f082002-03-27 09:05:40 +0000224 }
Daniel Veillard21458c82002-03-27 16:12:22 +0000225 return is_negative;
Daniel Veillard5fc1f082002-03-27 09:05:40 +0000226}
227
Bjorn Reese45029602001-08-21 09:23:53 +0000228#endif /* USE_IEEE_754 */
229
230
Bjorn Reese026d29f2002-01-19 15:40:18 +0000231/**
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000232 Generate negative zero.
233
234 @return Floating-point representation of negative zero.
235*/
236TRIO_PUBLIC double
237trio_nzero(TRIO_NOARGS)
238{
239#if defined(USE_IEEE_754)
240 return trio_make_double(ieee_754_negzero_array);
241#else
242 TRIO_VOLATILE double zero = 0.0;
243
244 return -zero;
245#endif
246}
247
248/**
Bjorn Reese026d29f2002-01-19 15:40:18 +0000249 Generate positive infinity.
250
251 @return Floating-point representation of positive infinity.
252*/
Daniel Veillardcda96922001-08-21 10:56:31 +0000253TRIO_PUBLIC double
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000254trio_pinf(TRIO_NOARGS)
Bjorn Reese45029602001-08-21 09:23:53 +0000255{
256 /* Cache the result */
257 static double result = 0.0;
258
259 if (result == 0.0) {
260
261#if defined(INFINITY) && defined(__STDC_IEC_559__)
262 result = (double)INFINITY;
263
264#elif defined(USE_IEEE_754)
265 result = trio_make_double(ieee_754_infinity_array);
266
267#else
268 /*
269 * If HUGE_VAL is different from DBL_MAX, then HUGE_VAL is used
270 * as infinity. Otherwise we have to resort to an overflow
271 * operation to generate infinity.
272 */
273# if defined(TRIO_PLATFORM_UNIX)
274 void (*signal_handler)(int) = signal(SIGFPE, SIG_IGN);
275# endif
276
277 result = HUGE_VAL;
278 if (HUGE_VAL == DBL_MAX) {
279 /* Force overflow */
280 result += HUGE_VAL;
281 }
282
283# if defined(TRIO_PLATFORM_UNIX)
284 signal(SIGFPE, signal_handler);
285# endif
286
287#endif
288 }
289 return result;
290}
291
Bjorn Reese026d29f2002-01-19 15:40:18 +0000292/**
293 Generate negative infinity.
294
295 @return Floating-point value of negative infinity.
296*/
Daniel Veillardcda96922001-08-21 10:56:31 +0000297TRIO_PUBLIC double
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000298trio_ninf(TRIO_NOARGS)
Bjorn Reese45029602001-08-21 09:23:53 +0000299{
300 static double result = 0.0;
301
302 if (result == 0.0) {
303 /*
304 * Negative infinity is calculated by negating positive infinity,
305 * which can be done because it is legal to do calculations on
306 * infinity (for example, 1 / infinity == 0).
307 */
308 result = -trio_pinf();
309 }
310 return result;
311}
312
Bjorn Reese026d29f2002-01-19 15:40:18 +0000313/**
314 Generate NaN.
315
316 @return Floating-point representation of NaN.
317*/
Daniel Veillardcda96922001-08-21 10:56:31 +0000318TRIO_PUBLIC double
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000319trio_nan(TRIO_NOARGS)
Bjorn Reese45029602001-08-21 09:23:53 +0000320{
321 /* Cache the result */
322 static double result = 0.0;
323
324 if (result == 0.0) {
325
326#if defined(TRIO_COMPILER_SUPPORTS_C99)
Bjorn Reese54d02fb2002-04-19 15:16:01 +0000327 result = nan("");
Bjorn Reese45029602001-08-21 09:23:53 +0000328
329#elif defined(NAN) && defined(__STDC_IEC_559__)
330 result = (double)NAN;
331
332#elif defined(USE_IEEE_754)
333 result = trio_make_double(ieee_754_qnan_array);
334
335#else
336 /*
337 * There are several ways to generate NaN. The one used here is
338 * to divide infinity by infinity. I would have preferred to add
339 * negative infinity to positive infinity, but that yields wrong
340 * result (infinity) on FreeBSD.
341 *
342 * This may fail if the hardware does not support NaN, or if
343 * the Invalid Operation floating-point exception is unmasked.
344 */
345# if defined(TRIO_PLATFORM_UNIX)
346 void (*signal_handler)(int) = signal(SIGFPE, SIG_IGN);
347# endif
348
349 result = trio_pinf() / trio_pinf();
350
351# if defined(TRIO_PLATFORM_UNIX)
352 signal(SIGFPE, signal_handler);
353# endif
354
355#endif
356 }
357 return result;
358}
359
Bjorn Reese026d29f2002-01-19 15:40:18 +0000360/**
361 Check for NaN.
362
363 @param number An arbitrary floating-point number.
364 @return Boolean value indicating whether or not the number is a NaN.
365*/
Daniel Veillardcda96922001-08-21 10:56:31 +0000366TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000367trio_isnan
368TRIO_ARGS1((number),
369 double number)
Bjorn Reese45029602001-08-21 09:23:53 +0000370{
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000371#if (defined(TRIO_COMPILER_SUPPORTS_C99) && defined(isnan)) \
372 || defined(TRIO_COMPILER_SUPPORTS_UNIX95)
Bjorn Reese45029602001-08-21 09:23:53 +0000373 /*
374 * C99 defines isnan() as a macro. UNIX95 defines isnan() as a
375 * function. This function was already present in XPG4, but this
376 * is a bit tricky to detect with compiler defines, so we choose
377 * the conservative approach and only use it for UNIX95.
378 */
379 return isnan(number);
380
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000381#elif defined(TRIO_COMPILER_MSVC) || defined(TRIO_COMPILER_BCB)
Bjorn Reese45029602001-08-21 09:23:53 +0000382 /*
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000383 * Microsoft Visual C++ and Borland C++ Builder have an _isnan()
384 * function.
Bjorn Reese45029602001-08-21 09:23:53 +0000385 */
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000386 return _isnan(number) ? TRIO_TRUE : TRIO_FALSE;
Bjorn Reese45029602001-08-21 09:23:53 +0000387
388#elif defined(USE_IEEE_754)
389 /*
390 * Examine IEEE 754 bit-pattern. A NaN must have a special exponent
391 * pattern, and a non-empty mantissa.
392 */
393 int has_mantissa;
394 int is_special_quantity;
395
396 is_special_quantity = trio_is_special_quantity(number, &has_mantissa);
397
398 return (is_special_quantity && has_mantissa);
399
400#else
401 /*
402 * Fallback solution
403 */
404 int status;
405 double integral, fraction;
406
407# if defined(TRIO_PLATFORM_UNIX)
408 void (*signal_handler)(int) = signal(SIGFPE, SIG_IGN);
409# endif
410
411 status = (/*
412 * NaN is the only number which does not compare to itself
413 */
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000414 ((TRIO_VOLATILE double)number != (TRIO_VOLATILE double)number) ||
Bjorn Reese45029602001-08-21 09:23:53 +0000415 /*
416 * Fallback solution if NaN compares to NaN
417 */
418 ((number != 0.0) &&
419 (fraction = modf(number, &integral),
420 integral == fraction)));
421
422# if defined(TRIO_PLATFORM_UNIX)
423 signal(SIGFPE, signal_handler);
424# endif
425
426 return status;
427
428#endif
429}
430
Bjorn Reese026d29f2002-01-19 15:40:18 +0000431/**
432 Check for infinity.
433
434 @param number An arbitrary floating-point number.
435 @return 1 if positive infinity, -1 if negative infinity, 0 otherwise.
436*/
Daniel Veillardcda96922001-08-21 10:56:31 +0000437TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000438trio_isinf
439TRIO_ARGS1((number),
440 double number)
Bjorn Reese45029602001-08-21 09:23:53 +0000441{
William M. Brack476cd962003-08-13 11:09:42 +0000442#if defined(TRIO_COMPILER_DECC) && !defined(__linux__)
Bjorn Reese45029602001-08-21 09:23:53 +0000443 /*
444 * DECC has an isinf() macro, but it works differently than that
445 * of C99, so we use the fp_class() function instead.
446 */
447 return ((fp_class(number) == FP_POS_INF)
448 ? 1
449 : ((fp_class(number) == FP_NEG_INF) ? -1 : 0));
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000450
Bjorn Reese45029602001-08-21 09:23:53 +0000451#elif defined(isinf)
452 /*
453 * C99 defines isinf() as a macro.
454 */
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000455 return isinf(number)
456 ? ((number > 0.0) ? 1 : -1)
457 : 0;
Bjorn Reese45029602001-08-21 09:23:53 +0000458
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000459#elif defined(TRIO_COMPILER_MSVC) || defined(TRIO_COMPILER_BCB)
Bjorn Reese45029602001-08-21 09:23:53 +0000460 /*
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000461 * Microsoft Visual C++ and Borland C++ Builder have an _fpclass()
462 * function that can be used to detect infinity.
Bjorn Reese45029602001-08-21 09:23:53 +0000463 */
464 return ((_fpclass(number) == _FPCLASS_PINF)
465 ? 1
466 : ((_fpclass(number) == _FPCLASS_NINF) ? -1 : 0));
467
468#elif defined(USE_IEEE_754)
469 /*
470 * Examine IEEE 754 bit-pattern. Infinity must have a special exponent
471 * pattern, and an empty mantissa.
472 */
473 int has_mantissa;
474 int is_special_quantity;
475
476 is_special_quantity = trio_is_special_quantity(number, &has_mantissa);
477
478 return (is_special_quantity && !has_mantissa)
479 ? ((number < 0.0) ? -1 : 1)
480 : 0;
481
482#else
483 /*
484 * Fallback solution.
485 */
486 int status;
487
488# if defined(TRIO_PLATFORM_UNIX)
489 void (*signal_handler)(int) = signal(SIGFPE, SIG_IGN);
490# endif
491
492 double infinity = trio_pinf();
493
494 status = ((number == infinity)
495 ? 1
496 : ((number == -infinity) ? -1 : 0));
497
498# if defined(TRIO_PLATFORM_UNIX)
499 signal(SIGFPE, signal_handler);
500# endif
501
502 return status;
503
504#endif
505}
506
William M. Bracka71a8ef2003-08-06 04:43:55 +0000507#if 0
508 /* Temporary fix - this routine is not used anywhere */
Daniel Veillard21458c82002-03-27 16:12:22 +0000509/**
510 Check for finity.
511
512 @param number An arbitrary floating-point number.
513 @return Boolean value indicating whether or not the number is a finite.
514*/
515TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000516trio_isfinite
517TRIO_ARGS1((number),
518 double number)
Daniel Veillard21458c82002-03-27 16:12:22 +0000519{
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000520#if defined(TRIO_COMPILER_SUPPORTS_C99) && defined(isfinite)
Daniel Veillard21458c82002-03-27 16:12:22 +0000521 /*
522 * C99 defines isfinite() as a macro.
523 */
524 return isfinite(number);
525
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000526#elif defined(TRIO_COMPILER_MSVC) || defined(TRIO_COMPILER_BCB)
Daniel Veillard21458c82002-03-27 16:12:22 +0000527 /*
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000528 * Microsoft Visual C++ and Borland C++ Builder use _finite().
Daniel Veillard21458c82002-03-27 16:12:22 +0000529 */
530 return _finite(number);
531
532#elif defined(USE_IEEE_754)
533 /*
534 * Examine IEEE 754 bit-pattern. For finity we do not care about the
535 * mantissa.
536 */
537 int dummy;
538
539 return (! trio_is_special_quantity(number, &dummy));
540
541#else
542 /*
543 * Fallback solution.
544 */
545 return ((trio_isinf(number) == 0) && (trio_isnan(number) == 0));
546
547#endif
548}
549
William M. Bracka71a8ef2003-08-06 04:43:55 +0000550#endif
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000551
Daniel Veillard21458c82002-03-27 16:12:22 +0000552/*
553 * The sign of NaN is always false
554 */
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000555TRIO_PUBLIC int
556trio_fpclassify_and_signbit
557TRIO_ARGS2((number, is_negative),
558 double number,
559 int *is_negative)
Daniel Veillard21458c82002-03-27 16:12:22 +0000560{
561#if defined(fpclassify) && defined(signbit)
562 /*
563 * C99 defines fpclassify() and signbit() as a macros
564 */
565 *is_negative = signbit(number);
566 switch (fpclassify(number)) {
567 case FP_NAN:
568 return TRIO_FP_NAN;
569 case FP_INFINITE:
570 return TRIO_FP_INFINITE;
571 case FP_SUBNORMAL:
572 return TRIO_FP_SUBNORMAL;
573 case FP_ZERO:
574 return TRIO_FP_ZERO;
575 default:
576 return TRIO_FP_NORMAL;
577 }
578
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000579#else
580# if defined(TRIO_COMPILER_DECC)
Daniel Veillard21458c82002-03-27 16:12:22 +0000581 /*
582 * DECC has an fp_class() function.
583 */
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000584# define TRIO_FPCLASSIFY(n) fp_class(n)
585# define TRIO_QUIET_NAN FP_QNAN
586# define TRIO_SIGNALLING_NAN FP_SNAN
587# define TRIO_POSITIVE_INFINITY FP_POS_INF
588# define TRIO_NEGATIVE_INFINITY FP_NEG_INF
589# define TRIO_POSITIVE_SUBNORMAL FP_POS_DENORM
590# define TRIO_NEGATIVE_SUBNORMAL FP_NEG_DENORM
591# define TRIO_POSITIVE_ZERO FP_POS_ZERO
592# define TRIO_NEGATIVE_ZERO FP_NEG_ZERO
593# define TRIO_POSITIVE_NORMAL FP_POS_NORM
594# define TRIO_NEGATIVE_NORMAL FP_NEG_NORM
595
596# elif defined(TRIO_COMPILER_MSVC) || defined(TRIO_COMPILER_BCB)
Daniel Veillard21458c82002-03-27 16:12:22 +0000597 /*
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000598 * Microsoft Visual C++ and Borland C++ Builder have an _fpclass()
599 * function.
Daniel Veillard21458c82002-03-27 16:12:22 +0000600 */
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000601# define TRIO_FPCLASSIFY(n) _fpclass(n)
602# define TRIO_QUIET_NAN _FPCLASS_QNAN
603# define TRIO_SIGNALLING_NAN _FPCLASS_SNAN
604# define TRIO_POSITIVE_INFINITY _FPCLASS_PINF
605# define TRIO_NEGATIVE_INFINITY _FPCLASS_NINF
606# define TRIO_POSITIVE_SUBNORMAL _FPCLASS_PD
607# define TRIO_NEGATIVE_SUBNORMAL _FPCLASS_ND
608# define TRIO_POSITIVE_ZERO _FPCLASS_PZ
609# define TRIO_NEGATIVE_ZERO _FPCLASS_NZ
610# define TRIO_POSITIVE_NORMAL _FPCLASS_PN
611# define TRIO_NEGATIVE_NORMAL _FPCLASS_NN
612
613# elif defined(FP_PLUS_NORM)
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000614 /*
615 * HP-UX 9.x and 10.x have an fpclassify() function, that is different
616 * from the C99 fpclassify() macro supported on HP-UX 11.x.
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000617 *
618 * AIX has class() for C, and _class() for C++, which returns the
619 * same values as the HP-UX fpclassify() function.
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000620 */
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000621# if defined(TRIO_PLATFORM_AIX)
622# if defined(__cplusplus)
623# define TRIO_FPCLASSIFY(n) _class(n)
624# else
625# define TRIO_FPCLASSIFY(n) class(n)
626# endif
627# else
628# define TRIO_FPCLASSIFY(n) fpclassify(n)
629# endif
630# define TRIO_QUIET_NAN FP_QNAN
631# define TRIO_SIGNALLING_NAN FP_SNAN
632# define TRIO_POSITIVE_INFINITY FP_PLUS_INF
633# define TRIO_NEGATIVE_INFINITY FP_MINUS_INF
634# define TRIO_POSITIVE_SUBNORMAL FP_PLUS_DENORM
635# define TRIO_NEGATIVE_SUBNORMAL FP_MINUS_DENORM
636# define TRIO_POSITIVE_ZERO FP_PLUS_ZERO
637# define TRIO_NEGATIVE_ZERO FP_MINUS_ZERO
638# define TRIO_POSITIVE_NORMAL FP_PLUS_NORM
639# define TRIO_NEGATIVE_NORMAL FP_MINUS_NORM
640# endif
641
642# if defined(TRIO_FPCLASSIFY)
643 switch (TRIO_FPCLASSIFY(number)) {
644 case TRIO_QUIET_NAN:
645 case TRIO_SIGNALLING_NAN:
646 *is_negative = TRIO_FALSE; /* NaN has no sign */
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000647 return TRIO_FP_NAN;
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000648 case TRIO_POSITIVE_INFINITY:
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000649 *is_negative = TRIO_FALSE;
650 return TRIO_FP_INFINITE;
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000651 case TRIO_NEGATIVE_INFINITY:
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000652 *is_negative = TRIO_TRUE;
653 return TRIO_FP_INFINITE;
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000654 case TRIO_POSITIVE_SUBNORMAL:
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000655 *is_negative = TRIO_FALSE;
656 return TRIO_FP_SUBNORMAL;
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000657 case TRIO_NEGATIVE_SUBNORMAL:
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000658 *is_negative = TRIO_TRUE;
659 return TRIO_FP_SUBNORMAL;
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000660 case TRIO_POSITIVE_ZERO:
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000661 *is_negative = TRIO_FALSE;
662 return TRIO_FP_ZERO;
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000663 case TRIO_NEGATIVE_ZERO:
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000664 *is_negative = TRIO_TRUE;
665 return TRIO_FP_ZERO;
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000666 case TRIO_POSITIVE_NORMAL:
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000667 *is_negative = TRIO_FALSE;
668 return TRIO_FP_NORMAL;
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000669 case TRIO_NEGATIVE_NORMAL:
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000670 *is_negative = TRIO_TRUE;
671 return TRIO_FP_NORMAL;
672 default:
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000673 /* Just in case... */
674 *is_negative = (number < 0.0);
675 return TRIO_FP_NORMAL;
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000676 }
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000677
678# else
Daniel Veillard21458c82002-03-27 16:12:22 +0000679 /*
680 * Fallback solution.
681 */
682 int rc;
683
684 if (number == 0.0) {
685 /*
686 * In IEEE 754 the sign of zero is ignored in comparisons, so we
687 * have to handle this as a special case by examining the sign bit
688 * directly.
689 */
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000690# if defined(USE_IEEE_754)
Daniel Veillard21458c82002-03-27 16:12:22 +0000691 *is_negative = trio_is_negative(number);
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000692# else
Daniel Veillard21458c82002-03-27 16:12:22 +0000693 *is_negative = TRIO_FALSE; /* FIXME */
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000694# endif
Daniel Veillard21458c82002-03-27 16:12:22 +0000695 return TRIO_FP_ZERO;
696 }
697 if (trio_isnan(number)) {
698 *is_negative = TRIO_FALSE;
699 return TRIO_FP_NAN;
700 }
701 if ((rc = trio_isinf(number))) {
702 *is_negative = (rc == -1);
703 return TRIO_FP_INFINITE;
704 }
705 if ((number > 0.0) && (number < DBL_MIN)) {
706 *is_negative = TRIO_FALSE;
707 return TRIO_FP_SUBNORMAL;
708 }
709 if ((number < 0.0) && (number > -DBL_MIN)) {
710 *is_negative = TRIO_TRUE;
711 return TRIO_FP_SUBNORMAL;
712 }
713 *is_negative = (number < 0.0);
714 return TRIO_FP_NORMAL;
715
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000716# endif
Daniel Veillard21458c82002-03-27 16:12:22 +0000717#endif
718}
719
720/**
721 Examine the sign of a number.
722
723 @param number An arbitrary floating-point number.
724 @return Boolean value indicating whether or not the number has the
725 sign bit set (i.e. is negative).
726*/
727TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000728trio_signbit
729TRIO_ARGS1((number),
730 double number)
Daniel Veillard21458c82002-03-27 16:12:22 +0000731{
732 int is_negative;
733
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000734 (void)trio_fpclassify_and_signbit(number, &is_negative);
Daniel Veillard21458c82002-03-27 16:12:22 +0000735 return is_negative;
736}
737
William M. Bracka71a8ef2003-08-06 04:43:55 +0000738#if 0
739 /* Temporary fix - this routine is not used in libxml */
Daniel Veillard21458c82002-03-27 16:12:22 +0000740/**
741 Examine the class of a number.
742
743 @param number An arbitrary floating-point number.
744 @return Enumerable value indicating the class of @p number
745*/
746TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000747trio_fpclassify
748TRIO_ARGS1((number),
749 double number)
Daniel Veillard21458c82002-03-27 16:12:22 +0000750{
751 int dummy;
752
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000753 return trio_fpclassify_and_signbit(number, &dummy);
Daniel Veillard21458c82002-03-27 16:12:22 +0000754}
755
William M. Bracka71a8ef2003-08-06 04:43:55 +0000756#endif
Daniel Veillard21458c82002-03-27 16:12:22 +0000757
Bjorn Reese026d29f2002-01-19 15:40:18 +0000758/** @} SpecialQuantities */
759
Bjorn Reese45029602001-08-21 09:23:53 +0000760/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +0000761 * For test purposes.
762 *
763 * Add the following compiler option to include this test code.
764 *
765 * Unix : -DSTANDALONE
766 * VMS : /DEFINE=(STANDALONE)
Bjorn Reese45029602001-08-21 09:23:53 +0000767 */
768#if defined(STANDALONE)
769# include <stdio.h>
770
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000771static TRIO_CONST char *
772getClassification
Daniel Veillarde645e8c2002-10-22 17:35:37 +0000773TRIO_ARGS1((type),
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000774 int type)
Daniel Veillard21458c82002-03-27 16:12:22 +0000775{
776 switch (type) {
777 case TRIO_FP_INFINITE:
778 return "FP_INFINITE";
779 case TRIO_FP_NAN:
780 return "FP_NAN";
781 case TRIO_FP_NORMAL:
782 return "FP_NORMAL";
783 case TRIO_FP_SUBNORMAL:
784 return "FP_SUBNORMAL";
785 case TRIO_FP_ZERO:
786 return "FP_ZERO";
787 default:
788 return "FP_UNKNOWN";
789 }
790}
791
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000792static void
793print_class
Daniel Veillarde645e8c2002-10-22 17:35:37 +0000794TRIO_ARGS2((prefix, number),
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000795 TRIO_CONST char *prefix,
796 double number)
Daniel Veillard21458c82002-03-27 16:12:22 +0000797{
798 printf("%-6s: %s %-15s %g\n",
799 prefix,
800 trio_signbit(number) ? "-" : "+",
Daniel Veillard9339b742003-10-15 08:18:00 +0000801 getClassification(TRIO_FPCLASSIFY(number)),
Daniel Veillard21458c82002-03-27 16:12:22 +0000802 number);
803}
804
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000805int main(TRIO_NOARGS)
Bjorn Reese45029602001-08-21 09:23:53 +0000806{
807 double my_nan;
808 double my_pinf;
809 double my_ninf;
810# if defined(TRIO_PLATFORM_UNIX)
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000811 void (*signal_handler) TRIO_PROTO((int));
Bjorn Reese45029602001-08-21 09:23:53 +0000812# endif
813
814 my_nan = trio_nan();
815 my_pinf = trio_pinf();
816 my_ninf = trio_ninf();
817
Daniel Veillard21458c82002-03-27 16:12:22 +0000818 print_class("Nan", my_nan);
819 print_class("PInf", my_pinf);
820 print_class("NInf", my_ninf);
821 print_class("PZero", 0.0);
822 print_class("NZero", -0.0);
823 print_class("PNorm", 1.0);
824 print_class("NNorm", -1.0);
825 print_class("PSub", 1.01e-307 - 1.00e-307);
826 print_class("NSub", 1.00e-307 - 1.01e-307);
827
Bjorn Reese45029602001-08-21 09:23:53 +0000828 printf("NaN : %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d)\n",
829 my_nan,
830 ((unsigned char *)&my_nan)[0],
831 ((unsigned char *)&my_nan)[1],
832 ((unsigned char *)&my_nan)[2],
833 ((unsigned char *)&my_nan)[3],
834 ((unsigned char *)&my_nan)[4],
835 ((unsigned char *)&my_nan)[5],
836 ((unsigned char *)&my_nan)[6],
837 ((unsigned char *)&my_nan)[7],
838 trio_isnan(my_nan), trio_isinf(my_nan));
839 printf("PInf: %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d)\n",
840 my_pinf,
841 ((unsigned char *)&my_pinf)[0],
842 ((unsigned char *)&my_pinf)[1],
843 ((unsigned char *)&my_pinf)[2],
844 ((unsigned char *)&my_pinf)[3],
845 ((unsigned char *)&my_pinf)[4],
846 ((unsigned char *)&my_pinf)[5],
847 ((unsigned char *)&my_pinf)[6],
848 ((unsigned char *)&my_pinf)[7],
849 trio_isnan(my_pinf), trio_isinf(my_pinf));
850 printf("NInf: %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d)\n",
851 my_ninf,
852 ((unsigned char *)&my_ninf)[0],
853 ((unsigned char *)&my_ninf)[1],
854 ((unsigned char *)&my_ninf)[2],
855 ((unsigned char *)&my_ninf)[3],
856 ((unsigned char *)&my_ninf)[4],
857 ((unsigned char *)&my_ninf)[5],
858 ((unsigned char *)&my_ninf)[6],
859 ((unsigned char *)&my_ninf)[7],
860 trio_isnan(my_ninf), trio_isinf(my_ninf));
861
862# if defined(TRIO_PLATFORM_UNIX)
863 signal_handler = signal(SIGFPE, SIG_IGN);
864# endif
865
866 my_pinf = DBL_MAX + DBL_MAX;
867 my_ninf = -my_pinf;
868 my_nan = my_pinf / my_pinf;
869
870# if defined(TRIO_PLATFORM_UNIX)
871 signal(SIGFPE, signal_handler);
872# endif
873
874 printf("NaN : %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d)\n",
875 my_nan,
876 ((unsigned char *)&my_nan)[0],
877 ((unsigned char *)&my_nan)[1],
878 ((unsigned char *)&my_nan)[2],
879 ((unsigned char *)&my_nan)[3],
880 ((unsigned char *)&my_nan)[4],
881 ((unsigned char *)&my_nan)[5],
882 ((unsigned char *)&my_nan)[6],
883 ((unsigned char *)&my_nan)[7],
884 trio_isnan(my_nan), trio_isinf(my_nan));
885 printf("PInf: %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d)\n",
886 my_pinf,
887 ((unsigned char *)&my_pinf)[0],
888 ((unsigned char *)&my_pinf)[1],
889 ((unsigned char *)&my_pinf)[2],
890 ((unsigned char *)&my_pinf)[3],
891 ((unsigned char *)&my_pinf)[4],
892 ((unsigned char *)&my_pinf)[5],
893 ((unsigned char *)&my_pinf)[6],
894 ((unsigned char *)&my_pinf)[7],
895 trio_isnan(my_pinf), trio_isinf(my_pinf));
896 printf("NInf: %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d)\n",
897 my_ninf,
898 ((unsigned char *)&my_ninf)[0],
899 ((unsigned char *)&my_ninf)[1],
900 ((unsigned char *)&my_ninf)[2],
901 ((unsigned char *)&my_ninf)[3],
902 ((unsigned char *)&my_ninf)[4],
903 ((unsigned char *)&my_ninf)[5],
904 ((unsigned char *)&my_ninf)[6],
905 ((unsigned char *)&my_ninf)[7],
906 trio_isnan(my_ninf), trio_isinf(my_ninf));
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000907
Bjorn Reese45029602001-08-21 09:23:53 +0000908 return 0;
909}
910#endif