#define PROGRAM enquire.c #define VERSION "5.1a" #define PURPOSE Everything you wanted to know about your machine and C compiler #define BUT didnt know who to ask #define FOR Any OS, any C compiler #define AUTHOR Steven Pemberton, CWI, Amsterdam; "Steven.Pemberton@cwi.nl" #define COPYRIGHT(c) 1993-9 Steven Pemberton, CWI. All rights reserved. #define NOTE Improvements gratefully received. Please mention the version. #define COMPILE On Unix compile with: "sh enquire.c"; see below for details #define WEB "http://www.cwi.nl/~steven/enquire.html" PURPOSE This is a program that determines many properties of the C compiler and machine that it is run on, such as minimum and maximum [un]signed char/int/long, many properties of float/ [long] double, and so on. As an option it produces the ANSI C float.h and limits.h files. As a further option, it even checks that the compiler reads the header files correctly. It is a good test-case for compilers, since it exercises them with many limiting values, such as the minimum and maximum floating-point numbers. COMPILING AND RUNNING ON UNIX MACHINES With luck and a following wind, on Unix systems just the following will work: sh enquire.c (or whatever filename you chose). Any parameters are passed to the C compiler, so if the compilation fails for any reason curable as explained below, you can do the following: sh enquire.c -DBAD_CPP If you do get compilation errors, check the line in question. Very often there is a comment attached saying which define to set. You can use a different C compiler than the default cc by setting CC: CC=gcc sh enquire.c -ansi You can load extra libraries by setting LIBS: CC=gcc LIBS=-lflong sh enquire.c -ansi Add ID="string" for the string to be added to the output; for instance: ID="`hostname` cc -ansi" sh enquire.c -ansi Compiling may give messages about unreachable code. These you can ignore. Some compilers offer various flags for different floating point modes; it's worth trying all possible combinations of these. Don't say I haven't tried to make life easy for you... COMPILING AND RUNNING ON NON-UNIX SYSTEMS On non-Unix systems, you must say (the equivalent of): cc enquire.c -o enquire enquire > enquire.out enquire -l > limits.h enquire -f > float.h cc -DVERIFY enquire.c -o verify #this includes limits.h and float.h verify -fl > verify.out If your compiler doesn't support: add flag: signed char (eg pcc) -DNO_SC unsigned char -DNO_UC unsigned short and long -DNO_UI void -DNO_VOID signal(), or setjmp/longjmp() -DNO_SIG Try to compile first with no flags, and see if you get any errors - you might be surprised. (Most non-ANSI compilers need -DNO_SC, though.) Some compilers need a -f flag for floating point. Don't use any optimisation flags: the program may not work if you do. Though "while (a+1.0-a-1.0 == 0.0)" may look like "while(1)" to an optimiser, to a floating-point unit there's a world of difference. Compiling may give messages about unreachable code. These you can ignore. Some compilers offer various flags for different floating point modes; it's worth trying all possible combinations of these. FAULTY COMPILERS Because of bugs and/or inadequacies, some compilers need the following defines: - If your C preprocessor doesn't have the predefined __FILE__ macro, and you don't want to call this file enquire.c but, say, tell.c, add the flag -DFILENAME=\"tell.c\" . - Some compilers won't accept the line "#include FILENAME". Add flag -DBAD_CPP. In that case, this file *must* be called enquire.c. - Some compilers can't cope with "#ifdef __FILE__". Use -DFILENAME= or -DBAD_CPP as above. - Some naughty compilers define __STDC__, but don't really support it. Some define it as 0, in which case we ignore it. But if your compiler defines it, and isn't really ANSI C, add flag -DBAD_STDC. (To those compiler writers: for shame). - Some naughty compilers define __STDC__, but don't have the stddef.h include file. Add flag -DBAD_STDDEF. (Gcc needs this on some machines, due to clashes between stddef.h and other include files.) - Some systems crash when you try to malloc all store. To save users of such defective systems too much grief, they may compile with -DBAD_MALLOC, which ignores that bit of the code. Summary of naughty-compiler flags: If your compiler doesn't support: add flag: __FILE__ (and you changed the filename) -DFILENAME=\"name.c\" #ifdef __FILE__ -DBAD_CPP or -DFILENAME=... #include FILENAME -DBAD_CPP __STDC__ (properly) -DBAD_STDC stddef.h -DBAD_STDDEF malloc(LOTS) == NULL -DBAD_MALLOC While it is not our policy to support defective compilers, pity has been taken on people with compilers that can't produce object files bigger than 32k (especially since it was an easy addition). Compile the program into separate parts like this: cc -c -DSEP -DPASS0 -o p0.o enquire.c cc -c -DSEP -DPASS1 -o p1.o enquire.c cc -c -DSEP -DPASS2 -o p2.o enquire.c cc -c -DSEP -DPASS3 -o p3.o enquire.c cc -o enquire p0.o p1.o p2.o p3.o SYSTEM DEPENDENCIES You may possibly need to add some calls to signal() for other sorts of exception on your machine than SIGFPE, SIGOVER, SIGBUS, and SIGSEGV. See lines beginning #ifdef SIGxxx (and communicate the differences to me!). OUTPUT Running without argument gives the information as English text. If run with argument -l (e.g. enquire -l), output is a series of #define's for the ANSI standard limits.h include file, excluding MB_MAX_CHAR. If run with argument -f, output is a series of #define's for the ANSI standard float.h include file (according to ANSI C Draft of Dec 7, 1988). Flag -v gives verbose output: output includes the English text above as C comments. The program exit(0)'s if everything went ok, otherwise it exits with a positive number, telling how many problems there were. VERIFYING THE COMPILER If, having produced the float.h and limits.h header files, you want to verify that the compiler reads them back correctly (there are a lot of boundary cases, of course, like minimum and maximum numbers), you can recompile enquire.c with -DVERIFY set (plus the other flags that you used when compiling the version that produced the header files). This then recompiles the program so that it #includes "limits.h" and "float.h", and checks that the constants it finds there are the same as the constants it produces. Run the resulting program with enquire -fl. Many compilers fail this test. NB: You *must* recompile with the same compiler and flags, otherwise you may get odd results. You can also use this option if your compiler already has both files, and you want to confirm that this program produces the right results. TROUBLESHOOTING. This program is now quite trustworthy, and suspicious and wrong output may well be caused by bugs in the compiler, not in the program (however of course, this is not guaranteed, and no responsibility can be accepted, etc.) The program only works if overflows are ignored by the C system or are catchable with signal(). If the program fails to run to completion (often with the error message "Unexpected signal at point x"), this often turns out to be a bug in the C compiler's run-time system. Check what was about to be printed, and try to narrow the problem down. Another possible problem is that you have compiled the program to produce loss-of-precision arithmetic traps. The program cannot cope with these, and you should re-compile without them. (They should never be the default). Make sure you compiled with optimisation turned off. Output preceded by *** WARNING: identifies behaviour of the C system deemed incorrect by the program. Likely problems are that printf or scanf don't cope properly with certain boundary numbers: this program goes to a lot of trouble to calculate its values, and these values are mostly boundary numbers. Experience has shown that often printf cannot cope with these values, and so in an attempt to increase confidence in the output, for each float and double that is printed, the printed value is checked by using sscanf to read it back. Care is taken that numbers are printed with enough digits to uniquely identify them, and therefore that they can be read back identically. If the number read back is different, then there is probably a bug in printf or sscanf, and the program prints the warning message. If the two numbers in the warning look identical, then printf is more than likely rounding the last digit(s) incorrectly. To put you at ease that the two really are different, the bit patterns of the two numbers are also printed. The difference is very likely in the last bit. Many scanf's read the minimum double back as 0.0, and similarly cause overflow when reading the maximum double. This program quite ruthlessly declares all these behaviours faulty. The point is that if you get one of these warnings, the output may be wrong, so you should check the result carefully if you intend to use the results. Of course, printf and sscanf may both be wrong, and cancel each other out, so you should check the output carefully anyway. The warning that "a cast didn't work" refers to cases like this: float f; #define C 1.234567890123456789 f= C; if (f != (float) C) printf ("Wrong!"); A faulty compiler will widen f to double and ignore the cast to float, and because there is more accuracy in a double than a float, fail to recognise that they are the same. In the actual case in point, f and C are passed as parameters to a function that discovers they are not equal, so it's just possible that the error was in the parameter passing, not in the cast (see function Verify()). For ANSI C, which has float constants, the error message is "constant has wrong precision". REPORTING PROBLEMS If the program doesn't work for you for any reason that can't be narrowed down to a problem in the C compiler, or it has to be changed in order to get it to compile, or it produces suspicious output (like a very low maximum float, for instance), please mail the problem and an example of the incorrect output to Steven.Pemberton@cwi.nl so that improvements can be worked into future versions. Try to give as much information as possible; DON'T FORGET TO MENTION THE VERSION NUMBER! The program tries to catch and diagnose bugs in the compiler/run-time system. I would be especially pleased to have reports of failures so that I can improve this service. I apologise unreservedly for the contorted use of the preprocessor... but it was fun! NEW VERSIONS Worried that you may not have the latest version? Ftp to ftp.cwi.nl, and look in directory pub/steven/enquire for file enquireXX.c; XX is the version number. Or look at http://www.cwi.nl/~steven/enquire.html HOW DOES ENQUIRE WORK? There is an article that explains a lot of the workings: The Ergonomics of Portability; available from the above addresses as file enquire.ps. THE SMALL PRINT This is not a public domain program; nor is any other program that carries a copyright notice. It is however freely copyable under the following conditions: You may copy and distribute verbatim copies of this source file. You may modify this source file, and copy and distribute such modified versions, provided that you leave the copyright notice at the top of the file and also cause the modified file to carry prominent notices stating that you changed the files and the date of any change; and cause the whole of any work that you distribute or publish, that in whole or in part contains or is a derivative of this program or any part thereof, to be licensed at no charge to all third parties on terms identical to those here. While every effort has been taken to make this program as reliable as possible, no responsibility can be taken for the correctness of the output, nor suitability for any particular use. If you do have a fix to any problem, please send it to me, so that other people can have the benefits. This program is an offshoot of a project funded by public funds. If you use this program for research or commercial use (i.e. more than just for the fun of knowing about your compiler) mailing a short note of acknowledgement may help keep enquire.c supported. ACKNOWLEDGEMENTS Many people have given time and ideas to making this program what it is. To all of them thanks, and apologies for not mentioning them by name. HISTORY Originally started as a program to generate configuration constants for a large piece of software we were writing, which later took on a life of its own... 1.0 Length 6658!; end 1984? Unix only. Only printed a dozen maximum int/double values. 2.0 Length 10535; Spring 1985 Prints values as #defines (about 20 of them) More extensive floating point, using Cody and Waite Handles signals better Programs around optimisations Handles Cybers 3.0 Length 12648; Aug 1987; prints about 42 values Added PASS stuff, so treats float as well as double 4.0 Length 33891; Feb 1989; prints around 85 values First GNU version (for gcc, where they called it hard-params.c) Generates float.h and limits.h files Handles long double Generates warnings for dubious output 4.1 Length 47738; April 1989 Added VERIFY and TEST 4.2 Length 63442; Feb 1990 Added SEP Fixed eps/epsneg Added check for pseudo-unsigned chars Added description for each #define output Added check for absence of defines during verify Added prototypes Added BAD_STDC and BAD_CPP Fixed alignments output 4.3 Length 75000; Oct 1990; around 114 lines of output Function xmalloc defined, Richard Stallman, June 89. Alignments computed from member offsets rather than structure sizes, Richard Stallman, Oct 89 Print whether char* and int* pointers have the same format; also char * and function * Update to Draft C version Dec 7, 1988 - types of constants produced in limits.h (whether to put a U after unsigned shorts and chars and whether to output -1024 as (-1023-1)) - values of SCHAR_MIN/MAX - values of *_EPSILON (not the smallest but the effective smallest) Added FILENAME, since ANSI C doesn't allow #define __FILE__ Renamed from config.c to enquire.c Added size_t and ptrdiff_t enquiries Added promotion enquiries Added type checks of #defines Added BAD_STDDEF Changed endian to allow for cases where not all bits are used Sanity check for max integrals Fixed definition of setjmp for -DNO_SIG Moved #define ... 0.0L inside #ifdef STDC, in case some cpp's tokenize Added BAD_MALLOC 5.0 Length 88228; February 1993; around 120 lines of output (depends on what you count) Added the 'sh enquire.c' horror/delight: thanks David Mankins@think Added checks for long names: thanks Steve Simon@leeds-poly Added FPE signal checks: thanks Leonid A. Broukhis@s514.ipmce.su Added check for dereferencing NULL Added TESTI Added LIBS, fixed showtype: thanks Rainer Orth@TechFak.Uni-Bielefeld.DE Added a free(): thanks nickc@perihelion.co.uk Added signal catching to the malloc part Renamed naughty-compiler defines to BAD_* Renamed and altered Verify() to better check faulty compilers Shut some compilers up from giving incorrect warnings. Fixed sign_of(): thanks Hugh Redelmeier@redvax Fixed USHRT_MAX for sizeof(short)=sizeof(int) but INT_MAX > SHRT_MAX Fixed NO_UI Fixed -DSEP: thanks Mike Black@seismo Fixed the case where stdio.h includes limits.h: thanks rms@gnu Fixed exponent(): thanks Christophe BINOT 5.0a Aug 1997 Made handling of ID= easier Improved the reporting of use of bits in Floating values. 5.1 Length 88739; Sep 1998 Fixed detection of infinity for machines with no overflow trap Speeded up search for max char (first 32 bit char machine turned up...) 5.1a Length 88832; Feb 1999 Changed identification message