home *** CD-ROM | disk | FTP | other *** search
- From decwrl!ucbvax!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!allbery Mon May 1 23:18:50 PDT 1989
- Article 865 of comp.sources.misc:
- Path: decwrl!ucbvax!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!allbery
- From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
- Newsgroups: comp.sources.misc
- Subject: v06i096: config.c - everything you wanted to know about your C compiler
- Message-ID: <53488@uunet.UU.NET>
- Date: 30 Apr 89 22:04:50 GMT
- Sender: allbery@uunet.UU.NET
- Reply-To: steven@cwi.nl (Steven Pemberton)
- Organization: CWI, Amsterdam
- Lines: 1727
- Approved: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
-
- Posting-number: Volume 6, Issue 96
- Submitted-by: steven@cwi.nl (Steven Pemberton)
- Archive-name: config2
-
- This is a new version of a program that I started writing a few years
- ago to automatically configure a large software system to the machine
- it was to run on. Earlier versions of the program were also
- distributed to the net.
-
- The program tells you about lots of properties of your machine and C
- compiler, and this version optionally produces the ANSI C float.h and
- limits.h files. It also pinpoints some possible faults in your
- compiler, and offers the option of verifying that the compiler is
- correctly able to read the header files.
-
- ---------------------- CUT HERE -------------------------------------
- #! /bin/sh
- # This file was wrapped with "dummyshar". "sh" this file to extract.
- # Contents: config.c
- echo extracting 'config.c'
- if test -f 'config.c' -a -z "$1"; then echo Not overwriting 'config.c'; else
- sed 's/^X//' << \EOF > 'config.c'
- X/* Everything you wanted to know about your machine and C compiler,
- X but didn't know who to ask.
- X Author: Steven Pemberton, CWI, Amsterdam; steven@cwi.nl
- X Bugfixes and upgrades gratefully received.
- X
- X Copyright (c) 1988, 1989 Steven Pemberton, CWI, Amsterdam.
- X All rights reserved.
- X
- X COMPILING
- X With luck and a following wind, just the following will work:
- X cc config.c -o config
- X
- X If your compiler doesn't support: add flag:
- X signed char (eg pcc) -DNO_SC
- X unsigned char -DNO_UC
- X unsigned short and long -DNO_UI
- X signal(), or setjmp/longjmp() -DNO_SIG
- X
- X Try it first with no flags, and see if you get any errors - you might be
- X surprised. (Most non-ANSI compilers need -DNO_SC, though.)
- X Some compilers need a -f flag for floating point.
- X
- X Don't use any optimisation flags: the program may not work if you do.
- X Though "while (a+1.0-a-1.0 == 0.0)" may look like "while(1)" to an
- X optimiser, to a floating-point unit there's a world of difference.
- X
- X Some compilers offer various flags for different floating point
- X modes; it's worth trying all possible combinations of these.
- X
- X Add -DID=\"name\" if you want the machine/flags identified in the output.
- X
- X SYSTEM DEPENDENCIES
- X You may possibly need to add some calls to signal() for other sorts of
- X exception on your machine than SIGFPE, and SIGOVER. See lines beginning
- X #ifdef SIGxxx in main() (and communicate the differences to me!).
- X
- X If your C preprocessor doesn't have the predefined __FILE__ macro, and
- X you want to call this file anything other than config.c, change the
- X #define command for __FILE__ accordingly. If it doesn't accept macro
- X names at all in #include lines, order a new C compiler. While you're
- X waiting for it to arrive, change the last #include in this file (the
- X last but one line) accordingly.
- X
- X OUTPUT
- X Run without argument to get the information as English text. If run
- X with argument -l (e.g. config -l), output is a series of #define's for
- X the ANSI standard limits.h include file, excluding MB_MAX_CHAR. If run
- X with argument -f, output is a series of #define's for the ANSI standard
- X float.h include file. Flag -v gives verbose output: output includes the
- X English text above as C comments. The program exit(0)'s if everything
- X went ok, otherwise it exits with a positive number, telling how many
- X problems there were.
- X
- X VERIFYING THE COMPILER
- X If, having produced the float.h and limits.h header files, you want to
- X verify that the compiler reads them back correctly (there are a lot of
- X boundary cases, of course, like minimum and maximum numbers), you can
- X recompile config.c with -DVERIFY set (plus the other flags that you used
- X when compiling the version that produced the header files). This then
- X recompiles the program so that it #includes "limits.h" and "float.h",
- X and checks that the constants it finds there are the same as the
- X constants it produces. Run the resulting program with config -fl. As of
- X this writing, of 21 compiler/flags combinations only 1 compiler has
- X passed without error! (The honour goes to 'pcc' on an IBM RT.)
- X
- X You can also use this option if your compiler already has both files,
- X and you want to confirm that this program produces the right results.
- X
- X TROUBLE SHOOTING.
- X This program is now quite trustworthy, and suspicious and wrong output
- X may well be caused by bugs in the compiler, not in the program (however
- X of course, this is not guaranteed, and no responsibility can be
- X accepted, etc.)
- X
- X The program only works if overflows are ignored by the C system or
- X are catchable with signal().
- X
- X If the program fails to run to completion (often with the error message
- X "Unexpected signal at point x"), this often turns out to be a bug in the
- X C compiler's run-time system. Check what was about to be printed, and
- X try to narrow the problem down.
- X
- X Another possible problem is that you have compiled the program to produce
- X loss-of-precision arithmetic traps. The program cannot cope with these,
- X and you should re-compile without them. (They should never be the default).
- X
- X Make sure you compiled with optimisation turned off.
- X
- X Output preceded by *** WARNING: identifies behaviour of the C system
- X deemed incorrect by the program. Likely problems are that printf or
- X scanf don't cope properly with certain boundary numbers. For each float
- X and double that is printed, the printed value is checked that it is
- X correct by using sscanf to read it back. Care is taken that numbers are
- X printed with enough digits to uniquely identify them, and therefore that
- X they can be read back identically. If the number read back is different,
- X the program prints a warning message. If the two numbers in the warning
- X look identical, then printf is more than likely rounding the last
- X digit(s) incorrectly. To put you at ease that the two really are
- X different, the bit patterns of the two numbers are also printed. The
- X difference is very likely in the last bit. Many scanf's read the
- X minimum double back as 0.0, and similarly cause overflow when reading
- X the maximum double. The program quite ruthlessly declares all these
- X behaviours faulty.
- X
- X The warning that "a cast didn't work" refers to cases like this:
- X
- X float f;
- X #define C 1.234567890123456789
- X f= C;
- X if (f != (float) C) printf ("Wrong!");
- X
- X A faulty compiler will widen f to double and ignore the cast to float,
- X and because there is more accuracy in a double than a float, fail to
- X recognise that they are the same. In the actual case in point, f and C
- X are passed as parameters to a function that discovers they are not equal,
- X so it's just possible that the error was in the parameter passing,
- X not in the cast (see function Validate()).
- X For ANSI C, which has float constants, the error message is "constant has
- X wrong precision".
- X
- X REPORTING PROBLEMS
- X If the program doesn't work for you for any reason that can't be
- X narrowed down to a problem in the C compiler, or it has to be changed in
- X order to get it to compile, or it produces suspicious output (like a very
- X low maximum float, for instance), please mail the problem and an example
- X of the incorrect output to steven@cwi.nl or mcvax!steven.uucp, so that
- X improvements can be worked into future versions; mcvax/cwi.nl is the
- X European backbone, and is connected to uunet and other fine hosts.
- X
- X This version of the program is the first to try to catch and diagnose
- X bugs in the compiler/run-time system. I would be especially pleased to
- X have reports of failures so that I can improve this service.
- X
- X I apologise unreservedly for the contorted use of the preprocessor...
- X
- X THE SMALL PRINT
- X You may copy and distribute verbatim copies of this source file.
- X
- X You may modify this source file, and copy and distribute such
- X modified versions, provided that you leave the copyright notice
- X at the top of the file and also cause the modified file to carry
- X prominent notices stating that you changed the files and the date
- X of any change; and cause the whole of any work that you distribute
- X or publish, that in whole or in part contains or is a derivative of
- X this program or any part thereof, to be licensed at no charge to
- X all third parties on terms identical to those here.
- X
- X If you do have a fix to any problem, please send it to me, so that
- X other people can have the benefits.
- X
- X While every effort has been taken to make this program as reliable as
- X possible, no responsibility can be taken for the correctness of the
- X output, or suitability for any particular use.
- X
- X ACKNOWLEDGEMENTS
- X Many people have given time and ideas to making this program what it is.
- X To all of them thanks, and apologies for not mentioning them by name.
- X*/
- X
- X#ifndef __FILE__
- X#define __FILE__ "config.c"
- X#endif
- X
- X#ifndef PASS
- X#define PASS 1
- X#define PASS1 1
- X#define VERSION "4.1"
- X
- X/* Procedure just marks the functions that don't return a result */
- X#ifdef Procedure
- X#undef Procedure
- X#endif
- X#define Procedure
- X
- X#define Vprintf if (V) printf
- X
- X/* stdc is used in tests like if (stdc) */
- X#ifdef __STDC__
- X#define stdc 1
- X#else
- X#define stdc 0
- X#endif
- X
- X/* volatile is used to reduce the chance of optimisation,
- X and to prevent variables being put in registers (when setjmp/longjmp
- X wouldn't work as we want) */
- X#ifndef __STDC__
- X#define volatile static
- X#endif
- X
- X#include <stdio.h>
- X
- X#ifdef VERIFY
- X#include "limits.h"
- X#include "float.h"
- X#endif
- X
- X#ifdef NO_SIG /* There's no signal(), or setjmp/longjmp() */
- X
- X /* Dummy routines instead */
- X int lab=1;
- X int setjmp(lab) int lab; { return(0); }
- X signal(i, p) int i, (*p)(); {}
- X
- X#else
- X
- X#include <signal.h>
- X#include <setjmp.h>
- X
- X jmp_buf lab;
- X overflow(sig) int sig; { /* what to do on overflow/underflow */
- X signal(sig, overflow);
- X longjmp(lab, 1);
- X }
- X
- X#endif /*NO_SIG*/
- X
- X#define Unexpected(place) if (setjmp(lab)!=0) croak(place)
- X
- Xint V= 0, /* verbose */
- X L= 0, /* produce limits.h */
- X F= 0, /* produce float.h */
- X bugs=0; /* The number of (possible) bugs in the output */
- X
- Xchar co[4], oc[4]; /* Comment starter and ender symbols */
- X
- Xint bits_per_byte; /* the number of bits per unit returned by sizeof() */
- X
- X#ifdef TEST
- X/* Set the fp modes on a SUN with 68881 chip, to check that different
- X rounding modes etc. get properly detected.
- X Compile with additional flag -DTEST, and run with additional parameter
- X +hex-number, to set the 68881 mode register to hex-number
- X*/
- X
- X/* Bits 0x30 = rounding mode: */
- X#define ROUND_BITS 0x30
- X#define TO_NEAREST 0x00
- X#define TO_ZERO 0x10
- X#define TO_MINUS_INF 0x20
- X#define TO_PLUS_INF 0x30 /* The SUN FP user's guide seems to be wrong here */
- X
- X/* Bits 0xc0 = extended rounding: */
- X#define EXT_BITS 0xc0
- X#define ROUND_EXTENDED 0x00
- X#define ROUND_SINGLE 0x40
- X#define ROUND_DOUBLE 0x80
- X
- X/* Enabled traps: */
- X#define EXE_INEX1 0x100
- X#define EXE_INEX2 0x200
- X#define EXE_DZ 0x400
- X#define EXE_UNFL 0x800
- X#define EXE_OVFL 0x1000
- X#define EXE_OPERR 0x2000
- X#define EXE_SNAN 0x4000
- X#define EXE_BSUN 0x8000
- X
- Xprintmode(new) unsigned new; {
- X fpmode_(&new);
- X printf("New fp mode:\n");
- X printf(" Round toward ");
- X switch (new & ROUND_BITS) {
- X case TO_NEAREST: printf("nearest"); break;
- X case TO_ZERO: printf("zero"); break;
- X case TO_MINUS_INF: printf("minus infinity"); break;
- X case TO_PLUS_INF: printf("plus infinity"); break;
- X default: printf("???"); break;
- X }
- X
- X printf("\n Extended rounding precision: ");
- X
- X switch (new & EXT_BITS) {
- X case ROUND_EXTENDED: printf("extended"); break;
- X case ROUND_SINGLE: printf("single"); break;
- X case ROUND_DOUBLE: printf("double"); break;
- X default: printf("???"); break;
- X }
- X
- X printf("\n Enabled exceptions:");
- X if (new & (unsigned) EXE_INEX1) printf(" inex1");
- X if (new & (unsigned) EXE_INEX2) printf(" inex2");
- X if (new & (unsigned) EXE_DZ) printf(" dz");
- X if (new & (unsigned) EXE_UNFL) printf(" unfl");
- X if (new & (unsigned) EXE_OVFL) printf(" ovfl");
- X if (new & (unsigned) EXE_OPERR) printf(" operr");
- X if (new & (unsigned) EXE_SNAN) printf(" snan");
- X if (new & (unsigned) EXE_BSUN) printf(" bsun");
- X printf("\n");
- X}
- X
- Xint setmode(s) char *s; {
- X unsigned mode=0, dig;
- X char c;
- X
- X while (*s) {
- X c= *s++;
- X if (c>='0' && c<='9') dig= c-'0';
- X else if (c>='a' && c<='f') dig= c-'a'+10;
- X else if (c>='A' && c<='F') dig= c-'A'+10;
- X else return 1;
- X mode= mode<<4 | dig;
- X }
- X printmode(mode);
- X return 0;
- X}
- X#else
- Xint setmode(s) char *s; {
- X fprintf(stderr, "Can't set mode: not compiled with TEST\n");
- X return(1);
- X}
- X#endif
- X
- Xcroak(place) int place; {
- X printf("*** Unexpected signal at point %d\n", place);
- X exit(bugs+1); /* An exit isn't essential here, but avoids loops */
- X}
- X
- Xmain(argc, argv) int argc; char *argv[]; {
- X int dprec, fprec, lprec, basic(), fprop(), dprop(), efprop(), edprop();
- X char *malloc();
- X unsigned int size;
- X long total;
- X int i; char *s; int bad;
- X
- X#ifdef SIGFPE
- X signal(SIGFPE, overflow);
- X#endif
- X#ifdef SIGOVER
- X signal(SIGOVER, overflow);
- X#endif
- X/* Add more calls as necessary */
- X
- X Unexpected(1);
- X
- X bad=0;
- X for (i=1; i < argc; i++) {
- X s= argv[i];
- X if (*s == '-') {
- X s++;
- X while (*s) {
- X switch (*(s++)) {
- X case 'v': V=1; break;
- X case 'l': L=1; break;
- X case 'f': F=1; break;
- X default: bad=1; break;
- X }
- X }
- X } else if (*s == '+') {
- X s++;
- X bad= setmode(s);
- X } else bad= 1;
- X }
- X if (bad) {
- X fprintf(stderr,
- X "Usage: %s [-vlf]\n v=Verbose l=Limits.h f=Float.h\n",
- X argv[0]);
- X exit(1);
- X }
- X if (L || F) {
- X co[0]= '/'; oc[0]= ' ';
- X co[1]= '*'; oc[1]= '*';
- X co[2]= ' '; oc[2]= '/';
- X co[3]= '\0'; oc[3]= '\0';
- X } else {
- X co[0]= '\0'; oc[0]= '\0';
- X V=1;
- X }
- X
- X if (L) printf("%slimits.h%s\n", co, oc);
- X if (F) printf("%sfloat.h%s\n", co, oc);
- X#ifdef ID
- X printf("%sProduced on %s by config version %s, CWI, Amsterdam%s\n",
- X co, ID, VERSION, oc);
- X#else
- X printf("%sProduced by config version %s, CWI, Amsterdam%s\n",
- X co, VERSION, oc);
- X#endif
- X
- X#ifdef VERIFY
- X printf("%sVerification phase%s\n", co, oc);
- X#endif
- X
- X#ifdef NO_SIG
- X Vprintf("%sCompiled without signal(): %s%s\n",
- X co,
- X "there's nothing that can be done if overflow occurs",
- X oc);
- X#endif
- X#ifdef NO_SC
- X Vprintf("%sCompiled without signed char%s\n", co, oc);
- X#endif
- X#ifdef NO_UC
- X Vprintf("%Compiled without unsigned char%s\n", co, oc);
- X#endif
- X#ifdef NO_UI
- X Vprintf("%Compiled without unsigned short or long%s\n", co, oc);
- X#endif
- X#ifdef __STDC__
- X Vprintf("%sCompiler claims to be ANSI C level %d%s\n",
- X co, __STDC__, oc);
- X#else
- X Vprintf("%sCompiler does not claim to be ANSI C%s\n", co, oc);
- X#endif
- X printf("\n");
- X bits_per_byte= basic();
- X Vprintf("\n");
- X if (F||V) {
- X fprec= fprop(bits_per_byte);
- X dprec= dprop(bits_per_byte);
- X lprec= ldprop(bits_per_byte);
- X efprop(fprec, dprec, lprec);
- X edprop(fprec, dprec, lprec);
- X eldprop(fprec, dprec, lprec);
- X }
- X if (V) {
- X /* An extra goody: the approximate amount of data-space */
- X /* Allocate store until no more available */
- X size=1<<((bits_per_byte*sizeof(int))-2);
- X total=0;
- X while (size!=0) {
- X while (malloc(size)!=(char *)NULL) total+=(size/2);
- X size/=2;
- X }
- X
- X Vprintf("%sMemory mallocatable ~= %ld Kbytes%s\n",
- X co, (total+511)/512, oc);
- X }
- X exit(bugs);
- X}
- X
- XProcedure eek_a_bug(problem) char *problem; {
- X printf("\n%s*** WARNING: %s%s\n", co, problem, oc);
- X bugs++;
- X}
- X
- XProcedure i_define(sort, name, val, req) char *sort, *name; long val, req; {
- X if (val >= 0) {
- X printf("#define %s%s %ld\n", sort, name, val);
- X } else {
- X printf("#define %s%s (%ld)\n", sort, name, val);
- X }
- X if (val != req) {
- X printf("%s*** Verify failed for above #define!\n", co);
- X printf(" Compiler has %ld for value%s\n\n", req, oc);
- X bugs++;
- X }
- X Vprintf("\n");
- X}
- X
- X#ifndef NO_UI
- X
- X#ifdef __STDC__
- X#define U "U"
- X#else
- X#define U ""
- X#endif
- X
- XProcedure u_define(sort, name, val, req) char *sort, *name; unsigned long val, req; {
- X printf("#define %s%s %lu%s\n", sort, name, val, U);
- X if (val != req) {
- X printf("%s*** Verify failed for above #define!\n", co);
- X printf(" Compiler has %lu for value%s\n\n", req, oc);
- X bugs++;
- X }
- X Vprintf("\n");
- X}
- X#endif
- X
- X/* Long_double is the longest floating point type available: */
- X#ifdef __STDC__
- X#define Long_double long double
- X#else
- X#define Long_double double
- X#endif
- X
- Xchar *f_rep();
- X
- XProcedure f_define(sort, name, precision, val, mark)
- X char *sort, *name; int precision; Long_double val; char *mark; {
- X if (stdc) {
- X printf("#define %s%s %s%s\n",
- X sort, name, f_rep(precision, val), mark);
- X } else if (*mark == 'F') {
- X /* non-ANSI C has no float constants, so cast the constant */
- X printf("#define %s%s ((float)%s)\n",
- X sort, name, f_rep(precision, val));
- X } else {
- X printf("#define %s%s %s\n", sort, name, f_rep(precision, val));
- X }
- X Vprintf("\n");
- X}
- X
- Xint floor_log(base, x) int base; Long_double x; { /* return floor(log base(x)) */
- X int r=0;
- X while (x>=base) { r++; x/=base; }
- X return r;
- X}
- X
- Xint ceil_log(base, x) int base; Long_double x; {
- X int r=0;
- X while (x>1.0) { r++; x/=base; }
- X return r;
- X}
- X
- Xint exponent(x, fract, exp) Long_double x; double *fract; int *exp; {
- X /* Split x into a fraction and a power of ten;
- X returns 0 if x is unusable, 1 otherwise.
- X Only used for error messages about faulty output.
- X */
- X int r=0, neg=0;
- X Long_double old;
- X *fract=0.0; *exp=0;
- X if (x<0.0) {
- X x= -x;
- X neg= 1;
- X }
- X if (x==0.0) return 1;
- X if (x>=10.0) {
- X while (x>=10.0) {
- X old=x; r++; x/=10.0;
- X if (old==x) return 0;
- X }
- X } else {
- X while (x<1.0) {
- X old=x; r--; x*=10.0;
- X if (old==x) return 0;
- X }
- X }
- X if (neg) *fract= -x;
- X else *fract=x;
- X *exp=r;
- X return 1;
- X}
- X
- X#define fabs(x) (((x)<0.0)?(-x):(x))
- X
- Xchar *f_rep(precision, val) int precision; Long_double val; {
- X static char buf[1024];
- X char *f1;
- X if (sizeof(double) == sizeof(Long_double)) {
- X /* Assume they're the same, and use non-stdc format */
- X /* This is for stdc compilers using non-stdc libraries */
- X f1= "%.*e";
- X } else {
- X /* It had better support Le then */
- X f1= "%.*Le";
- X }
- X sprintf(buf, f1, precision, val);
- X return buf;
- X}
- X
- XProcedure bitpattern(p, size) char *p; int size; {
- X char c;
- X int i, j;
- X
- X for (i=1; i<=size; i++) {
- X c= *p;
- X p++;
- X for (j=bits_per_byte-1; j>=0; j--)
- X printf("%c", (c>>j)&1 ? '1' : '0');
- X if (i!=size) printf(" ");
- X }
- X}
- X
- X#define Order(x, px, mode)\
- X printf("%s %s ", co, mode); for (i=0; i<sizeof(x); i++) px[i]= c[i]; \
- X for (i=1; i<=sizeof(x); i++) { putchar((char)((x>>(bits_per_byte*(sizeof(x)-i)))&mask)); }\
- X printf("%s\n", oc);
- X
- XProcedure endian(bits_per_byte) int bits_per_byte; {
- X /*unsigned*/ short s=0;
- X /*unsigned*/ int j=0;
- X /*unsigned*/ long l=0;
- X
- X char *ps= (char *) &s,
- X *pj= (char *) &j,
- X *pl= (char *) &l,
- X *c= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- X unsigned int mask, i;
- X
- X mask=0;
- X for (i=1; i<=bits_per_byte; i++) mask= (mask<<1)|1;
- X
- X if (V) {
- X printf("%sCharacter order:%s\n", co, oc);
- X Order(s, ps, "short:");
- X Order(j, pj, "int: ");
- X Order(l, pl, "long: ");
- X }
- X}
- X
- X#ifdef VERIFY
- X#ifndef SCHAR_MAX
- X#define SCHAR_MAX char_max
- X#define SCHAR_MIN char_min
- X#endif
- X#ifndef UCHAR_MAX
- X#define UCHAR_MAX char_max
- X#endif
- X#else
- X#define CHAR_BIT char_bit
- X#define CHAR_MAX char_max
- X#define CHAR_MIN char_min
- X#define SCHAR_MAX char_max
- X#define SCHAR_MIN char_min
- X#define UCHAR_MAX char_max
- X#endif /* VERIFY */
- X
- Xint cprop() { /* Properties of character */
- X volatile char c, char_max, char_min;
- X volatile int bits_per_byte, is_signed;
- X long char_bit;
- X
- X Unexpected(2);
- X
- X /* Calculate number of bits per character *************************/
- X c=1; bits_per_byte=0;
- X do { c=c<<1; bits_per_byte++; } while(c!=0);
- X c= (char)(-1);
- X if (((int)c)<0) is_signed=1;
- X else is_signed=0;
- X Vprintf("%sChar = %d bits, %ssigned%s\n",
- X co, (int)sizeof(c)*bits_per_byte, (is_signed?"":"un"), oc);
- X char_bit=(long)(sizeof(c)*bits_per_byte);
- X if (L) i_define("CHAR", "_BIT", char_bit, (long) CHAR_BIT);
- X
- X c=0; char_max=0;
- X c++;
- X if (setjmp(lab)==0) { /* Yields char_max */
- X while (c>char_max) {
- X char_max=c;
- X c++;
- X }
- X } else {
- X Vprintf("%sCharacter overflow generates a trap!%s\n", co, oc);
- X }
- X c=0; char_min=0;
- X c--;
- X if (setjmp(lab)==0) { /* Yields char_min */
- X while (c<char_min) {
- X char_min=c;
- X c--;
- X }
- X }
- X Unexpected(3);
- X
- X if (L) {
- X i_define("CHAR", "_MAX", (long) char_max, (long) CHAR_MAX);
- X i_define("CHAR", "_MIN", (long) char_min, (long) CHAR_MIN);
- X if (is_signed) {
- X i_define("SCHAR", "_MAX", (long) char_max,
- X (long) SCHAR_MAX);
- X i_define("SCHAR", "_MIN", (long) char_min,
- X (long) SCHAR_MIN);
- X } else {
- X i_define("UCHAR", "_MAX", (long) char_max,
- X (long) UCHAR_MAX);
- X }
- X
- X if (is_signed) {
- X#ifndef NO_UC
- X volatile unsigned char c, char_max;
- X c=0; char_max=0;
- X c++;
- X if (setjmp(lab)==0) { /* Yields char_max */
- X while (c>char_max) {
- X char_max=c;
- X c++;
- X }
- X }
- X Unexpected(4);
- X i_define("UCHAR", "_MAX", (long) char_max,
- X (long) UCHAR_MAX);
- X#endif
- X } else {
- X#ifndef NO_SC /* Define NO_SC if the next line gives a syntax error */
- X volatile signed char c, char_max, char_min;
- X c=0; char_max=0;
- X c++;
- X if (setjmp(lab)==0) { /* Yields char_max */
- X while (c>char_max) {
- X char_max=c;
- X c++;
- X }
- X }
- X c=0; char_min=0;
- X c--;
- X if (setjmp(lab)==0) { /* Yields char_min */
- X while (c<char_min) {
- X char_min=c;
- X c--;
- X }
- X }
- X Unexpected(5);
- X i_define("SCHAR", "_MIN", (long) char_min,
- X (long) SCHAR_MIN);
- X i_define("SCHAR", "_MAX", (long) char_max,
- X (long) SCHAR_MAX);
- X#endif /* NO_SC */
- X }
- X }
- X return bits_per_byte;
- X}
- X
- Xint basic() {
- X /* The properties of the basic types.
- X Returns number of bits per sizeof unit */
- X volatile int bits_per_byte;
- X
- X bits_per_byte= cprop();
- X
- X /* Shorts, ints and longs *****************************************/
- X Vprintf("%sShort=%d int=%d long=%d float=%d double=%d bits %s\n",
- X co,
- X (int) sizeof(short)*bits_per_byte,
- X (int) sizeof(int)*bits_per_byte,
- X (int) sizeof(long)*bits_per_byte,
- X (int) sizeof(float)*bits_per_byte,
- X (int) sizeof(double)*bits_per_byte, oc);
- X if (stdc) {
- X Vprintf("%sLong double=%d bits%s\n",
- X co, (int) sizeof(Long_double)*bits_per_byte, oc);
- X }
- X Vprintf("%sChar pointers = %d bits%s%s\n",
- X co, (int)sizeof(char *)*bits_per_byte,
- X sizeof(char *)>sizeof(int)?" BEWARE! larger than int!":"",
- X oc);
- X Vprintf("%sInt pointers = %d bits%s%s\n",
- X co, (int)sizeof(int *)*bits_per_byte,
- X sizeof(int *)>sizeof(int)?" BEWARE! larger than int!":"",
- X oc);
- X sprop();
- X iprop();
- X lprop();
- X usprop();
- X uiprop();
- X ulprop();
- X
- X Unexpected(6);
- X
- X /* Alignment constants ********************************************/
- X Vprintf("%sAlignments used for char=%d short=%d int=%d long=%d%s\n",
- X co,
- X (int)sizeof(struct{char i1; char c1;})-(int)sizeof(char),
- X (int)sizeof(struct{short i2; char c2;})-(int)sizeof(short),
- X (int)sizeof(struct{int i3; char c3;})-(int)sizeof(int),
- X (int)sizeof(struct{long i4; char c4;})-(int)sizeof(long),
- X oc);
- X
- X /* Ten little endians *********************************************/
- X
- X endian(bits_per_byte);
- X
- X /* Pointers *******************************************************/
- X if (V) {
- X if ("abcd"=="abcd")
- X printf("%sStrings are shared%s\n", co, oc);
- X else printf("%sStrings are not shared%s\n", co, oc);
- X }
- X
- X return bits_per_byte;
- X}
- X
- X#endif /* ifndef PASS */
- X
- X/* As I said, I apologise for the contortions below. The functions are
- X expanded by the preprocessor twice or three times (for float and double,
- X and maybe for long double, and for short, int and long). That way,
- X I never make a change to one that I forget to make to the other.
- X You can look on it as C's fault for not supporting multi-line macro's.
- X This whole file is read 3 times by the preprocessor, with PASSn set for
- X n=1, 2 or 3, to decide which parts to reprocess.
- X*/
- X
- X/* #undef on an already undefined thing is (wrongly) flagged as an error
- X by some compilers, therefore the #ifdef that follows:
- X*/
- X#ifdef Number
- X#undef Number
- X#undef THING
- X#undef Thing
- X#undef thing
- X#undef FPROP
- X#undef Fname
- X#undef Store
- X#undef Sum
- X#undef Diff
- X#undef Mul
- X#undef Div
- X#undef Self
- X#undef F_check
- X#undef Validate
- X#undef EPROP
- X#undef MARK
- X
- X#undef F_RADIX
- X#undef F_MANT_DIG
- X#undef F_DIG
- X#undef F_ROUNDS
- X#undef F_EPSILON
- X#undef F_MIN_EXP
- X#undef F_MIN
- X#undef F_MIN_10_EXP
- X#undef F_MAX_EXP
- X#undef F_MAX
- X#undef F_MAX_10_EXP
- X#endif
- X
- X#ifdef Integer
- X#undef Integer
- X#undef INT
- X#undef IPROP
- X#undef Iname
- X#undef UPROP
- X#undef Uname
- X#undef OK_UI
- X
- X#undef I_MAX
- X#undef I_MIN
- X#undef U_MAX
- X#endif
- X
- X#ifdef PASS1
- X
- X#define Number float
- X#define THING "FLOAT"
- X#define Thing "Float"
- X#define thing "float"
- X#define Fname "FLT"
- X#define FPROP fprop
- X#define Store fStore
- X#define Sum fSum
- X#define Diff fDiff
- X#define Mul fMul
- X#define Div fDiv
- X#define Self fSelf
- X#define F_check fCheck
- X#define Validate fValidate
- X#define MARK "F"
- X
- X#define EPROP efprop
- X
- X#define Integer short
- X#define INT "short"
- X#define IPROP sprop
- X#define Iname "SHRT"
- X#ifndef NO_UI
- X#define OK_UI 1
- X#endif
- X
- X#define UPROP usprop
- X#define Uname "USHRT"
- X
- X#ifdef VERIFY
- X#define I_MAX SHRT_MAX
- X#define I_MIN SHRT_MIN
- X#define U_MAX USHRT_MAX
- X
- X#define F_RADIX FLT_RADIX
- X#define F_MANT_DIG FLT_MANT_DIG
- X#define F_DIG FLT_DIG
- X#define F_ROUNDS FLT_ROUNDS
- X#define F_EPSILON FLT_EPSILON
- X#define F_MIN_EXP FLT_MIN_EXP
- X#define F_MIN FLT_MIN
- X#define F_MIN_10_EXP FLT_MIN_10_EXP
- X#define F_MAX_EXP FLT_MAX_EXP
- X#define F_MAX FLT_MAX
- X#define F_MAX_10_EXP FLT_MAX_10_EXP
- X#endif /* VERIFY */
- X
- X#endif /* PASS1 */
- X
- X#ifdef PASS2
- X
- X#define Number double
- X#define THING "DOUBLE"
- X#define Thing "Double"
- X#define thing "double"
- X#define Fname "DBL"
- X#define FPROP dprop
- X#define Store dStore
- X#define Sum dSum
- X#define Diff dDiff
- X#define Mul dMul
- X#define Div dDiv
- X#define Self dSelf
- X#define F_check dCheck
- X#define Validate dValidate
- X#define MARK ""
- X
- X#define EPROP edprop
- X
- X#define Integer int
- X#define INT "int"
- X#define IPROP iprop
- X#define Iname "INT"
- X#define OK_UI 1 /* Unsigned int is always possible */
- X
- X#define UPROP uiprop
- X#define Uname "UINT"
- X
- X#ifdef VERIFY
- X#define I_MAX INT_MAX
- X#define I_MIN INT_MIN
- X#define U_MAX UINT_MAX
- X
- X#define F_MANT_DIG DBL_MANT_DIG
- X#define F_DIG DBL_DIG
- X#define F_EPSILON DBL_EPSILON
- X#define F_MIN_EXP DBL_MIN_EXP
- X#define F_MIN DBL_MIN
- X#define F_MIN_10_EXP DBL_MIN_10_EXP
- X#define F_MAX_EXP DBL_MAX_EXP
- X#define F_MAX DBL_MAX
- X#define F_MAX_10_EXP DBL_MAX_10_EXP
- X#endif /* VERIFY */
- X
- X#endif /* PASS2 */
- X
- X#ifdef PASS3
- X
- X#ifdef __STDC__
- X#define Number long double
- X#endif
- X
- X#define THING "LONG DOUBLE"
- X#define Thing "Long double"
- X#define thing "long double"
- X#define Fname "LDBL"
- X#define FPROP ldprop
- X#define Store ldStore
- X#define Sum ldSum
- X#define Diff ldDiff
- X#define Mul ldMul
- X#define Div ldDiv
- X#define Self ldSelf
- X#define F_check ldCheck
- X#define Validate ldValidate
- X#define MARK "L"
- X
- X#define EPROP eldprop
- X
- X#define Integer long
- X#define INT "long"
- X#define IPROP lprop
- X#define Iname "LONG"
- X#ifndef NO_UI
- X#define OK_UI 1
- X#endif
- X
- X#define UPROP ulprop
- X#define Uname "ULONG"
- X
- X#ifdef VERIFY
- X#define I_MAX LONG_MAX
- X#define I_MIN LONG_MIN
- X#define U_MAX ULONG_MAX
- X
- X#define F_MANT_DIG LDBL_MANT_DIG
- X#define F_DIG LDBL_DIG
- X#define F_EPSILON LDBL_EPSILON
- X#define F_MIN_EXP LDBL_MIN_EXP
- X#define F_MIN LDBL_MIN
- X#define F_MIN_10_EXP LDBL_MIN_10_EXP
- X#define F_MAX_EXP LDBL_MAX_EXP
- X#define F_MAX LDBL_MAX
- X#define F_MAX_10_EXP LDBL_MAX_10_EXP
- X#endif /* VERIFY */
- X
- X#endif /* PASS3 */
- X
- X#ifndef VERIFY
- X#define I_MAX int_max
- X#define I_MIN int_min
- X#define U_MAX int_max
- X
- X#define F_RADIX f_radix
- X#define F_MANT_DIG f_mant_dig
- X#define F_DIG f_dig
- X#define F_ROUNDS f_rounds
- X#define F_EPSILON f_epsilon
- X#define F_MIN_EXP f_min_exp
- X#define F_MIN f_min
- X#define F_MIN_10_EXP f_min_10_exp
- X#define F_MAX_EXP f_max_exp
- X#define F_MAX f_max
- X#define F_MAX_10_EXP f_max_10_exp
- X#endif
- X
- XProcedure IPROP() { /* for short, int, and long */
- X volatile Integer newi, int_max, maxeri, int_min, minneri;
- X volatile int ibits, ipower, two=2;
- X
- X /* Calculate max short/int/long ***********************************/
- X /* Calculate 2**n-1 until overflow - then use the previous value */
- X
- X newi=1; int_max=0;
- X
- X if (setjmp(lab)==0) { /* Yields int_max */
- X for(ipower=0; newi>int_max; ipower++) {
- X int_max=newi;
- X newi=newi*two+1;
- X }
- X Vprintf("%sOverflow of a%s %s does not generate a trap%s\n",
- X co, INT[0]=='i'?"n":"", INT, oc);
- X } else {
- X Vprintf("%sOverflow of a%s %s generates a trap%s\n",
- X co, INT[0]=='i'?"n":"", INT, oc);
- X }
- X Unexpected(7);
- X
- X /* Minimum value: assume either two's or one's complement *********/
- X int_min= -int_max;
- X if (setjmp(lab)==0) { /* Yields int_min */
- X if (int_min-1 < int_min) int_min--;
- X }
- X Unexpected(8);
- X
- X /* Now for those daft Cybers: */
- X
- X maxeri=0; newi=int_max;
- X
- X if (setjmp(lab)==0) { /* Yields maxeri */
- X for(ibits=ipower; newi>maxeri; ibits++) {
- X maxeri=newi;
- X newi=newi+newi+1;
- X }
- X }
- X Unexpected(9);
- X
- X minneri= -maxeri;
- X if (setjmp(lab)==0) { /* Yields minneri */
- X if (minneri-1 < minneri) minneri--;
- X }
- X Unexpected(10);
- X
- X Vprintf("%sMaximum %s = %ld (= 2**%d-1)%s\n",
- X co, INT, (long)int_max, ipower, oc);
- X Vprintf("%sMinimum %s = %ld%s\n", co, INT, (long)int_min, oc);
- X
- X if (L) i_define(Iname, "_MAX", (long) int_max, (long) I_MAX);
- X if (L) i_define(Iname, "_MIN", (long) int_min, (long) I_MIN);
- X
- X if (maxeri>int_max) {
- X Vprintf("%sThere is a larger %s, %ld (= 2**%d-1), %s %s%s\n",
- X co, INT, (long)maxeri, ibits,
- X "but only for addition, not multiplication",
- X "(I smell a Cyber!)",
- X oc);
- X }
- X
- X if (minneri<int_min) {
- X Vprintf("%sThere is a smaller %s, %ld, %s %s%s\n",
- X co, INT, (long)minneri,
- X "but only for addition, not multiplication",
- X "(I smell a Cyber!)",
- X oc);
- X }
- X}
- X
- XProcedure UPROP () { /* unsigned short/int/long */
- X#ifdef OK_UI
- X volatile unsigned Integer int_max, newi, two;
- X newi=1; int_max=0; two=2;
- X
- X if (setjmp(lab)==0) { /* Yields int_max */
- X while(newi>int_max) {
- X int_max=newi;
- X newi=newi*two+1;
- X }
- X }
- X Unexpected(11);
- X Vprintf("%sMaximum unsigned %s = %lu%s\n",
- X co, INT, (unsigned long) int_max, oc);
- X if (L) u_define(Uname, "_MAX", (unsigned long) int_max,
- X (unsigned long) U_MAX);
- X#endif
- X}
- X
- X
- X#ifdef Number
- X
- X/* These routines are intended to defeat any attempt at optimisation
- X or use of extended precision, and to defeat faulty narrowing casts:
- X*/
- XProcedure Store(a, b) Number a, *b; { *b=a; }
- XNumber Sum(a, b) Number a, b; { Number r; Store(a+b, &r); return (r); }
- XNumber Diff(a, b) Number a, b; { Number r; Store(a-b, &r); return (r); }
- XNumber Mul(a, b) Number a, b; { Number r; Store(a*b, &r); return (r); }
- XNumber Div(a, b) Number a, b; { Number r; Store(a/b, &r); return (r); }
- XNumber Self(a) Number a; { Number r; Store(a, &r); return (r); }
- X
- XProcedure F_check(precision, val1) int precision; Long_double val1; {
- X /* You don't think I'm going to go to all the trouble of writing
- X a program that works out what all sorts of values are, only to
- X have printf go and print the wrong values out, do you?
- X No, you're right, so this function tries to see if printf
- X has written the right value, by reading it back again.
- X This introduces a new problem of course: suppose printf writes
- X the correct value, and scanf reads it back wrong... oh well.
- X But I'm adamant about this: the precision given is enough
- X to uniquely identify the printed number, therefore I insist
- X that sscanf read the number back identically. Harsh yes, but
- X sometimes you've got to be cruel to be kind.
- X */
- X Long_double new1;
- X Number val, new, diff;
- X double rem;
- X int e;
- X char *rep;
- X char *f2;
- X
- X if (sizeof(double) == sizeof(Long_double)) {
- X /* Assume they're the same, and use non-stdc format */
- X /* This is for stdc compilers using non-stdc libraries */
- X f2= "%le"; /* Input */
- X } else {
- X /* It had better support Le then */
- X f2= "%Le";
- X }
- X val= val1;
- X rep= f_rep(precision, (Long_double) val);
- X if (setjmp(lab)==0) {
- X sscanf(rep, f2, &new1);
- X } else {
- X eek_a_bug("sscanf caused a trap");
- X printf("%s scanning: %s format: %s%s\n\n", co, rep, f2, oc);
- X Unexpected(12);
- X return;
- X }
- X
- X if (setjmp(lab)==0) { /* See if new is usable */
- X new= new1;
- X if (new != 0.0) {
- X diff= val/new - 1.0;
- X if (diff < 0.1) diff= 1.0;
- X /* That should be enough to generate a trap */
- X }
- X } else {
- X eek_a_bug("sscanf returned an unusable number");
- X printf("%s scanning: %s with format: %s%s\n\n",
- X co, rep, f2, oc);
- X Unexpected(13);
- X return;
- X }
- X
- X Unexpected(14);
- X if (new != val) {
- X eek_a_bug("Possibly bad output from printf above");
- X if (!exponent(val, &rem, &e)) {
- X printf("%s but value was an unusable number%s\n\n",
- X co, oc);
- X return;
- X }
- X printf("%s expected value around %.*fe%d, bit pattern:\n ",
- X co, precision, rem, e);
- X bitpattern((char *) &val, sizeof(val));
- X printf ("%s\n", oc);
- X printf("%s sscanf gave %s, bit pattern:\n ",
- X co, f_rep(precision, (Long_double) new));
- X bitpattern((char *) &new, sizeof(new));
- X printf ("%s\n", oc);
- X printf("%s difference= %s%s\n\n",
- X co, f_rep(precision, (Long_double) (val-new)), oc);
- X }
- X}
- X
- XProcedure Validate(prec, val, req, same) int prec, same; Long_double val, req; {
- X Unexpected(15);
- X if (!same) {
- X printf("%s*** Verify failed for above #define!\n", co);
- X if (setjmp(lab) == 0) { /* for the case that req == nan */
- X printf(" Compiler has %s for value%s\n",
- X f_rep(prec, req), oc);
- X } else {
- X printf(" Compiler has %s for value%s\n",
- X "an unusable number", oc);
- X }
- X if (setjmp(lab) == 0) {
- X F_check(prec, (Long_double) req);
- X } /*else forget it*/
- X if (setjmp(lab) == 0) {
- X if (req > 0.0 && val > 0.0) {
- X printf("%s difference= %s%s\n",
- X co, f_rep(prec, val-req), oc);
- X }
- X } /*else forget it*/
- X Unexpected(16);
- X printf("\n");
- X bugs++;
- X } else if (val != req) {
- X if (stdc) {
- X printf("%s*** Verify failed for above #define!\n", co);
- X printf(" Constant has the wrong precision%s\n",
- X oc);
- X bugs++;
- X } else eek_a_bug("the cast didn't work");
- X printf("\n");
- X }
- X}
- X
- Xint FPROP(bits_per_byte) int bits_per_byte; {
- X /* Properties of floating types, using algorithms by Cody and Waite
- X from MA Malcolm, as modified by WM Gentleman and SB Marovich.
- X Further extended by S Pemberton.
- X
- X Returns the number of digits in the fraction.
- X */
- X
- X volatile int i, f_radix, iexp, irnd, mrnd, f_rounds, f_mant_dig,
- X iz, k, inf, machep, f_max_exp, f_min_exp, mx, negeps,
- X mantbits, digs, f_dig, trap,
- X hidden, normal, f_min_10_exp, f_max_10_exp;
- X volatile Number a, b, base, basein, basem1, f_epsilon, epsneg,
- X f_max, newxmax, f_min, xminner, y, y1, z, z1, z2;
- X
- X Unexpected(17);
- X
- X Vprintf("%sPROPERTIES OF %s:%s\n", co, THING, oc);
- X
- X /* Base and size of mantissa **************************************/
- X /* First repeatedly double until adding 1 has no effect. */
- X /* For instance, if base is 10, with 3 significant digits */
- X /* it will try 1, 2, 4, 8, ... 512, 1024, and stop there, */
- X /* since 1024 is only representable as 1020. */
- X a=1.0;
- X if (setjmp(lab)==0) { /* inexact trap? */
- X do { a=Sum(a, a); }
- X while (Diff(Diff(Sum(a, 1.0), a), 1.0) == 0.0);
- X } else {
- X fprintf(stderr, "*** Program got loss-of-precision trap!\n");
- X /* And supporting those is just TOO much trouble! */
- X exit(bugs+1);
- X }
- X Unexpected(18);
- X /* Now double until you find a number that can be added to the */
- X /* above number. For 1020 this is 8 or 16, depending whether the */
- X /* result is rounded or truncated. */
- X /* In either case the result is 1030. 1030-1020= the base, 10. */
- X b=1.0;
- X do { b=Sum(b, b); } while ((base=Diff(Sum(a, b), a)) == 0.0);
- X f_radix=base;
- X Vprintf("%sBase = %d%s\n", co, f_radix, oc);
- X
- X /* Sanity check; if base<2, I can't guarantee the rest will work */
- X if (f_radix < 2) {
- X eek_a_bug("Function return or parameter passing faulty? (This is a guess.)");
- X printf("\n");
- X return(0);
- X }
- X
- X#ifdef PASS1 /* only for FLT */
- X if (F) i_define("FLT", "_RADIX", (long) f_radix, (long) F_RADIX);
- X#endif
- X
- X /* Now the number of digits precision: */
- X f_mant_dig=0; b=1.0;
- X do { f_mant_dig++; b=Mul(b, base); }
- X while (Diff(Diff(Sum(b,1.0),b),1.0) == 0.0);
- X f_dig=floor_log(10, (Long_double)(b/base)) + (base==10?1:0);
- X Vprintf("%sSignificant base digits = %d %s %d %s%s\n",
- X co, f_mant_dig, "(= at least", f_dig, "decimal digits)", oc);
- X if (F) i_define(Fname, "_MANT_DIG", (long) f_mant_dig,
- X (long) F_MANT_DIG);
- X if (F) i_define(Fname, "_DIG", (long) f_dig, (long) F_DIG);
- X digs= ceil_log(10, (Long_double)b); /* the number of digits to printf */
- X
- X /* Rounding *******************************************************/
- X basem1=Diff(base, 0.5);
- X if (Diff(Sum(a, basem1), a) != 0.0) {
- X if (f_radix == 2) basem1=0.375;
- X else basem1=1.0;
- X if (Diff(Sum(a, basem1), a) != 0.0) irnd=2; /* away from 0 */
- X else irnd=1; /* to nearest */
- X } else irnd=0; /* towards 0 */
- X
- X basem1=Diff(base, 0.5);
- X
- X if (Diff(Diff(-a, basem1), -a) != 0.0) {
- X if (f_radix == 2) basem1=0.375;
- X else basem1=1.0;
- X if (Diff(Diff(-a, basem1), -a) != 0.0) mrnd=2; /* away from 0*/
- X else mrnd=1; /* to nearest */
- X } else mrnd=0; /* towards 0 */
- X
- X f_rounds=4; /* Unknown rounding */
- X if (irnd==0 && mrnd==0) f_rounds=0; /* zero = chops */
- X if (irnd==1 && mrnd==1) f_rounds=1; /* nearest */
- X if (irnd==2 && mrnd==0) f_rounds=2; /* +inf */
- X if (irnd==0 && mrnd==2) f_rounds=3; /* -inf */
- X
- X if (f_rounds != 4) {
- X Vprintf("%sArithmetic rounds towards ", co);
- X switch (f_rounds) {
- X case 0: Vprintf("zero (i.e. it chops)"); break;
- X case 1: Vprintf("nearest"); break;
- X case 2: Vprintf("+infinity"); break;
- X case 3: Vprintf("-infinity"); break;
- X default: Vprintf("???"); break;
- X }
- X Vprintf("%s\n", oc);
- X } else { /* Hmm, try to give some help here: */
- X Vprintf("%sArithmetic rounds oddly: %s\n", co, oc);
- X Vprintf("%s Negative numbers %s%s\n",
- X co, mrnd==0 ? "towards zero" :
- X mrnd==1 ? "to nearest" :
- X "away from zero",
- X oc);
- X Vprintf("%s Positive numbers %s%s\n",
- X co, irnd==0 ? "towards zero" :
- X irnd==1 ? "to nearest" :
- X "away from zero",
- X oc);
- X }
- X /* An extra goody */
- X if (f_radix == 2 && f_rounds == 1) {
- X if (Diff(Sum(a, 1.0), a) != 0.0) {
- X Vprintf("%s Tie breaking rounds up%s\n", co, oc);
- X } else if (Diff(Sum(a, 3.0), a) == 4.0) {
- X Vprintf("%s Tie breaking rounds to even%s\n", co, oc);
- X } else {
- X Vprintf("%s Tie breaking rounds down%s\n", co, oc);
- X }
- X }
- X#ifdef PASS1 /* only for FLT */
- X if (F) i_define("FLT", "_ROUNDS", (long) f_rounds, (long) F_ROUNDS);
- X#endif
- X
- X /* Various flavours of epsilon ************************************/
- X negeps=f_mant_dig+f_mant_dig;
- X basein=1.0/base;
- X a=1.0;
- X for(i=1; i<=negeps; i++) a*=basein;
- X
- X b=a;
- X while (Diff(Diff(1.0, a), 1.0) == 0.0) {
- X a*=base;
- X negeps--;
- X }
- X negeps= -negeps;
- X Vprintf("%sSmallest x such that 1.0-base**x != 1.0 = %d%s\n",
- X co, negeps, oc);
- X
- X epsneg=a;
- X if ((f_radix!=2) && irnd) {
- X /* a=(a*(1.0+a))/(1.0+1.0); => */
- X a=Div(Mul(a, Sum(1.0, a)), Sum(1.0, 1.0));
- X /* if ((1.0-a)-1.0 != 0.0) epsneg=a; => */
- X if (Diff(Diff(1.0, a), 1.0) != 0.0) epsneg=a;
- X }
- X Vprintf("%sSmall x such that 1.0-x != 1.0 = %s%s\n",
- X co, f_rep(digs, (Long_double) epsneg), oc);
- X /* it may not be the smallest */
- X if (V) F_check(digs, (Long_double) epsneg);
- X Unexpected(19);
- X
- X machep= -f_mant_dig-f_mant_dig;
- X a=b;
- X while (Diff(Sum(1.0, a), 1.0) == 0.0) { a*=base; machep++; }
- X Vprintf("%sSmallest x such that 1.0+base**x != 1.0 = %d%s\n",
- X co, machep, oc);
- X
- X f_epsilon=a;
- X if ((f_radix!=2) && irnd) {
- X /* a=(a*(1.0+a))/(1.0+1.0); => */
- X a=Div(Mul(a, Sum(1.0, a)), Sum(1.0, 1.0));
- X /* if ((1.0+a)-1.0 != 0.0) f_epsilon=a; => */
- X if (Diff(Sum(1.0, a), 1.0) != 0.0) f_epsilon=a;
- X }
- X Vprintf("%sSmallest x such that 1.0+x != 1.0 = %s%s\n",
- X co, f_rep(digs, (Long_double) f_epsilon), oc);
- X /* Possible loss of precision warnings here from non-stdc compilers: */
- X if (F) f_define(Fname, "_EPSILON", digs, (Long_double) f_epsilon, MARK);
- X if (V || F) F_check(digs, (Long_double) f_epsilon);
- X Unexpected(20);
- X if (F) Validate(digs, (Long_double) f_epsilon, (Long_double) F_EPSILON,
- X f_epsilon == Self(F_EPSILON));
- X Unexpected(21);
- X
- X /* Extra chop info *************************************************/
- X if (f_rounds == 0) {
- X if (Diff(Mul(Sum(1.0,f_epsilon),1.0),1.0) != 0.0) {
- X Vprintf("%sAlthough arithmetic chops, it uses guard digits%s\n", co, oc);
- X }
- X }
- X
- X /* Size of and minimum normalised exponent ************************/
- X y=0; i=0; k=1; z=basein; z1=(1.0+f_epsilon)/base;
- X
- X /* Coarse search for the largest power of two */
- X if (setjmp(lab)==0) { /* for underflow trap */ /* Yields i, k, y, y1 */
- X do {
- X y=z; y1=z1;
- X z=Mul(y,y); z1=Mul(z1, y);
- X a=Mul(z,1.0);
- X z2=Div(z1,y);
- X if (z2 != y1) break;
- X if ((Sum(a,a) == 0.0) || (fabs(z) >= y)) break;
- X i++;
- X k+=k;
- X } while(1);
- X } else {
- X Vprintf("%s%s underflow generates a trap%s\n", co, Thing, oc);
- X }
- X Unexpected(22);
- X
- X if (f_radix != 10) {
- X iexp=i+1; /* for the sign */
- X mx=k+k;
- X } else {
- X iexp=2;
- X iz=f_radix;
- X while (k >= iz) { iz*=f_radix; iexp++; }
- X mx=iz+iz-1;
- X }
- X
- X /* Fine tune starting with y and y1 */
- X if (setjmp(lab)==0) { /* for underflow trap */ /* Yields k, f_min */
- X do {
- X f_min=y; z1=y1;
- X y=Div(y,base); y1=Div(y1,base);
- X a=Mul(y,1.0);
- X z2=Mul(y1,base);
- X if (z2 != z1) break;
- X if ((Sum(a,a) == 0.0) || (fabs(y) >= f_min)) break;
- X k++;
- X } while (1);
- X }
- X Unexpected(23);
- X
- X f_min_exp=(-k)+1;
- X
- X if ((mx <= k+k-3) && (f_radix != 10)) { mx+=mx; iexp+=1; }
- X Vprintf("%sNumber of bits used for exponent = %d%s\n", co, iexp, oc);
- X Vprintf("%sMinimum normalised exponent = %d%s\n", co, f_min_exp, oc);
- X if (F) i_define(Fname, "_MIN_EXP", (long) f_min_exp, (long) F_MIN_EXP);
- X
- X if (setjmp(lab)==0) {
- X Vprintf("%sMinimum normalised positive number = %s%s\n",
- X co, f_rep(digs, (Long_double) f_min), oc);
- X } else {
- X eek_a_bug("printf can't print the smallest normalised number");
- X printf("\n");
- X }
- X Unexpected(24);
- X /* Possible loss of precision warnings here from non-stdc compilers: */
- X if (setjmp(lab) == 0) {
- X if (F) f_define(Fname, "_MIN", digs, (Long_double) f_min, MARK);
- X if (V || F) F_check(digs, (Long_double) f_min);
- X } else {
- X eek_a_bug("xxx_MIN caused a trap");
- X printf("\n");
- X }
- X
- X if (setjmp(lab) == 0) {
- X if (F) Validate(digs, (Long_double) f_min, (Long_double) F_MIN,
- X f_min == Self(F_MIN));
- X } else {
- X printf("%s*** Verify failed for above #define!\n %s %s\n\n",
- X co, "Compiler has an unusable number for value", oc);
- X bugs++;
- X }
- X Unexpected(25);
- X
- X a=1.0; f_min_10_exp=0;
- X while (a > f_min*10.0) { a/=10.0; f_min_10_exp--; }
- X if (F) i_define(Fname, "_MIN_10_EXP", (long) f_min_10_exp,
- X (long) F_MIN_10_EXP);
- X
- X /* Minimum exponent ************************************************/
- X if (setjmp(lab)==0) { /* for underflow trap */ /* Yields xminner */
- X do {
- X xminner=y;
- X y=Div(y,base);
- X a=Mul(y,1.0);
- X if ((Sum(a,a) == 0.0) || (fabs(y) >= xminner)) break;
- X } while (1);
- X }
- X Unexpected(26);
- X
- X if (xminner != 0.0 && xminner != f_min) {
- X normal= 0;
- X Vprintf("%sThe smallest numbers are not kept normalised%s\n",
- X co, oc);
- X if (setjmp(lab)==0) {
- X Vprintf("%sSmallest unnormalised positive number = %s%s\n",
- X co, f_rep(digs, (Long_double) xminner), oc);
- X if (V) F_check(digs, (Long_double) xminner);
- X } else {
- X eek_a_bug("printf can't print the smallest unnormalised number.");
- X printf("\n");
- X }
- X Unexpected(27);
- X } else {
- X normal= 1;
- X Vprintf("%sThe smallest numbers are normalised%s\n", co, oc);
- X }
- X
- X /* Maximum exponent ************************************************/
- X f_max_exp=2; f_max=1.0; newxmax=base+1.0;
- X inf=0; trap=0;
- X while (f_max<newxmax) {
- X f_max=newxmax;
- X if (setjmp(lab) == 0) { /* Yields inf, f_max_exp */
- X newxmax=Mul(newxmax, base);
- X } else {
- X trap=1;
- X break;
- X }
- X if (Div(newxmax, base) != f_max) {
- X inf=1; /* ieee infinity */
- X break;
- X }
- X f_max_exp++;
- X }
- X Unexpected(28);
- X if (trap) {
- X Vprintf("%s%s overflow generates a trap%s\n", co, Thing, oc);
- X }
- X
- X if (inf) Vprintf("%sThere is an 'infinite' value%s\n", co, oc);
- X Vprintf("%sMaximum exponent = %d%s\n", co, f_max_exp, oc);
- X if (F) i_define(Fname, "_MAX_EXP", (long) f_max_exp, (long) F_MAX_EXP);
- X
- X /* Largest number ***************************************************/
- X f_max=Diff(1.0, epsneg);
- X if (Mul(f_max,1.0) != f_max) f_max=Diff(1.0, Mul(base,epsneg));
- X for (i=1; i<=f_max_exp; i++) f_max=Mul(f_max, base);
- X
- X if (setjmp(lab)==0) {
- X Vprintf("%sMaximum number = %s%s\n",
- X co, f_rep(digs, (Long_double) f_max), oc);
- X } else {
- X eek_a_bug("printf can't print the largest double.");
- X printf("\n");
- X }
- X if (setjmp(lab)==0) {
- X /* Possible loss of precision warnings here from non-stdc compilers: */
- X if (F) f_define(Fname, "_MAX", digs, (Long_double) f_max, MARK);
- X if (V || F) F_check(digs, (Long_double) f_max);
- X } else {
- X eek_a_bug("xxx_MAX caused a trap");
- X printf("\n");
- X }
- X if (setjmp(lab)==0) {
- X if (F) Validate(digs, (Long_double) f_max, (Long_double) F_MAX,
- X f_max == Self(F_MAX));
- X } else {
- X printf("%s*** Verify failed for above #define!\n %s %s\n\n",
- X co, "Compiler has an unusable number for value", oc);
- X bugs++;
- X }
- X Unexpected(29);
- X
- X a=1.0; f_max_10_exp=0;
- X while (a < f_max/10.0) { a*=10.0; f_max_10_exp++; }
- X if (F) i_define(Fname, "_MAX_10_EXP", (long) f_max_10_exp,
- X (long) F_MAX_10_EXP);
- X
- X /* Hidden bit + sanity check ****************************************/
- X if (f_radix != 10) {
- X hidden=0;
- X mantbits=floor_log(2, (Long_double)f_radix)*f_mant_dig;
- X if (mantbits+iexp == (int)sizeof(Number)*bits_per_byte) {
- X hidden=1;
- X Vprintf("%sArithmetic uses a hidden bit%s\n", co, oc);
- X } else if (mantbits+iexp+1 == (int)sizeof(Number)*bits_per_byte) {
- X Vprintf("%sArithmetic doesn't use a hidden bit%s\n",
- X co, oc);
- X } else {
- X printf("\n%s%s\n %s %s %s!%s\n\n",
- X co,
- X "*** Something fishy here!",
- X "Exponent size + mantissa size doesn't match",
- X "with the size of a", thing,
- X oc);
- X }
- X if (hidden && f_radix == 2 && f_max_exp+f_min_exp==3) {
- X Vprintf("%sIt looks like %s length IEEE format%s\n",
- X co, f_mant_dig==24 ? "single" :
- X f_mant_dig==53 ? "double" :
- X f_mant_dig >53 ? "extended" :
- X "some", oc);
- X if (f_rounds != 1 || normal) {
- X Vprintf("%s though ", co);
- X if (f_rounds != 1) {
- X Vprintf("the rounding is unusual");
- X if (normal) Vprintf(" and ");
- X }
- X if (normal) Vprintf("the normalisation is unusual");
- X Vprintf("%s\n", oc);
- X }
- X } else {
- X Vprintf("%sIt doesn't look like IEEE format%s\n",
- X co, oc);
- X }
- X }
- X printf("\n"); /* regardless of verbosity */
- X return f_mant_dig;
- X}
- X
- XProcedure EPROP(fprec, dprec, lprec) int fprec, dprec, lprec; {
- X /* See if expressions are evaluated in extended precision.
- X Some compilers optimise even if you don't want it,
- X and then this function fails to produce the right result.
- X We try to diagnose this if it happens.
- X */
- X volatile int eprec;
- X volatile double a, b, base, old;
- X volatile Number d, oldd, dbase, one, zero;
- X volatile int bad=0;
- X
- X /* Size of mantissa **************************************/
- X a=1.0;
- X if (setjmp(lab) == 0) { /* Yields nothing */
- X do { old=a; a=a+a; }
- X while ((((a+1.0)-a)-1.0) == 0.0 && a>old);
- X } else bad=1;
- X if (a <= old) bad=1;
- X
- X if (!bad) {
- X b=1.0;
- X if (setjmp(lab) == 0) { /* Yields nothing */
- X do { old=b; b=b+b; }
- X while ((base=((a+b)-a)) == 0.0 && b>old);
- X if (b <= old) bad=1;
- X } else bad=1;
- X }
- X
- X if (!bad) {
- X eprec=0; d=1.0; dbase=base; one=1.0; zero=0.0;
- X if (setjmp(lab) == 0) { /* Yields nothing */
- X do { eprec++; oldd=d; d=d*dbase; }
- X while ((((d+one)-d)-one) == zero && d>oldd);
- X if (d <= oldd) bad=1;
- X } else bad=1;
- X }
- X
- X Unexpected(30);
- X
- X if (bad) {
- X Vprintf("%sCan't determine precision for %s expressions:\n%s%s\n",
- X co, thing, " check that you compiled without optimisation!",
- X oc);
- X } else if (eprec==dprec) {
- X Vprintf("%s%s expressions are evaluated in double precision%s\n",
- X co, Thing, oc);
- X } else if (eprec==fprec) {
- X Vprintf("%s%s expressions are evaluated in float precision%s\n",
- X co, Thing, oc);
- X } else if (eprec==lprec) {
- X Vprintf("%s%s expressions are evaluated in long double precision%s\n",
- X co, Thing, oc);
- X } else {
- X Vprintf("%s%s expressions are evaluated in a %s %s %d %s%s\n",
- X co, Thing, eprec>dprec ? "higher" : "lower",
- X "precision than double,\n using",
- X eprec, "base digits",
- X oc);
- X }
- X}
- X
- X#else /* Number */
- X
- X#ifdef FPROP
- X/* ARGSUSED */
- Xint FPROP(bits_per_byte) int bits_per_byte; {
- X return 0;
- X}
- X#endif
- X#ifdef EPROP
- X/* ARGSUSED */
- XProcedure EPROP(fprec, dprec, lprec) int fprec, dprec, lprec; {}
- X#endif
- X
- X#endif /* ifdef Number */
- X
- X#ifdef PASS3
- X#undef PASS
- X#endif
- X
- X#ifdef PASS2
- X#undef PASS2
- X#define PASS3 1
- X#endif
- X
- X#ifdef PASS1
- X#undef PASS1
- X#define PASS2 1
- X#endif
- X
- X/* If your C compiler doesn't accept the next #include,
- X replace __FILE__ with the file name - and get a new C compiler... */
- X
- X#ifdef PASS
- X#include __FILE__
- X#endif
- X
- EOF
- chars=`wc -c < 'config.c'`
- if test $chars != 49439; then echo 'config.c' is $chars characters, should be 49439 characters!; fi
- fi
- exit 0
-
-
-