Commit 45029607 authored by Bjorn Reese's avatar Bjorn Reese
Browse files

Re-worked NaN and Inf support

parent 344cee76
Tue Aug 21 11:18:45 CEST 2001 Bjorn Reese <breese@users.sourceforge.net>
* Makefile.am trio.c triodef.h trionan.c xpath.c
include/libxml/Makefile.am include/libxml/trionan.h:
Re-worked Not-A-Number and Infinity support.
* xmlcatalog.c: added readline include files
Mon Aug 20 02:04:13 CEST 2001 Daniel Veillard <daniel@veillard.com>
* Makefile.am xmlcatalog.c libxml.spec.in: renaming
......
......@@ -23,14 +23,14 @@ libxml2_la_SOURCES = SAX.c entities.c encoding.c error.c parserInternals.c \
parser.c tree.c hash.c list.c xmlIO.c xmlmemory.c uri.c \
valid.c xlink.c HTMLparser.c HTMLtree.c debugXML.c xpath.c \
xpointer.c xinclude.c nanohttp.c nanoftp.c DOCBparser.c \
catalog.c strio.c trio.c
catalog.c trionan.c strio.c trio.c
else
libxml2_la_SOURCES = SAX.c entities.c encoding.c error.c parserInternals.c \
parser.c tree.c hash.c list.c xmlIO.c xmlmemory.c uri.c \
valid.c xlink.c HTMLparser.c HTMLtree.c debugXML.c xpath.c \
xpointer.c xinclude.c nanohttp.c nanoftp.c DOCBparser.c \
catalog.c
catalog.c trionan.c
endif
......
......@@ -29,7 +29,8 @@ xmlinc_HEADERS = \
xmlversion.h \
xmlwin32version.h \
DOCBparser.h \
catalog.h
catalog.h \
trionan.h
install-exec-hook:
$(mkinstalldirs) $(DESTDIR)$(xmlincdir) $(DESTDIR)$(xmlincdir)/libxml
......
/*************************************************************************
*
* $Id$
*
* Copyright (C) 2001 Bjorn Reese <breese@users.sourceforge.net>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
* CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
*
************************************************************************/
#ifndef __TRIO_NAN_H__
#define __TRIO_NAN_H__
#ifdef __cplusplus
extern "C" {
#endif
/*
* Return NaN (Not-a-Number).
*/
double trio_nan(void);
/*
* Return positive infinity.
*/
double trio_pinf(void);
/*
* Return negative infinity.
*/
double trio_ninf(void);
/*
* If number is a NaN return non-zero, otherwise return zero.
*/
int trio_isnan(double number);
/*
* If number is positive infinity return 1, if number is negative
* infinity return -1, otherwise return 0.
*/
int trio_isinf(double number);
#ifdef __cplusplus
}
#endif
#endif /* __TRIO_NAN_H__ */
......@@ -46,8 +46,10 @@ static const char rcsid[] = "@(#)$Id$";
/*************************************************************************
* Trio include files
*/
#include "triodef.h"
#include "trio.h"
#include "triop.h"
#include "trionan.h"
#include "strio.h"
/*
......@@ -60,32 +62,6 @@ static const char rcsid[] = "@(#)$Id$";
# define TRIO_ERROR_RETURN(x,y) (-1)
#endif
/*************************************************************************
* Platform and compiler support detection
*/
#if defined(unix) || defined(__xlC__) || defined(_AIX) || defined(__QNX__)
# define PLATFORM_UNIX
#elif defined(AMIGA) && defined(__GNUC__)
# define PLATFORM_UNIX
#elif defined(WIN32) || defined(_WIN32) || defined(_MSC_VER)
# define PLATFORM_WIN32
# define TRIO_MSVC_5 1100
#endif
#if defined(__STDC__) && defined(__STDC_VERSION__)
# if (__STDC_VERSION__ >= 199409L)
# define TRIO_COMPILER_SUPPORTS_ISO94
# endif
# if (__STDC_VERSION__ >= 199901L)
# define TRIO_COMPILER_SUPPORTS_C99
# endif
#endif
#if defined(_XOPEN_SOURCE) && defined(_XOPEN_SOURCE_EXTENDED)
# define TRIO_COMPILER_SUPPORTS_UNIX98
#endif
#if defined(__STDC_ISO_10646__) || defined(MB_LEN_MAX) || defined(USE_MULTIBYTE) || TRIO_WIDECHAR
# define TRIO_COMPILER_SUPPORTS_MULTIBYTE
# if !defined(MB_LEN_MAX)
......@@ -127,7 +103,7 @@ static const char rcsid[] = "@(#)$Id$";
#define VALID(x) (NULL != (x))
/* xlC crashes on log10(0) */
#define guarded_log10(x) (((x) == 0.0) ? -HUGE_VAL : log10(x))
#define guarded_log10(x) (((x) == 0.0) ? trio_ninf() : log10(x))
#define guarded_log16(x) (guarded_log10(x) / log10(16.0))
......@@ -146,6 +122,8 @@ static const char rcsid[] = "@(#)$Id$";
# define write _write
#endif /* PLATFORM_WIN32 */
#define TRIO_MSVC_VERSION_5 1100
#if TRIO_WIDECHAR
# if defined(TRIO_COMPILER_SUPPORTS_ISO94)
# include <wchar.h>
......@@ -192,7 +170,7 @@ typedef int wint_t;
typedef signed long long int trio_longlong_t;
typedef unsigned long long int trio_ulonglong_t;
#elif defined(_MSC_VER)
# if (_MSC_VER >= TRIO_MSVC_5)
# if (_MSC_VER >= TRIO_MSVC_VERSION_5)
typedef signed __int64 trio_longlong_t;
typedef unsigned __int64 trio_ulonglong_t;
# else
......@@ -781,74 +759,6 @@ TrioIsQualifier(const char ch)
}
}
/*************************************************************************
* TrioGenerateNan [private]
*
* Calculating NaN portably is difficult. Some compilers will emit
* warnings about divide by zero, and others will simply fail to
* generate a NaN.
*/
static double
TrioDivide(double dividend, double divisor)
{
return dividend / divisor;
}
static double
TrioGenerateNaN(void)
{
#if defined(TRIO_COMPILER_SUPPORTS_C99)
return nan(NULL);
#elif defined(DBL_QNAN)
return DBL_QNAN;
#elif defined(PLATFORM_UNIX)
double value;
void (*signal_handler)(int);
signal_handler = signal(SIGFPE, SIG_IGN);
value = TrioDivide(0.0, 0.0);
signal(SIGFPE, signal_handler);
return value;
#else
return TrioDivide(0.0, 0.0);
#endif
}
/*************************************************************************
* TrioIsNan [private]
*/
static int
TrioIsNan(double number)
{
#ifdef isnan
/* C99 defines isnan() as a macro */
return isnan(number);
#else
double integral, fraction;
return (/* NaN is the only number which does not compare to itself */
(number != number) ||
/* Fallback solution if NaN compares to NaN */
((number != 0.0) &&
(fraction = modf(number, &integral),
integral == fraction)));
#endif
}
/*************************************************************************
* TrioIsInfinite [private]
*/
static int
TrioIsInfinite(double number)
{
#ifdef isinf
/* C99 defines isinf() as a macro */
return isinf(number);
#else
return ((number == HUGE_VAL) ? 1 : ((number == -HUGE_VAL) ? -1 : 0));
#endif
}
/*************************************************************************
* TrioSetLocale [private]
*/
......@@ -1930,7 +1840,7 @@ TrioPreprocess(int type,
*/
static void
TrioWriteNumber(trio_T *self,
trio_uintmax_t number,
trio_intmax_t snumber,
unsigned long flags,
int width,
int precision,
......@@ -1941,6 +1851,7 @@ TrioWriteNumber(trio_T *self,
char *bufferend;
char *pointer;
const char *digits;
trio_uintmax_t number;
int i;
int length;
char *p;
......@@ -1956,9 +1867,8 @@ TrioWriteNumber(trio_T *self,
isNegative = (flags & FLAGS_UNSIGNED)
? FALSE
: ((trio_intmax_t)number < 0);
if (isNegative)
number = -number;
: (snumber < 0);
number = (isNegative) ? -snumber : snumber;
if (flags & FLAGS_QUAD)
number &= (trio_ulonglong_t)-1;
......@@ -2374,7 +2284,7 @@ TrioWriteDouble(trio_T *self,
number = (double)longdoubleNumber;
/* Look for infinite numbers and non-a-number first */
switch (TrioIsInfinite(number))
switch (trio_isinf(number))
{
case 1:
/* Positive infinity */
......@@ -2398,7 +2308,7 @@ TrioWriteDouble(trio_T *self,
/* Finitude */
break;
}
if (TrioIsNan(number))
if (trio_isnan(number))
{
TrioWriteString(self,
(flags & FLAGS_UPPER)
......@@ -2448,7 +2358,7 @@ TrioWriteDouble(trio_T *self,
{
/* Scale the number */
workNumber = guarded_log10(number);
if (workNumber == -HUGE_VAL)
if (trio_isinf(workNumber) == -1)
{
exponent = 0;
/* Undo setting */
......@@ -4916,14 +4826,14 @@ TrioReadDouble(trio_T *self,
StrEqual(&doubleString[start], LONG_INFINITE_UPPER))
{
*target = ((start == 1 && doubleString[0] == '-'))
? -HUGE_VAL
: HUGE_VAL;
? trio_ninf()
: trio_pinf();
return TRUE;
}
if (StrEqual(doubleString, NAN_LOWER))
{
/* NaN must not have a preceeding + nor - */
*target = TrioGenerateNaN();
*target = trio_nan();
return TRUE;
}
return FALSE;
......
/*************************************************************************
*
* $Id$
*
* Copyright (C) 2001 Bjorn Reese <breese@users.sourceforge.net>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
* CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
*
************************************************************************/
#ifndef __TRIO_TRIODEF_H__
#define __TRIO_TRIODEF_H__
/*************************************************************************
* Platform and compiler support detection
*/
#if defined(__GNUC__)
# define TRIO_COMPILER_GCC
#elif defined(__SUNPRO_C)
# define TRIO_COMPILER_SUNPRO
#elif defined(__SUNPRO_CC)
# define TRIO_COMPILER_SUNPRO
# define __SUNPRO_C __SUNPRO_CC
#elif defined(__xlC__) || defined(__IBMC__) || defined(__IBMCPP__)
# define TRIO_COMPILER_XLC
#elif defined(_AIX) && !defined(__GNUC__)
# define TRIO_COMPILER_XLC /* Workaround for old xlc */
#elif defined(__DECC) || defined(__DECCXX)
# define TRIO_COMPILER_DECC
#elif defined(_MSC_VER)
# define TRIO_COMPILER_MSVC
#endif
#if defined(unix) || defined(__unix) || defined(__unix__)
# define TRIO_PLATFORM_UNIX
#elif defined(TRIO_COMPILER_XLC) || defined(_AIX)
# define TRIO_PLATFORM_UNIX
#elif defined(TRIO_COMPILER_DECC) || defined(__osf__)
# define TRIO_PLATFORM_UNIX
#elif defined(__NetBSD__)
# define TRIO_PLATFORM_UNIX
#elif defined(__QNX__)
# define TRIO_PLATFORM_UNIX
#elif defined(AMIGA) && defined(TRIO_COMPILER_GCC)
# define TRIO_PLATFORM_UNIX
#elif defined(TRIO_COMPILER_MSVC) || defined(WIN32) || defined(_WIN32)
# define TRIO_PLATFORM_WIN32
#endif
#if defined(__STDC__)
# define TRIO_COMPILER_SUPPORTS_C90
# if defined(__STDC_VERSION__)
# if (__STDC_VERSION__ >= 199409L)
# define TRIO_COMPILER_SUPPORTS_C94
# endif
# if (__STDC_VERSION__ >= 199901L)
# define TRIO_COMPILER_SUPPORTS_C99
# endif
# elif defined(TRIO_COMPILER_SUNPRO)
# if (__SUNPRO_C >= 0x420)
# define TRIO_COMPILER_SUPPORTS_C94
# endif
# endif
#endif
#if defined(_XOPEN_SOURCE)
# if defined(_XOPEN_SOURCE_EXTENDED)
# define TRIO_COMPILER_SUPPORTS_UNIX95
# endif
# if (_XOPEN_VERSION >= 500)
# define TRIO_COMPILER_SUPPORTS_UNIX98
# endif
#endif
#endif /* __TRIO_TRIODEF_H__ */
/*************************************************************************
*
* $Id$
*
* Copyright (C) 2001 Bjorn Reese <breese@users.sourceforge.net>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
* CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
*
************************************************************************
*
* Functions to handle special quantities in floating-point numbers
* (that is, NaNs and infinity). They provide the capability to detect
* and fabricate special quantities.
*
* Although written to be as portable as possible, it can never be
* guaranteed to work on all platforms, as not all hardware supports
* special quantities.
*
* The approach used here (approximately) is to:
*
* 1. Use C99 functionality when available.
* 2. Use IEEE 754 bit-patterns if possible.
* 3. Use platform-specific techniques.
*
* This program has been tested on the following platforms (in
* alphabetic order)
*
* OS CPU Compiler
* -------------------------------------------------
* AIX 4.1.4 PowerPC gcc
* Darwin 1.3.7 PowerPC gcc
* FreeBSD 2.2 x86 gcc
* FreeBSD 3.3 x86 gcc
* FreeBSD 4.3 x86 gcc
* FreeBSD 4.3 Alpha gcc
* HP-UX 10.20 PA-RISC gcc
* HP-UX 10.20 PA-RISC HP C++
* IRIX 6.5 MIPS MIPSpro C
* Linux 2.2 x86 gcc
* Linux 2.2 Alpha gcc
* Linux 2.4 IA64 gcc
* Linux 2.4 StrongARM gcc
* NetBSD 1.4 x86 gcc
* NetBSD 1.4 StrongARM gcc
* NetBSD 1.5 Alpha gcc
* RISC OS 4 StrongARM Norcroft C
* Solaris 2.5.1 x86 gcc
* Solaris 2.5.1 Sparc gcc
* Solaris 2.6 Sparc WorkShop 4.2
* Solaris 8 Sparc Forte C 6
* Tru64 4.0D Alpha gcc
* Tru64 5.1 Alpha gcc
* WinNT x86 MSVC 5.0 & 6.0
*
************************************************************************/
static const char rcsid[] = "@(#)$Id$";
/*************************************************************************
* Include files
*/
#include "triodef.h"
#include "trionan.h"
#include <math.h>
#include <string.h>
#include <limits.h>
#include <float.h>
#if defined(TRIO_PLATFORM_UNIX)
# include <signal.h>
#endif
#include <assert.h>
#ifndef __STDC__
# define volatile
# define const
#endif
/*************************************************************************
* Definitions
*/
/* We must enable IEEE floating-point on Alpha */
#if defined(__alpha) && !defined(_IEEE_FP)
# if defined(TRIO_COMPILER_DECC)
# error "Must be compiled with option -ieee"
# elif defined(TRIO_COMPILER_GCC) && (defined(__osf__) || defined(__linux__))
# error "Must be compiled with option -mieee"
# endif
#endif /* __alpha && ! _IEEE_FP */
/*
* In ANSI/IEEE 754-1985 64-bits double format numbers have the
* following properties (amoungst others)
*
* o FLT_RADIX == 2: binary encoding
* o DBL_MAX_EXP == 1024: 11 bits exponent, where one bit is used
* to indicate special numbers (e.g. NaN and Infinity), so the
* maximum exponent is 10 bits wide (2^10 == 1024).
* o DBL_MANT_DIG == 53: The mantissa is 52 bits wide, but because
* numbers are normalized the initial binary 1 is represented
* implictly (the so-called "hidden bit"), which leaves us with
* the ability to represent 53 bits wide mantissa.
*/
#if (FLT_RADIX == 2) && (DBL_MAX_EXP == 1024) && (DBL_MANT_DIG == 53)
# define USE_IEEE_754
#endif
/*************************************************************************
* Data
*/
#if defined(USE_IEEE_754)
/*
* Endian-agnostic indexing macro.
*
* The value of internalEndianMagic, when converted into a 64-bit
* integer, becomes 0x0001020304050607 (we could have used a 64-bit
* integer value instead of a double, but not all platforms supports
* that type). The value is automatically encoded with the correct
* endianess by the compiler, which means that we can support any
* kind of endianess. The individual bytes are then used as an index
* for the IEEE 754 bit-patterns and masks.
*/
#define TRIO_DOUBLE_INDEX(x) (((unsigned char *)&internalEndianMagic)[(x)])
static const double internalEndianMagic = 1.4015997730788920e-309;
/* Mask for the exponent */
static const unsigned char ieee_754_exponent_mask[] = {
0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
/* Mask for the mantissa */
static const unsigned char ieee_754_mantissa_mask[] = {
0x00, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
/* Bit-pattern for infinity */
static const unsigned char ieee_754_infinity_array[] = {
0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
/* Bit-pattern for quiet NaN */
static const unsigned char ieee_754_qnan_array[] = {
0x7F, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
/*************************************************************************
* trio_make_double
*/
static double
trio_make_double(const unsigned char *values)
{
volatile double result;
int i;
for (i = 0; i < (int)sizeof(double); i++) {
((volatile unsigned char *)&result)[TRIO_DOUBLE_INDEX(i)] = values[i];
}
return result;
}
/*************************************************************************
* trio_examine_double
*/
static int
trio_is_special_quantity(double number,
int *has_mantissa)
{
unsigned int i;
unsigned char current;
int is_special_quantity = (1 == 1);
*has_mantissa = 0;
for (i = 0; i < (unsigned int)sizeof(double); i++) {
current = ((unsigned char *)&number)[TRIO_DOUBLE_INDEX(i)];
is_special_quantity
&= ((current & ieee_754_exponent_mask[i]) == ieee_754_exponent_mask[i]);
*has_mantissa |= (current & ieee_754_mantissa_mask[i]);
}
return is_special_quantity;
}
#endif /* USE_IEEE_754 */
/*************************************************************************
* trio_pinf
*/
double
trio_pinf(void)
{
/* Cache the result */
static double result = 0.0;
if (result == 0.0) {
#if defined(INFINITY) && defined(__STDC_IEC_559__)
result = (double)INFINITY;
#elif defined(USE_IEEE_754)
result = trio_make_double(ieee_754_infinity_array);
#else
/*
* If HUGE_VAL is different from DBL_MAX, then HUGE_VAL is used
* as infinity. Otherwise we have to resort to an overflow
* operation to generate infinity.
*/
# if defined(TRIO_PLATFORM_UNIX)
void (*signal_handler)(int) = signal(SIGFPE, SIG_IGN);
# endif
result = HUGE_VAL;
if (HUGE_VAL == DBL_MAX) {
/* Force overflow */
result += HUGE_VAL;
}
# if defined(TRIO_PLATFORM_UNIX)
signal(SIGFPE, signal_handler);
# endif
#endif
}
return result;
}
/*************************************************************************
* trio_ninf
*/
double
trio_ninf(void)
{
static double result = 0.0;
if (result == 0.0) {
/*
* Negative infinity is calculated by negating positive infinity,
* which can be done because it is legal to do calculations on