00b2042673e06c633aa1193a8158ee16e96bddae
[reactos.git] / reactos / lib / 3rdparty / libxml2 / trionan.c
1 /*************************************************************************
2 *
3 * $Id: trionan.c 3790 2008-09-01 13:08:57Z veillard $
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 *
32 ************************************************************************/
33
34 /*
35 * TODO:
36 * o Put all the magic into trio_fpclassify_and_signbit(), and use this from
37 * trio_isnan() etc.
38 */
39
40 /*************************************************************************
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
53 #if defined(TRIO_COMPILER_DECC)
54 # if defined(__linux__)
55 # include <cpml.h>
56 # else
57 # include <fp_class.h>
58 # endif
59 #endif
60 /* Small ReactOS hack */
61 #define fpclassify _fpclass
62 #include <assert.h>
63
64 #if defined(TRIO_DOCUMENTATION)
65 # include "doc/doc_nan.h"
66 #endif
67 /** @addtogroup SpecialQuantities
68 @{
69 */
70
71 /*************************************************************************
72 * Definitions
73 */
74
75 #define TRIO_TRUE (1 == 1)
76 #define TRIO_FALSE (0 == 1)
77
78 /*
79 * We must enable IEEE floating-point on Alpha
80 */
81 #if defined(__alpha) && !defined(_IEEE_FP)
82 # if defined(TRIO_COMPILER_DECC)
83 # if defined(TRIO_PLATFORM_VMS)
84 # error "Must be compiled with option /IEEE_MODE=UNDERFLOW_TO_ZERO/FLOAT=IEEE"
85 # else
86 # if !defined(_CFE)
87 # error "Must be compiled with option -ieee"
88 # endif
89 # endif
90 # elif defined(TRIO_COMPILER_GCC) && (defined(__osf__) || defined(__linux__))
91 # error "Must be compiled with option -mieee"
92 # endif
93 #endif /* __alpha && ! _IEEE_FP */
94
95 /*
96 * In ANSI/IEEE 754-1985 64-bits double format numbers have the
97 * following properties (amoungst others)
98 *
99 * o FLT_RADIX == 2: binary encoding
100 * o DBL_MAX_EXP == 1024: 11 bits exponent, where one bit is used
101 * to indicate special numbers (e.g. NaN and Infinity), so the
102 * maximum exponent is 10 bits wide (2^10 == 1024).
103 * o DBL_MANT_DIG == 53: The mantissa is 52 bits wide, but because
104 * numbers are normalized the initial binary 1 is represented
105 * implicitly (the so-called "hidden bit"), which leaves us with
106 * the ability to represent 53 bits wide mantissa.
107 */
108 #if (FLT_RADIX == 2) && (DBL_MAX_EXP == 1024) && (DBL_MANT_DIG == 53)
109 # define USE_IEEE_754
110 #endif
111
112
113 /*************************************************************************
114 * Constants
115 */
116
117 static TRIO_CONST char rcsid[] = "@(#)$Id: trionan.c 3790 2008-09-01 13:08:57Z veillard $";
118
119 #if defined(USE_IEEE_754)
120
121 /*
122 * Endian-agnostic indexing macro.
123 *
124 * The value of internalEndianMagic, when converted into a 64-bit
125 * integer, becomes 0x0706050403020100 (we could have used a 64-bit
126 * integer value instead of a double, but not all platforms supports
127 * that type). The value is automatically encoded with the correct
128 * endianess by the compiler, which means that we can support any
129 * kind of endianess. The individual bytes are then used as an index
130 * for the IEEE 754 bit-patterns and masks.
131 */
132 #define TRIO_DOUBLE_INDEX(x) (((unsigned char *)&internalEndianMagic)[7-(x)])
133
134 #if (defined(__BORLANDC__) && __BORLANDC__ >= 0x0590)
135 static TRIO_CONST double internalEndianMagic = 7.949928895127362e-275;
136 #else
137 static TRIO_CONST double internalEndianMagic = 7.949928895127363e-275;
138 #endif
139
140 /* Mask for the exponent */
141 static TRIO_CONST unsigned char ieee_754_exponent_mask[] = {
142 0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
143 };
144
145 /* Mask for the mantissa */
146 static TRIO_CONST unsigned char ieee_754_mantissa_mask[] = {
147 0x00, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
148 };
149
150 /* Mask for the sign bit */
151 static TRIO_CONST unsigned char ieee_754_sign_mask[] = {
152 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
153 };
154
155 /* Bit-pattern for negative zero */
156 static TRIO_CONST unsigned char ieee_754_negzero_array[] = {
157 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
158 };
159
160 /* Bit-pattern for infinity */
161 static TRIO_CONST unsigned char ieee_754_infinity_array[] = {
162 0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
163 };
164
165 /* Bit-pattern for quiet NaN */
166 static TRIO_CONST unsigned char ieee_754_qnan_array[] = {
167 0x7F, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
168 };
169
170
171 /*************************************************************************
172 * Functions
173 */
174
175 /*
176 * trio_make_double
177 */
178 TRIO_PRIVATE double
179 trio_make_double
180 TRIO_ARGS1((values),
181 TRIO_CONST unsigned char *values)
182 {
183 TRIO_VOLATILE double result;
184 int i;
185
186 for (i = 0; i < (int)sizeof(double); i++) {
187 ((TRIO_VOLATILE unsigned char *)&result)[TRIO_DOUBLE_INDEX(i)] = values[i];
188 }
189 return result;
190 }
191
192 /*
193 * trio_is_special_quantity
194 */
195 TRIO_PRIVATE int
196 trio_is_special_quantity
197 TRIO_ARGS2((number, has_mantissa),
198 double number,
199 int *has_mantissa)
200 {
201 unsigned int i;
202 unsigned char current;
203 int is_special_quantity = TRIO_TRUE;
204
205 *has_mantissa = 0;
206
207 for (i = 0; i < (unsigned int)sizeof(double); i++) {
208 current = ((unsigned char *)&number)[TRIO_DOUBLE_INDEX(i)];
209 is_special_quantity
210 &= ((current & ieee_754_exponent_mask[i]) == ieee_754_exponent_mask[i]);
211 *has_mantissa |= (current & ieee_754_mantissa_mask[i]);
212 }
213 return is_special_quantity;
214 }
215
216 /*
217 * trio_is_negative
218 */
219 TRIO_PRIVATE int
220 trio_is_negative
221 TRIO_ARGS1((number),
222 double number)
223 {
224 unsigned int i;
225 int is_negative = TRIO_FALSE;
226
227 for (i = 0; i < (unsigned int)sizeof(double); i++) {
228 is_negative |= (((unsigned char *)&number)[TRIO_DOUBLE_INDEX(i)]
229 & ieee_754_sign_mask[i]);
230 }
231 return is_negative;
232 }
233
234 #endif /* USE_IEEE_754 */
235
236
237 /**
238 Generate negative zero.
239
240 @return Floating-point representation of negative zero.
241 */
242 TRIO_PUBLIC double
243 trio_nzero(TRIO_NOARGS)
244 {
245 #if defined(USE_IEEE_754)
246 return trio_make_double(ieee_754_negzero_array);
247 #else
248 TRIO_VOLATILE double zero = 0.0;
249
250 return -zero;
251 #endif
252 }
253
254 /**
255 Generate positive infinity.
256
257 @return Floating-point representation of positive infinity.
258 */
259 TRIO_PUBLIC double
260 trio_pinf(TRIO_NOARGS)
261 {
262 /* Cache the result */
263 static double result = 0.0;
264
265 if (result == 0.0) {
266
267 #if defined(INFINITY) && defined(__STDC_IEC_559__)
268 result = (double)INFINITY;
269
270 #elif defined(USE_IEEE_754)
271 result = trio_make_double(ieee_754_infinity_array);
272
273 #else
274 /*
275 * If HUGE_VAL is different from DBL_MAX, then HUGE_VAL is used
276 * as infinity. Otherwise we have to resort to an overflow
277 * operation to generate infinity.
278 */
279 # if defined(TRIO_PLATFORM_UNIX)
280 void (*signal_handler)(int) = signal(SIGFPE, SIG_IGN);
281 # endif
282
283 result = HUGE_VAL;
284 if (HUGE_VAL == DBL_MAX) {
285 /* Force overflow */
286 result += HUGE_VAL;
287 }
288
289 # if defined(TRIO_PLATFORM_UNIX)
290 signal(SIGFPE, signal_handler);
291 # endif
292
293 #endif
294 }
295 return result;
296 }
297
298 /**
299 Generate negative infinity.
300
301 @return Floating-point value of negative infinity.
302 */
303 TRIO_PUBLIC double
304 trio_ninf(TRIO_NOARGS)
305 {
306 static double result = 0.0;
307
308 if (result == 0.0) {
309 /*
310 * Negative infinity is calculated by negating positive infinity,
311 * which can be done because it is legal to do calculations on
312 * infinity (for example, 1 / infinity == 0).
313 */
314 result = -trio_pinf();
315 }
316 return result;
317 }
318
319 /**
320 Generate NaN.
321
322 @return Floating-point representation of NaN.
323 */
324 TRIO_PUBLIC double
325 trio_nan(TRIO_NOARGS)
326 {
327 /* Cache the result */
328 static double result = 0.0;
329
330 if (result == 0.0) {
331
332 #if defined(TRIO_COMPILER_SUPPORTS_C99)
333 result = nan("");
334
335 #elif defined(NAN) && defined(__STDC_IEC_559__)
336 result = (double)NAN;
337
338 #elif defined(USE_IEEE_754)
339 result = trio_make_double(ieee_754_qnan_array);
340
341 #else
342 /*
343 * There are several ways to generate NaN. The one used here is
344 * to divide infinity by infinity. I would have preferred to add
345 * negative infinity to positive infinity, but that yields wrong
346 * result (infinity) on FreeBSD.
347 *
348 * This may fail if the hardware does not support NaN, or if
349 * the Invalid Operation floating-point exception is unmasked.
350 */
351 # if defined(TRIO_PLATFORM_UNIX)
352 void (*signal_handler)(int) = signal(SIGFPE, SIG_IGN);
353 # endif
354
355 result = trio_pinf() / trio_pinf();
356
357 # if defined(TRIO_PLATFORM_UNIX)
358 signal(SIGFPE, signal_handler);
359 # endif
360
361 #endif
362 }
363 return result;
364 }
365
366 /**
367 Check for NaN.
368
369 @param number An arbitrary floating-point number.
370 @return Boolean value indicating whether or not the number is a NaN.
371 */
372 TRIO_PUBLIC int
373 trio_isnan
374 TRIO_ARGS1((number),
375 double number)
376 {
377 #if (defined(TRIO_COMPILER_SUPPORTS_C99) && defined(isnan)) \
378 || defined(TRIO_COMPILER_SUPPORTS_UNIX95)
379 /*
380 * C99 defines isnan() as a macro. UNIX95 defines isnan() as a
381 * function. This function was already present in XPG4, but this
382 * is a bit tricky to detect with compiler defines, so we choose
383 * the conservative approach and only use it for UNIX95.
384 */
385 return isnan(number);
386
387 #elif defined(TRIO_COMPILER_MSVC) || defined(TRIO_COMPILER_BCB)
388 /*
389 * Microsoft Visual C++ and Borland C++ Builder have an _isnan()
390 * function.
391 */
392 return _isnan(number) ? TRIO_TRUE : TRIO_FALSE;
393
394 #elif defined(USE_IEEE_754)
395 /*
396 * Examine IEEE 754 bit-pattern. A NaN must have a special exponent
397 * pattern, and a non-empty mantissa.
398 */
399 int has_mantissa;
400 int is_special_quantity;
401
402 is_special_quantity = trio_is_special_quantity(number, &has_mantissa);
403
404 return (is_special_quantity && has_mantissa);
405
406 #else
407 /*
408 * Fallback solution
409 */
410 int status;
411 double integral, fraction;
412
413 # if defined(TRIO_PLATFORM_UNIX)
414 void (*signal_handler)(int) = signal(SIGFPE, SIG_IGN);
415 # endif
416
417 status = (/*
418 * NaN is the only number which does not compare to itself
419 */
420 ((TRIO_VOLATILE double)number != (TRIO_VOLATILE double)number) ||
421 /*
422 * Fallback solution if NaN compares to NaN
423 */
424 ((number != 0.0) &&
425 (fraction = modf(number, &integral),
426 integral == fraction)));
427
428 # if defined(TRIO_PLATFORM_UNIX)
429 signal(SIGFPE, signal_handler);
430 # endif
431
432 return status;
433
434 #endif
435 }
436
437 /**
438 Check for infinity.
439
440 @param number An arbitrary floating-point number.
441 @return 1 if positive infinity, -1 if negative infinity, 0 otherwise.
442 */
443 TRIO_PUBLIC int
444 trio_isinf
445 TRIO_ARGS1((number),
446 double number)
447 {
448 #if defined(TRIO_COMPILER_DECC) && !defined(__linux__)
449 /*
450 * DECC has an isinf() macro, but it works differently than that
451 * of C99, so we use the fp_class() function instead.
452 */
453 return ((fp_class(number) == FP_POS_INF)
454 ? 1
455 : ((fp_class(number) == FP_NEG_INF) ? -1 : 0));
456
457 #elif defined(isinf)
458 /*
459 * C99 defines isinf() as a macro.
460 */
461 return isinf(number)
462 ? ((number > 0.0) ? 1 : -1)
463 : 0;
464
465 #elif defined(TRIO_COMPILER_MSVC) || defined(TRIO_COMPILER_BCB)
466 /*
467 * Microsoft Visual C++ and Borland C++ Builder have an _fpclass()
468 * function that can be used to detect infinity.
469 */
470 return ((_fpclass(number) == _FPCLASS_PINF)
471 ? 1
472 : ((_fpclass(number) == _FPCLASS_NINF) ? -1 : 0));
473
474 #elif defined(USE_IEEE_754)
475 /*
476 * Examine IEEE 754 bit-pattern. Infinity must have a special exponent
477 * pattern, and an empty mantissa.
478 */
479 int has_mantissa;
480 int is_special_quantity;
481
482 is_special_quantity = trio_is_special_quantity(number, &has_mantissa);
483
484 return (is_special_quantity && !has_mantissa)
485 ? ((number < 0.0) ? -1 : 1)
486 : 0;
487
488 #else
489 /*
490 * Fallback solution.
491 */
492 int status;
493
494 # if defined(TRIO_PLATFORM_UNIX)
495 void (*signal_handler)(int) = signal(SIGFPE, SIG_IGN);
496 # endif
497
498 double infinity = trio_pinf();
499
500 status = ((number == infinity)
501 ? 1
502 : ((number == -infinity) ? -1 : 0));
503
504 # if defined(TRIO_PLATFORM_UNIX)
505 signal(SIGFPE, signal_handler);
506 # endif
507
508 return status;
509
510 #endif
511 }
512
513 #if 0
514 /* Temporary fix - this routine is not used anywhere */
515 /**
516 Check for finity.
517
518 @param number An arbitrary floating-point number.
519 @return Boolean value indicating whether or not the number is a finite.
520 */
521 TRIO_PUBLIC int
522 trio_isfinite
523 TRIO_ARGS1((number),
524 double number)
525 {
526 #if defined(TRIO_COMPILER_SUPPORTS_C99) && defined(isfinite)
527 /*
528 * C99 defines isfinite() as a macro.
529 */
530 return isfinite(number);
531
532 #elif defined(TRIO_COMPILER_MSVC) || defined(TRIO_COMPILER_BCB)
533 /*
534 * Microsoft Visual C++ and Borland C++ Builder use _finite().
535 */
536 return _finite(number);
537
538 #elif defined(USE_IEEE_754)
539 /*
540 * Examine IEEE 754 bit-pattern. For finity we do not care about the
541 * mantissa.
542 */
543 int dummy;
544
545 return (! trio_is_special_quantity(number, &dummy));
546
547 #else
548 /*
549 * Fallback solution.
550 */
551 return ((trio_isinf(number) == 0) && (trio_isnan(number) == 0));
552
553 #endif
554 }
555
556 #endif
557
558 /*
559 * The sign of NaN is always false
560 */
561 TRIO_PUBLIC int
562 trio_fpclassify_and_signbit
563 TRIO_ARGS2((number, is_negative),
564 double number,
565 int *is_negative)
566 {
567 #if defined(fpclassify) && defined(signbit)
568 /*
569 * C99 defines fpclassify() and signbit() as a macros
570 */
571 *is_negative = signbit(number);
572 switch (fpclassify(number)) {
573 case FP_NAN:
574 return TRIO_FP_NAN;
575 case FP_INFINITE:
576 return TRIO_FP_INFINITE;
577 case FP_SUBNORMAL:
578 return TRIO_FP_SUBNORMAL;
579 case FP_ZERO:
580 return TRIO_FP_ZERO;
581 default:
582 return TRIO_FP_NORMAL;
583 }
584
585 #else
586 # if defined(TRIO_COMPILER_DECC)
587 /*
588 * DECC has an fp_class() function.
589 */
590 # define TRIO_FPCLASSIFY(n) fp_class(n)
591 # define TRIO_QUIET_NAN FP_QNAN
592 # define TRIO_SIGNALLING_NAN FP_SNAN
593 # define TRIO_POSITIVE_INFINITY FP_POS_INF
594 # define TRIO_NEGATIVE_INFINITY FP_NEG_INF
595 # define TRIO_POSITIVE_SUBNORMAL FP_POS_DENORM
596 # define TRIO_NEGATIVE_SUBNORMAL FP_NEG_DENORM
597 # define TRIO_POSITIVE_ZERO FP_POS_ZERO
598 # define TRIO_NEGATIVE_ZERO FP_NEG_ZERO
599 # define TRIO_POSITIVE_NORMAL FP_POS_NORM
600 # define TRIO_NEGATIVE_NORMAL FP_NEG_NORM
601
602 # elif defined(TRIO_COMPILER_MSVC) || defined(TRIO_COMPILER_BCB)
603 /*
604 * Microsoft Visual C++ and Borland C++ Builder have an _fpclass()
605 * function.
606 */
607 # define TRIO_FPCLASSIFY(n) _fpclass(n)
608 # define TRIO_QUIET_NAN _FPCLASS_QNAN
609 # define TRIO_SIGNALLING_NAN _FPCLASS_SNAN
610 # define TRIO_POSITIVE_INFINITY _FPCLASS_PINF
611 # define TRIO_NEGATIVE_INFINITY _FPCLASS_NINF
612 # define TRIO_POSITIVE_SUBNORMAL _FPCLASS_PD
613 # define TRIO_NEGATIVE_SUBNORMAL _FPCLASS_ND
614 # define TRIO_POSITIVE_ZERO _FPCLASS_PZ
615 # define TRIO_NEGATIVE_ZERO _FPCLASS_NZ
616 # define TRIO_POSITIVE_NORMAL _FPCLASS_PN
617 # define TRIO_NEGATIVE_NORMAL _FPCLASS_NN
618
619 # elif defined(FP_PLUS_NORM)
620 /*
621 * HP-UX 9.x and 10.x have an fpclassify() function, that is different
622 * from the C99 fpclassify() macro supported on HP-UX 11.x.
623 *
624 * AIX has class() for C, and _class() for C++, which returns the
625 * same values as the HP-UX fpclassify() function.
626 */
627 # if defined(TRIO_PLATFORM_AIX)
628 # if defined(__cplusplus)
629 # define TRIO_FPCLASSIFY(n) _class(n)
630 # else
631 # define TRIO_FPCLASSIFY(n) class(n)
632 # endif
633 # else
634 # define TRIO_FPCLASSIFY(n) fpclassify(n)
635 # endif
636 # define TRIO_QUIET_NAN FP_QNAN
637 # define TRIO_SIGNALLING_NAN FP_SNAN
638 # define TRIO_POSITIVE_INFINITY FP_PLUS_INF
639 # define TRIO_NEGATIVE_INFINITY FP_MINUS_INF
640 # define TRIO_POSITIVE_SUBNORMAL FP_PLUS_DENORM
641 # define TRIO_NEGATIVE_SUBNORMAL FP_MINUS_DENORM
642 # define TRIO_POSITIVE_ZERO FP_PLUS_ZERO
643 # define TRIO_NEGATIVE_ZERO FP_MINUS_ZERO
644 # define TRIO_POSITIVE_NORMAL FP_PLUS_NORM
645 # define TRIO_NEGATIVE_NORMAL FP_MINUS_NORM
646 # endif
647
648 # if defined(TRIO_FPCLASSIFY)
649 switch (TRIO_FPCLASSIFY(number)) {
650 case TRIO_QUIET_NAN:
651 case TRIO_SIGNALLING_NAN:
652 *is_negative = TRIO_FALSE; /* NaN has no sign */
653 return TRIO_FP_NAN;
654 case TRIO_POSITIVE_INFINITY:
655 *is_negative = TRIO_FALSE;
656 return TRIO_FP_INFINITE;
657 case TRIO_NEGATIVE_INFINITY:
658 *is_negative = TRIO_TRUE;
659 return TRIO_FP_INFINITE;
660 case TRIO_POSITIVE_SUBNORMAL:
661 *is_negative = TRIO_FALSE;
662 return TRIO_FP_SUBNORMAL;
663 case TRIO_NEGATIVE_SUBNORMAL:
664 *is_negative = TRIO_TRUE;
665 return TRIO_FP_SUBNORMAL;
666 case TRIO_POSITIVE_ZERO:
667 *is_negative = TRIO_FALSE;
668 return TRIO_FP_ZERO;
669 case TRIO_NEGATIVE_ZERO:
670 *is_negative = TRIO_TRUE;
671 return TRIO_FP_ZERO;
672 case TRIO_POSITIVE_NORMAL:
673 *is_negative = TRIO_FALSE;
674 return TRIO_FP_NORMAL;
675 case TRIO_NEGATIVE_NORMAL:
676 *is_negative = TRIO_TRUE;
677 return TRIO_FP_NORMAL;
678 default:
679 /* Just in case... */
680 *is_negative = (number < 0.0);
681 return TRIO_FP_NORMAL;
682 }
683
684 # else
685 /*
686 * Fallback solution.
687 */
688 int rc;
689
690 if (number == 0.0) {
691 /*
692 * In IEEE 754 the sign of zero is ignored in comparisons, so we
693 * have to handle this as a special case by examining the sign bit
694 * directly.
695 */
696 # if defined(USE_IEEE_754)
697 *is_negative = trio_is_negative(number);
698 # else
699 *is_negative = TRIO_FALSE; /* FIXME */
700 # endif
701 return TRIO_FP_ZERO;
702 }
703 if (trio_isnan(number)) {
704 *is_negative = TRIO_FALSE;
705 return TRIO_FP_NAN;
706 }
707 if ((rc = trio_isinf(number))) {
708 *is_negative = (rc == -1);
709 return TRIO_FP_INFINITE;
710 }
711 if ((number > 0.0) && (number < DBL_MIN)) {
712 *is_negative = TRIO_FALSE;
713 return TRIO_FP_SUBNORMAL;
714 }
715 if ((number < 0.0) && (number > -DBL_MIN)) {
716 *is_negative = TRIO_TRUE;
717 return TRIO_FP_SUBNORMAL;
718 }
719 *is_negative = (number < 0.0);
720 return TRIO_FP_NORMAL;
721
722 # endif
723 #endif
724 }
725
726 /**
727 Examine the sign of a number.
728
729 @param number An arbitrary floating-point number.
730 @return Boolean value indicating whether or not the number has the
731 sign bit set (i.e. is negative).
732 */
733 TRIO_PUBLIC int
734 trio_signbit
735 TRIO_ARGS1((number),
736 double number)
737 {
738 int is_negative;
739
740 (void)trio_fpclassify_and_signbit(number, &is_negative);
741 return is_negative;
742 }
743
744 #if 0
745 /* Temporary fix - this routine is not used in libxml */
746 /**
747 Examine the class of a number.
748
749 @param number An arbitrary floating-point number.
750 @return Enumerable value indicating the class of @p number
751 */
752 TRIO_PUBLIC int
753 trio_fpclassify
754 TRIO_ARGS1((number),
755 double number)
756 {
757 int dummy;
758
759 return trio_fpclassify_and_signbit(number, &dummy);
760 }
761
762 #endif
763
764 /** @} SpecialQuantities */
765
766 /*************************************************************************
767 * For test purposes.
768 *
769 * Add the following compiler option to include this test code.
770 *
771 * Unix : -DSTANDALONE
772 * VMS : /DEFINE=(STANDALONE)
773 */
774 #if defined(STANDALONE)
775 # include <stdio.h>
776
777 static TRIO_CONST char *
778 getClassification
779 TRIO_ARGS1((type),
780 int type)
781 {
782 switch (type) {
783 case TRIO_FP_INFINITE:
784 return "FP_INFINITE";
785 case TRIO_FP_NAN:
786 return "FP_NAN";
787 case TRIO_FP_NORMAL:
788 return "FP_NORMAL";
789 case TRIO_FP_SUBNORMAL:
790 return "FP_SUBNORMAL";
791 case TRIO_FP_ZERO:
792 return "FP_ZERO";
793 default:
794 return "FP_UNKNOWN";
795 }
796 }
797
798 static void
799 print_class
800 TRIO_ARGS2((prefix, number),
801 TRIO_CONST char *prefix,
802 double number)
803 {
804 printf("%-6s: %s %-15s %g\n",
805 prefix,
806 trio_signbit(number) ? "-" : "+",
807 getClassification(TRIO_FPCLASSIFY(number)),
808 number);
809 }
810
811 int main(TRIO_NOARGS)
812 {
813 double my_nan;
814 double my_pinf;
815 double my_ninf;
816 # if defined(TRIO_PLATFORM_UNIX)
817 void (*signal_handler) TRIO_PROTO((int));
818 # endif
819
820 my_nan = trio_nan();
821 my_pinf = trio_pinf();
822 my_ninf = trio_ninf();
823
824 print_class("Nan", my_nan);
825 print_class("PInf", my_pinf);
826 print_class("NInf", my_ninf);
827 print_class("PZero", 0.0);
828 print_class("NZero", -0.0);
829 print_class("PNorm", 1.0);
830 print_class("NNorm", -1.0);
831 print_class("PSub", 1.01e-307 - 1.00e-307);
832 print_class("NSub", 1.00e-307 - 1.01e-307);
833
834 printf("NaN : %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d)\n",
835 my_nan,
836 ((unsigned char *)&my_nan)[0],
837 ((unsigned char *)&my_nan)[1],
838 ((unsigned char *)&my_nan)[2],
839 ((unsigned char *)&my_nan)[3],
840 ((unsigned char *)&my_nan)[4],
841 ((unsigned char *)&my_nan)[5],
842 ((unsigned char *)&my_nan)[6],
843 ((unsigned char *)&my_nan)[7],
844 trio_isnan(my_nan), trio_isinf(my_nan));
845 printf("PInf: %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d)\n",
846 my_pinf,
847 ((unsigned char *)&my_pinf)[0],
848 ((unsigned char *)&my_pinf)[1],
849 ((unsigned char *)&my_pinf)[2],
850 ((unsigned char *)&my_pinf)[3],
851 ((unsigned char *)&my_pinf)[4],
852 ((unsigned char *)&my_pinf)[5],
853 ((unsigned char *)&my_pinf)[6],
854 ((unsigned char *)&my_pinf)[7],
855 trio_isnan(my_pinf), trio_isinf(my_pinf));
856 printf("NInf: %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d)\n",
857 my_ninf,
858 ((unsigned char *)&my_ninf)[0],
859 ((unsigned char *)&my_ninf)[1],
860 ((unsigned char *)&my_ninf)[2],
861 ((unsigned char *)&my_ninf)[3],
862 ((unsigned char *)&my_ninf)[4],
863 ((unsigned char *)&my_ninf)[5],
864 ((unsigned char *)&my_ninf)[6],
865 ((unsigned char *)&my_ninf)[7],
866 trio_isnan(my_ninf), trio_isinf(my_ninf));
867
868 # if defined(TRIO_PLATFORM_UNIX)
869 signal_handler = signal(SIGFPE, SIG_IGN);
870 # endif
871
872 my_pinf = DBL_MAX + DBL_MAX;
873 my_ninf = -my_pinf;
874 my_nan = my_pinf / my_pinf;
875
876 # if defined(TRIO_PLATFORM_UNIX)
877 signal(SIGFPE, signal_handler);
878 # endif
879
880 printf("NaN : %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d)\n",
881 my_nan,
882 ((unsigned char *)&my_nan)[0],
883 ((unsigned char *)&my_nan)[1],
884 ((unsigned char *)&my_nan)[2],
885 ((unsigned char *)&my_nan)[3],
886 ((unsigned char *)&my_nan)[4],
887 ((unsigned char *)&my_nan)[5],
888 ((unsigned char *)&my_nan)[6],
889 ((unsigned char *)&my_nan)[7],
890 trio_isnan(my_nan), trio_isinf(my_nan));
891 printf("PInf: %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d)\n",
892 my_pinf,
893 ((unsigned char *)&my_pinf)[0],
894 ((unsigned char *)&my_pinf)[1],
895 ((unsigned char *)&my_pinf)[2],
896 ((unsigned char *)&my_pinf)[3],
897 ((unsigned char *)&my_pinf)[4],
898 ((unsigned char *)&my_pinf)[5],
899 ((unsigned char *)&my_pinf)[6],
900 ((unsigned char *)&my_pinf)[7],
901 trio_isnan(my_pinf), trio_isinf(my_pinf));
902 printf("NInf: %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d)\n",
903 my_ninf,
904 ((unsigned char *)&my_ninf)[0],
905 ((unsigned char *)&my_ninf)[1],
906 ((unsigned char *)&my_ninf)[2],
907 ((unsigned char *)&my_ninf)[3],
908 ((unsigned char *)&my_ninf)[4],
909 ((unsigned char *)&my_ninf)[5],
910 ((unsigned char *)&my_ninf)[6],
911 ((unsigned char *)&my_ninf)[7],
912 trio_isnan(my_ninf), trio_isinf(my_ninf));
913
914 return 0;
915 }
916 #endif