1 /*************************************************************************
5 * Copyright (C) 2001 Bjorn Reese <breese@users.sourceforge.net>
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.
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.
16 ************************************************************************
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.
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
26 * The approach used here (approximately) is to:
28 * 1. Use C99 functionality when available.
29 * 2. Use IEEE 754 bit-patterns if possible.
30 * 3. Use platform-specific techniques.
32 ************************************************************************/
36 * o Put all the magic into trio_fpclassify_and_signbit(), and use this from
40 /*************************************************************************
50 #if defined(TRIO_PLATFORM_UNIX)
53 #if defined(TRIO_COMPILER_DECC)
54 # include <fp_class.h>
58 #if defined(TRIO_DOCUMENTATION)
59 # include "doc/doc_nan.h"
61 /** @addtogroup SpecialQuantities
65 /*************************************************************************
69 #define TRIO_TRUE (1 == 1)
70 #define TRIO_FALSE (0 == 1)
72 /* We must enable IEEE floating-point on Alpha */
73 #if defined(__alpha) && !defined(_IEEE_FP)
74 # if defined(TRIO_COMPILER_DECC)
75 # if defined(TRIO_PLATFORM_VMS)
76 # error "Must be compiled with option /IEEE_MODE=UNDERFLOW_TO_ZERO/FLOAT=IEEE"
79 # error "Must be compiled with option -ieee"
82 # elif defined(TRIO_COMPILER_GCC) && (defined(__osf__) || defined(__linux__))
83 # error "Must be compiled with option -mieee"
85 #endif /* __alpha && ! _IEEE_FP */
88 * In ANSI/IEEE 754-1985 64-bits double format numbers have the
89 * following properties (amoungst others)
91 * o FLT_RADIX == 2: binary encoding
92 * o DBL_MAX_EXP == 1024: 11 bits exponent, where one bit is used
93 * to indicate special numbers (e.g. NaN and Infinity), so the
94 * maximum exponent is 10 bits wide (2^10 == 1024).
95 * o DBL_MANT_DIG == 53: The mantissa is 52 bits wide, but because
96 * numbers are normalized the initial binary 1 is represented
97 * implicitly (the so-called "hidden bit"), which leaves us with
98 * the ability to represent 53 bits wide mantissa.
100 #if (FLT_RADIX == 2) && (DBL_MAX_EXP == 1024) && (DBL_MANT_DIG == 53)
101 # define USE_IEEE_754
105 /*************************************************************************
109 static TRIO_CONST char rcsid[] = "@(#)$Id$";
111 #if defined(USE_IEEE_754)
114 * Endian-agnostic indexing macro.
116 * The value of internalEndianMagic, when converted into a 64-bit
117 * integer, becomes 0x0706050403020100 (we could have used a 64-bit
118 * integer value instead of a double, but not all platforms supports
119 * that type). The value is automatically encoded with the correct
120 * endianess by the compiler, which means that we can support any
121 * kind of endianess. The individual bytes are then used as an index
122 * for the IEEE 754 bit-patterns and masks.
124 #define TRIO_DOUBLE_INDEX(x) (((unsigned char *)&internalEndianMagic)[7-(x)])
126 static TRIO_CONST double internalEndianMagic = 7.949928895127363e-275;
128 /* Mask for the exponent */
129 static TRIO_CONST unsigned char ieee_754_exponent_mask[] = {
130 0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
133 /* Mask for the mantissa */
134 static TRIO_CONST unsigned char ieee_754_mantissa_mask[] = {
135 0x00, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
138 /* Mask for the sign bit */
139 static TRIO_CONST unsigned char ieee_754_sign_mask[] = {
140 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
143 /* Bit-pattern for negative zero */
144 static TRIO_CONST unsigned char ieee_754_negzero_array[] = {
145 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
148 /* Bit-pattern for infinity */
149 static TRIO_CONST unsigned char ieee_754_infinity_array[] = {
150 0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
153 /* Bit-pattern for quiet NaN */
154 static TRIO_CONST unsigned char ieee_754_qnan_array[] = {
155 0x7F, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
159 /*************************************************************************
169 TRIO_CONST unsigned char *values)
171 TRIO_VOLATILE double result;
174 for (i = 0; i < (int)sizeof(double); i++) {
175 ((TRIO_VOLATILE unsigned char *)&result)[TRIO_DOUBLE_INDEX(i)] = values[i];
181 * trio_is_special_quantity
184 trio_is_special_quantity
185 TRIO_ARGS2((number, has_mantissa),
190 unsigned char current;
191 int is_special_quantity = TRIO_TRUE;
195 for (i = 0; i < (unsigned int)sizeof(double); i++) {
196 current = ((unsigned char *)&number)[TRIO_DOUBLE_INDEX(i)];
198 &= ((current & ieee_754_exponent_mask[i]) == ieee_754_exponent_mask[i]);
199 *has_mantissa |= (current & ieee_754_mantissa_mask[i]);
201 return is_special_quantity;
213 int is_negative = TRIO_FALSE;
215 for (i = 0; i < (unsigned int)sizeof(double); i++) {
216 is_negative |= (((unsigned char *)&number)[TRIO_DOUBLE_INDEX(i)]
217 & ieee_754_sign_mask[i]);
222 #endif /* USE_IEEE_754 */
226 Generate negative zero.
228 @return Floating-point representation of negative zero.
231 trio_nzero(TRIO_NOARGS)
233 #if defined(USE_IEEE_754)
234 return trio_make_double(ieee_754_negzero_array);
236 TRIO_VOLATILE double zero = 0.0;
243 Generate positive infinity.
245 @return Floating-point representation of positive infinity.
248 trio_pinf(TRIO_NOARGS)
250 /* Cache the result */
251 static double result = 0.0;
255 #if defined(INFINITY) && defined(__STDC_IEC_559__)
256 result = (double)INFINITY;
258 #elif defined(USE_IEEE_754)
259 result = trio_make_double(ieee_754_infinity_array);
263 * If HUGE_VAL is different from DBL_MAX, then HUGE_VAL is used
264 * as infinity. Otherwise we have to resort to an overflow
265 * operation to generate infinity.
267 # if defined(TRIO_PLATFORM_UNIX)
268 void (*signal_handler)(int) = signal(SIGFPE, SIG_IGN);
272 if (HUGE_VAL == DBL_MAX) {
277 # if defined(TRIO_PLATFORM_UNIX)
278 signal(SIGFPE, signal_handler);
287 Generate negative infinity.
289 @return Floating-point value of negative infinity.
292 trio_ninf(TRIO_NOARGS)
294 static double result = 0.0;
298 * Negative infinity is calculated by negating positive infinity,
299 * which can be done because it is legal to do calculations on
300 * infinity (for example, 1 / infinity == 0).
302 result = -trio_pinf();
310 @return Floating-point representation of NaN.
313 trio_nan(TRIO_NOARGS)
315 /* Cache the result */
316 static double result = 0.0;
320 #if defined(TRIO_COMPILER_SUPPORTS_C99)
323 #elif defined(NAN) && defined(__STDC_IEC_559__)
324 result = (double)NAN;
326 #elif defined(USE_IEEE_754)
327 result = trio_make_double(ieee_754_qnan_array);
331 * There are several ways to generate NaN. The one used here is
332 * to divide infinity by infinity. I would have preferred to add
333 * negative infinity to positive infinity, but that yields wrong
334 * result (infinity) on FreeBSD.
336 * This may fail if the hardware does not support NaN, or if
337 * the Invalid Operation floating-point exception is unmasked.
339 # if defined(TRIO_PLATFORM_UNIX)
340 void (*signal_handler)(int) = signal(SIGFPE, SIG_IGN);
343 result = trio_pinf() / trio_pinf();
345 # if defined(TRIO_PLATFORM_UNIX)
346 signal(SIGFPE, signal_handler);
357 @param number An arbitrary floating-point number.
358 @return Boolean value indicating whether or not the number is a NaN.
365 #if (defined(TRIO_COMPILER_SUPPORTS_C99) && defined(isnan)) \
366 || defined(TRIO_COMPILER_SUPPORTS_UNIX95)
368 * C99 defines isnan() as a macro. UNIX95 defines isnan() as a
369 * function. This function was already present in XPG4, but this
370 * is a bit tricky to detect with compiler defines, so we choose
371 * the conservative approach and only use it for UNIX95.
373 return isnan(number);
375 #elif defined(TRIO_COMPILER_MSVC)
377 * MSVC has an _isnan() function
379 return _isnan(number);
381 #elif defined(USE_IEEE_754)
383 * Examine IEEE 754 bit-pattern. A NaN must have a special exponent
384 * pattern, and a non-empty mantissa.
387 int is_special_quantity;
389 is_special_quantity = trio_is_special_quantity(number, &has_mantissa);
391 return (is_special_quantity && has_mantissa);
398 double integral, fraction;
400 # if defined(TRIO_PLATFORM_UNIX)
401 void (*signal_handler)(int) = signal(SIGFPE, SIG_IGN);
405 * NaN is the only number which does not compare to itself
407 ((TRIO_VOLATILE double)number != (TRIO_VOLATILE double)number) ||
409 * Fallback solution if NaN compares to NaN
412 (fraction = modf(number, &integral),
413 integral == fraction)));
415 # if defined(TRIO_PLATFORM_UNIX)
416 signal(SIGFPE, signal_handler);
427 @param number An arbitrary floating-point number.
428 @return 1 if positive infinity, -1 if negative infinity, 0 otherwise.
435 #if defined(TRIO_COMPILER_DECC)
437 * DECC has an isinf() macro, but it works differently than that
438 * of C99, so we use the fp_class() function instead.
440 return ((fp_class(number) == FP_POS_INF)
442 : ((fp_class(number) == FP_NEG_INF) ? -1 : 0));
446 * C99 defines isinf() as a macro.
449 ? ((number > 0.0) ? 1 : -1)
452 #elif defined(TRIO_COMPILER_MSVC)
454 * MSVC has an _fpclass() function that can be used to detect infinity.
456 return ((_fpclass(number) == _FPCLASS_PINF)
458 : ((_fpclass(number) == _FPCLASS_NINF) ? -1 : 0));
460 #elif defined(USE_IEEE_754)
462 * Examine IEEE 754 bit-pattern. Infinity must have a special exponent
463 * pattern, and an empty mantissa.
466 int is_special_quantity;
468 is_special_quantity = trio_is_special_quantity(number, &has_mantissa);
470 return (is_special_quantity && !has_mantissa)
471 ? ((number < 0.0) ? -1 : 1)
480 # if defined(TRIO_PLATFORM_UNIX)
481 void (*signal_handler)(int) = signal(SIGFPE, SIG_IGN);
484 double infinity = trio_pinf();
486 status = ((number == infinity)
488 : ((number == -infinity) ? -1 : 0));
490 # if defined(TRIO_PLATFORM_UNIX)
491 signal(SIGFPE, signal_handler);
503 @param number An arbitrary floating-point number.
504 @return Boolean value indicating whether or not the number is a finite.
511 #if defined(TRIO_COMPILER_SUPPORTS_C99) && defined(isfinite)
513 * C99 defines isfinite() as a macro.
515 return isfinite(number);
517 #elif defined(TRIO_COMPILER_MSVC)
519 * MSVC uses _finite().
521 return _finite(number);
523 #elif defined(USE_IEEE_754)
525 * Examine IEEE 754 bit-pattern. For finity we do not care about the
530 return (! trio_is_special_quantity(number, &dummy));
536 return ((trio_isinf(number) == 0) && (trio_isnan(number) == 0));
542 * The sign of NaN is always false
545 trio_fpclassify_and_signbit
546 TRIO_ARGS2((number, is_negative),
550 #if defined(fpclassify) && defined(signbit)
552 * C99 defines fpclassify() and signbit() as a macros
554 *is_negative = signbit(number);
555 switch (fpclassify(number)) {
559 return TRIO_FP_INFINITE;
561 return TRIO_FP_SUBNORMAL;
565 return TRIO_FP_NORMAL;
568 #elif defined(TRIO_COMPILER_DECC)
570 * DECC has an fp_class() function.
572 switch (fp_class(number)) {
575 *is_negative = TRIO_FALSE; /* NaN has no sign */
578 *is_negative = TRIO_FALSE;
579 return TRIO_FP_INFINITE;
581 *is_negative = TRIO_TRUE;
582 return TRIO_FP_INFINITE;
584 *is_negative = TRIO_FALSE;
585 return TRIO_FP_SUBNORMAL;
587 *is_negative = TRIO_TRUE;
588 return TRIO_FP_SUBNORMAL;
590 *is_negative = TRIO_FALSE;
593 *is_negative = TRIO_TRUE;
596 *is_negative = TRIO_FALSE;
597 return TRIO_FP_NORMAL;
599 *is_negative = TRIO_TRUE;
600 return TRIO_FP_NORMAL;
602 /* Just in case... */
603 *is_negative = (number < 0.0);
604 return TRIO_FP_NORMAL;
607 #elif defined(TRIO_COMPILER_MSVC)
609 * MSVC has an _fpclass() function.
611 switch (_fpclass(number)) {
614 *is_negative = TRIO_FALSE;
617 *is_negative = TRIO_FALSE;
618 return TRIO_FP_INFINITE;
620 *is_negative = TRIO_TRUE;
621 return TRIO_FP_INFINITE;
623 *is_negative = TRIO_FALSE;
624 return TRIO_FP_SUBNORMAL;
626 *is_negative = TRIO_TRUE;
627 return TRIO_FP_SUBNORMAL;
629 *is_negative = TRIO_FALSE;
632 *is_negative = TRIO_TRUE;
635 *is_negative = TRIO_FALSE;
636 return TRIO_FP_NORMAL;
638 *is_negative = TRIO_TRUE;
639 return TRIO_FP_NORMAL;
641 /* Just in case... */
642 *is_negative = (number < 0.0);
643 return TRIO_FP_NORMAL;
646 #elif defined(FP_PLUS_NORM)
649 * HP-UX 9.x and 10.x have an fpclassify() function, that is different
650 * from the C99 fpclassify() macro supported on HP-UX 11.x.
652 * AIX has class() for C, and _class() for C++, which returns the
653 * same values as the HP-UX fpclassify() function.
656 # if defined(TRIO_PLATFORM_AIX)
657 # if defined(__cplusplus)
658 # define fpclassify(x) _class(x)
660 # define fpclassify(x) class(x)
664 switch (fpclassify(number)) {
667 *is_negative = TRIO_FALSE;
670 *is_negative = TRIO_FALSE;
671 return TRIO_FP_INFINITE;
673 *is_negative = TRIO_TRUE;
674 return TRIO_FP_INFINITE;
676 *is_negative = TRIO_FALSE;
677 return TRIO_FP_SUBNORMAL;
678 case FP_MINUS_DENORM:
679 *is_negative = TRIO_TRUE;
680 return TRIO_FP_SUBNORMAL;
682 *is_negative = TRIO_FALSE;
685 *is_negative = TRIO_TRUE;
688 *is_negative = TRIO_FALSE;
689 return TRIO_FP_NORMAL;
691 *is_negative = TRIO_TRUE;
692 return TRIO_FP_NORMAL;
705 * In IEEE 754 the sign of zero is ignored in comparisons, so we
706 * have to handle this as a special case by examining the sign bit
709 #if defined(USE_IEEE_754)
710 *is_negative = trio_is_negative(number);
712 *is_negative = TRIO_FALSE; /* FIXME */
716 if (trio_isnan(number)) {
717 *is_negative = TRIO_FALSE;
720 if ((rc = trio_isinf(number))) {
721 *is_negative = (rc == -1);
722 return TRIO_FP_INFINITE;
724 if ((number > 0.0) && (number < DBL_MIN)) {
725 *is_negative = TRIO_FALSE;
726 return TRIO_FP_SUBNORMAL;
728 if ((number < 0.0) && (number > -DBL_MIN)) {
729 *is_negative = TRIO_TRUE;
730 return TRIO_FP_SUBNORMAL;
732 *is_negative = (number < 0.0);
733 return TRIO_FP_NORMAL;
739 Examine the sign of a number.
741 @param number An arbitrary floating-point number.
742 @return Boolean value indicating whether or not the number has the
743 sign bit set (i.e. is negative).
752 (void)trio_fpclassify_and_signbit(number, &is_negative);
757 Examine the class of a number.
759 @param number An arbitrary floating-point number.
760 @return Enumerable value indicating the class of @p number
769 return trio_fpclassify_and_signbit(number, &dummy);
773 /** @} SpecialQuantities */
775 /*************************************************************************
778 * Add the following compiler option to include this test code.
780 * Unix : -DSTANDALONE
781 * VMS : /DEFINE=(STANDALONE)
783 #if defined(STANDALONE)
786 static TRIO_CONST char *
792 case TRIO_FP_INFINITE:
793 return "FP_INFINITE";
798 case TRIO_FP_SUBNORMAL:
799 return "FP_SUBNORMAL";
809 TRIO_ARGS2((prefix, number),
810 TRIO_CONST char *prefix,
813 printf("%-6s: %s %-15s %g\n",
815 trio_signbit(number) ? "-" : "+",
816 getClassification(trio_fpclassify(number)),
820 int main(TRIO_NOARGS)
825 # if defined(TRIO_PLATFORM_UNIX)
826 void (*signal_handler) TRIO_PROTO((int));
830 my_pinf = trio_pinf();
831 my_ninf = trio_ninf();
833 print_class("Nan", my_nan);
834 print_class("PInf", my_pinf);
835 print_class("NInf", my_ninf);
836 print_class("PZero", 0.0);
837 print_class("NZero", -0.0);
838 print_class("PNorm", 1.0);
839 print_class("NNorm", -1.0);
840 print_class("PSub", 1.01e-307 - 1.00e-307);
841 print_class("NSub", 1.00e-307 - 1.01e-307);
843 printf("NaN : %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d)\n",
845 ((unsigned char *)&my_nan)[0],
846 ((unsigned char *)&my_nan)[1],
847 ((unsigned char *)&my_nan)[2],
848 ((unsigned char *)&my_nan)[3],
849 ((unsigned char *)&my_nan)[4],
850 ((unsigned char *)&my_nan)[5],
851 ((unsigned char *)&my_nan)[6],
852 ((unsigned char *)&my_nan)[7],
853 trio_isnan(my_nan), trio_isinf(my_nan));
854 printf("PInf: %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d)\n",
856 ((unsigned char *)&my_pinf)[0],
857 ((unsigned char *)&my_pinf)[1],
858 ((unsigned char *)&my_pinf)[2],
859 ((unsigned char *)&my_pinf)[3],
860 ((unsigned char *)&my_pinf)[4],
861 ((unsigned char *)&my_pinf)[5],
862 ((unsigned char *)&my_pinf)[6],
863 ((unsigned char *)&my_pinf)[7],
864 trio_isnan(my_pinf), trio_isinf(my_pinf));
865 printf("NInf: %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d)\n",
867 ((unsigned char *)&my_ninf)[0],
868 ((unsigned char *)&my_ninf)[1],
869 ((unsigned char *)&my_ninf)[2],
870 ((unsigned char *)&my_ninf)[3],
871 ((unsigned char *)&my_ninf)[4],
872 ((unsigned char *)&my_ninf)[5],
873 ((unsigned char *)&my_ninf)[6],
874 ((unsigned char *)&my_ninf)[7],
875 trio_isnan(my_ninf), trio_isinf(my_ninf));
877 # if defined(TRIO_PLATFORM_UNIX)
878 signal_handler = signal(SIGFPE, SIG_IGN);
881 my_pinf = DBL_MAX + DBL_MAX;
883 my_nan = my_pinf / my_pinf;
885 # if defined(TRIO_PLATFORM_UNIX)
886 signal(SIGFPE, signal_handler);
889 printf("NaN : %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d)\n",
891 ((unsigned char *)&my_nan)[0],
892 ((unsigned char *)&my_nan)[1],
893 ((unsigned char *)&my_nan)[2],
894 ((unsigned char *)&my_nan)[3],
895 ((unsigned char *)&my_nan)[4],
896 ((unsigned char *)&my_nan)[5],
897 ((unsigned char *)&my_nan)[6],
898 ((unsigned char *)&my_nan)[7],
899 trio_isnan(my_nan), trio_isinf(my_nan));
900 printf("PInf: %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d)\n",
902 ((unsigned char *)&my_pinf)[0],
903 ((unsigned char *)&my_pinf)[1],
904 ((unsigned char *)&my_pinf)[2],
905 ((unsigned char *)&my_pinf)[3],
906 ((unsigned char *)&my_pinf)[4],
907 ((unsigned char *)&my_pinf)[5],
908 ((unsigned char *)&my_pinf)[6],
909 ((unsigned char *)&my_pinf)[7],
910 trio_isnan(my_pinf), trio_isinf(my_pinf));
911 printf("NInf: %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d)\n",
913 ((unsigned char *)&my_ninf)[0],
914 ((unsigned char *)&my_ninf)[1],
915 ((unsigned char *)&my_ninf)[2],
916 ((unsigned char *)&my_ninf)[3],
917 ((unsigned char *)&my_ninf)[4],
918 ((unsigned char *)&my_ninf)[5],
919 ((unsigned char *)&my_ninf)[6],
920 ((unsigned char *)&my_ninf)[7],
921 trio_isnan(my_ninf), trio_isinf(my_ninf));