/************************************************************************* * * $Id: trionan.c,v 1.33 2005/05/29 11:57:25 breese Exp $ * * Copyright (C) 2001 Bjorn Reese * * 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. * ************************************************************************/ /************************************************************************* * Include files */ #include "triodef.h" #include "trionan.h" #include #include #include #if !defined(TRIO_PLATFORM_SYMBIAN) # include #endif #if defined(TRIO_PLATFORM_UNIX) # include #endif #if defined(TRIO_COMPILER_DECC) # include #endif #include #if defined(TRIO_DOCUMENTATION) # include "doc/doc_nan.h" #endif /** @addtogroup SpecialQuantities @{ */ /************************************************************************* * Definitions */ #if !defined(TRIO_PUBLIC_NAN) # define TRIO_PUBLIC_NAN TRIO_PUBLIC #endif #if !defined(TRIO_PRIVATE_NAN) # define TRIO_PRIVATE_NAN TRIO_PRIVATE #endif #define TRIO_TRUE (1 == 1) #define TRIO_FALSE (0 == 1) /* * We must enable IEEE floating-point on Alpha */ #if defined(__alpha) && !defined(_IEEE_FP) # if defined(TRIO_COMPILER_DECC) # if defined(TRIO_PLATFORM_VMS) # error "Must be compiled with option /IEEE_MODE=UNDERFLOW_TO_ZERO/FLOAT=IEEE" # else # if !defined(_CFE) # error "Must be compiled with option -ieee" # endif # endif # else # if defined(TRIO_COMPILER_GCC) # error "Must be compiled with option -mieee" # endif # 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 * implicitly (the so-called "hidden bit"), which leaves us with * the ability to represent 53 bits wide mantissa. */ #if defined(__STDC_IEC_559__) # define TRIO_IEEE_754 #else # if (FLT_RADIX - 0 == 2) && (DBL_MAX_EXP - 0 == 1024) && (DBL_MANT_DIG - 0 == 53) # define TRIO_IEEE_754 # endif #endif /* * Determine which fpclassify_and_sign() function to use. */ #if defined(TRIO_FUNC_FPCLASSIFY_AND_SIGNBIT) # if defined(PREDEF_STANDARD_C99) && defined(fpclassify) # define TRIO_FUNC_C99_FPCLASSIFY_AND_SIGNBIT # else # if defined(TRIO_COMPILER_DECC) # define TRIO_FUNC_DECC_FPCLASSIFY_AND_SIGNBIT # else # if defined(TRIO_COMPILER_VISUALC) || defined(TRIO_COMPILER_BORLAND) # define TRIO_FUNC_MS_FPCLASSIFY_AND_SIGNBIT # else # if defined(TRIO_COMPILER_HP) && defined(FP_PLUS_NORM) # define TRIO_FUNC_HP_FPCLASSIFY_AND_SIGNBIT # else # if defined(TRIO_COMPILER_XLC) && defined(FP_PLUS_NORM) # define TRIO_FUNC_XLC_FPCLASSIFY_AND_SIGNBIT # else # define TRIO_FUNC_INTERNAL_FPCLASSIFY_AND_SIGNBIT # endif # endif # endif # endif # endif #endif /* * Determine how to generate negative zero. */ #if defined(TRIO_FUNC_NZERO) # if defined(TRIO_IEEE_754) # define TRIO_NZERO_IEEE_754 # else # define TRIO_NZERO_FALLBACK # endif #endif /* * Determine how to generate positive infinity. */ #if defined(TRIO_FUNC_PINF) # if defined(INFINITY) && defined(__STDC_IEC_559__) # define TRIO_PINF_C99_MACRO # else # if defined(TRIO_IEEE_754) # define TRIO_PINF_IEEE_754 # else # define TRIO_PINF_FALLBACK # endif # endif #endif /* * Determine how to generate NaN. */ #if defined(TRIO_FUNC_NAN) # if defined(PREDEF_STANDARD_C99) && !defined(TRIO_COMPILER_DECC) # define TRIO_NAN_C99_FUNCTION # else # if defined(NAN) && defined(__STDC_IEC_559__) # define TRIO_NAN_C99_MACRO # else # if defined(TRIO_IEEE_754) # define TRIO_NAN_IEEE_754 # else # define TRIO_NAN_FALLBACK # endif # endif # endif #endif /* * Resolve internal dependencies. */ #if defined(TRIO_FUNC_INTERNAL_FPCLASSIFY_AND_SIGNBIT) # define TRIO_FUNC_INTERNAL_ISNAN # define TRIO_FUNC_INTERNAL_ISINF # if defined(TRIO_IEEE_754) # define TRIO_FUNC_INTERNAL_IS_SPECIAL_QUANTITY # define TRIO_FUNC_INTERNAL_IS_NEGATIVE # endif #endif #if defined(TRIO_NZERO_IEEE_754) \ || defined(TRIO_PINF_IEEE_754) \ || defined(TRIO_NAN_IEEE_754) # define TRIO_FUNC_INTERNAL_MAKE_DOUBLE #endif #if defined(TRIO_FUNC_INTERNAL_ISNAN) # if defined(PREDEF_STANDARD_XPG3) # define TRIO_INTERNAL_ISN
#!/usr/bin/perl
# Copyright 2000 john Summerfield ,summer@os2.ami.com.au>
# Your choice of licence: GPL 2 or later, or same licence as Perl.
#
# Warranty? 					None
# If it breaks? 				The pieces are yours
# If it breaks something?	You drove it.
# Bugs?							At least one.

