home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / NeXT / GnuSource / cc-61.0.1 / cc / enquire.c < prev    next >
C/C++ Source or Header  |  1991-05-05  |  75KB  |  2,743 lines

  1. /* Everything you wanted to know about your machine and C compiler,
  2.    but didn't know who to ask. */
  3.  
  4. #ifndef VERSION
  5. #define VERSION "4.3"
  6. #endif
  7.  
  8. /* Author: Steven Pemberton, CWI, Amsterdam; steven@cwi.nl
  9.    Bugfixes and upgrades gratefully received.
  10.  
  11.    Copyright (c) 1988, 1989, 1990 Steven Pemberton, CWI, Amsterdam.
  12.    All rights reserved.
  13.  
  14.    Undef CHAR_BIT, etc., if defined in stdio.h, Richard Stallman, Aug 90.
  15.    In EPROP, avoid a <= old if bad is set, Richard Stallman, May 91.
  16.  
  17.    COMPILING
  18.    With luck and a following wind, just the following will work:
  19.     cc enquire.c -o enquire
  20.    You may get some messages about unreachable code, which you can ignore.
  21.  
  22.    If your compiler doesn't support:        add flag:
  23.     signed char (eg pcc)            -DNO_SC
  24.     unsigned char                -DNO_UC
  25.     unsigned short and long            -DNO_UI
  26.     void                    -DNO_VOID
  27.     signal(), or setjmp/longjmp()        -DNO_SIG
  28.  
  29.    Try to compile first with no flags, and see if you get any errors -
  30.    you might be surprised. (Most non-ANSI compilers need -DNO_SC, though.)
  31.    Some compilers need a -f flag for floating point.
  32.  
  33.    Don't use any optimisation flags: the program may not work if you do.
  34.    Though "while (a+1.0-a-1.0 == 0.0)" may look like "while(1)" to an
  35.    optimiser, to a floating-point unit there's a world of difference.
  36.  
  37.    Some compilers offer various flags for different floating point
  38.    modes; it's worth trying all possible combinations of these.
  39.  
  40.    Add -DID=\"name\" if you want the machine/flags identified in the output.
  41.  
  42.    FAULTY COMPILERS
  43.    Because of bugs and/or inadequacies, some compilers need the following
  44.    defines:
  45.  
  46.    If your C preprocessor doesn't have the predefined __FILE__ macro, and
  47.    you don't want to call this file enquire.c but, say, tell.c, add the
  48.    flag -DFILENAME=\"tell.c\" .
  49.  
  50.    Some compilers won't accept the line "#include FILENAME".
  51.    Add flag -DNO_FILE. In that case, this file *must* be called enquire.c.
  52.  
  53.    Some compilers can't cope with "#ifdef __FILE__". Use -DFILENAME=
  54.    or -DNO_FILE as above.
  55.  
  56.    Some naughty compilers define __STDC__, but don't really support it.
  57.    Some define it as 0, in which case we treat it as undefined.
  58.    But if your compiler defines it, and isn't really ANSI C,
  59.    add flag -DNO_STDC. (To those compiler writers: for shame).
  60.  
  61.    Some naughty compilers define __STDC__, but don't have the stddef.h
  62.    include file. Add flag -DNO_STDDEF.
  63.  
  64.    Summary of naughty-compiler flags:
  65.    If your compiler doesn't support:         add flag:
  66.     __FILE__ (and you changed the filename)    -DFILENAME=\"name.c\"
  67.     #ifdef __FILE__                -DNO_FILE or -DFILENAME=...
  68.     #include FILENAME            -DNO_FILE
  69.     __STDC__ (properly)            -DNO_STDC
  70.     stddef.h                -DNO_STDDEF
  71.  
  72.    Some systems crash when you try to malloc all store. To save users of
  73.    such defective systems too much grief, they may compile with -DNO_MEM,
  74.    which ignores that bit of the code.
  75.  
  76.    While it is not our policy to support defective compilers, pity has been
  77.    taken on people with compilers that can't produce object files bigger than
  78.    32k (especially since it was an easy addition). Compile the program
  79.    into separate parts like this:
  80.        cc -DSEP -DPASS0 -o p0.o <other flags> enquire.c
  81.        cc -DSEP -DPASS1 -o p1.o <other flags> enquire.c
  82.        cc -DSEP -DPASS2 -o p2.o <other flags> enquire.c
  83.        cc -DSEP -DPASS3 -o p3.o <other flags> enquire.c
  84.        cc -o enquire p0.o p1.o p2.o p3.o
  85.  
  86.    SYSTEM DEPENDENCIES
  87.    You may possibly need to add some calls to signal() for other sorts of
  88.    exception on your machine than SIGFPE, and SIGOVER. See lines beginning
  89.    #ifdef SIGxxx in main() (and communicate the differences to me!).
  90.  
  91.    OUTPUT
  92.    Run without argument to get the information as English text. If run
  93.    with argument -l (e.g. enquire -l), output is a series of #define's for
  94.    the ANSI standard limits.h include file, excluding MB_MAX_CHAR. If run
  95.    with argument -f, output is a series of #define's for the ANSI standard
  96.    float.h include file (according to ANSI C Draft of Dec 7, 1988).
  97.    Flag -v gives verbose output: output includes the English text above
  98.    as C comments. The program exit(0)'s if everything went ok, otherwise
  99.    it exits with a positive number, telling how many problems there were.
  100.  
  101.    VERIFYING THE COMPILER
  102.    If, having produced the float.h and limits.h header files, you want to
  103.    verify that the compiler reads them back correctly (there are a lot of
  104.    boundary cases, of course, like minimum and maximum numbers), you can
  105.    recompile enquire.c with -DVERIFY set (plus the other flags that you used
  106.    when compiling the version that produced the header files). This then
  107.    recompiles the program so that it #includes "limits.h" and "float.h",
  108.    and checks that the constants it finds there are the same as the
  109.    constants it produces. Run the resulting program with enquire -fl.
  110.    Very few compilers have passed without error.
  111.    NB: You *must* recompile with the same compiler and flags, otherwise
  112.    you may get odd results.
  113.  
  114.    You can also use this option if your compiler already has both files,
  115.    and you want to confirm that this program produces the right results.
  116.  
  117.    TROUBLESHOOTING.
  118.    This program is now quite trustworthy, and suspicious and wrong output
  119.    may well be caused by bugs in the compiler, not in the program (however
  120.    of course, this is not guaranteed, and no responsibility can be
  121.    accepted, etc.)
  122.  
  123.    The program only works if overflows are ignored by the C system or
  124.    are catchable with signal().
  125.  
  126.    If the program fails to run to completion (often with the error message
  127.    "Unexpected signal at point x"), this often turns out to be a bug in the
  128.    C compiler's run-time system. Check what was about to be printed, and
  129.    try to narrow the problem down.
  130.  
  131.    Another possible problem is that you have compiled the program to produce
  132.    loss-of-precision arithmetic traps. The program cannot cope with these,
  133.    and you should re-compile without them. (They should never be the default).
  134.  
  135.    Make sure you compiled with optimisation turned off.
  136.  
  137.    Output preceded by *** WARNING: identifies behaviour of the C system
  138.    deemed incorrect by the program. Likely problems are that printf or
  139.    scanf don't cope properly with certain boundary numbers: this program
  140.    goes to a lot of trouble to calculate its values, and these values
  141.    are mostly boundary numbers. Experience has shown that often printf
  142.    cannot cope with these values, and so in an attempt to increase
  143.    confidence in the output, for each float and double that is printed,
  144.    the printed value is checked by using sscanf to read it back.
  145.        Care is taken that numbers are printed with enough digits to uniquely
  146.    identify them, and therefore that they can be read back identically.
  147.    If the number read back is different, then there is probably a bug in
  148.    printf or sscanf, and the program prints the warning message.
  149.    If the two numbers in the warning look identical, then printf is more
  150.    than likely rounding the last digit(s) incorrectly. To put you at ease
  151.    that the two really are different, the bit patterns of the two numbers
  152.    are also printed. The difference is very likely in the last bit.
  153.        Many scanf's read the minimum double back as 0.0, and similarly cause
  154.    overflow when reading the maximum double. This program quite ruthlessly
  155.    declares all these behaviours faulty. The point is that if you get
  156.    one of these warnings, the output may be wrong, so you should check
  157.    the result carefully if you intend to use the results. Of course, printf
  158.    and sscanf may both be wrong, and cancel each other out, so you should
  159.    check the output carefully anyway.
  160.  
  161.    The warning that "a cast didn't work" refers to cases like this:
  162.  
  163.       float f;
  164.       #define C 1.234567890123456789
  165.       f= C;
  166.       if (f != (float) C) printf ("Wrong!");
  167.  
  168.    A faulty compiler will widen f to double and ignore the cast to float,
  169.    and because there is more accuracy in a double than a float, fail to
  170.    recognise that they are the same. In the actual case in point, f and C
  171.    are passed as parameters to a function that discovers they are not equal,
  172.    so it's just possible that the error was in the parameter passing,
  173.    not in the cast (see function Validate()).
  174.    For ANSI C, which has float constants, the error message is "constant has
  175.    wrong precision".
  176.  
  177.    REPORTING PROBLEMS
  178.    If the program doesn't work for you for any reason that can't be
  179.    narrowed down to a problem in the C compiler, or it has to be changed in
  180.    order to get it to compile, or it produces suspicious output (like a very
  181.    low maximum float, for instance), please mail the problem and an example
  182.    of the incorrect output to steven@cwi.nl or ..!hp4nl!cwi.nl!steven, so that
  183.    improvements can be worked into future versions; cwi.nl is the European
  184.    backbone, and is connected to uunet and other fine hosts.
  185.  
  186.    The program tries to catch and diagnose bugs in the compiler/run-time
  187.    system. I would be especially pleased to have reports of failures so
  188.    that I can improve this service.
  189.  
  190.    I apologise unreservedly for the contorted use of the preprocessor...
  191.  
  192.    THE SMALL PRINT
  193.    You may copy and distribute verbatim copies of this source file.
  194.  
  195.    You may modify this source file, and copy and distribute such
  196.    modified versions, provided that you leave the copyright notice
  197.    at the top of the file and also cause the modified file to carry
  198.    prominent notices stating that you changed the files and the date
  199.    of any change; and cause the whole of any work that you distribute
  200.    or publish, that in whole or in part contains or is a derivative of
  201.    this program or any part thereof, to be licensed at no charge to
  202.    all third parties on terms identical to those here.
  203.  
  204.    If you do have a fix to any problem, please send it to me, so that
  205.    other people can have the benefits.
  206.  
  207.    While every effort has been taken to make this program as reliable as
  208.    possible, no responsibility can be taken for the correctness of the
  209.    output, nor suitability for any particular use.
  210.  
  211.    This program is an offshoot of a project funded by public funds.
  212.    If you use this program for research or commercial use (i.e. more
  213.    than just for the fun of knowing about your compiler) mailing a short
  214.    note of acknowledgement may help keep enquire.c supported.
  215.  
  216.    ACKNOWLEDGEMENTS
  217.    Many people have given time and ideas to making this program what it is.
  218.    To all of them thanks, and apologies for not mentioning them by name.
  219.  
  220.    HISTORY
  221.    Originally started as a program to generate configuration constants
  222.    for a large piece of software we were writing, which later took on
  223.    a life of its own...
  224.    1.0 Length 6658!; end 1984?
  225.        Unix only. Only printed a dozen maximum int/double values.
  226.    2.0 Length 10535; Spring 1985
  227.        Prints values as #defines (about 20 of them)
  228.        More extensive floating point, using Cody and Waite
  229.        Handles signals better
  230.        Programs around optimisations
  231.        Handles Cybers
  232.    3.0 Length 12648; Aug 1987; prints about 42 values
  233.        Added PASS stuff, so treats float as well as double
  234.    4.0 Length 33891; Feb 1989; prints around 85 values
  235.        First GNU version (for gcc, where they call it hard-params.c)
  236.        Generates float.h and limits.h files
  237.        Handles long double
  238.        Generates warnings for dubious output
  239.    4.1 Length 47738; April 1989
  240.        Added VERIFY and TEST
  241.    4.2 Length 63442; Feb 1990
  242.        Added SEP
  243.        Fixed eps/epsneg
  244.        Added check for pseudo-unsigned chars
  245.        Added description for each #define output
  246.        Added check for absence of defines during verify
  247.        Added prototypes
  248.        Added NO_STDC and NO_FILE
  249.        Fixed alignments output
  250.    4.3 Length 75000; Oct 1990; around 114 lines of output
  251.        Function xmalloc defined, Richard Stallman, June 89.
  252.        Alignments computed from member offsets rather than structure sizes,
  253.           Richard Stallman, Oct 89.
  254.        Print whether char* and int* pointers have the same format;
  255.           also char * and function *.
  256.        Update to Draft C version Dec 7, 1988
  257.       - types of constants produced in limits.h
  258.         (whether to put a U after unsigned shorts and chars and
  259.          whether to output -1024 as (-1023-1))
  260.       - values of SCHAR_MIN/MAX
  261.       - values of *_EPSILON (not the smallest but the effective smallest)
  262.        Added FILENAME, since standard C doesn't allow #define __FILE__
  263.        Renamed from config.c to enquire.c
  264.        Added size_t and ptrdiff_t enquiries
  265.        Added promotion enquiries
  266.        Added type checks of #defines
  267.        Added NO_STDDEF
  268.        Changed endian to allow for cases where not all bits are used
  269.        Sanity check for max integrals
  270.        Fixed definition of setjmp for -DNO_SIG
  271.        Moved #define ... 0.0L inside #ifdef STDC, in case some cpp's tokenize
  272.        Added NO_MEM
  273. */
  274.  
  275. /* Set FILENAME to the name of this file */
  276. #ifndef FILENAME
  277. #ifdef NO_FILE
  278. #define FILENAME "enquire.c"
  279. #else
  280. #ifdef __FILE__ /* It's a compiler bug if this fails. Compile with -DNO_FILE */
  281. #define FILENAME __FILE__
  282. #else
  283. #define FILENAME "enquire.c"
  284. #endif /* __FILE__ */
  285. #endif /* NO_FILE */
  286. #endif /* FILENAME */
  287.  
  288. /* If PASS isn't defined, then this is the first pass over this file. */
  289. #ifndef PASS
  290. #ifndef SEP
  291. #define PASS 1
  292. #define PASS0 1
  293. #define PASS1 1
  294. #endif /* SEP */
  295.  
  296. /* A description of the ANSI constants */
  297. #define D_CHAR_BIT "Number of bits in a storage unit"
  298. #define D_CHAR_MAX "Maximum char"
  299. #define D_CHAR_MIN "Minimum char"
  300. #define D_SCHAR_MAX "Maximum signed char"
  301. #define D_SCHAR_MIN "Minimum signed char"
  302. #define D_UCHAR_MAX "Maximum unsigned char (minimum is always 0)"
  303.  
  304. #define D_INT_MAX "Maximum %s"
  305. #define D_INT_MIN "Minimum %s"
  306. #define D_UINT_MAX "Maximum unsigned %s (minimum is always 0)"
  307.  
  308. #define D_FLT_ROUNDS "Addition rounds to 0: zero, 1: nearest, 2: +inf, 3: -inf, -1: unknown"
  309. #define D_FLT_RADIX "Radix of exponent representation"
  310. #define D_MANT_DIG "Number of base-FLT_RADIX digits in the significand of a %s"
  311. #define D_DIG "Number of decimal digits of precision in a %s"
  312. #define D_MIN_EXP "Minimum int x such that FLT_RADIX**(x-1) is a normalised %s"
  313. #define D_MIN_10_EXP "Minimum int x such that 10**x is a normalised %s"
  314. #define D_MAX_EXP "Maximum int x such that FLT_RADIX**(x-1) is a representable %s"
  315. #define D_MAX_10_EXP "Maximum int x such that 10**x is a representable %s"
  316. #define D_MAX "Maximum %s"
  317. #define D_EPSILON "Difference between 1.0 and the minimum %s greater than 1.0"
  318. #define D_MIN "Minimum normalised %s"
  319.  
  320. /* Procedure just marks the functions that don't return a result */
  321. #ifdef NO_VOID
  322. #define Procedure int
  323. #else
  324. #define Procedure void
  325. #endif
  326.  
  327. /* Some bad compilers define __STDC__, when they don't support it.
  328.    Compile with -DNO_STDC to get round this.
  329. */
  330. #ifndef NO_STDC
  331. #ifdef __STDC__
  332. #if __STDC__ /* If __STDC__ is 0, assume it isn't supported */
  333. #define STDC
  334. #endif
  335. #endif
  336. #endif
  337.  
  338. /* Stuff different for ANSI C, and old C:
  339.    ARGS and NOARGS are used for function prototypes.
  340.    Volatile is used to reduce the chance of optimisation,
  341.       and to prevent variables being put in registers (when setjmp/longjmp
  342.       wouldn't work as we want)
  343.    Long_double is the longest floating point type available.
  344.    stdc is used in tests like "if (stdc)", which is less ugly than #ifdef.
  345.    U is output after unsigned constants.
  346.  */
  347. #ifdef STDC
  348.  
  349. #define ARGS(x) x
  350. #define NOARGS (void)
  351. #define Volatile volatile
  352. #define Long_double long double
  353. #define stdc 1
  354. #define U "U"
  355.  
  356. #else /* Old style C */
  357.  
  358. #define ARGS(x) ()
  359. #define NOARGS ()
  360. #define Volatile static
  361. #define Long_double double
  362. #define stdc 0
  363. #define U ""
  364.  
  365. #endif /* STDC */
  366.  
  367. /* include files */
  368. #include <stdio.h>
  369.  
  370. #ifdef STDC
  371. #ifndef NO_STDDEF
  372. #include <stddef.h> /* for size_t: if this fails, define NO_STDDEF */
  373. #endif
  374. #endif
  375.  
  376. #ifdef NO_SIG
  377. #define jmp_buf int
  378. #else
  379. #include <signal.h>
  380. #include <setjmp.h>
  381. #endif
  382.  
  383. /* Kludge around the possiblity that <stdio.h> includes <limits.h> */
  384. #ifdef CHAR_BIT
  385. #undef CHAR_BIT
  386. #undef CHAR_MAX
  387. #undef CHAR_MIN
  388. #undef SCHAR_MAX
  389. #undef SCHAR_MIN
  390. #undef UCHAR_MAX
  391. #undef UCHAR_MIN
  392. #endif
  393.  
  394. #ifdef VERIFY
  395. #include "limits.h"
  396. #include "float.h"
  397. #endif
  398.  
  399. #define Vprintf if (V) printf
  400. #define Unexpected(place) if (setjmp(lab)!=0) croak(place)
  401. #define fabs(x) (((x)<0.0)?(-x):(x))
  402.  
  403. #endif /* PASS */
  404.  
  405. #ifdef PASS0
  406.  
  407. /* Prototypes for what's to come: */
  408.  
  409. int false NOARGS;
  410.  
  411. #ifdef NO_STDDEF
  412. char *malloc (); /* Old style prototype */
  413. #else
  414. char *malloc ARGS((size_t size));
  415. #endif
  416.  
  417. Procedure exit ARGS((int status));
  418.  
  419. char *f_rep ARGS((int precision, Long_double val));
  420.  
  421. int maximum_int NOARGS;
  422. int cprop NOARGS;
  423. int basic NOARGS;
  424. Procedure sprop NOARGS;
  425. Procedure iprop NOARGS;
  426. Procedure lprop NOARGS;
  427. Procedure usprop NOARGS;
  428. Procedure uiprop NOARGS;
  429. Procedure ulprop NOARGS;
  430. int fprop ARGS((int bits_per_byte));
  431. int dprop ARGS((int bits_per_byte));
  432. int ldprop ARGS((int bits_per_byte));
  433. Procedure efprop ARGS((int fprec, int dprec, int lprec));
  434. Procedure edprop ARGS((int fprec, int dprec, int lprec));
  435. Procedure eldprop ARGS((int fprec, int dprec, int lprec));
  436.  
  437. int setmode ARGS((char *s));
  438. Procedure farewell ARGS((int bugs));
  439. Procedure describe ARGS((char *description, char *extra));
  440. Procedure missing ARGS((char *s));
  441. Procedure fmissing ARGS((char *s));
  442. Procedure check_defines NOARGS;
  443. Procedure bitpattern ARGS((char *p, unsigned int size));
  444. int ceil_log ARGS((int base, Long_double x));
  445. Procedure croak ARGS((int place));
  446. Procedure eek_a_bug ARGS((char *problem));
  447. Procedure endian ARGS((int bits_per_byte));
  448. int exponent ARGS((Long_double x, double *fract, int *exp));
  449. int floor_log ARGS((int base, Long_double x));
  450. Procedure f_define ARGS((char *desc, char *extra, char *sort, char *name,
  451.              int prec, Long_double val, char *mark));
  452. Procedure i_define ARGS((char *desc, char *extra, char *sort, char *name,
  453.              long val, long lim, long req, char *mark));
  454. Procedure u_define ARGS((char *desc, char *extra, char *sort, char *name,
  455.              unsigned long val, unsigned long req, char *mark));
  456.  
  457. #ifdef NO_SIG  /* There's no signal(), or setjmp/longjmp() */
  458.  
  459.     /* Dummy routines instead */
  460.  
  461.     int setjmp ARGS((int lab));
  462.  
  463.     int lab=1;
  464.     int setjmp(lab) int lab; { return(0); }
  465.     Procedure signal(i, p) int i, (*p)(); {}
  466.  
  467. #else
  468.     jmp_buf lab;
  469.     Procedure overflow(sig) int sig; { /* what to do on over/underflow */
  470.         signal(sig, overflow);
  471.         longjmp(lab, 1);
  472.     }
  473.  
  474. #endif /*NO_SIG*/
  475.  
  476. int V= 0,    /* verbose */
  477.     L= 0,    /* produce limits.h */
  478.     F= 0,    /* produce float.h  */
  479.     bugs=0;    /* The number of (possible) bugs in the output */
  480.  
  481. char co[4], oc[4]; /* Comment starter and ender symbols */
  482.  
  483. int bits_per_byte; /* the number of bits per unit returned by sizeof() */
  484. int flt_rounds;    /* The calculated value of FLT_ROUNDS */
  485. int flt_radix;     /* The calculated value of FLT_RADIX */
  486.  
  487. #ifdef TEST
  488. /* Set the fp modes on a SUN with 68881 chip, to check that different
  489.    rounding modes etc. get properly detected.
  490.    Compile with -f68881 for cc, -m68881 for gcc, and with additional flag
  491.    -DTEST. Run with additional parameter +hex-number, to set the 68881 mode
  492.    register to hex-number
  493. */
  494.  
  495. /* Bits 0x30 = rounding mode */
  496. #define ROUND_BITS    0x30
  497. #define TO_NEAREST    0x00
  498. #define TO_ZERO        0x10
  499. #define TO_MINUS_INF    0x20
  500. #define TO_PLUS_INF    0x30 /* The SUN FP user's guide seems to be wrong here */
  501.  
  502. /* Bits 0xc0 = extended rounding */
  503. #define EXT_BITS    0xc0
  504. #define ROUND_EXTENDED    0x00
  505. #define ROUND_SINGLE    0x40
  506. #define ROUND_DOUBLE    0x80
  507.  
  508. /* Enabled traps */
  509. #define EXE_INEX1  0x100
  510. #define EXE_INEX2  0x200
  511. #define EXE_DZ       0x400
  512. #define EXE_UNFL   0x800
  513. #define EXE_OVFL  0x1000
  514. #define EXE_OPERR 0x2000
  515. #define EXE_SNAN  0x4000
  516. #define EXE_BSUN  0x8000
  517.  
  518. /* Only used for testing, on a Sun with 68881 chip */
  519. /* Print the FP mode */
  520. printmode(new) unsigned new; {
  521.     fpmode_(&new);
  522.     printf("New fp mode:\n");
  523.     printf("  Round toward ");
  524.     switch (new & ROUND_BITS) {
  525.           case TO_NEAREST:   printf("nearest"); break;
  526.           case TO_ZERO:      printf("zero"); break;
  527.           case TO_MINUS_INF: printf("minus infinity"); break;
  528.           case TO_PLUS_INF:  printf("plus infinity"); break;
  529.           default: printf("???"); break;
  530.     }
  531.  
  532.     printf("\n  Extended rounding precision: ");
  533.  
  534.     switch (new & EXT_BITS) {
  535.           case ROUND_EXTENDED: printf("extended"); break;
  536.           case ROUND_SINGLE:   printf("single"); break;
  537.           case ROUND_DOUBLE:   printf("double"); break;
  538.           default: printf("???"); break;
  539.     }
  540.  
  541.     printf("\n  Enabled exceptions:");
  542.     if (new & (unsigned) EXE_INEX1) printf(" inex1");
  543.     if (new & (unsigned) EXE_INEX2) printf(" inex2");
  544.     if (new & (unsigned) EXE_DZ)    printf(" dz");
  545.     if (new & (unsigned) EXE_UNFL)  printf(" unfl");
  546.     if (new & (unsigned) EXE_OVFL)  printf(" ovfl");
  547.     if (new & (unsigned) EXE_OPERR) printf(" operr");
  548.     if (new & (unsigned) EXE_SNAN)  printf(" snan");
  549.     if (new & (unsigned) EXE_BSUN)  printf(" bsun");
  550.     printf("\n");
  551. }
  552.  
  553. /* Only used for testing, on a Sun with 68881 chip */
  554. /* Set the FP mode */
  555. int setmode(s) char *s; {
  556.     unsigned mode=0, dig;
  557.     char c;
  558.  
  559.     while (*s) {
  560.         c= *s++;
  561.         if  (c>='0' && c<='9') dig= c-'0';
  562.         else if (c>='a' && c<='f') dig= c-'a'+10;
  563.         else if (c>='A' && c<='F') dig= c-'A'+10;
  564.         else return 1;
  565.         mode= mode<<4 | dig;
  566.     }
  567.     printmode(mode);
  568.     return 0;
  569. }
  570. #else
  571. /* ARGSUSED */
  572. int setmode(s) char *s; {
  573.     fprintf(stderr, "Can't set mode: not compiled with TEST\n");
  574.     return(1);
  575. }
  576. #endif
  577.  
  578. Procedure farewell(bugs) int bugs; {
  579.     if (bugs == 0) exit(0);
  580.     printf("\n%sFor hints on dealing with the ", co);
  581.     if (bugs == 1) printf("problem");
  582.     else printf("%d problems", bugs);
  583.     printf(" above\n   see the section 'TROUBLESHOOTING' in the file ");
  584.     printf("%s%s\n", FILENAME, oc);
  585.     exit(bugs);
  586. }
  587.  
  588. /* The program has received a signal where it wasn't expecting one */
  589. Procedure croak(place) int place; {
  590.     printf("*** Unexpected signal at point %d\n", place);
  591.     farewell(bugs+1); /* An exit isn't essential here, but avoids loops */
  592. }
  593.  
  594. /* This is here in case alloca.c is used, which calls this. */
  595. char *xmalloc(size) unsigned size; {
  596.     char *malloc();
  597.     char *value = malloc(size);
  598.     if (value == 0) {
  599.         fprintf(stderr, "Virtual memory exceeded\n");
  600.         exit(bugs+1);
  601.     }
  602.     return value;
  603. }
  604.  
  605. int maxint;
  606.  
  607. int maximum_int() {
  608.     /* Find the maximum integer */
  609.     Volatile int newi, int_max, two=2;
  610.  
  611.     /* Calculate maxint ***********************************/
  612.     /* Calculate 2**n-1 until overflow - then use the previous value  */
  613.  
  614.     newi=1; int_max=0;
  615.  
  616.     if (setjmp(lab)==0) { /* Yields int_max */
  617.         while(newi>int_max) {
  618.             int_max=newi;
  619.             newi=newi*two+1;
  620.         }
  621.     }
  622.     Unexpected(0);
  623.     return int_max;
  624. }
  625.  
  626. int main(argc, argv) int argc; char *argv[]; {
  627.     int dprec, fprec, lprec;
  628.     unsigned int size;
  629.     long total;
  630.     int i; char *s; int bad;
  631.  
  632. #ifdef SIGFPE
  633.     signal(SIGFPE, overflow);
  634. #endif
  635. #ifdef SIGOVER
  636.     signal(SIGOVER, overflow);
  637. #endif
  638. /* Add more calls as necessary */
  639.  
  640.     Unexpected(1);
  641.  
  642.     bad=0;
  643.     for (i=1; i < argc; i++) {
  644.         s= argv[i];
  645.         if (*s == '-') {
  646.             s++;
  647.             while (*s) {
  648.                 switch (*(s++)) {
  649.                       case 'v': V=1; break;
  650.                       case 'l': L=1; break;
  651.                       case 'f': F=1; break;
  652.                       default: bad=1; break;
  653.                 }
  654.             }
  655.         } else if (*s == '+') {
  656.             s++;
  657.             bad= setmode(s);
  658.         } else bad= 1;
  659.     }
  660.     if (bad) {
  661.         fprintf(stderr,
  662.             "Usage: %s [-vlf]\n  v=Verbose l=Limits.h f=Float.h\n",
  663.             argv[0]);
  664.         exit(1);
  665.     }
  666.     if (L || F) {
  667.         co[0]= '/'; oc[0]= ' ';
  668.         co[1]= '*'; oc[1]= '*';
  669.         co[2]= ' '; oc[2]= '/';
  670.         co[3]= '\0'; oc[3]= '\0';
  671.     } else {
  672.         co[0]= '\0'; oc[0]= '\0';
  673.         V=1;
  674.     }
  675.  
  676.     if (L) printf("%slimits.h%s\n", co, oc);
  677.     if (F) printf("%sfloat.h%s\n", co, oc);
  678. #ifdef ID
  679.     printf("%sProduced on %s by enquire version %s, CWI, Amsterdam%s\n",
  680.            co, ID, VERSION, oc);
  681. #else
  682.     printf("%sProduced by enquire version %s, CWI, Amsterdam%s\n",
  683.            co, VERSION, oc);
  684. #endif
  685.  
  686. #ifdef VERIFY
  687.     printf("%sVerification phase%s\n", co, oc);
  688. #endif
  689.  
  690. #ifdef NO_SIG
  691.     Vprintf("%sCompiled without signal(): %s%s\n",
  692.         co,
  693.         "there's nothing that can be done if overflow occurs",
  694.         oc);
  695. #endif
  696. #ifdef NO_SC
  697.     Vprintf("%sCompiled without signed char%s\n", co, oc);
  698. #endif
  699. #ifdef NO_UC
  700.     Vprintf("%Compiled without unsigned char%s\n", co, oc);
  701. #endif
  702. #ifdef NO_UI
  703.     Vprintf("%Compiled without unsigned short or long%s\n", co, oc);
  704. #endif
  705. #ifdef __STDC__
  706.     Vprintf("%sCompiler claims to be ANSI C level %d%s\n",
  707.         co, __STDC__, oc);
  708. #else
  709.     Vprintf("%sCompiler does not claim to be ANSI C%s\n", co, oc);
  710. #endif
  711.     printf("\n");
  712.     check_defines();
  713.  
  714.     maxint= maximum_int();
  715.     bits_per_byte= basic();
  716.     Vprintf("\n");
  717.     if (F||V) {
  718.         fprec= fprop(bits_per_byte);
  719.         dprec= dprop(bits_per_byte);
  720.         lprec= ldprop(bits_per_byte);
  721.         efprop(fprec, dprec, lprec);
  722.         edprop(fprec, dprec, lprec);
  723.         eldprop(fprec, dprec, lprec);
  724.     }
  725. #ifndef NO_MEM
  726.     if (V) {
  727.         /* An extra goody: the approximate amount of data-space */
  728.         /* Allocate store until no more available */
  729.         /* Different implementations have a different argument type
  730.            to malloc. Here we assume that it's the same type as
  731.            that which sizeof() returns */
  732.         size=1<<((bits_per_byte*sizeof(int))-2);
  733.         total=0;
  734.         while (size!=0) {
  735.             while ( malloc((false()?sizeof(int):size)) !=
  736.                     (char *)NULL
  737.                    ) {
  738.                 total+=(size/2);
  739.             }
  740.             size/=2;
  741.         }
  742.  
  743.         Vprintf("%sMemory mallocatable ~= %ld Kbytes%s\n",
  744.             co, (total+511)/512, oc);
  745.     }
  746. #endif
  747.     farewell(bugs);
  748.     return bugs; /* To keep compilers and lint happy */
  749. }
  750.  
  751. Procedure eek_a_bug(problem) char *problem; {
  752.     /* The program has discovered a problem */
  753.     printf("\n%s*** WARNING: %s%s\n", co, problem, oc);
  754.     bugs++;
  755. }
  756.  
  757. Procedure describe(description, extra) char *description, *extra; {
  758.     /* Produce the description for a #define */
  759.     printf("   %s", co);
  760.     printf(description, extra);
  761.     printf("%s\n", oc);
  762. }
  763.  
  764. Procedure i_define(desc, extra, sort, name, val, lim, req, mark)
  765.      char *desc, *extra, *sort, *name; long val, lim, req; char *mark; {
  766.     /* Produce a #define for a signed int type */
  767.     describe(desc, extra);
  768.     if (val >= 0) {
  769.         printf("#define %s%s %ld%s\n", sort, name, val, mark);
  770.     } else if (val + lim < 0) {
  771.         /* We may not produce a constant like -1024 if the max
  772.            allowable value is 1023. It has then to be output as
  773.            -1023-1. lim is the max allowable value. */
  774.         printf("#define %s%s (%ld%s%ld%s)\n",
  775.                sort, name, -lim, mark, val+lim, mark);
  776.     } else {
  777.         printf("#define %s%s (%ld%s)\n", sort, name, val, mark);
  778.     }
  779.     /* If VERIFY is not set, val and req are just the same value;
  780.        if it is set, val is the value as calculated, and req is
  781.        the #defined constant
  782.     */
  783.     if (val != req) {
  784.         printf("%s*** Verify failed for above #define!\n", co);
  785.         printf("       Compiler has %ld for value%s\n\n", req, oc);
  786.         bugs++;
  787.     }
  788.     Vprintf("\n");
  789. }
  790.  
  791. Procedure u_define(desc, extra, sort, name, val, req, mark)
  792.      char *desc, *extra, *sort, *name; unsigned long val, req; char *mark; {
  793.     /* Produce a #define for an unsigned value */
  794.     describe(desc, extra);
  795.     printf("#define %s%s %lu%s%s\n", sort, name, val, U, mark);
  796.     if (val != req) {
  797.         printf("%s*** Verify failed for above #define!\n", co);
  798.         printf("       Compiler has %lu for value%s\n\n", req, oc);
  799.         bugs++;
  800.     }
  801.     Vprintf("\n");
  802. }
  803.  
  804. Procedure f_define(desc, extra, sort, name, precision, val, mark)
  805.      char *desc, *extra, *sort, *name; int precision;
  806.      Long_double val; char *mark; {
  807.     /* Produce a #define for a float/double/long double */
  808.     describe(desc, extra);
  809.     if (stdc) {
  810.         printf("#define %s%s %s%s\n",
  811.                sort, name, f_rep(precision, val), mark);
  812.     } else if (*mark == 'F') {
  813.         /* non-ANSI C has no float constants, so cast the constant */
  814.         printf("#define %s%s ((float)%s)\n",
  815.                sort, name, f_rep(precision, val));
  816.     } else {
  817.         printf("#define %s%s %s\n", sort, name, f_rep(precision, val));
  818.     }
  819.     Vprintf("\n");
  820. }
  821.  
  822. int floor_log(base, x) int base; Long_double x; {
  823.     /* return floor(log base(x)) */
  824.     int r=0;
  825.     while (x>=base) { r++; x/=base; }
  826.     return r;
  827. }
  828.  
  829. int ceil_log(base, x) int base; Long_double x; {
  830.     int r=0;
  831.     while (x>1.0) { r++; x/=base; }
  832.     return r;
  833. }
  834.  
  835. int exponent(x, fract, exp) Long_double x; double *fract; int *exp; {
  836.     /* Split x into a fraction and a power of ten;
  837.        returns 0 if x is unusable, 1 otherwise.
  838.        Only used for error messages about faulty output.
  839.     */
  840.     int r=0, neg=0;
  841.     Long_double old;
  842.     *fract=0.0; *exp=0;
  843.     if (x<0.0) {
  844.         x= -x;
  845.         neg= 1;
  846.     }
  847.     if (x==0.0) return 1;
  848.     if (x>=10.0) {
  849.         while (x>=10.0) {
  850.             old=x; r++; x/=10.0;
  851.             if (old==x) return 0;
  852.         }
  853.     } else {
  854.         while (x<1.0) {
  855.             old=x; r--; x*=10.0;
  856.             if (old==x) return 0;
  857.         }
  858.     }
  859.     if (neg) *fract= (double) -x;
  860.     else *fract=(double) x;
  861.     *exp=r;
  862.     return 1;
  863. }
  864.  
  865. char *f_rep(precision, val) int precision; Long_double val; {
  866.     /* Return the floating representation of val */
  867.     static char buf[1024];
  868.     char *f1;
  869.     if (sizeof(double) == sizeof(Long_double)) {
  870.         /* Assume they're the same, and use non-stdc format */
  871.         /* This is for stdc compilers using non-stdc libraries */
  872.         f1= "%.*e";
  873.     } else {
  874.         /* It had better support Le then */
  875.         f1= "%.*Le";
  876.     }
  877.     sprintf(buf, f1, precision, val);
  878.     return buf;
  879. }
  880.  
  881. Procedure bitpattern(p, size) char *p; unsigned int size; {
  882.     /* Printf the bit-pattern of p */
  883.     char c;
  884.     int i, j;
  885.  
  886.     for (i=1; i<=size; i++) {
  887.         c= *p;
  888.         p++;
  889.         for (j=bits_per_byte-1; j>=0; j--)
  890.             printf("%c", (c>>j)&1 ? '1' : '0');
  891.         if (i!=size) printf(" ");
  892.     }
  893. }
  894.  
  895. #define Order(x, px, mode)\
  896.    printf("%s%s ", co, mode); for (i=0; i<sizeof(x); i++) px[i]= ab[i]; \
  897.    for (i=1; i<=sizeof(x); i++) { c=((x>>(bits_per_byte*(sizeof(x)-i)))&mask);\
  898.       putchar(c==0 ? '?' : (char)c); }\
  899.    printf("%s\n", oc);
  900.  
  901. Procedure endian(bits_per_byte) int bits_per_byte; {
  902.     /* Printf the byte-order used on this machine */
  903.     /*unsigned*/ short s=0;
  904.     /*unsigned*/ int j=0;
  905.     /*unsigned*/ long l=0;
  906.  
  907.     char *ps= (char *) &s,
  908.          *pj= (char *) &j,
  909.          *pl= (char *) &l,
  910.          *ab= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  911.     unsigned int mask, i, c;
  912.  
  913.     mask=0;
  914.     for (i=1; i<=(unsigned)bits_per_byte; i++) mask= (mask<<1)|1;
  915.  
  916.     if (V) {
  917.         printf("%sCHARACTER ORDER%s\n", co, oc);
  918.         Order(s, ps, "short:");
  919.         Order(j, pj, "int:  ");
  920.         Order(l, pl, "long: ");
  921.     }
  922. }
  923.  
  924. Procedure missing(s) char *s; {
  925.     printf("%s*** #define %s missing from limits.h%s\n", co, s, oc);
  926.     bugs++;
  927. }
  928.  
  929. Procedure fmissing(s) char *s; {
  930.     printf("%s*** #define %s missing from float.h%s\n", co, s, oc);
  931.     bugs++;
  932. }
  933.  
  934. /* To try and fool optimisers */
  935. int false() { return 0; }
  936.  
  937. #define Promoted(x) (false()?(x):(-1))
  938. #define is_signed(x) (Promoted(x) < 0)
  939. #define sign_of(x) ((x)?"signed":"unsigned")
  940. #define Signed 1
  941. #define Unsigned 0
  942. #define sgn(x) ((is_signed(x))?Signed:Unsigned)
  943.  
  944. #define showtype(t, x) Vprintf("%s%s %s %s%s\n", co, t, sign_of(is_signed(x)), type_of(sizeof(x)), oc)
  945.  
  946. char *type_of(x) int x; {
  947.     if (x == sizeof(char)) {
  948.         if (sizeof(char) == sizeof(int)) return "char/short/int";
  949.         if (sizeof(char) == sizeof(short)) return "char/short";
  950.         return "char";
  951.     }
  952.     if (x == sizeof(short)) {
  953.         if (sizeof(short) == sizeof(int)) return "short/int";
  954.         return "short";
  955.     }
  956.     if (x == sizeof(int)) {
  957.         if (sizeof(int) == sizeof(long)) return "int/long";
  958.         return "int";
  959.     }
  960.     if (x == sizeof(long)) return "long";
  961.     return "unknown-type";
  962. }
  963.  
  964. char *ftype_of(x) int x; {
  965.     if (x == sizeof(float)) {
  966.         return "float";
  967.     }
  968.     if (x == sizeof(double)) {
  969.         if (sizeof(double) == sizeof(Long_double))
  970.           return "(long)double";
  971.         return "double";
  972.     }
  973.     if (x == sizeof(Long_double)) {
  974.         return "long double";
  975.     }
  976.     return "unknown-type";
  977. }
  978.  
  979. Procedure typerr(name, esign, esize, sign, size)
  980.   char *name; int esign, esize, sign, size;
  981. {
  982.        Vprintf("*** %s has wrong type: expected %s %s, found %s %s\n",
  983.            name, sign_of(esign), type_of(esize),
  984.            sign_of(sign), type_of(size));
  985. }
  986.  
  987. Procedure ftyperr(name, esize, size) char *name; int esize, size; {
  988.        Vprintf("*** %s has wrong type: expected %s, found %s\n",
  989.            name, ftype_of(esize), ftype_of(size));
  990. }
  991.  
  992. promotions() {
  993.     int si; long sl;
  994.     unsigned int ui; unsigned long ul;
  995.     short ss; unsigned short us;
  996.  
  997.     Vprintf("\n%sPROMOTIONS%s\n", co, oc);
  998.  
  999.     if (
  1000.         /* Possible warnings here; no problem */
  1001.         (sizeof(Promoted(si)) != sizeof(int)) ||
  1002.         (sizeof(Promoted(sl)) != sizeof(long)) ||
  1003.         (sizeof(Promoted(ss)) != sizeof(int)) ||
  1004.         (sizeof(Promoted(ui)) != sizeof(int)) ||
  1005.         (sizeof(Promoted(ul)) != sizeof(long)) ||
  1006.         (sizeof(Promoted(us)) != sizeof(int)) ||
  1007.         is_signed(ui) || is_signed(ul) ||
  1008.         !is_signed(si) || !is_signed(sl)
  1009.         )
  1010.       {
  1011.         eek_a_bug("promotions don't work properly in conditional expressions\n");
  1012.       }
  1013.  
  1014.     showtype("unsigned short promotes to", Promoted((unsigned short)0));
  1015.     showtype("long+unsigned gives", sl+ui);
  1016. }
  1017.  
  1018. #define checktype(x, n, s, t) if((sgn(x)!=s)||(sizeof(x)!=sizeof(t))) typerr(n, s, sizeof(t), sign_of(x), sizeof(x));
  1019.  
  1020. #define fchecktype(x, n, t) if (sizeof(x) != sizeof(t)) ftyperr(n, sizeof(x), sizeof(t));
  1021.  
  1022. Procedure check_defines() {
  1023.     /* ensure that all #defines are present and have the correct type */
  1024. #ifdef VERIFY
  1025.     int usign;
  1026.  
  1027. #ifdef NO_UI
  1028.     usign= Signed;
  1029. #else
  1030.     /* Implementations promote unsigned short differently */
  1031.     usign= is_signed((unsigned short)0);
  1032. #endif
  1033.  
  1034.     if (L) {
  1035. #ifdef CHAR_BIT
  1036.     checktype(CHAR_BIT, "CHAR_BIT", Signed, int);
  1037. #else
  1038.     missing("CHAR_BIT");
  1039. #endif
  1040. #ifdef CHAR_MAX
  1041.     checktype(CHAR_MAX, "CHAR_MAX", Signed, int);
  1042. #else
  1043.     missing("CHAR_MAX");
  1044. #endif
  1045. #ifdef CHAR_MIN
  1046.     checktype(CHAR_MIN, "CHAR_MIN", Signed, int);
  1047. #else
  1048.     missing("CHAR_MIN");
  1049. #endif
  1050. #ifdef SCHAR_MAX
  1051.     checktype(SCHAR_MAX, "SCHAR_MAX", Signed, int);
  1052. #else
  1053.     missing("SCHAR_MAX");
  1054. #endif
  1055. #ifdef SCHAR_MIN
  1056.     checktype(SCHAR_MIN, "SCHAR_MIN", Signed, int);
  1057. #else
  1058.     missing("SCHAR_MIN");
  1059. #endif
  1060. #ifdef UCHAR_MAX
  1061.     checktype(UCHAR_MAX, "UCHAR_MAX", Signed, int);
  1062. #else
  1063.     missing("UCHAR_MAX");
  1064. #endif
  1065. #ifdef SHRT_MAX
  1066.     checktype(SHRT_MAX, "SHRT_MAX", Signed, int);
  1067. #else
  1068.     missing("SHRT_MAX");
  1069. #endif
  1070. #ifdef SHRT_MIN
  1071.     checktype(SHRT_MIN, "SHRT_MIN", Signed, int);
  1072. #else
  1073.     missing("SHRT_MIN");
  1074. #endif
  1075. #ifdef INT_MAX
  1076.     checktype(INT_MAX, "INT_MAX", Signed, int);
  1077. #else
  1078.     missing("INT_MAX");
  1079. #endif
  1080. #ifdef INT_MIN
  1081.     checktype(INT_MIN, "INT_MIN", Signed, int);
  1082. #else
  1083.     missing("INT_MIN");
  1084. #endif
  1085. #ifdef LONG_MAX
  1086.     checktype(LONG_MAX, "LONG_MAX", Signed, long);
  1087. #else
  1088.     missing("LONG_MAX");
  1089. #endif
  1090. #ifdef LONG_MIN
  1091.     checktype(LONG_MIN, "LONG_MIN", Signed, long);
  1092. #else
  1093.     missing("LONG_MIN");
  1094. #endif
  1095. #ifdef USHRT_MAX
  1096.     checktype(USHRT_MAX, "USHRT_MAX", usign, int);
  1097. #else
  1098.     missing("USHRT_MAX");
  1099. #endif
  1100. #ifdef UINT_MAX
  1101.     checktype(UINT_MAX, "UINT_MAX", Unsigned, int);
  1102. #else
  1103.     missing("UINT_MAX");
  1104. #endif
  1105. #ifdef ULONG_MAX
  1106.     checktype(ULONG_MAX, "ULONG_MAX", Unsigned, long);
  1107. #else
  1108.     missing("ULONG_MAX");
  1109. #endif
  1110.     } /* if (L) */
  1111.  
  1112.     if (F) {
  1113. #ifdef FLT_RADIX
  1114.     checktype(FLT_RADIX, "FLT_RADIX", Signed, int);
  1115. #else
  1116.     fmissing("FLT_RADIX");
  1117. #endif
  1118. #ifdef FLT_MANT_DIG
  1119.     checktype(FLT_MANT_DIG, "FLT_MANT_DIG", Signed, int);
  1120. #else
  1121.     fmissing("FLT_MANT_DIG");
  1122. #endif
  1123. #ifdef FLT_DIG
  1124.     checktype(FLT_DIG, "FLT_DIG", Signed, int);
  1125. #else
  1126.     fmissing("FLT_DIG");
  1127. #endif
  1128. #ifdef FLT_ROUNDS
  1129.     checktype(FLT_ROUNDS, "FLT_ROUNDS", Signed, int);
  1130. #else
  1131.     fmissing("FLT_ROUNDS");
  1132. #endif
  1133. #ifdef FLT_EPSILON
  1134.     fchecktype(FLT_EPSILON, "FLT_EPSILON", float);
  1135. #else
  1136.     fmissing("FLT_EPSILON");
  1137. #endif
  1138. #ifdef FLT_MIN_EXP
  1139.     checktype(FLT_MIN_EXP, "FLT_MIN_EXP", Signed, int);
  1140. #else
  1141.     fmissing("FLT_MIN_EXP");
  1142. #endif
  1143. #ifdef FLT_MIN
  1144.     fchecktype(FLT_MIN, "FLT_MIN", float);
  1145. #else
  1146.     fmissing("FLT_MIN");
  1147. #endif
  1148. #ifdef FLT_MIN_10_EXP
  1149.     checktype(FLT_MIN_10_EXP, "FLT_MIN_10_EXP", Signed, int);
  1150. #else
  1151.     fmissing("FLT_MIN_10_EXP");
  1152. #endif
  1153. #ifdef FLT_MAX_EXP
  1154.     checktype(FLT_MAX_EXP, "FLT_MAX_EXP", Signed, int);
  1155. #else
  1156.     fmissing("FLT_MAX_EXP");
  1157. #endif
  1158. #ifdef FLT_MAX
  1159.     fchecktype(FLT_MAX, "FLT_MAX", float);
  1160. #else
  1161.     fmissing("FLT_MAX");
  1162. #endif
  1163. #ifdef FLT_MAX_10_EXP
  1164.     checktype(FLT_MAX_10_EXP, "FLT_MAX_10_EXP", Signed, int);
  1165. #else
  1166.     fmissing("FLT_MAX_10_EXP");
  1167. #endif
  1168. #ifdef DBL_MANT_DIG
  1169.     checktype(DBL_MANT_DIG, "DBL_MANT_DIG", Signed, int);
  1170. #else
  1171.     fmissing("DBL_MANT_DIG");
  1172. #endif
  1173. #ifdef DBL_DIG
  1174.     checktype(DBL_DIG, "DBL_DIG", Signed, int);
  1175. #else
  1176.     fmissing("DBL_DIG");
  1177. #endif
  1178. #ifdef DBL_EPSILON
  1179.     fchecktype(DBL_EPSILON, "DBL_EPSILON", double);
  1180. #else
  1181.     fmissing("DBL_EPSILON");
  1182. #endif
  1183. #ifdef DBL_MIN_EXP
  1184.     checktype(DBL_MIN_EXP, "DBL_MIN_EXP", Signed, int);
  1185. #else
  1186.     fmissing("DBL_MIN_EXP");
  1187. #endif
  1188. #ifdef DBL_MIN
  1189.     fchecktype(DBL_MIN, "DBL_MIN", double);
  1190. #else
  1191.     fmissing("DBL_MIN");
  1192. #endif
  1193. #ifdef DBL_MIN_10_EXP
  1194.     checktype(DBL_MIN_10_EXP, "DBL_MIN_10_EXP", Signed, int);
  1195. #else
  1196.     fmissing("DBL_MIN_10_EXP");
  1197. #endif
  1198. #ifdef DBL_MAX_EXP
  1199.     checktype(DBL_MAX_EXP, "DBL_MAX_EXP", Signed, int);
  1200. #else
  1201.     fmissing("DBL_MAX_EXP");
  1202. #endif
  1203. #ifdef DBL_MAX
  1204.     fchecktype(DBL_MAX, "DBL_MAX", double);
  1205. #else
  1206.     fmissing("DBL_MAX");
  1207. #endif
  1208. #ifdef DBL_MAX_10_EXP
  1209.     checktype(DBL_MAX_10_EXP, "DBL_MAX_10_EXP", Signed, int);
  1210. #else
  1211.     fmissing("DBL_MAX_10_EXP");
  1212. #endif
  1213. #ifdef STDC
  1214. #ifdef LDBL_MANT_DIG
  1215.     checktype(LDBL_MANT_DIG, "LDBL_MANT_DIG", Signed, int);
  1216. #else
  1217.     fmissing("LDBL_MANT_DIG");
  1218. #endif
  1219. #ifdef LDBL_DIG
  1220.     checktype(LDBL_DIG, "LDBL_DIG", Signed, int);
  1221. #else
  1222.     fmissing("LDBL_DIG");
  1223. #endif
  1224. #ifdef LDBL_EPSILON
  1225.     fchecktype(LDBL_EPSILON, "LDBL_EPSILON", long double);
  1226. #else
  1227.     fmissing("LDBL_EPSILON");
  1228. #endif
  1229. #ifdef LDBL_MIN_EXP
  1230.     checktype(LDBL_MIN_EXP, "LDBL_MIN_EXP", Signed, int);
  1231. #else
  1232.     fmissing("LDBL_MIN_EXP");
  1233. #endif
  1234. #ifdef LDBL_MIN
  1235.     fchecktype(LDBL_MIN, "LDBL_MIN", long double);
  1236. #else
  1237.     fmissing("LDBL_MIN");
  1238. #endif
  1239. #ifdef LDBL_MIN_10_EXP
  1240.     checktype(LDBL_MIN_10_EXP, "LDBL_MIN_10_EXP", Signed, int);
  1241. #else
  1242.     fmissing("LDBL_MIN_10_EXP");
  1243. #endif
  1244. #ifdef LDBL_MAX_EXP
  1245.     checktype(LDBL_MAX_EXP, "LDBL_MAX_EXP", Signed, int);
  1246. #else
  1247.     fmissing("LDBL_MAX_EXP");
  1248. #endif
  1249. #ifdef LDBL_MAX
  1250.     fchecktype(LDBL_MAX, "LDBL_MAX", long double);
  1251. #else
  1252.     fmissing("LDBL_MAX");
  1253. #endif
  1254. #ifdef LDBL_MAX_10_EXP
  1255.     checktype(LDBL_MAX_10_EXP, "LDBL_MAX_10_EXP", Signed, int);
  1256. #else
  1257.     fmissing("LDBL_MAX_10_EXP");
  1258. #endif
  1259. #endif /* STDC */
  1260.     } /* if (F) */
  1261. #endif /* VERIFY */
  1262. }
  1263.  
  1264. #ifdef VERIFY
  1265. #ifndef SCHAR_MAX
  1266. #define SCHAR_MAX    char_max
  1267. #endif
  1268. #ifndef SCHAR_MIN
  1269. #define SCHAR_MIN    char_min
  1270. #endif
  1271. #ifndef UCHAR_MAX
  1272. #define UCHAR_MAX    char_max
  1273. #endif
  1274. #endif /* VERIFY */
  1275.  
  1276. #ifndef CHAR_BIT
  1277. #define CHAR_BIT    char_bit
  1278. #endif
  1279. #ifndef CHAR_MAX
  1280. #define CHAR_MAX    char_max
  1281. #endif
  1282. #ifndef CHAR_MIN
  1283. #define CHAR_MIN    char_min
  1284. #endif
  1285. #ifndef SCHAR_MAX
  1286. #define SCHAR_MAX    char_max
  1287. #endif
  1288. #ifndef SCHAR_MIN
  1289. #define SCHAR_MIN    char_min
  1290. #endif
  1291. #ifndef UCHAR_MAX
  1292. #define UCHAR_MAX    char_max
  1293. #endif
  1294.  
  1295. int cprop() {
  1296.     /* Properties of type char */
  1297.     Volatile char c, char_max, char_min;
  1298.     Volatile int bits_per_byte, c_signed;
  1299.     long char_bit;
  1300.  
  1301.     Unexpected(2);
  1302.  
  1303.     /* Calculate number of bits per character *************************/
  1304.     c=1; bits_per_byte=0;
  1305.     do { c=c<<1; bits_per_byte++; } while(c!=0);
  1306.     c= (char)(-1);
  1307.     if (((int)c)<0) c_signed=1;
  1308.     else c_signed=0;
  1309.     Vprintf("%schar = %d bits, %ssigned%s\n",
  1310.         co, (int)sizeof(c)*bits_per_byte, (c_signed?"":"un"), oc);
  1311.     char_bit=(long)(sizeof(c)*bits_per_byte);
  1312.     if (L) i_define(D_CHAR_BIT, "", "CHAR", "_BIT",
  1313.             char_bit, 0L, (long) CHAR_BIT, "");
  1314.  
  1315.     c=0; char_max=0;
  1316.     c++;
  1317.     if (setjmp(lab)==0) { /* Yields char_max */
  1318.         while (c>char_max) {
  1319.             char_max=c;
  1320.             c++;
  1321.         }
  1322.     } else {
  1323.         Vprintf("%sCharacter overflow generates a trap!%s\n", co, oc);
  1324.     }
  1325.     c=0; char_min=0;
  1326.     c--;
  1327.     if (setjmp(lab)==0) { /* Yields char_min */
  1328.         while (c<char_min) {
  1329.             char_min=c;
  1330.             c--;
  1331.         }
  1332.     }
  1333.     if (c_signed && char_min == 0) {
  1334.         Vprintf("%sBEWARE! Chars are pseudo-unsigned:%s\n", co, oc);
  1335.         Vprintf("%s   %s%s%s\n",
  1336.             "They contain only nonnegative values, ",
  1337.             "but sign extend when used as integers.", co, oc);
  1338.     }
  1339.     Unexpected(3);
  1340.  
  1341.     if (L) {
  1342.         /* Because of the integer promotions, you must use a U after
  1343.            the MAX_CHARS in the following cases */
  1344.         if ((sizeof(char) == sizeof(int)) && !c_signed) {
  1345.             u_define(D_CHAR_MAX, "", "CHAR", "_MAX",
  1346.                  (long) char_max,
  1347.                  (long) CHAR_MAX, "");
  1348.         } else {
  1349.             i_define(D_CHAR_MAX, "", "CHAR", "_MAX",
  1350.                  (long) char_max, 0L,
  1351.                  (long) CHAR_MAX, "");
  1352.         }
  1353.         i_define(D_CHAR_MIN, "", "CHAR", "_MIN",
  1354.              (long) char_min, (long) maxint,
  1355.              (long) CHAR_MIN, "");
  1356.         if (c_signed) {
  1357.             i_define(D_SCHAR_MAX, "", "SCHAR", "_MAX",
  1358.                  (long) char_max, 0L,
  1359.                  (long) SCHAR_MAX, "");
  1360.             i_define(D_SCHAR_MIN, "", "SCHAR", "_MIN",
  1361.                  (long) char_min, (long) maxint,
  1362.                  (long) SCHAR_MIN, "");
  1363.         } else {
  1364.             if (sizeof(char) == sizeof(int)) {
  1365.                 u_define(D_UCHAR_MAX, "", "UCHAR", "_MAX",
  1366.                      (long) char_max,
  1367.                      (long) UCHAR_MAX, "");
  1368.             } else {
  1369.                 i_define(D_UCHAR_MAX, "", "UCHAR", "_MAX",
  1370.                      (long) char_max, 0L,
  1371.                      (long) UCHAR_MAX, "");
  1372.             }
  1373.         }
  1374.  
  1375.         if (c_signed) {
  1376. #ifndef NO_UC
  1377.             Volatile unsigned char c, char_max;
  1378.             c=0; char_max=0;
  1379.             c++;
  1380.             if (setjmp(lab)==0) { /* Yields char_max */
  1381.                 while (c>char_max) {
  1382.                     char_max=c;
  1383.                     c++;
  1384.                 }
  1385.             }
  1386.             Unexpected(4);
  1387.             if (sizeof(char) == sizeof(int)) {
  1388.                 u_define(D_UCHAR_MAX, "", "UCHAR", "_MAX",
  1389.                      (long) char_max,
  1390.                      (long) UCHAR_MAX, "");
  1391.             } else {
  1392.                 i_define(D_UCHAR_MAX, "", "UCHAR", "_MAX",
  1393.                      (long) char_max, 0L,
  1394.                      (long) UCHAR_MAX, "");
  1395.             }
  1396. #endif
  1397.         } else {
  1398. #ifndef NO_SC
  1399. /* Define NO_SC if this gives a syntax error */ Volatile signed char c, char_max, char_min;
  1400.             c=0; char_max=0;
  1401.             c++;
  1402.             if (setjmp(lab)==0) { /* Yields char_max */
  1403.                 while (c>char_max) {
  1404.                     char_max=c;
  1405.                     c++;
  1406.                 }
  1407.             }
  1408.             c=0; char_min=0;
  1409.             c--;
  1410.             if (setjmp(lab)==0) { /* Yields char_min */
  1411.                 while (c<char_min) {
  1412.                     char_min=c;
  1413.                     c--;
  1414.                 }
  1415.             }
  1416.             Unexpected(5);
  1417.             i_define(D_SCHAR_MIN, "", "SCHAR", "_MIN",
  1418.                  (long) char_min, (long) maxint,
  1419.                  (long) SCHAR_MIN, "");
  1420.             i_define(D_SCHAR_MAX, "", "SCHAR", "_MAX",
  1421.                  (long) char_max, 0L,
  1422.                  (long) SCHAR_MAX, "");
  1423. #endif /* NO_SC */
  1424.         }
  1425.     }
  1426.     return bits_per_byte;
  1427. }
  1428.  
  1429. int basic() {
  1430.     /* The properties of the basic types.
  1431.        Returns number of bits per sizeof unit */
  1432.     Volatile int bits_per_byte;
  1433.     typedef int function ();
  1434.     int variable;
  1435.     int *p, *q;
  1436.  
  1437.     Vprintf("%sSIZES%s\n", co, oc);
  1438.     bits_per_byte= cprop();
  1439.  
  1440.     /* Shorts, ints and longs *****************************************/
  1441.     Vprintf("%sshort=%d int=%d long=%d float=%d double=%d bits %s\n",
  1442.         co,
  1443.         (int) sizeof(short)*bits_per_byte,
  1444.         (int) sizeof(int)*bits_per_byte,
  1445.         (int) sizeof(long)*bits_per_byte,
  1446.         (int) sizeof(float)*bits_per_byte,
  1447.         (int) sizeof(double)*bits_per_byte, oc);
  1448.     if (stdc) {
  1449.         Vprintf("%slong double=%d bits%s\n",
  1450.             co, (int) sizeof(Long_double)*bits_per_byte, oc);
  1451.     }
  1452.     Vprintf("%schar*=%d bits%s%s\n",
  1453.         co, (int)sizeof(char *)*bits_per_byte,
  1454.         sizeof(char *)>sizeof(int)?" BEWARE! larger than int!":"",
  1455.         oc);
  1456.     Vprintf("%sint* =%d bits%s%s\n",
  1457.         co, (int)sizeof(int *)*bits_per_byte,
  1458.         sizeof(int *)>sizeof(int)?" BEWARE! larger than int!":"",
  1459.         oc);
  1460.     Vprintf("%sfunc*=%d bits%s%s\n",
  1461.         co, (int)sizeof(function *)*bits_per_byte,
  1462.         sizeof(function *)>sizeof(int)?" BEWARE! larger than int!":"",
  1463.         oc);
  1464. if (V) printf ("%s%s %s %s%s\n", co, "Type size_t is",
  1465.                ((((false()?( sizeof(int)):(-1))  < 0) )?
  1466.             "signed":"unsigned") ,
  1467.                type_of(sizeof(
  1468.                       sizeof(int)+0
  1469.                       )
  1470.                    ),
  1471.            oc);
  1472.     showtype("Type size_t is", sizeof(0));
  1473.  
  1474.     /* Alignment constants ********************************************/
  1475.  
  1476. #define alignment(TYPE) \
  1477.     ((long)((char *)&((struct{char c; TYPE d;}*)0)->d - (char *)0))
  1478.  
  1479.     Vprintf("\n%sALIGNMENTS%s\n", co, oc);
  1480.  
  1481.     Vprintf("%schar=%ld short=%ld int=%ld long=%ld%s\n",
  1482.         co,
  1483.         alignment(char), alignment(short),
  1484.         alignment(int), alignment(long),
  1485.         oc);
  1486.  
  1487.     Vprintf("%sfloat=%ld double=%ld%s\n",
  1488.         co,
  1489.         alignment(float), alignment(double),
  1490.         oc);
  1491.  
  1492.     if (stdc) {
  1493.         Vprintf("%slong double=%ld%s\n",
  1494.             co,
  1495.             alignment(Long_double),
  1496.             oc);
  1497.     }
  1498.     Vprintf("%schar*=%ld int*=%ld func*=%ld%s\n",
  1499.         co,
  1500.         alignment(char *), alignment(int *), alignment(function *),
  1501.         oc);
  1502.  
  1503.     Vprintf("\n");
  1504.  
  1505.     /* Ten little endians *********************************************/
  1506.  
  1507.     endian(bits_per_byte);
  1508.  
  1509.     /* Pointers *******************************************************/
  1510.  
  1511.     Vprintf("\n%sPROPERTIES OF POINTERS%s\n", co, oc);
  1512.  
  1513.     if ((long) (char *) &variable == (long) (int *) &variable)
  1514.         Vprintf("%sChar and int pointer formats seem identical%s\n",
  1515.                co, oc);
  1516.     else
  1517.         Vprintf("%sChar and int pointer formats are different%s\n",
  1518.                co, oc);
  1519.     if ((long) (char *) &variable == (long) (function *) &variable)
  1520.         Vprintf("%sChar and function pointer formats seem identical%s\n",
  1521.                co, oc);
  1522.     else
  1523.         Vprintf("%sChar and function pointer formats are different%s\n",
  1524.                co, oc);
  1525.  
  1526.     if (V) {
  1527.         if ("abcd"=="abcd")
  1528.             printf("%sStrings are shared%s\n", co, oc);
  1529.         else printf("%sStrings are not shared%s\n", co, oc);
  1530.     }
  1531.  
  1532.     p=0; q=0;
  1533.     showtype("Type ptrdiff_t is", p-q);
  1534.  
  1535.     Vprintf("\n%sPROPERTIES OF INTEGRAL TYPES%s\n", co, oc);
  1536.  
  1537.     sprop();
  1538.     iprop();
  1539.     lprop();
  1540.     usprop();
  1541.     uiprop();
  1542.     ulprop();
  1543.  
  1544.     promotions();
  1545.  
  1546.     Unexpected(6);
  1547.  
  1548.     return bits_per_byte;
  1549. }
  1550.  
  1551. #else /* not PASS0 */
  1552.  
  1553. #ifdef SEP
  1554. extern jmp_buf lab;
  1555. extern int V, L, F, bugs, bits_per_byte;
  1556. extern char co[], oc[];
  1557. extern char *f_rep();
  1558. #endif /* SEP */
  1559. #endif /* ifdef PASS0 */
  1560.  
  1561. /* As I said, I apologise for the contortions below. The functions are
  1562.    expanded by the preprocessor twice or three times (for float and double,
  1563.    and maybe for long double, and for short, int and long). That way,
  1564.    I never make a change to one that I forget to make to the other.
  1565.    You can look on it as C's fault for not supporting multi-line macro's.
  1566.    This whole file is read 3 times by the preprocessor, with PASSn set for
  1567.    n=1, 2 or 3, to decide which parts to reprocess.
  1568. */
  1569.  
  1570. /* #undef on an already undefined thing is (wrongly) flagged as an error
  1571.    by some compilers, therefore the #ifdef that follows:
  1572. */
  1573. #ifdef Number
  1574. #undef Number
  1575. #undef THING
  1576. #undef Thing
  1577. #undef thing
  1578. #undef FPROP
  1579. #undef Fname
  1580. #undef Store
  1581. #undef Sum
  1582. #undef Diff
  1583. #undef Mul
  1584. #undef Div
  1585. #undef ZERO
  1586. #undef HALF
  1587. #undef ONE
  1588. #undef TWO
  1589. #undef THREE
  1590. #undef FOUR
  1591. #undef Self
  1592. #undef F_check
  1593. #undef Validate
  1594. #undef EPROP
  1595. #undef MARK
  1596.  
  1597. /* These are the float.h constants */
  1598. #undef F_RADIX
  1599. #undef F_MANT_DIG
  1600. #undef F_DIG
  1601. #undef F_ROUNDS
  1602. #undef F_EPSILON
  1603. #undef F_MIN_EXP
  1604. #undef F_MIN
  1605. #undef F_MIN_10_EXP
  1606. #undef F_MAX_EXP
  1607. #undef F_MAX
  1608. #undef F_MAX_10_EXP
  1609. #endif
  1610.  
  1611. #ifdef Integer
  1612. #undef Integer
  1613. #undef INT
  1614. #undef IPROP
  1615. #undef Iname
  1616. #undef UPROP
  1617. #undef Uname
  1618. #undef OK_UI
  1619. #undef IMARK
  1620.  
  1621. #undef I_MAX
  1622. #undef I_MIN
  1623. #undef U_MAX
  1624. #endif
  1625.  
  1626. #ifdef PASS1
  1627.  
  1628. /* Define the things we're going to use this pass */
  1629.  
  1630. #define Number    float
  1631. #define THING    "FLOAT"
  1632. #define Thing    "Float"
  1633. #define thing    "float"
  1634. #define Fname    "FLT"
  1635. #define FPROP    fprop
  1636. #define Store    fStore
  1637. #define Sum    fSum
  1638. #define Diff    fDiff
  1639. #define Mul    fMul
  1640. #define Div    fDiv
  1641. #define ZERO    0.0
  1642. #define HALF    0.5
  1643. #define ONE    1.0
  1644. #define TWO    2.0
  1645. #define THREE    3.0
  1646. #define FOUR    4.0
  1647. #define Self    fSelf
  1648. #define F_check    fCheck
  1649. #define MARK    "F"
  1650. #ifdef VERIFY
  1651. #define Validate(prec, val, req, same) fValidate(prec, val, req, same)
  1652. #endif
  1653.  
  1654. #define EPROP    efprop
  1655.  
  1656. #define Integer    short
  1657. #define INT    "short"
  1658. #define IPROP    sprop
  1659. #define Iname    "SHRT"
  1660. #ifndef NO_UI
  1661. #define OK_UI 1
  1662. #endif
  1663. #define IMARK    ""
  1664.  
  1665. #define UPROP    usprop
  1666. #define Uname    "USHRT"
  1667.  
  1668. #ifdef VERIFY
  1669. #ifdef SHRT_MAX
  1670. #define I_MAX        SHRT_MAX
  1671. #endif
  1672. #ifdef SHRT_MIN
  1673. #define I_MIN        SHRT_MIN
  1674. #endif
  1675. #ifdef USHRT_MAX
  1676. #define U_MAX        USHRT_MAX
  1677. #endif
  1678.  
  1679. #ifdef FLT_RADIX
  1680. #define F_RADIX        FLT_RADIX
  1681. #endif
  1682. #ifdef FLT_MANT_DIG
  1683. #define F_MANT_DIG    FLT_MANT_DIG
  1684. #endif
  1685. #ifdef FLT_DIG
  1686. #define F_DIG        FLT_DIG
  1687. #endif
  1688. #ifdef FLT_ROUNDS
  1689. #define F_ROUNDS    FLT_ROUNDS
  1690. #endif
  1691. #ifdef FLT_EPSILON
  1692. #define F_EPSILON    FLT_EPSILON
  1693. #endif
  1694. #ifdef FLT_MIN_EXP
  1695. #define F_MIN_EXP    FLT_MIN_EXP
  1696. #endif
  1697. #ifdef FLT_MIN
  1698. #define F_MIN        FLT_MIN
  1699. #endif
  1700. #ifdef FLT_MIN_10_EXP
  1701. #define F_MIN_10_EXP    FLT_MIN_10_EXP
  1702. #endif
  1703. #ifdef FLT_MAX_EXP
  1704. #define F_MAX_EXP    FLT_MAX_EXP
  1705. #endif
  1706. #ifdef FLT_MAX
  1707. #define F_MAX        FLT_MAX
  1708. #endif
  1709. #ifdef FLT_MAX_10_EXP
  1710. #define F_MAX_10_EXP    FLT_MAX_10_EXP
  1711. #endif
  1712. #endif /* VERIFY */
  1713.  
  1714. #endif /* PASS1 */
  1715.  
  1716. #ifdef PASS2
  1717.  
  1718. #define Number    double
  1719. #define THING    "DOUBLE"
  1720. #define Thing    "Double"
  1721. #define thing    "double"
  1722. #define Fname    "DBL"
  1723. #define FPROP    dprop
  1724. #define Store    dStore
  1725. #define Sum    dSum
  1726. #define Diff    dDiff
  1727. #define Mul    dMul
  1728. #define Div    dDiv
  1729. #define ZERO    0.0
  1730. #define HALF    0.5
  1731. #define ONE    1.0
  1732. #define TWO    2.0
  1733. #define THREE    3.0
  1734. #define FOUR    4.0
  1735. #define Self    dSelf
  1736. #define F_check    dCheck
  1737. #define MARK    ""
  1738. #ifdef VERIFY
  1739. #define Validate(prec, val, req, same) dValidate(prec, val, req, same)
  1740. #endif
  1741.  
  1742. #define EPROP    edprop
  1743.  
  1744. #define Integer    int
  1745. #define INT    "int"
  1746. #define IPROP    iprop
  1747. #define Iname    "INT"
  1748. #define OK_UI    1 /* Unsigned int is always possible */
  1749. #define IMARK    ""
  1750.  
  1751. #define UPROP    uiprop
  1752. #define Uname    "UINT"
  1753.  
  1754. #ifdef VERIFY
  1755. #ifdef INT_MAX
  1756. #define I_MAX        INT_MAX
  1757. #endif
  1758. #ifdef INT_MIN
  1759. #define I_MIN        INT_MIN
  1760. #endif
  1761. #ifdef UINT_MAX
  1762. #define U_MAX        UINT_MAX
  1763. #endif
  1764.  
  1765. #ifdef DBL_MANT_DIG
  1766. #define F_MANT_DIG    DBL_MANT_DIG
  1767. #endif
  1768. #ifdef DBL_DIG
  1769. #define F_DIG        DBL_DIG
  1770. #endif
  1771. #ifdef DBL_EPSILON
  1772. #define F_EPSILON    DBL_EPSILON
  1773. #endif
  1774. #ifdef DBL_MIN_EXP
  1775. #define F_MIN_EXP    DBL_MIN_EXP
  1776. #endif
  1777. #ifdef DBL_MIN
  1778. #define F_MIN        DBL_MIN
  1779. #endif
  1780. #ifdef DBL_MIN_10_EXP
  1781. #define F_MIN_10_EXP    DBL_MIN_10_EXP
  1782. #endif
  1783. #ifdef DBL_MAX_EXP
  1784. #define F_MAX_EXP    DBL_MAX_EXP
  1785. #endif
  1786. #ifdef DBL_MAX
  1787. #define F_MAX        DBL_MAX
  1788. #endif
  1789. #ifdef DBL_MAX_10_EXP
  1790. #define F_MAX_10_EXP    DBL_MAX_10_EXP
  1791. #endif
  1792. #endif /* VERIFY */
  1793.  
  1794. #endif /* PASS2 */
  1795.  
  1796. #ifdef PASS3
  1797.  
  1798. #ifdef STDC
  1799. #define Number    long double
  1800.  
  1801. #define ZERO    0.0L
  1802. #define HALF    0.5L
  1803. #define ONE    1.0L
  1804. #define TWO    2.0L
  1805. #define THREE    3.0L
  1806. #define FOUR    4.0L
  1807. #endif
  1808.  
  1809. #define THING    "LONG DOUBLE"
  1810. #define Thing    "Long double"
  1811. #define thing    "long double"
  1812. #define Fname    "LDBL"
  1813. #define FPROP    ldprop
  1814. #define Store    ldStore
  1815. #define Sum    ldSum
  1816. #define Diff    ldDiff
  1817. #define Mul    ldMul
  1818. #define Div    ldDiv
  1819. #define Self    ldSelf
  1820. #define F_check    ldCheck
  1821. #define MARK    "L"
  1822. #ifdef VERIFY
  1823. #define Validate(prec, val, req, same) ldValidate(prec, val, req, same)
  1824. #endif
  1825.  
  1826. #define EPROP    eldprop
  1827.  
  1828. #define Integer    long
  1829. #define INT    "long"
  1830. #define IPROP    lprop
  1831. #define Iname    "LONG"
  1832. #ifndef NO_UI
  1833. #define OK_UI    1
  1834. #endif
  1835. #define IMARK    "L"
  1836.  
  1837. #define UPROP    ulprop
  1838. #define Uname    "ULONG"
  1839.  
  1840. #ifdef VERIFY
  1841. #ifdef LONG_MAX
  1842. #define I_MAX    LONG_MAX
  1843. #endif
  1844. #ifdef LONG_MIN
  1845. #define I_MIN    LONG_MIN
  1846. #endif
  1847. #ifdef ULONG_MAX
  1848. #define U_MAX    ULONG_MAX
  1849. #endif
  1850.  
  1851. #ifdef LDBL_MANT_DIG
  1852. #define F_MANT_DIG    LDBL_MANT_DIG
  1853. #endif
  1854. #ifdef LDBL_DIG
  1855. #define F_DIG        LDBL_DIG
  1856. #endif
  1857. #ifdef LDBL_EPSILON
  1858. #define F_EPSILON    LDBL_EPSILON
  1859. #endif
  1860. #ifdef LDBL_MIN_EXP
  1861. #define F_MIN_EXP    LDBL_MIN_EXP
  1862. #endif
  1863. #ifdef LDBL_MIN
  1864. #define F_MIN        LDBL_MIN
  1865. #endif
  1866. #ifdef LDBL_MIN_10_EXP
  1867. #define F_MIN_10_EXP    LDBL_MIN_10_EXP
  1868. #endif
  1869. #ifdef LDBL_MAX_EXP
  1870. #define F_MAX_EXP    LDBL_MAX_EXP
  1871. #endif
  1872. #ifdef LDBL_MAX
  1873. #define F_MAX        LDBL_MAX
  1874. #endif
  1875. #ifdef LDBL_MAX_10_EXP
  1876. #define F_MAX_10_EXP    LDBL_MAX_10_EXP
  1877. #endif
  1878. #endif /* VERIFY */
  1879.  
  1880. #endif /* PASS3 */
  1881.  
  1882. #ifndef I_MAX
  1883. #define I_MAX    int_max
  1884. #endif
  1885. #ifndef I_MIN
  1886. #define I_MIN    int_min
  1887. #endif
  1888. #ifndef U_MAX
  1889. #define U_MAX    u_max
  1890. #endif
  1891.  
  1892. #ifndef F_RADIX
  1893. #define F_RADIX        f_radix
  1894. #endif
  1895. #ifndef F_MANT_DIG
  1896. #define F_MANT_DIG    f_mant_dig
  1897. #endif
  1898. #ifndef F_DIG
  1899. #define F_DIG        f_dig
  1900. #endif
  1901. #ifndef F_ROUNDS
  1902. #define F_ROUNDS    f_rounds
  1903. #endif
  1904. #ifndef F_EPSILON
  1905. #define F_EPSILON    f_epsilon
  1906. #endif
  1907. #ifndef F_MIN_EXP
  1908. #define F_MIN_EXP    f_min_exp
  1909. #endif
  1910. #ifndef F_MIN
  1911. #define F_MIN        f_min
  1912. #endif
  1913. #ifndef F_MIN_10_EXP
  1914. #define F_MIN_10_EXP    f_min_10_exp
  1915. #endif
  1916. #ifndef F_MAX_EXP
  1917. #define F_MAX_EXP    f_max_exp
  1918. #endif
  1919. #ifndef F_MAX
  1920. #define F_MAX        f_max
  1921. #endif
  1922. #ifndef F_MAX_10_EXP
  1923. #define F_MAX_10_EXP    f_max_10_exp
  1924. #endif
  1925.  
  1926. #ifndef VERIFY
  1927. #define Validate(prec, val, req, same) {;}
  1928. #endif
  1929.  
  1930. #ifdef Integer
  1931.  
  1932. Procedure IPROP() {
  1933.     /* the properties of short, int, and long */
  1934.     Volatile Integer newi, int_max, maxeri, int_min, minneri;
  1935.     Volatile int ibits, ipower, two=2;
  1936.  
  1937.     /* Calculate max short/int/long ***********************************/
  1938.     /* Calculate 2**n-1 until overflow - then use the previous value  */
  1939.  
  1940.     newi=1; int_max=0;
  1941.  
  1942.     if (setjmp(lab)==0) { /* Yields int_max */
  1943.         for(ipower=0; newi>int_max; ipower++) {
  1944.             int_max=newi;
  1945.             newi=newi*two+1;
  1946.         }
  1947.         Vprintf("%sOverflow of a%s %s does not generate a trap%s\n",
  1948.             co, INT[0]=='i'?"n":"", INT, oc);
  1949.     } else {
  1950.         Vprintf("%sOverflow of a%s %s generates a trap%s\n",
  1951.             co, INT[0]=='i'?"n":"", INT, oc);
  1952.     }
  1953.     Unexpected(7);
  1954.  
  1955.     /* Minimum value: assume either two's or one's complement *********/
  1956.     int_min= -int_max;
  1957.     if (setjmp(lab)==0) { /* Yields int_min */
  1958.         if (int_min-1 < int_min) int_min--;
  1959.     }
  1960.     Unexpected(8);
  1961.  
  1962.     /* Now for those daft Cybers */
  1963.  
  1964.     maxeri=0; newi=int_max;
  1965.  
  1966.     if (setjmp(lab)==0) { /* Yields maxeri */
  1967.         for(ibits=ipower; newi>maxeri; ibits++) {
  1968.             maxeri=newi;
  1969.             newi=newi+newi+1;
  1970.         }
  1971.     }
  1972.     Unexpected(9);
  1973.  
  1974.     minneri= -maxeri;
  1975.     if (setjmp(lab)==0) { /* Yields minneri */
  1976.         if (minneri-1 < minneri) minneri--;
  1977.     }
  1978.     Unexpected(10);
  1979.  
  1980.     Vprintf("%sMaximum %s = %ld (= 2**%d-1)%s\n",
  1981.         co, INT, (long)int_max, ipower, oc);
  1982.     Vprintf("%sMinimum %s = %ld%s\n", co, INT, (long)int_min, oc);
  1983.  
  1984.     if (L) i_define(D_INT_MAX, INT, Iname, "_MAX",
  1985.             (long) int_max, 0L,
  1986.             (long) I_MAX, IMARK);
  1987.     if (L) i_define(D_INT_MIN, INT, Iname, "_MIN",
  1988.             (long) int_min, (long) (PASS==1?maxint:int_max),
  1989.             (long) I_MIN, IMARK);
  1990.  
  1991.     if(int_max < 0) { /* It has happened */
  1992.         eek_a_bug("signed integral comparison faulty?");
  1993.     }
  1994.  
  1995.     if (maxeri>int_max) {
  1996.         Vprintf("%sThere is a larger %s, %ld (= 2**%d-1), %s %s%s\n",
  1997.             co, INT, (long)maxeri, ibits,
  1998.             "but only for addition, not multiplication",
  1999.             "(I smell a Cyber!)",
  2000.             oc);
  2001.     }
  2002.  
  2003.     if (minneri<int_min) {
  2004.         Vprintf("%sThere is a smaller %s, %ld, %s %s%s\n",
  2005.             co, INT, (long)minneri,
  2006.             "but only for addition, not multiplication",
  2007.             "(I smell a Cyber!)",
  2008.             oc);
  2009.     }
  2010. }
  2011.  
  2012. Procedure UPROP () {
  2013.     /* The properties of unsigned short/int/long */
  2014. #ifdef OK_UI
  2015.     Volatile unsigned Integer u_max, newi, two;
  2016.     newi=1; u_max=0; two=2;
  2017.  
  2018.     if (setjmp(lab)==0) { /* Yields u_max */
  2019.         while(newi>u_max) {
  2020.             u_max=newi;
  2021.             newi=newi*two+1;
  2022.         }
  2023.     }
  2024.     Unexpected(11);
  2025.     Vprintf("%sMaximum unsigned %s = %lu%s\n",
  2026.         co, INT, (unsigned long) u_max, oc);
  2027.  
  2028.     /* Oh woe: new standard C defines value preserving promotions */
  2029.     if (L) {
  2030.         if (PASS == 1 && sizeof(short) < sizeof(int)) {
  2031.             /* Special only for short */
  2032.             i_define(D_UINT_MAX, INT, Uname, "_MAX",
  2033.                  (unsigned long) u_max, 0L,
  2034.                  (unsigned long) U_MAX, IMARK);
  2035.         } else {
  2036.             u_define(D_UINT_MAX, INT, Uname, "_MAX",
  2037.                  (unsigned long) u_max,
  2038.                  (unsigned long) U_MAX, IMARK);
  2039.         }
  2040.     }
  2041. #endif
  2042. }
  2043.  
  2044. #endif /* Integer */
  2045.  
  2046. #ifdef Number
  2047.  
  2048. /* The following routines are intended to defeat any attempt at optimisation
  2049.    or use of extended precision, and to defeat faulty narrowing casts.
  2050.    The weird prototypes are because of widening incompatibilities.
  2051. */
  2052. #ifdef STDC
  2053. #define ARGS1(atype, a) (atype a)
  2054. #define ARGS2(atype, a, btype, b) (atype a, btype b)
  2055. #else
  2056. #define ARGS1(atype, a) (a) atype a;
  2057. #define ARGS2(atype, a, btype, b) (a, b) atype a; btype b;
  2058. #endif
  2059.  
  2060. Procedure Store ARGS2(Number, a, Number *, b) { *b=a; }
  2061. Number Sum ARGS2(Number, a, Number, b) {Number r; Store(a+b, &r); return (r); }
  2062. Number Diff ARGS2(Number, a, Number, b){Number r; Store(a-b, &r); return (r); }
  2063. Number Mul ARGS2(Number, a, Number, b) {Number r; Store(a*b, &r); return (r); }
  2064. Number Div ARGS2(Number, a, Number, b) {Number r; Store(a/b, &r); return (r); }
  2065. Number Self ARGS1(Number, a)           {Number r; Store(a,   &r); return (r); }
  2066.  
  2067. Procedure F_check ARGS((int precision, Long_double val1));
  2068.  
  2069. Procedure F_check(precision, val1) int precision; Long_double val1; {
  2070.     /* You don't think I'm going to go to all the trouble of writing
  2071.        a program that works out what all sorts of values are, only to
  2072.        have printf go and print the wrong values out, do you?
  2073.        No, you're right, so this function tries to see if printf
  2074.        has written the right value, by reading it back again.
  2075.        This introduces a new problem of course: suppose printf writes
  2076.        the correct value, and scanf reads it back wrong... oh well.
  2077.        But I'm adamant about this: the precision given is enough
  2078.        to uniquely identify the printed number, therefore I insist
  2079.        that sscanf read the number back identically. Harsh yes, but
  2080.        sometimes you've got to be cruel to be kind.
  2081.     */
  2082.     Long_double new1;
  2083.     Number val, new, diff;
  2084.     double rem;
  2085.     int e;
  2086.     char *rep;
  2087.     char *f2;
  2088.  
  2089.     if (sizeof(double) == sizeof(Long_double)) {
  2090.         /* Assume they're the same, and use non-stdc format */
  2091.         /* This is for stdc compilers using non-stdc libraries */
  2092.         f2= "%le";   /* Input */
  2093.     } else {
  2094.         /* It had better support Le then */
  2095.         f2= "%Le";
  2096.     }
  2097.     val= val1;
  2098.     rep= f_rep(precision, (Long_double) val);
  2099.     if (setjmp(lab)==0) {
  2100.         sscanf(rep, f2, &new1);
  2101.     } else {
  2102.         eek_a_bug("sscanf caused a trap");
  2103.         printf("%s    scanning: %s format: %s%s\n\n", co, rep, f2, oc);
  2104.         Unexpected(12);
  2105.         return;
  2106.     }
  2107.  
  2108.     if (setjmp(lab)==0) { /* See if new is usable */
  2109.         new= new1;
  2110.         if (new != 0.0) {
  2111.             diff= val/new - 1.0;
  2112.             if (diff < 0.1) diff= 1.0;
  2113.             /* That should be enough to generate a trap */
  2114.         }
  2115.     } else {
  2116.         eek_a_bug("sscanf returned an unusable number");
  2117.         printf("%s    scanning: %s with format: %s%s\n\n",
  2118.                co, rep, f2, oc);
  2119.         Unexpected(13);
  2120.         return;
  2121.     }
  2122.  
  2123.     Unexpected(14);
  2124.     if (new != val) {
  2125.         eek_a_bug("Possibly bad output from printf above");
  2126.         if (!exponent((Long_double)val, &rem, &e)) {
  2127.             printf("%s    but value was an unusable number%s\n\n",
  2128.                    co, oc);
  2129.             return;
  2130.         }
  2131.         printf("%s    expected value around %.*fe%d, bit pattern:\n    ",
  2132.                co, precision, rem, e);
  2133.         bitpattern((char *) &val, (unsigned)sizeof(val));
  2134.         printf ("%s\n", oc);
  2135.         printf("%s    sscanf gave           %s, bit pattern:\n    ",
  2136.                co, f_rep(precision, (Long_double) new));
  2137.         bitpattern((char *) &new, (unsigned)sizeof(new));
  2138.         printf ("%s\n", oc);
  2139.         if (setjmp(lab) == 0) {
  2140.             diff= val-new;
  2141.             printf("%s    difference= %s%s\n\n",
  2142.                    co, f_rep(precision, (Long_double) diff), oc);
  2143.         } /* else forget it */
  2144.         Unexpected(15);
  2145.     }
  2146. }
  2147.  
  2148. #ifdef VERIFY
  2149. Procedure Validate(prec, val, req, same) int prec, same; Long_double val, req; {
  2150.     /* Check that the compiler has read a #define value correctly */
  2151.     Unexpected(16);
  2152.     if (!same) {
  2153.         printf("%s*** Verify failed for above #define!\n", co);
  2154.         if (setjmp(lab) == 0) { /* for the case that req == nan */
  2155.             printf("       Compiler has %s for value%s\n",
  2156.                    f_rep(prec, req), oc);
  2157.         } else {
  2158.             printf("       Compiler has %s for value%s\n",
  2159.                    "an unusable number", oc);
  2160.         }
  2161.         if (setjmp(lab) == 0) {
  2162.             F_check(prec, (Long_double) req);
  2163.         } /*else forget it*/
  2164.         if (setjmp(lab) == 0) {
  2165.             if (req > 0.0 && val > 0.0) {
  2166.                 printf("%s    difference= %s%s\n",
  2167.                        co, f_rep(prec, val-req), oc);
  2168.             }
  2169.         } /*else forget it*/
  2170.         Unexpected(17);
  2171.         printf("\n");
  2172.         bugs++;
  2173.     } else if (val != req) {
  2174.         if (stdc) eek_a_bug("constant has the wrong precision");
  2175.         else eek_a_bug("the cast didn't work");
  2176.         printf("\n");
  2177.     }
  2178. }
  2179. #endif /* VERIFY */
  2180.  
  2181. int FPROP(bits_per_byte) int bits_per_byte; {
  2182.     /* Properties of floating types, using algorithms by Cody and Waite
  2183.        from MA Malcolm, as modified by WM Gentleman and SB Marovich.
  2184.        Further extended by S Pemberton.
  2185.  
  2186.        Returns the number of digits in the fraction.
  2187.     */
  2188.  
  2189.     Volatile int
  2190.         i, f_radix, iexp, irnd, mrnd, f_rounds, f_mant_dig,
  2191.         iz, k, inf, machep, f_max_exp, f_min_exp, mx, negeps,
  2192.         mantbits, digs, f_dig, trap,
  2193.         hidden, normal, f_min_10_exp, f_max_10_exp;
  2194.     Volatile Number
  2195.         a, b, base, basein, basem1, f_epsilon, epsneg,
  2196.         eps, epsp1, etop, ebot,
  2197.         f_max, newxmax, f_min, xminner, y, y1, z, z1, z2;
  2198.  
  2199.     Unexpected(18);
  2200.  
  2201.     Vprintf("%sPROPERTIES OF %s%s\n", co, THING, oc);
  2202.  
  2203.     /* Base and size of significand **************************************/
  2204.     /* First repeatedly double until adding 1 has no effect.      */
  2205.     /* For instance, if base is 10, with 3 significant digits      */
  2206.     /* it will try 1, 2, 4, 8, ... 512, 1024, and stop there,      */
  2207.     /* since 1024 is only representable as 1020.              */
  2208.     a=1.0;
  2209.     if (setjmp(lab)==0) { /* inexact trap? */
  2210.         do { a=Sum(a, a); }
  2211.         while (Diff(Diff(Sum(a, ONE), a), ONE) == ZERO);
  2212.     } else {
  2213.         fprintf(stderr, "*** Program got loss-of-precision trap!\n");
  2214.         /* And supporting those is just TOO much trouble! */
  2215.         farewell(bugs+1);
  2216.     }
  2217.     Unexpected(19);
  2218.     /* Now double until you find a number that can be added to the      */
  2219.     /* above number. For 1020 this is 8 or 16, depending whether the  */
  2220.     /* result is rounded or truncated.                  */
  2221.     /* In either case the result is 1030. 1030-1020= the base, 10.      */
  2222.     b=1.0;
  2223.     do { b=Sum(b, b); } while ((base=Diff(Sum(a, b), a)) == ZERO);
  2224.     f_radix=base;
  2225.     Vprintf("%sBase = %d%s\n", co, f_radix, oc);
  2226.  
  2227.     /* Sanity check; if base<2, I can't guarantee the rest will work  */
  2228.     if (f_radix < 2) {
  2229.         eek_a_bug("Function return or parameter passing faulty? (This is a guess.)");
  2230.         printf("\n");
  2231.         return(0);
  2232.     }
  2233.  
  2234.     if (PASS == 1) { /* only for FLT */
  2235.         flt_radix= f_radix;
  2236.         if (F) i_define(D_FLT_RADIX, "", "FLT", "_RADIX",
  2237.                 (long) f_radix, 0L, (long) F_RADIX, "");
  2238.     } else if (f_radix != flt_radix) {
  2239.         printf("\n%s*** WARNING: %s %s (%d) %s%s\n",
  2240.                co, thing, "arithmetic has a different radix",
  2241.                f_radix, "from float", oc);
  2242.         bugs++;
  2243.     }
  2244.  
  2245.     /* Now the number of digits precision */
  2246.     f_mant_dig=0; b=1.0;
  2247.     do { f_mant_dig++; b=Mul(b, base); }
  2248.     while (Diff(Diff(Sum(b, ONE), b), ONE) == ZERO);
  2249.     f_dig=floor_log(10, (Long_double)(b/base)) + (base==10?1:0);
  2250.     Vprintf("%sSignificant base digits = %d %s %d %s%s\n",
  2251.         co, f_mant_dig, "(= at least", f_dig, "decimal digits)", oc);
  2252.     if (F) i_define(D_MANT_DIG, thing, Fname, "_MANT_DIG",
  2253.             (long) f_mant_dig, 0L, (long) F_MANT_DIG, "");
  2254.     if (F) i_define(D_DIG, thing, Fname, "_DIG",
  2255.             (long) f_dig, 0L, (long) F_DIG, "");
  2256.     digs= ceil_log(10, (Long_double)b); /* the number of digits to printf */
  2257.  
  2258.     /* Rounding *******************************************************/
  2259.     basem1=Diff(base, HALF);
  2260.     if (Diff(Sum(a, basem1), a) != ZERO) {
  2261.         if (f_radix == 2) basem1=0.375;
  2262.         else basem1=1.0;
  2263.         if (Diff(Sum(a, basem1), a) != ZERO) irnd=2; /* away from 0 */
  2264.         else irnd=1; /* to nearest */
  2265.     } else irnd=0; /* towards 0 */
  2266.  
  2267.     basem1=Diff(base, HALF);
  2268.  
  2269.     if (Diff(Diff(-a, basem1), -a) != ZERO) {
  2270.         if (f_radix == 2) basem1=0.375;
  2271.         else basem1=1.0;
  2272.         if (Diff(Diff(-a, basem1), -a) != ZERO) mrnd=2; /* away from 0*/
  2273.         else mrnd=1; /* to nearest */
  2274.     } else mrnd=0; /* towards 0 */
  2275.  
  2276.     f_rounds= -1; /* Unknown rounding */
  2277.     if (irnd==0 && mrnd==0) f_rounds=0; /* zero = chops */
  2278.     if (irnd==1 && mrnd==1) f_rounds=1; /* nearest */
  2279.     if (irnd==2 && mrnd==0) f_rounds=2; /* +inf */
  2280.     if (irnd==0 && mrnd==2) f_rounds=3; /* -inf */
  2281.  
  2282.     if (f_rounds != -1) {
  2283.         Vprintf("%sArithmetic rounds towards ", co);
  2284.         switch (f_rounds) {
  2285.               case 0: Vprintf("zero (i.e. it chops)"); break;
  2286.               case 1: Vprintf("nearest"); break;
  2287.               case 2: Vprintf("+infinity"); break;
  2288.               case 3: Vprintf("-infinity"); break;
  2289.               default: Vprintf("???"); break;
  2290.         }
  2291.         Vprintf("%s\n", oc);
  2292.     } else { /* Hmm, try to give some help here */
  2293.         Vprintf("%sArithmetic rounds oddly: %s\n", co, oc);
  2294.         Vprintf("%s    Negative numbers %s%s\n",
  2295.             co, mrnd==0 ? "towards zero" :
  2296.                 mrnd==1 ? "to nearest" :
  2297.                       "away from zero",
  2298.             oc);
  2299.         Vprintf("%s    Positive numbers %s%s\n",
  2300.             co, irnd==0 ? "towards zero" :
  2301.                 irnd==1 ? "to nearest" :
  2302.                       "away from zero",
  2303.             oc);
  2304.     }
  2305.     /* An extra goody */
  2306.     if (f_radix == 2 && f_rounds == 1) {
  2307.         if (Diff(Sum(a, ONE), a) != ZERO) {
  2308.             Vprintf("%s   Tie breaking rounds up%s\n", co, oc);
  2309.         } else if (Diff(Sum(a, THREE), a) == FOUR) {
  2310.             Vprintf("%s   Tie breaking rounds to even%s\n", co, oc);
  2311.         } else {
  2312.             Vprintf("%s   Tie breaking rounds down%s\n", co, oc);
  2313.         }
  2314.     }
  2315.     if (PASS == 1) { /* only for FLT */
  2316.         flt_rounds= f_rounds;
  2317.         if (F)
  2318.           i_define(D_FLT_ROUNDS, "", "FLT", "_ROUNDS",
  2319.                (long) f_rounds, 1L, (long) F_ROUNDS, "");
  2320.     } else if (f_rounds != flt_rounds) {
  2321.         printf("\n%s*** WARNING: %s %s (%d) %s%s\n",
  2322.                co, thing, "arithmetic rounds differently",
  2323.                f_rounds, "from float", oc);
  2324.         bugs++;
  2325.     }
  2326.  
  2327.     /* Various flavours of epsilon ************************************/
  2328.     negeps=f_mant_dig+f_mant_dig;
  2329.     basein=1.0/base;
  2330.     a=1.0;
  2331.     for(i=1; i<=negeps; i++) a*=basein;
  2332.  
  2333.     b=a;
  2334.     while (Diff(Diff(ONE, a), ONE) == ZERO) {
  2335.         a*=base;
  2336.         negeps--;
  2337.     }
  2338.     negeps= -negeps;
  2339.     Vprintf("%sSmallest x such that 1.0-base**x != 1.0 = %d%s\n",
  2340.         co, negeps, oc);
  2341.  
  2342.     etop = ONE;
  2343.     ebot = ZERO;
  2344.     eps = Sum(ebot, Div(Diff(etop, ebot), TWO));
  2345.     /* find the smallest epsneg (1-epsneg != 1) by binary search.
  2346.        ebot and etop are the current bounds */
  2347.     while (eps != ebot && eps != etop) {
  2348.         epsp1 = Diff(ONE, eps);
  2349.         if (epsp1 < ONE) etop = eps;
  2350.         else ebot = eps;
  2351.         eps = Sum(ebot, Div(Diff(etop, ebot), TWO));
  2352.     }
  2353.     eps= etop;
  2354.     /* Sanity check */
  2355.     if (Diff(ONE, etop) >= ONE || Diff(ONE, ebot) != ONE) {
  2356.         eek_a_bug("internal error calculating epsneg");
  2357.     }
  2358.     Vprintf("%sSmallest x such that 1.0-x != 1.0 = %s%s\n",
  2359.         co, f_rep(digs, (Long_double) eps), oc);
  2360.     if (V) F_check(digs, (Long_double) eps);
  2361.  
  2362.     epsneg=a;
  2363.     if ((f_radix!=2) && irnd) {
  2364.     /*    a=(a*(1.0+a))/(1.0+1.0); => */
  2365.         a=Div(Mul(a, Sum(ONE, a)), Sum(ONE, ONE));
  2366.     /*    if ((1.0-a)-1.0 != 0.0) epsneg=a; => */
  2367.         if (Diff(Diff(ONE, a), ONE) != ZERO) epsneg=a;
  2368.     }
  2369.     /* epsneg is used later */
  2370.     Unexpected(20);
  2371.  
  2372.     machep= -f_mant_dig-f_mant_dig;
  2373.     a=b;
  2374.     while (Diff(Sum(ONE, a), ONE) == ZERO) { a*=base; machep++; }
  2375.     Vprintf("%sSmallest x such that 1.0+base**x != 1.0 = %d%s\n",
  2376.         co, machep, oc);
  2377.  
  2378.     etop = ONE;
  2379.     ebot = ZERO;
  2380.     eps = Sum(ebot, Div(Diff(etop, ebot), TWO));
  2381.     /* find the smallest eps (1+eps != 1) by binary search.
  2382.        ebot and etop are the current bounds */
  2383.     while (eps != ebot && eps != etop) {
  2384.         epsp1 = Sum(ONE, eps);
  2385.         if (epsp1 > ONE) etop = eps;
  2386.         else ebot = eps;
  2387.         eps = Sum(ebot, Div(Diff(etop, ebot), TWO));
  2388.     }
  2389.     /* Sanity check */
  2390.     if (Sum(ONE, etop) <= ONE || Sum(ONE, ebot) != ONE) {
  2391.         eek_a_bug("internal error calculating eps");
  2392.     }
  2393.     f_epsilon=etop;
  2394.  
  2395.     Vprintf("%sSmallest x such that 1.0+x != 1.0 = %s%s\n",
  2396.         co, f_rep(digs, (Long_double) f_epsilon), oc);
  2397.  
  2398.     f_epsilon= Diff(Sum(ONE, f_epsilon), ONE); /* New C standard defn */
  2399.     Vprintf("%s(Above number + 1.0) - 1.0 = %s%s\n",
  2400.         co, f_rep(digs, (Long_double) (f_epsilon)), oc);
  2401.  
  2402.     /* Possible loss of precision warnings here from non-stdc compilers */
  2403.     if (F) f_define(D_EPSILON, thing,
  2404.             Fname, "_EPSILON", digs, (Long_double) f_epsilon, MARK);
  2405.     if (V || F) F_check(digs, (Long_double) f_epsilon);
  2406.     Unexpected(21);
  2407.     if (F) Validate(digs, (Long_double) f_epsilon, (Long_double) F_EPSILON,
  2408.             f_epsilon == Self(F_EPSILON));
  2409.     Unexpected(22);
  2410.  
  2411.     /* Extra chop info *************************************************/
  2412.     if (f_rounds == 0) {
  2413.         if (Diff(Mul(Sum(ONE,f_epsilon),ONE),ONE) !=  ZERO) {
  2414.             Vprintf("%sAlthough arithmetic chops, it uses guard digits%s\n", co, oc);
  2415.         }
  2416.     }
  2417.  
  2418.     /* Size of and minimum normalised exponent ************************/
  2419.     y=0; i=0; k=1; z=basein; z1=(1.0+f_epsilon)/base;
  2420.  
  2421.     /* Coarse search for the largest power of two */
  2422.     if (setjmp(lab)==0) { /* for underflow trap */ /* Yields i, k, y, y1 */
  2423.         do {
  2424.             y=z; y1=z1;
  2425.             z=Mul(y,y); z1=Mul(z1, y);
  2426.             a=Mul(z,ONE);
  2427.             z2=Div(z1,y);
  2428.             if (z2 != y1) break;
  2429.             if ((Sum(a,a) == ZERO) || (fabs(z) >= y)) break;
  2430.             i++;
  2431.             k+=k;
  2432.         } while(1);
  2433.     } else {
  2434.         Vprintf("%s%s underflow generates a trap%s\n", co, Thing, oc);
  2435.     }
  2436.     Unexpected(23);
  2437.  
  2438.     if (f_radix != 10) {
  2439.         iexp=i+1; /* for the sign */
  2440.         mx=k+k;
  2441.     } else {
  2442.         iexp=2;
  2443.         iz=f_radix;
  2444.         while (k >= iz) { iz*=f_radix; iexp++; }
  2445.         mx=iz+iz-1;
  2446.     }
  2447.  
  2448.     /* Fine tune starting with y and y1 */
  2449.     if (setjmp(lab)==0) { /* for underflow trap */ /* Yields k, f_min */
  2450.         do {
  2451.             f_min=y; z1=y1;
  2452.             y=Div(y,base); y1=Div(y1,base);
  2453.             a=Mul(y,ONE);
  2454.             z2=Mul(y1,base);
  2455.             if (z2 != z1) break;
  2456.             if ((Sum(a,a) == ZERO) || (fabs(y) >= f_min)) break;
  2457.             k++;
  2458.         } while (1);
  2459.     }
  2460.     Unexpected(24);
  2461.  
  2462.     f_min_exp=(-k)+1;
  2463.  
  2464.     if ((mx <= k+k-3) && (f_radix != 10)) { mx+=mx; iexp+=1; }
  2465.     Vprintf("%sNumber of bits used for exponent = %d%s\n", co, iexp, oc);
  2466.     Vprintf("%sMinimum normalised exponent = %d%s\n", co, f_min_exp-1, oc);
  2467.     if (F)
  2468.       i_define(D_MIN_EXP, thing, Fname, "_MIN_EXP",
  2469.            (long) f_min_exp, (long) maxint, (long) F_MIN_EXP, "");
  2470.  
  2471.     if (setjmp(lab)==0) {
  2472.         Vprintf("%sMinimum normalised positive number = %s%s\n",
  2473.             co, f_rep(digs, (Long_double) f_min), oc);
  2474.     } else {
  2475.         eek_a_bug("printf can't print the smallest normalised number");
  2476.         printf("\n");
  2477.     }
  2478.     Unexpected(25);
  2479.     /* Possible loss of precision warnings here from non-stdc compilers */
  2480.     if (setjmp(lab) == 0) {
  2481.         if (F) f_define(D_MIN, thing,
  2482.                 Fname, "_MIN", digs, (Long_double) f_min, MARK);
  2483.         if (V || F) F_check(digs, (Long_double) f_min);
  2484.     } else {
  2485.         eek_a_bug("xxx_MIN caused a trap");
  2486.         printf("\n");
  2487.     }
  2488.  
  2489.     if (setjmp(lab) == 0) {
  2490.         if (F) Validate(digs, (Long_double) f_min, (Long_double) F_MIN,
  2491.                 f_min == Self(F_MIN));
  2492.     } else {
  2493.         printf("%s*** Verify failed for above #define!\n    %s %s\n\n",
  2494.                co, "Compiler has an unusable number for value", oc);
  2495.         bugs++;
  2496.     }
  2497.     Unexpected(26);
  2498.  
  2499.     a=1.0; f_min_10_exp=0;
  2500.     while (a > f_min*10.0) { a/=10.0; f_min_10_exp--; }
  2501.     if (F) i_define(D_MIN_10_EXP, thing, Fname, "_MIN_10_EXP",
  2502.             (long) f_min_10_exp, (long) maxint,
  2503.             (long) F_MIN_10_EXP, "");
  2504.  
  2505.     /* Minimum exponent ************************************************/
  2506.     if (setjmp(lab)==0) { /* for underflow trap */ /* Yields xminner */
  2507.         do {
  2508.             xminner=y;
  2509.             y=Div(y,base);
  2510.             a=Mul(y,ONE);
  2511.             if ((Sum(a,a) == ZERO) || (fabs(y) >= xminner)) break;
  2512.         } while (1);
  2513.     }
  2514.     Unexpected(27);
  2515.  
  2516.     if (xminner != 0.0 && xminner != f_min) {
  2517.         normal= 0;
  2518.         Vprintf("%sThe smallest numbers are not kept normalised%s\n",
  2519.             co, oc);
  2520.         if (setjmp(lab)==0) {
  2521.             Vprintf("%sSmallest unnormalised positive number = %s%s\n",
  2522.                 co, f_rep(digs, (Long_double) xminner), oc);
  2523.             if (V) F_check(digs, (Long_double) xminner);
  2524.         } else {
  2525.             eek_a_bug("printf can't print the smallest unnormalised number.");
  2526.             printf("\n");
  2527.         }
  2528.         Unexpected(28);
  2529.     } else {
  2530.         normal= 1;
  2531.         Vprintf("%sThe smallest numbers are normalised%s\n", co, oc);
  2532.     }
  2533.  
  2534.     /* Maximum exponent ************************************************/
  2535.     f_max_exp=2; f_max=1.0; newxmax=base+1.0;
  2536.     inf=0; trap=0;
  2537.     while (f_max<newxmax) {
  2538.         f_max=newxmax;
  2539.         if (setjmp(lab) == 0) { /* Yields inf, f_max_exp */
  2540.             newxmax=Mul(newxmax, base);
  2541.         } else {
  2542.             trap=1;
  2543.             break;
  2544.         }
  2545.         if (Div(newxmax, base) != f_max) {
  2546.             inf=1; /* ieee infinity */
  2547.             break;
  2548.         }
  2549.         f_max_exp++;
  2550.     }
  2551.     Unexpected(29);
  2552.     if (trap) {
  2553.         Vprintf("%s%s overflow generates a trap%s\n", co, Thing, oc);
  2554.     }
  2555.  
  2556.     if (inf) Vprintf("%sThere is an 'infinite' value%s\n", co, oc);
  2557.     Vprintf("%sMaximum exponent = %d%s\n", co, f_max_exp, oc);
  2558.     if (F) i_define(D_MAX_EXP, thing, Fname, "_MAX_EXP",
  2559.             (long) f_max_exp, 0L, (long) F_MAX_EXP, "");
  2560.  
  2561.     /* Largest number ***************************************************/
  2562.     f_max=Diff(ONE, epsneg);
  2563.     if (Mul(f_max,ONE) != f_max) f_max=Diff(ONE, Mul(base,epsneg));
  2564.     for (i=1; i<=f_max_exp; i++) f_max=Mul(f_max, base);
  2565.  
  2566.     if (setjmp(lab)==0) {
  2567.         Vprintf("%sMaximum number = %s%s\n",
  2568.             co, f_rep(digs, (Long_double) f_max), oc);
  2569.     } else {
  2570.         eek_a_bug("printf can't print the largest double.");
  2571.         printf("\n");
  2572.     }
  2573.     if (setjmp(lab)==0) {
  2574.     /* Possible loss of precision warnings here from non-stdc compilers */
  2575.         if (F) f_define(D_MAX, thing,
  2576.                 Fname, "_MAX", digs, (Long_double) f_max, MARK);
  2577.         if (V || F) F_check(digs, (Long_double) f_max);
  2578.     } else {
  2579.         eek_a_bug("xxx_MAX caused a trap");
  2580.         printf("\n");
  2581.     }
  2582.     if (setjmp(lab)==0) {
  2583.         if (F) Validate(digs, (Long_double) f_max, (Long_double) F_MAX,
  2584.                 f_max == Self(F_MAX));
  2585.     } else {
  2586.         printf("%s*** Verify failed for above #define!\n    %s %s\n\n",
  2587.                co, "Compiler has an unusable number for value", oc);
  2588.         bugs++;
  2589.     }
  2590.     Unexpected(30);
  2591.  
  2592.     a=1.0; f_max_10_exp=0;
  2593.     while (a < f_max/10.0) { a*=10.0; f_max_10_exp++; }
  2594.     if (F) i_define(D_MAX_10_EXP, thing, Fname, "_MAX_10_EXP",
  2595.             (long) f_max_10_exp, 0L, (long) F_MAX_10_EXP, "");
  2596.  
  2597.     /* Hidden bit + sanity check ****************************************/
  2598.     if (f_radix != 10) {
  2599.         hidden=0;
  2600.         mantbits=floor_log(2, (Long_double)f_radix)*f_mant_dig;
  2601.         if (mantbits+iexp == (int)sizeof(Number)*bits_per_byte) {
  2602.             hidden=1;
  2603.             Vprintf("%sArithmetic uses a hidden bit%s\n", co, oc);
  2604.         } else if (mantbits+iexp+1 == (int)sizeof(Number)*bits_per_byte) {
  2605.             Vprintf("%sArithmetic doesn't use a hidden bit%s\n",
  2606.                 co, oc);
  2607.         } else {
  2608.             printf("\n%s%s\n    %s %s %s!%s\n\n",
  2609.                    co,
  2610.                    "*** Something fishy here!",
  2611.                    "Exponent size + significand size doesn't match",
  2612.                    "with the size of a", thing,
  2613.                    oc);
  2614.         }
  2615.         if (hidden && f_radix == 2 && f_max_exp+f_min_exp==3) {
  2616.             Vprintf("%sIt looks like %s length IEEE format%s\n",
  2617.                 co, f_mant_dig==24 ? "single" :
  2618.                     f_mant_dig==53 ? "double" :
  2619.                     f_mant_dig >53 ? "extended" :
  2620.                         "some", oc);
  2621.             if (f_rounds != 1 || normal) {
  2622.                 Vprintf("%s   though ", co);
  2623.                 if (f_rounds != 1) {
  2624.                     Vprintf("the rounding is unusual");
  2625.                     if (normal) Vprintf(" and ");
  2626.                 }
  2627.                 if (normal) Vprintf("the normalisation is unusual");
  2628.                 Vprintf("%s\n", oc);
  2629.             }
  2630.         } else {
  2631.             Vprintf("%sIt doesn't look like IEEE format%s\n",
  2632.                 co, oc);
  2633.         }
  2634.     }
  2635.     printf("\n"); /* regardless of verbosity */
  2636.     return f_mant_dig;
  2637. }
  2638.  
  2639. Procedure EPROP(fprec, dprec, lprec) int fprec, dprec, lprec; {
  2640.     /* See if expressions are evaluated in extended precision.
  2641.        Some compilers optimise even if you don't want it,
  2642.        and then this function fails to produce the right result.
  2643.        We try to diagnose this if it happens.
  2644.     */
  2645.     Volatile int eprec;
  2646.     Volatile double a, b, base, old;
  2647.     Volatile Number d, oldd, dbase, one, zero;
  2648.     Volatile int bad=0;
  2649.  
  2650.     /* Size of significand **************************************/
  2651.     a=1.0;
  2652.     if (setjmp(lab) == 0) { /* Yields nothing */
  2653.         do { old=a; a=a+a; }
  2654.         while ((((a+1.0)-a)-1.0) == 0.0 && a>old);
  2655.     } else bad=1;
  2656.  
  2657.     /* Avoid the comparison if bad is set,
  2658.        to avoid trouble on the convex.  */
  2659.     if (!bad && (a <= old)) bad=1;
  2660.  
  2661.     if (!bad) {
  2662.         b=1.0;
  2663.         if (setjmp(lab) == 0) { /* Yields nothing */
  2664.             do { old=b; b=b+b; }
  2665.             while ((base=((a+b)-a)) == 0.0 && b>old);
  2666.             if (b <= old) bad=1;
  2667.         } else bad=1;
  2668.     }
  2669.  
  2670.     if (!bad) {
  2671.         eprec=0; d=1.0; dbase=base; one=1.0; zero=0.0;
  2672.         if (setjmp(lab) == 0) { /* Yields nothing */
  2673.             do { eprec++; oldd=d; d=d*dbase; }
  2674.             while ((((d+one)-d)-one) == zero && d>oldd);
  2675.             if (d <= oldd) bad=1;
  2676.         } else bad=1;
  2677.     }
  2678.  
  2679.     Unexpected(31);
  2680.  
  2681.     if (bad) {
  2682.       Vprintf("%sCan't determine precision for %s expressions:\n%s%s\n",
  2683.          co, thing, "   check that you compiled without optimisation!",
  2684.          oc);
  2685.     } else if (eprec==dprec) {
  2686.       Vprintf("%s%s expressions are evaluated in double precision%s\n",
  2687.           co, Thing, oc);
  2688.     } else if (eprec==fprec) {
  2689.       Vprintf("%s%s expressions are evaluated in float precision%s\n",
  2690.           co, Thing, oc);
  2691.     } else if (eprec==lprec) {
  2692.       Vprintf("%s%s expressions are evaluated in long double precision%s\n",
  2693.           co, Thing, oc);
  2694.     } else {
  2695.         Vprintf("%s%s expressions are evaluated in a %s %s %d %s%s\n",
  2696.             co, Thing, eprec>dprec ? "higher" : "lower",
  2697.             "precision than double,\n   using",
  2698.             eprec, "base digits",
  2699.                 oc);
  2700.     }
  2701. }
  2702.  
  2703. #else /* not Number */
  2704.  
  2705. #ifdef FPROP /* Then create dummy routines for long double */
  2706. /* ARGSUSED */
  2707. int FPROP(bits_per_byte) int bits_per_byte; { return 0; }
  2708. #endif
  2709. #ifdef EPROP
  2710. /* ARGSUSED */
  2711. Procedure EPROP(fprec, dprec, lprec) int fprec, dprec, lprec; {}
  2712. #endif
  2713.  
  2714. #endif /* ifdef Number */
  2715.  
  2716. /* Increment the pass number */
  2717. #undef PASS
  2718.  
  2719. #ifdef PASS2
  2720. #undef PASS2
  2721. #define PASS 3
  2722. #define PASS3 1
  2723. #endif
  2724.  
  2725. #ifdef PASS1
  2726. #undef PASS1
  2727. #define PASS 2
  2728. #define PASS2 1
  2729. #endif
  2730.  
  2731. #ifdef PASS0
  2732. #undef PASS0
  2733. #endif
  2734.  
  2735. #ifdef PASS /* then rescan this file */
  2736. #ifdef NO_FILE
  2737. #include "enquire.c"
  2738. #else
  2739. #include FILENAME  /* if this line fails to compile, define NO_FILE */
  2740. #endif
  2741. #endif /* PASS */
  2742.  
  2743.