# now we've cleared the air;
#	This supposed to allow one to talk pop-3 to a mail server. If you're lucky (and know how)
#		you might also be able to talk a few other Internet protocols with it.
#	Typically, it's run thus:
#		pop2test.1 <mailserver> [<mailport>]
#	mailport's optional; default is 110 (pop-3).
#	
#	Having started, you type away much as you would with telnet.
#	
#	
#	
#	It has this great advantage over telnet: it reads its input from stdin and writes to stdout;
#		you can prepare the entire sequence in a file, then run it this:
#			pop2test.1 <thefileyoujustcreated >theresultsyouwanttoperuse host port
#	
#	
#	uses:
#		1	Debugging POP3 (and maybe imap does anyone know?) mail problems
#		2	Deleting the occasional piece of mail that's too big or stuffs fetchmail.
#		3	Talking to sendmail
#	
use Socket;
sub hx;
sub getreply;
$timeout=1;
$RemoteHost = $ARGV[0];shift;
$RemotePort = $ARGV[0] || 110;shift;
($PRname,$PRaliases,$PRport,$PRproto) = getservbyname($RemotePort,'tcp');
$PRport=$RemotePort unless $PRport;
$proto=getprotobyname($PRproto);  
$RemoteIP = inet_aton $RemoteHost or die "Can't resolve $RemoteHost";
$that = pack 'Sna4x8',AF_INET, $PRport, $RemoteIP;
socket(REMOTESITE,AF_INET,SOCK_STREAM,$proto)
        or die "Can't create socket to $RemoteHost: $!\n";;
connect(REMOTESITE, $that) or die "Can't connect: $!\n";
select(REMOTESITE);$|=1;select STDOUT;
$rin = $win = $ein = '';
vec($rin,fileno(REMOTESITE),1) = 1;
#vec($win,fileno(REMOTESITE),1) = 1;
$ein = $rin | $win;
getreply;
while ($L=<STDIN>)
{
	chomp $L;
	print REMOTESITE $L . "\r\n";
	print "send: " . $L . "\n";
	getreply;
}
print REMOTESITE "Quit\r\n";
getreply;
#print <REMOTESITE>;
close REMOTESITE;
exit;
# P
sub hx
{
	$N=$_[0];shift;
	$S=$_[0];shift;
	return "$N(" . unpack("h", $S) . ") "; 
}
sub getreply
{
	while ('x')
	{
		($nfound,$timeleft) = select($rout=$rin, undef, $eout=$ein, $timeout);
		last if $nfound == 0;
#		print "nf($nfound) tl($timeleft) " . hx("rin",$rin) . hx("rout", $rout) . hx("ein",$ein) . hx("eout",$eout) . "\n";
		$Reply= <REMOTESITE>;
		print "recv: " . $Reply;
		last if $Reply eq '';
		$Reply =~ s/[\r\n]*//;
		last if $Reply eq '.';
	}
}
f(); } return ninf_value; } #endif /** Generate NaN. @return Floating-point representation of NaN. */ #if defined(TRIO_FUNC_NAN) TRIO_PUBLIC_NAN double trio_nan(TRIO_NOARGS) { /* Cache the result */ static double nan_value = 0.0; if (nan_value == 0.0) { # if defined(TRIO_NAN_C99_FUNCTION) || defined(TRIO_PLATFORM_SYMBIAN) nan_value = nan(""); # endif # if defined(TRIO_NAN_C99_MACRO) nan_value = (double)NAN; # endif # if defined(TRIO_NAN_IEEE_754) nan_value = internal_make_double(ieee_754_qnan_array); # endif # if defined(TRIO_NAN_FALLBACK) /* * There are several ways to generate NaN. The one used here is * to divide infinity by infinity. I would have preferred to add * negative infinity to positive infinity, but that yields wrong * result (infinity) on FreeBSD. * * This may fail if the hardware does not support NaN, or if * the Invalid Operation floating-point exception is unmasked. */ # if defined(TRIO_PLATFORM_UNIX) void (*signal_handler)(int) = signal(SIGFPE, SIG_IGN); # endif nan_value = trio_pinf() / trio_pinf(); # if defined(TRIO_PLATFORM_UNIX) signal(SIGFPE, signal_handler); # endif # endif } return nan_value; } #endif /** @} SpecialQuantities */ /************************************************************************* * For test purposes. * * Add the following compiler option to include this test code. * * Unix : -DSTANDALONE * VMS : /DEFINE=(STANDALONE) */ #if defined(STANDALONE) # include static TRIO_CONST char * getClassification TRIO_ARGS1((type), int type) { switch (type) { case TRIO_FP_INFINITE: return "FP_INFINITE"; case TRIO_FP_NAN: return "FP_NAN"; case TRIO_FP_NORMAL: return "FP_NORMAL"; case TRIO_FP_SUBNORMAL: return "FP_SUBNORMAL"; case TRIO_FP_ZERO: return "FP_ZERO"; default: return "FP_UNKNOWN"; } } static void print_class TRIO_ARGS2((prefix, number), TRIO_CONST char *prefix, double number) { printf("%-6s: %s %-15s %g\n", prefix, trio_signbit(number) ? "-" : "+", getClassification(trio_fpclassify(number)), number); } int main(TRIO_NOARGS) { double my_nan; double my_pinf; double my_ninf; # if defined(TRIO_PLATFORM_UNIX) void (*signal_handler) TRIO_PROTO((int)); # endif my_nan = trio_nan(); my_pinf = trio_pinf(); my_ninf = trio_ninf(); print_class("Nan", my_nan); print_class("PInf", my_pinf); print_class("NInf", my_ninf); print_class("PZero", 0.0); print_class("NZero", -0.0); print_class("PNorm", 1.0); print_class("NNorm", -1.0); print_class("PSub", 1.01e-307 - 1.00e-307); print_class("NSub", 1.00e-307 - 1.01e-307); printf("NaN : %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d, %2d)\n", my_nan, ((unsigned char *)&my_nan)[0], ((unsigned char *)&my_nan)[1], ((unsigned char *)&my_nan)[2], ((unsigned char *)&my_nan)[3], ((unsigned char *)&my_nan)[4], ((unsigned char *)&my_nan)[5], ((unsigned char *)&my_nan)[6], ((unsigned char *)&my_nan)[7], trio_isnan(my_nan), trio_isinf(my_nan), trio_isfinite(my_nan)); printf("PInf: %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d, %2d)\n", my_pinf, ((unsigned char *)&my_pinf)[0], ((unsigned char *)&my_pinf)[1], ((unsigned char *)&my_pinf)[2], ((unsigned char *)&my_pinf)[3], ((unsigned char *)&my_pinf)[4], ((unsigned char *)&my_pinf)[5], ((unsigned char *)&my_pinf)[6], ((unsigned char *)&my_pinf)[7], trio_isnan(my_pinf), trio_isinf(my_pinf), trio_isfinite(my_pinf)); printf("NInf: %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d, %2d)\n", my_ninf, ((unsigned char *)&my_ninf)[0], ((unsigned char *)&my_ninf)[1], ((unsigned char *)&my_ninf)[2], ((unsigned char *)&my_ninf)[3], ((unsigned char *)&my_ninf)[4], ((unsigned char *)&my_ninf)[5], ((unsigned char *)&my_ninf)[6], ((unsigned char *)&my_ninf)[7], trio_isnan(my_ninf), trio_isinf(my_ninf), trio_isfinite(my_ninf)); # if defined(TRIO_PLATFORM_UNIX) signal_handler = signal(SIGFPE, SIG_IGN); # endif my_pinf = DBL_MAX + DBL_MAX; my_ninf = -my_pinf; my_nan = my_pinf / my_pinf; # if defined(TRIO_PLATFORM_UNIX) signal(SIGFPE, signal_handler); # endif printf("NaN : %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d, %2d)\n", my_nan, ((unsigned char *)&my_nan)[0], ((unsigned char *)&my_nan)[1], ((unsigned char *)&my_nan)[2], ((unsigned char *)&my_nan)[3], ((unsigned char *)&my_nan)[4], ((unsigned char *)&my_nan)[5], ((unsigned char *)&my_nan)[6], ((unsigned char *)&my_nan)[7], trio_isnan(my_nan), trio_isinf(my_nan), trio_isfinite(my_nan)); printf("PInf: %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d, %2d)\n", my_pinf, ((unsigned char *)&my_pinf)[0], ((unsigned char *)&my_pinf)[1], ((unsigned char *)&my_pinf)[2], ((unsigned char *)&my_pinf)[3], ((unsigned char *)&my_pinf)[4], ((unsigned char *)&my_pinf)[5], ((unsigned char *)&my_pinf)[6], ((unsigned char *)&my_pinf)[7], trio_isnan(my_pinf), trio_isinf(my_pinf), trio_isfinite(my_pinf)); printf("NInf: %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d, %2d)\n", my_ninf, ((unsigned char *)&my_ninf)[0], ((unsigned char *)&my_ninf)[1], ((unsigned char *)&my_ninf)[2], ((unsigned char *)&my_ninf)[3], ((unsigned char *)&my_ninf)[4], ((unsigned char *)&my_ninf)[5], ((unsigned char *)&my_ninf)[6], ((unsigned char *)&my_ninf)[7], trio_isnan(my_ninf), trio_isinf(my_ninf), trio_isfinite(my_ninf)); return 0; } #endif