home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume06 / config2 < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  50.0 KB

  1. From decwrl!ucbvax!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!allbery Mon May  1 23:18:50 PDT 1989
  2. Article 865 of comp.sources.misc:
  3. Path: decwrl!ucbvax!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!allbery
  4. From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  5. Newsgroups: comp.sources.misc
  6. Subject: v06i096: config.c - everything you wanted to know about your C compiler
  7. Message-ID: <53488@uunet.UU.NET>
  8. Date: 30 Apr 89 22:04:50 GMT
  9. Sender: allbery@uunet.UU.NET
  10. Reply-To: steven@cwi.nl (Steven Pemberton)
  11. Organization: CWI, Amsterdam
  12. Lines: 1727
  13. Approved: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  14.  
  15. Posting-number: Volume 6, Issue 96
  16. Submitted-by: steven@cwi.nl (Steven Pemberton)
  17. Archive-name: config2
  18.  
  19. This is a new version of a program that I started writing a few years
  20. ago to automatically configure a large software system to the machine
  21. it was to run on. Earlier versions of the program were also
  22. distributed to the net.
  23.  
  24. The program tells you about lots of properties of your machine and C
  25. compiler, and this version optionally produces the ANSI C float.h and
  26. limits.h files. It also pinpoints some possible faults in your
  27. compiler, and offers the option of verifying that the compiler is
  28. correctly able to read the header files.
  29.  
  30. ---------------------- CUT HERE -------------------------------------
  31. #! /bin/sh
  32. # This file was wrapped with "dummyshar".  "sh" this file to extract.
  33. # Contents:  config.c
  34. echo extracting 'config.c'
  35. if test -f 'config.c' -a -z "$1"; then echo Not overwriting 'config.c'; else
  36. sed 's/^X//' << \EOF > 'config.c'
  37. X/* Everything you wanted to know about your machine and C compiler,
  38. X   but didn't know who to ask.
  39. X   Author: Steven Pemberton, CWI, Amsterdam; steven@cwi.nl
  40. X   Bugfixes and upgrades gratefully received.
  41. X
  42. X   Copyright (c) 1988, 1989 Steven Pemberton, CWI, Amsterdam.
  43. X   All rights reserved.
  44. X
  45. X   COMPILING
  46. X   With luck and a following wind, just the following will work:
  47. X    cc config.c -o config
  48. X
  49. X   If your compiler doesn't support:        add flag:
  50. X    signed char (eg pcc)            -DNO_SC
  51. X    unsigned char                -DNO_UC
  52. X    unsigned short and long            -DNO_UI
  53. X    signal(), or setjmp/longjmp()        -DNO_SIG
  54. X
  55. X   Try it first with no flags, and see if you get any errors - you might be
  56. X   surprised. (Most non-ANSI compilers need -DNO_SC, though.)
  57. X   Some compilers need a -f flag for floating point.
  58. X
  59. X   Don't use any optimisation flags: the program may not work if you do.
  60. X   Though "while (a+1.0-a-1.0 == 0.0)" may look like "while(1)" to an
  61. X   optimiser, to a floating-point unit there's a world of difference.
  62. X
  63. X   Some compilers offer various flags for different floating point
  64. X   modes; it's worth trying all possible combinations of these.
  65. X
  66. X   Add -DID=\"name\" if you want the machine/flags identified in the output.
  67. X
  68. X   SYSTEM DEPENDENCIES
  69. X   You may possibly need to add some calls to signal() for other sorts of
  70. X   exception on your machine than SIGFPE, and SIGOVER.  See lines beginning
  71. X   #ifdef SIGxxx in main() (and communicate the differences to me!).
  72. X
  73. X   If your C preprocessor doesn't have the predefined __FILE__ macro, and
  74. X   you want to call this file anything other than config.c, change the
  75. X   #define command for __FILE__ accordingly.  If it doesn't accept macro
  76. X   names at all in #include lines, order a new C compiler. While you're
  77. X   waiting for it to arrive, change the last #include in this file (the
  78. X   last but one line) accordingly.
  79. X
  80. X   OUTPUT
  81. X   Run without argument to get the information as English text.  If run
  82. X   with argument -l (e.g. config -l), output is a series of #define's for
  83. X   the ANSI standard limits.h include file, excluding MB_MAX_CHAR.  If run
  84. X   with argument -f, output is a series of #define's for the ANSI standard
  85. X   float.h include file.  Flag -v gives verbose output: output includes the
  86. X   English text above as C comments.  The program exit(0)'s if everything
  87. X   went ok, otherwise it exits with a positive number, telling how many
  88. X   problems there were.
  89. X
  90. X   VERIFYING THE COMPILER
  91. X   If, having produced the float.h and limits.h header files, you want to
  92. X   verify that the compiler reads them back correctly (there are a lot of
  93. X   boundary cases, of course, like minimum and maximum numbers), you can
  94. X   recompile config.c with -DVERIFY set (plus the other flags that you used
  95. X   when compiling the version that produced the header files).  This then
  96. X   recompiles the program so that it #includes "limits.h" and "float.h",
  97. X   and checks that the constants it finds there are the same as the
  98. X   constants it produces. Run the resulting program with config -fl.  As of
  99. X   this writing, of 21 compiler/flags combinations only 1 compiler has
  100. X   passed without error! (The honour goes to 'pcc' on an IBM RT.)
  101. X
  102. X   You can also use this option if your compiler already has both files,
  103. X   and you want to confirm that this program produces the right results.
  104. X
  105. X   TROUBLE SHOOTING.
  106. X   This program is now quite trustworthy, and suspicious and wrong output
  107. X   may well be caused by bugs in the compiler, not in the program (however
  108. X   of course, this is not guaranteed, and no responsibility can be
  109. X   accepted, etc.)
  110. X
  111. X   The program only works if overflows are ignored by the C system or
  112. X   are catchable with signal().
  113. X
  114. X   If the program fails to run to completion (often with the error message
  115. X   "Unexpected signal at point x"), this often turns out to be a bug in the
  116. X   C compiler's run-time system. Check what was about to be printed, and
  117. X   try to narrow the problem down.
  118. X
  119. X   Another possible problem is that you have compiled the program to produce
  120. X   loss-of-precision arithmetic traps. The program cannot cope with these,
  121. X   and you should re-compile without them. (They should never be the default).
  122. X
  123. X   Make sure you compiled with optimisation turned off.
  124. X
  125. X   Output preceded by *** WARNING: identifies behaviour of the C system
  126. X   deemed incorrect by the program. Likely problems are that printf or
  127. X   scanf don't cope properly with certain boundary numbers.  For each float
  128. X   and double that is printed, the printed value is checked that it is
  129. X   correct by using sscanf to read it back.  Care is taken that numbers are
  130. X   printed with enough digits to uniquely identify them, and therefore that
  131. X   they can be read back identically. If the number read back is different,
  132. X   the program prints a warning message. If the two numbers in the warning
  133. X   look identical, then printf is more than likely rounding the last
  134. X   digit(s) incorrectly.  To put you at ease that the two really are
  135. X   different, the bit patterns of the two numbers are also printed.  The
  136. X   difference is very likely in the last bit.  Many scanf's read the
  137. X   minimum double back as 0.0, and similarly cause overflow when reading
  138. X   the maximum double.  The program quite ruthlessly declares all these
  139. X   behaviours faulty.
  140. X
  141. X   The warning that "a cast didn't work" refers to cases like this:
  142. X
  143. X      float f;
  144. X      #define C 1.234567890123456789
  145. X      f= C;
  146. X      if (f != (float) C) printf ("Wrong!");
  147. X
  148. X   A faulty compiler will widen f to double and ignore the cast to float,
  149. X   and because there is more accuracy in a double than a float, fail to
  150. X   recognise that they are the same. In the actual case in point, f and C
  151. X   are passed as parameters to a function that discovers they are not equal,
  152. X   so it's just possible that the error was in the parameter passing,
  153. X   not in the cast (see function Validate()).
  154. X   For ANSI C, which has float constants, the error message is "constant has
  155. X   wrong precision".
  156. X
  157. X   REPORTING PROBLEMS
  158. X   If the program doesn't work for you for any reason that can't be
  159. X   narrowed down to a problem in the C compiler, or it has to be changed in
  160. X   order to get it to compile, or it produces suspicious output (like a very
  161. X   low maximum float, for instance), please mail the problem and an example
  162. X   of the incorrect output to steven@cwi.nl or mcvax!steven.uucp, so that
  163. X   improvements can be worked into future versions; mcvax/cwi.nl is the
  164. X   European backbone, and is connected to uunet and other fine hosts.
  165. X
  166. X   This version of the program is the first to try to catch and diagnose
  167. X   bugs in the compiler/run-time system. I would be especially pleased to
  168. X   have reports of failures so that I can improve this service.
  169. X
  170. X   I apologise unreservedly for the contorted use of the preprocessor...
  171. X
  172. X   THE SMALL PRINT
  173. X   You may copy and distribute verbatim copies of this source file.
  174. X
  175. X   You may modify this source file, and copy and distribute such
  176. X   modified versions, provided that you leave the copyright notice
  177. X   at the top of the file and also cause the modified file to carry
  178. X   prominent notices stating that you changed the files and the date
  179. X   of any change; and cause the whole of any work that you distribute
  180. X   or publish, that in whole or in part contains or is a derivative of
  181. X   this program or any part thereof, to be licensed at no charge to
  182. X   all third parties on terms identical to those here.
  183. X
  184. X   If you do have a fix to any problem, please send it to me, so that
  185. X   other people can have the benefits.
  186. X
  187. X   While every effort has been taken to make this program as reliable as
  188. X   possible, no responsibility can be taken for the correctness of the
  189. X   output, or suitability for any particular use.
  190. X
  191. X   ACKNOWLEDGEMENTS
  192. X   Many people have given time and ideas to making this program what it is.
  193. X   To all of them thanks, and apologies for not mentioning them by name.
  194. X*/
  195. X
  196. X#ifndef __FILE__
  197. X#define __FILE__ "config.c"
  198. X#endif
  199. X
  200. X#ifndef PASS
  201. X#define PASS 1
  202. X#define PASS1 1
  203. X#define VERSION "4.1"
  204. X
  205. X/* Procedure just marks the functions that don't return a result */
  206. X#ifdef Procedure
  207. X#undef Procedure
  208. X#endif
  209. X#define Procedure
  210. X
  211. X#define Vprintf if (V) printf
  212. X
  213. X/* stdc is used in tests like if (stdc) */
  214. X#ifdef __STDC__
  215. X#define stdc 1
  216. X#else
  217. X#define stdc 0
  218. X#endif
  219. X
  220. X/* volatile is used to reduce the chance of optimisation,
  221. X   and to prevent variables being put in registers (when setjmp/longjmp
  222. X   wouldn't work as we want)  */
  223. X#ifndef __STDC__
  224. X#define volatile static
  225. X#endif
  226. X
  227. X#include <stdio.h>
  228. X
  229. X#ifdef VERIFY
  230. X#include "limits.h"
  231. X#include "float.h"
  232. X#endif
  233. X
  234. X#ifdef NO_SIG  /* There's no signal(), or setjmp/longjmp() */
  235. X
  236. X    /* Dummy routines instead */
  237. X    int lab=1;
  238. X    int setjmp(lab) int lab; { return(0); }
  239. X    signal(i, p) int i, (*p)(); {}
  240. X
  241. X#else
  242. X
  243. X#include <signal.h>
  244. X#include <setjmp.h>
  245. X
  246. X    jmp_buf lab;
  247. X    overflow(sig) int sig; { /* what to do on overflow/underflow */
  248. X        signal(sig, overflow);
  249. X        longjmp(lab, 1);
  250. X    }
  251. X
  252. X#endif /*NO_SIG*/
  253. X
  254. X#define Unexpected(place) if (setjmp(lab)!=0) croak(place)
  255. X
  256. Xint V= 0,    /* verbose */
  257. X    L= 0,    /* produce limits.h */
  258. X    F= 0,    /* produce float.h  */
  259. X    bugs=0;    /* The number of (possible) bugs in the output */
  260. X
  261. Xchar co[4], oc[4]; /* Comment starter and ender symbols */
  262. X
  263. Xint bits_per_byte; /* the number of bits per unit returned by sizeof() */
  264. X
  265. X#ifdef TEST
  266. X/* Set the fp modes on a SUN with 68881 chip, to check that different
  267. X   rounding modes etc. get properly detected.
  268. X   Compile with additional flag -DTEST, and run with additional parameter
  269. X   +hex-number, to set the 68881 mode register to hex-number
  270. X*/
  271. X
  272. X/* Bits 0x30 = rounding mode: */
  273. X#define ROUND_BITS    0x30
  274. X#define TO_NEAREST    0x00
  275. X#define TO_ZERO        0x10
  276. X#define TO_MINUS_INF    0x20
  277. X#define TO_PLUS_INF    0x30 /* The SUN FP user's guide seems to be wrong here */
  278. X
  279. X/* Bits 0xc0 = extended rounding: */
  280. X#define EXT_BITS    0xc0
  281. X#define ROUND_EXTENDED    0x00
  282. X#define ROUND_SINGLE     0x40
  283. X#define ROUND_DOUBLE    0x80
  284. X
  285. X/* Enabled traps: */
  286. X#define EXE_INEX1  0x100
  287. X#define EXE_INEX2  0x200
  288. X#define EXE_DZ       0x400
  289. X#define EXE_UNFL   0x800
  290. X#define EXE_OVFL  0x1000
  291. X#define EXE_OPERR 0x2000
  292. X#define EXE_SNAN  0x4000
  293. X#define EXE_BSUN  0x8000
  294. X
  295. Xprintmode(new) unsigned new; {
  296. X    fpmode_(&new);
  297. X    printf("New fp mode:\n");
  298. X    printf("  Round toward ");
  299. X    switch (new & ROUND_BITS) {
  300. X          case TO_NEAREST:   printf("nearest"); break;
  301. X          case TO_ZERO:      printf("zero"); break;
  302. X          case TO_MINUS_INF: printf("minus infinity"); break;
  303. X          case TO_PLUS_INF:  printf("plus infinity"); break;
  304. X          default: printf("???"); break;
  305. X    }
  306. X
  307. X    printf("\n  Extended rounding precision: ");
  308. X
  309. X    switch (new & EXT_BITS) {
  310. X          case ROUND_EXTENDED: printf("extended"); break;
  311. X          case ROUND_SINGLE:   printf("single"); break;
  312. X          case ROUND_DOUBLE:   printf("double"); break;
  313. X          default: printf("???"); break;
  314. X    }
  315. X
  316. X    printf("\n  Enabled exceptions:");
  317. X    if (new & (unsigned) EXE_INEX1) printf(" inex1");
  318. X    if (new & (unsigned) EXE_INEX2) printf(" inex2");
  319. X    if (new & (unsigned) EXE_DZ)    printf(" dz"); 
  320. X    if (new & (unsigned) EXE_UNFL)  printf(" unfl"); 
  321. X    if (new & (unsigned) EXE_OVFL)  printf(" ovfl"); 
  322. X    if (new & (unsigned) EXE_OPERR) printf(" operr"); 
  323. X    if (new & (unsigned) EXE_SNAN)  printf(" snan"); 
  324. X    if (new & (unsigned) EXE_BSUN)  printf(" bsun"); 
  325. X    printf("\n");
  326. X}
  327. X
  328. Xint setmode(s) char *s; {
  329. X    unsigned mode=0, dig;
  330. X    char c;
  331. X
  332. X    while (*s) {
  333. X        c= *s++;
  334. X        if  (c>='0' && c<='9') dig= c-'0';
  335. X        else if (c>='a' && c<='f') dig= c-'a'+10;
  336. X        else if (c>='A' && c<='F') dig= c-'A'+10;
  337. X        else return 1;
  338. X        mode= mode<<4 | dig;
  339. X    }
  340. X    printmode(mode);
  341. X    return 0;
  342. X}
  343. X#else
  344. Xint setmode(s) char *s; {
  345. X    fprintf(stderr, "Can't set mode: not compiled with TEST\n");
  346. X    return(1);
  347. X}
  348. X#endif
  349. X
  350. Xcroak(place) int place; {
  351. X    printf("*** Unexpected signal at point %d\n", place);
  352. X    exit(bugs+1); /* An exit isn't essential here, but avoids loops */
  353. X}
  354. X
  355. Xmain(argc, argv) int argc; char *argv[]; {
  356. X    int dprec, fprec, lprec, basic(), fprop(), dprop(), efprop(), edprop();
  357. X    char *malloc();
  358. X    unsigned int size;
  359. X    long total;
  360. X    int i; char *s; int bad;
  361. X
  362. X#ifdef SIGFPE
  363. X    signal(SIGFPE, overflow);
  364. X#endif
  365. X#ifdef SIGOVER
  366. X    signal(SIGOVER, overflow);
  367. X#endif
  368. X/* Add more calls as necessary */
  369. X
  370. X    Unexpected(1);
  371. X
  372. X    bad=0;
  373. X    for (i=1; i < argc; i++) {
  374. X        s= argv[i];
  375. X        if (*s == '-') {
  376. X            s++;
  377. X            while (*s) {
  378. X                switch (*(s++)) {
  379. X                      case 'v': V=1; break;
  380. X                      case 'l': L=1; break;
  381. X                      case 'f': F=1; break;
  382. X                      default: bad=1; break;
  383. X                }
  384. X            }
  385. X        } else if (*s == '+') {
  386. X            s++;
  387. X            bad= setmode(s);
  388. X        } else bad= 1;
  389. X    }
  390. X    if (bad) {
  391. X        fprintf(stderr,
  392. X            "Usage: %s [-vlf]\n  v=Verbose l=Limits.h f=Float.h\n",
  393. X            argv[0]);
  394. X        exit(1);
  395. X    }
  396. X    if (L || F) {
  397. X        co[0]= '/'; oc[0]= ' ';
  398. X        co[1]= '*'; oc[1]= '*';
  399. X        co[2]= ' '; oc[2]= '/';
  400. X        co[3]= '\0'; oc[3]= '\0';
  401. X    } else {
  402. X        co[0]= '\0'; oc[0]= '\0';
  403. X        V=1;
  404. X    }
  405. X
  406. X    if (L) printf("%slimits.h%s\n", co, oc);
  407. X    if (F) printf("%sfloat.h%s\n", co, oc);
  408. X#ifdef ID
  409. X    printf("%sProduced on %s by config version %s, CWI, Amsterdam%s\n",
  410. X           co, ID, VERSION, oc);
  411. X#else
  412. X    printf("%sProduced by config version %s, CWI, Amsterdam%s\n",
  413. X           co, VERSION, oc);
  414. X#endif
  415. X
  416. X#ifdef VERIFY
  417. X    printf("%sVerification phase%s\n", co, oc);
  418. X#endif
  419. X
  420. X#ifdef NO_SIG
  421. X    Vprintf("%sCompiled without signal(): %s%s\n",
  422. X        co,
  423. X        "there's nothing that can be done if overflow occurs",
  424. X        oc);
  425. X#endif
  426. X#ifdef NO_SC
  427. X    Vprintf("%sCompiled without signed char%s\n", co, oc);
  428. X#endif
  429. X#ifdef NO_UC
  430. X    Vprintf("%Compiled without unsigned char%s\n", co, oc);
  431. X#endif
  432. X#ifdef NO_UI
  433. X    Vprintf("%Compiled without unsigned short or long%s\n", co, oc);
  434. X#endif
  435. X#ifdef __STDC__
  436. X    Vprintf("%sCompiler claims to be ANSI C level %d%s\n",
  437. X        co, __STDC__, oc);
  438. X#else
  439. X    Vprintf("%sCompiler does not claim to be ANSI C%s\n", co, oc);
  440. X#endif
  441. X    printf("\n");
  442. X    bits_per_byte= basic();
  443. X    Vprintf("\n");
  444. X    if (F||V) {
  445. X        fprec= fprop(bits_per_byte);
  446. X        dprec= dprop(bits_per_byte);
  447. X        lprec= ldprop(bits_per_byte);
  448. X        efprop(fprec, dprec, lprec);
  449. X        edprop(fprec, dprec, lprec);
  450. X        eldprop(fprec, dprec, lprec);
  451. X    }
  452. X    if (V) {
  453. X        /* An extra goody: the approximate amount of data-space */
  454. X        /* Allocate store until no more available */
  455. X        size=1<<((bits_per_byte*sizeof(int))-2);
  456. X        total=0;
  457. X        while (size!=0) {
  458. X            while (malloc(size)!=(char *)NULL) total+=(size/2);
  459. X            size/=2;
  460. X        }
  461. X
  462. X        Vprintf("%sMemory mallocatable ~= %ld Kbytes%s\n",
  463. X            co, (total+511)/512, oc);
  464. X    }
  465. X    exit(bugs);
  466. X}
  467. X
  468. XProcedure eek_a_bug(problem) char *problem; {
  469. X    printf("\n%s*** WARNING: %s%s\n", co, problem, oc);
  470. X    bugs++;
  471. X}
  472. X
  473. XProcedure i_define(sort, name, val, req) char *sort, *name; long val, req; {
  474. X    if (val >= 0) {
  475. X        printf("#define %s%s %ld\n", sort, name, val);
  476. X    } else {
  477. X        printf("#define %s%s (%ld)\n", sort, name, val);
  478. X    }
  479. X    if (val != req) {
  480. X        printf("%s*** Verify failed for above #define!\n", co);
  481. X        printf("       Compiler has %ld for value%s\n\n", req, oc);
  482. X        bugs++;
  483. X    }
  484. X    Vprintf("\n");
  485. X}
  486. X
  487. X#ifndef NO_UI
  488. X
  489. X#ifdef __STDC__
  490. X#define U "U"
  491. X#else
  492. X#define U ""
  493. X#endif
  494. X
  495. XProcedure u_define(sort, name, val, req) char *sort, *name; unsigned long val, req; {
  496. X    printf("#define %s%s %lu%s\n", sort, name, val, U);
  497. X    if (val != req) {
  498. X        printf("%s*** Verify failed for above #define!\n", co);
  499. X        printf("       Compiler has %lu for value%s\n\n", req, oc);
  500. X        bugs++;
  501. X    }
  502. X    Vprintf("\n");
  503. X}
  504. X#endif
  505. X
  506. X/* Long_double is the longest floating point type available: */
  507. X#ifdef __STDC__
  508. X#define Long_double long double
  509. X#else
  510. X#define Long_double double
  511. X#endif
  512. X
  513. Xchar *f_rep();
  514. X
  515. XProcedure f_define(sort, name, precision, val, mark)
  516. X     char *sort, *name; int precision; Long_double val; char *mark; {
  517. X    if (stdc) {
  518. X        printf("#define %s%s %s%s\n",
  519. X               sort, name, f_rep(precision, val), mark);
  520. X    } else if (*mark == 'F') {
  521. X        /* non-ANSI C has no float constants, so cast the constant */
  522. X        printf("#define %s%s ((float)%s)\n",
  523. X               sort, name, f_rep(precision, val));
  524. X    } else {
  525. X        printf("#define %s%s %s\n", sort, name, f_rep(precision, val));
  526. X    }
  527. X    Vprintf("\n");
  528. X}
  529. X
  530. Xint floor_log(base, x) int base; Long_double x; { /* return floor(log base(x)) */
  531. X    int r=0;
  532. X    while (x>=base) { r++; x/=base; }
  533. X    return r;
  534. X}
  535. X
  536. Xint ceil_log(base, x) int base; Long_double x; {
  537. X    int r=0;
  538. X    while (x>1.0) { r++; x/=base; }
  539. X    return r;
  540. X}
  541. X
  542. Xint exponent(x, fract, exp) Long_double x; double *fract; int *exp; {
  543. X    /* Split x into a fraction and a power of ten;
  544. X       returns 0 if x is unusable, 1 otherwise.
  545. X       Only used for error messages about faulty output.
  546. X    */
  547. X    int r=0, neg=0;
  548. X    Long_double old;
  549. X    *fract=0.0; *exp=0;
  550. X    if (x<0.0) {
  551. X        x= -x;
  552. X        neg= 1;
  553. X    }
  554. X    if (x==0.0) return 1;
  555. X    if (x>=10.0) {
  556. X        while (x>=10.0) {
  557. X            old=x; r++; x/=10.0;
  558. X            if (old==x) return 0;
  559. X        }
  560. X    } else {
  561. X        while (x<1.0) {
  562. X            old=x; r--; x*=10.0;
  563. X            if (old==x) return 0;
  564. X        }
  565. X    }
  566. X    if (neg) *fract= -x;
  567. X    else *fract=x;
  568. X    *exp=r;
  569. X    return 1;
  570. X}
  571. X
  572. X#define fabs(x) (((x)<0.0)?(-x):(x))
  573. X
  574. Xchar *f_rep(precision, val) int precision; Long_double val; {
  575. X    static char buf[1024];
  576. X    char *f1;
  577. X    if (sizeof(double) == sizeof(Long_double)) {
  578. X        /* Assume they're the same, and use non-stdc format */
  579. X        /* This is for stdc compilers using non-stdc libraries */
  580. X        f1= "%.*e";
  581. X    } else {
  582. X        /* It had better support Le then */
  583. X        f1= "%.*Le";
  584. X    }
  585. X    sprintf(buf, f1, precision, val);
  586. X    return buf;
  587. X}
  588. X
  589. XProcedure bitpattern(p, size) char *p; int size; {
  590. X    char c;
  591. X    int i, j;
  592. X
  593. X    for (i=1; i<=size; i++) {
  594. X        c= *p;
  595. X        p++;
  596. X        for (j=bits_per_byte-1; j>=0; j--)
  597. X            printf("%c", (c>>j)&1 ? '1' : '0');
  598. X        if (i!=size) printf(" ");
  599. X    }
  600. X}
  601. X
  602. X#define Order(x, px, mode)\
  603. X   printf("%s    %s ", co, mode); for (i=0; i<sizeof(x); i++) px[i]= c[i]; \
  604. X   for (i=1; i<=sizeof(x); i++) { putchar((char)((x>>(bits_per_byte*(sizeof(x)-i)))&mask)); }\
  605. X   printf("%s\n", oc);
  606. X
  607. XProcedure endian(bits_per_byte) int bits_per_byte; {
  608. X    /*unsigned*/ short s=0;
  609. X    /*unsigned*/ int j=0;
  610. X    /*unsigned*/ long l=0;
  611. X
  612. X    char *ps= (char *) &s,
  613. X         *pj= (char *) &j,
  614. X         *pl= (char *) &l,
  615. X         *c= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  616. X    unsigned int mask, i;
  617. X
  618. X    mask=0;
  619. X    for (i=1; i<=bits_per_byte; i++) mask= (mask<<1)|1;
  620. X
  621. X    if (V) {
  622. X        printf("%sCharacter order:%s\n", co, oc);
  623. X        Order(s, ps, "short:");
  624. X        Order(j, pj, "int:  ");
  625. X        Order(l, pl, "long: ");
  626. X    }
  627. X}
  628. X
  629. X#ifdef VERIFY
  630. X#ifndef SCHAR_MAX
  631. X#define SCHAR_MAX char_max
  632. X#define SCHAR_MIN char_min
  633. X#endif
  634. X#ifndef UCHAR_MAX
  635. X#define UCHAR_MAX char_max
  636. X#endif
  637. X#else
  638. X#define CHAR_BIT char_bit
  639. X#define CHAR_MAX char_max
  640. X#define CHAR_MIN char_min
  641. X#define SCHAR_MAX char_max
  642. X#define SCHAR_MIN char_min
  643. X#define UCHAR_MAX char_max
  644. X#endif /* VERIFY */
  645. X
  646. Xint cprop() { /* Properties of character */
  647. X    volatile char c, char_max, char_min;
  648. X    volatile int bits_per_byte, is_signed;
  649. X    long char_bit;
  650. X
  651. X    Unexpected(2);
  652. X
  653. X    /* Calculate number of bits per character *************************/
  654. X    c=1; bits_per_byte=0;
  655. X    do { c=c<<1; bits_per_byte++; } while(c!=0);
  656. X    c= (char)(-1);
  657. X    if (((int)c)<0) is_signed=1;
  658. X    else is_signed=0;
  659. X    Vprintf("%sChar = %d bits, %ssigned%s\n",
  660. X        co, (int)sizeof(c)*bits_per_byte, (is_signed?"":"un"), oc);
  661. X    char_bit=(long)(sizeof(c)*bits_per_byte);
  662. X    if (L) i_define("CHAR", "_BIT", char_bit, (long) CHAR_BIT);
  663. X
  664. X    c=0; char_max=0;
  665. X    c++;
  666. X    if (setjmp(lab)==0) { /* Yields char_max */
  667. X        while (c>char_max) {
  668. X            char_max=c;
  669. X            c++;
  670. X        }
  671. X    } else {
  672. X        Vprintf("%sCharacter overflow generates a trap!%s\n", co, oc);
  673. X    }
  674. X    c=0; char_min=0;
  675. X    c--;
  676. X    if (setjmp(lab)==0) { /* Yields char_min */
  677. X        while (c<char_min) {
  678. X            char_min=c;
  679. X            c--;
  680. X        }
  681. X    }
  682. X    Unexpected(3);
  683. X
  684. X    if (L) {
  685. X        i_define("CHAR", "_MAX", (long) char_max, (long) CHAR_MAX);
  686. X        i_define("CHAR", "_MIN", (long) char_min, (long) CHAR_MIN);
  687. X        if (is_signed) {
  688. X            i_define("SCHAR", "_MAX", (long) char_max,
  689. X                 (long) SCHAR_MAX);
  690. X            i_define("SCHAR", "_MIN", (long) char_min,
  691. X                 (long) SCHAR_MIN);
  692. X        } else {
  693. X            i_define("UCHAR", "_MAX", (long) char_max,
  694. X                 (long) UCHAR_MAX);
  695. X        }
  696. X
  697. X        if (is_signed) {
  698. X#ifndef NO_UC
  699. X            volatile unsigned char c, char_max;
  700. X            c=0; char_max=0;
  701. X            c++;
  702. X            if (setjmp(lab)==0) { /* Yields char_max */
  703. X                while (c>char_max) {
  704. X                    char_max=c;
  705. X                    c++;
  706. X                }
  707. X            }
  708. X            Unexpected(4);
  709. X            i_define("UCHAR", "_MAX", (long) char_max,
  710. X                 (long) UCHAR_MAX);
  711. X#endif
  712. X        } else {
  713. X#ifndef NO_SC /* Define NO_SC if the next line gives a syntax error */
  714. X            volatile signed char c, char_max, char_min;
  715. X            c=0; char_max=0;
  716. X            c++;
  717. X            if (setjmp(lab)==0) { /* Yields char_max */
  718. X                while (c>char_max) {
  719. X                    char_max=c;
  720. X                    c++;
  721. X                }
  722. X            }
  723. X            c=0; char_min=0;
  724. X            c--;
  725. X            if (setjmp(lab)==0) { /* Yields char_min */
  726. X                while (c<char_min) {
  727. X                    char_min=c;
  728. X                    c--;
  729. X                }
  730. X            }
  731. X            Unexpected(5);
  732. X            i_define("SCHAR", "_MIN", (long) char_min,
  733. X                 (long) SCHAR_MIN);
  734. X            i_define("SCHAR", "_MAX", (long) char_max,
  735. X                 (long) SCHAR_MAX);
  736. X#endif /* NO_SC */
  737. X        }
  738. X    }
  739. X    return bits_per_byte;
  740. X}
  741. X
  742. Xint basic() {
  743. X    /* The properties of the basic types.
  744. X       Returns number of bits per sizeof unit */
  745. X    volatile int bits_per_byte;
  746. X
  747. X    bits_per_byte= cprop();
  748. X
  749. X    /* Shorts, ints and longs *****************************************/
  750. X    Vprintf("%sShort=%d int=%d long=%d float=%d double=%d bits %s\n",
  751. X        co,
  752. X        (int) sizeof(short)*bits_per_byte,
  753. X        (int) sizeof(int)*bits_per_byte,
  754. X        (int) sizeof(long)*bits_per_byte,
  755. X        (int) sizeof(float)*bits_per_byte,
  756. X        (int) sizeof(double)*bits_per_byte, oc);
  757. X    if (stdc) {
  758. X        Vprintf("%sLong double=%d bits%s\n",
  759. X            co, (int) sizeof(Long_double)*bits_per_byte, oc);
  760. X    }
  761. X    Vprintf("%sChar pointers = %d bits%s%s\n",
  762. X        co, (int)sizeof(char *)*bits_per_byte,
  763. X        sizeof(char *)>sizeof(int)?" BEWARE! larger than int!":"",
  764. X        oc);
  765. X    Vprintf("%sInt pointers = %d bits%s%s\n",
  766. X        co, (int)sizeof(int *)*bits_per_byte,
  767. X        sizeof(int *)>sizeof(int)?" BEWARE! larger than int!":"",
  768. X        oc);
  769. X    sprop();
  770. X    iprop();
  771. X    lprop();
  772. X    usprop();
  773. X    uiprop();
  774. X    ulprop();
  775. X
  776. X    Unexpected(6);
  777. X
  778. X    /* Alignment constants ********************************************/
  779. X    Vprintf("%sAlignments used for char=%d short=%d int=%d long=%d%s\n",
  780. X        co,
  781. X        (int)sizeof(struct{char i1; char c1;})-(int)sizeof(char),
  782. X        (int)sizeof(struct{short i2; char c2;})-(int)sizeof(short),
  783. X        (int)sizeof(struct{int i3; char c3;})-(int)sizeof(int),
  784. X        (int)sizeof(struct{long i4; char c4;})-(int)sizeof(long),
  785. X        oc);
  786. X
  787. X    /* Ten little endians *********************************************/
  788. X
  789. X    endian(bits_per_byte);
  790. X
  791. X    /* Pointers *******************************************************/
  792. X    if (V) {
  793. X        if ("abcd"=="abcd")
  794. X            printf("%sStrings are shared%s\n", co, oc);
  795. X        else printf("%sStrings are not shared%s\n", co, oc);
  796. X    }
  797. X
  798. X    return bits_per_byte;
  799. X}
  800. X
  801. X#endif /* ifndef PASS */
  802. X
  803. X/* As I said, I apologise for the contortions below. The functions are
  804. X   expanded by the preprocessor twice or three times (for float and double,
  805. X   and maybe for long double, and for short, int and long). That way,
  806. X   I never make a change to one that I forget to make to the other.
  807. X   You can look on it as C's fault for not supporting multi-line macro's.
  808. X   This whole file is read 3 times by the preprocessor, with PASSn set for
  809. X   n=1, 2 or 3, to decide which parts to reprocess.
  810. X*/
  811. X
  812. X/* #undef on an already undefined thing is (wrongly) flagged as an error
  813. X   by some compilers, therefore the #ifdef that follows: 
  814. X*/
  815. X#ifdef Number
  816. X#undef Number
  817. X#undef THING
  818. X#undef Thing
  819. X#undef thing
  820. X#undef FPROP
  821. X#undef Fname
  822. X#undef Store
  823. X#undef Sum
  824. X#undef Diff
  825. X#undef Mul
  826. X#undef Div
  827. X#undef Self
  828. X#undef F_check
  829. X#undef Validate
  830. X#undef EPROP
  831. X#undef MARK
  832. X
  833. X#undef F_RADIX
  834. X#undef F_MANT_DIG
  835. X#undef F_DIG
  836. X#undef F_ROUNDS
  837. X#undef F_EPSILON
  838. X#undef F_MIN_EXP
  839. X#undef F_MIN
  840. X#undef F_MIN_10_EXP
  841. X#undef F_MAX_EXP
  842. X#undef F_MAX
  843. X#undef F_MAX_10_EXP
  844. X#endif
  845. X
  846. X#ifdef Integer
  847. X#undef Integer
  848. X#undef INT
  849. X#undef IPROP
  850. X#undef Iname
  851. X#undef UPROP
  852. X#undef Uname
  853. X#undef OK_UI
  854. X
  855. X#undef I_MAX
  856. X#undef I_MIN
  857. X#undef U_MAX
  858. X#endif
  859. X
  860. X#ifdef PASS1
  861. X
  862. X#define Number float
  863. X#define THING "FLOAT"
  864. X#define Thing "Float"
  865. X#define thing "float"
  866. X#define Fname "FLT"
  867. X#define FPROP fprop
  868. X#define Store fStore
  869. X#define Sum fSum
  870. X#define Diff fDiff
  871. X#define Mul fMul
  872. X#define Div fDiv
  873. X#define Self fSelf
  874. X#define F_check fCheck
  875. X#define Validate fValidate
  876. X#define MARK "F"
  877. X
  878. X#define EPROP efprop
  879. X
  880. X#define Integer short
  881. X#define INT "short"
  882. X#define IPROP sprop
  883. X#define Iname "SHRT"
  884. X#ifndef NO_UI
  885. X#define OK_UI 1
  886. X#endif
  887. X
  888. X#define UPROP usprop
  889. X#define Uname "USHRT"
  890. X
  891. X#ifdef VERIFY
  892. X#define I_MAX SHRT_MAX
  893. X#define I_MIN SHRT_MIN
  894. X#define U_MAX USHRT_MAX
  895. X
  896. X#define F_RADIX FLT_RADIX
  897. X#define F_MANT_DIG FLT_MANT_DIG
  898. X#define F_DIG FLT_DIG
  899. X#define F_ROUNDS FLT_ROUNDS
  900. X#define F_EPSILON FLT_EPSILON
  901. X#define F_MIN_EXP FLT_MIN_EXP
  902. X#define F_MIN FLT_MIN
  903. X#define F_MIN_10_EXP FLT_MIN_10_EXP
  904. X#define F_MAX_EXP FLT_MAX_EXP
  905. X#define F_MAX FLT_MAX
  906. X#define F_MAX_10_EXP FLT_MAX_10_EXP
  907. X#endif /* VERIFY */
  908. X
  909. X#endif /* PASS1 */
  910. X
  911. X#ifdef PASS2
  912. X
  913. X#define Number double
  914. X#define THING "DOUBLE"
  915. X#define Thing "Double"
  916. X#define thing "double"
  917. X#define Fname "DBL"
  918. X#define FPROP dprop
  919. X#define Store dStore
  920. X#define Sum dSum
  921. X#define Diff dDiff
  922. X#define Mul dMul
  923. X#define Div dDiv
  924. X#define Self dSelf
  925. X#define F_check dCheck
  926. X#define Validate dValidate
  927. X#define MARK ""
  928. X
  929. X#define EPROP edprop
  930. X
  931. X#define Integer int
  932. X#define INT "int"
  933. X#define IPROP iprop
  934. X#define Iname "INT"
  935. X#define OK_UI 1 /* Unsigned int is always possible */
  936. X
  937. X#define UPROP uiprop
  938. X#define Uname "UINT"
  939. X
  940. X#ifdef VERIFY
  941. X#define I_MAX INT_MAX
  942. X#define I_MIN INT_MIN
  943. X#define U_MAX UINT_MAX
  944. X
  945. X#define F_MANT_DIG DBL_MANT_DIG
  946. X#define F_DIG DBL_DIG
  947. X#define F_EPSILON DBL_EPSILON
  948. X#define F_MIN_EXP DBL_MIN_EXP
  949. X#define F_MIN DBL_MIN
  950. X#define F_MIN_10_EXP DBL_MIN_10_EXP
  951. X#define F_MAX_EXP DBL_MAX_EXP
  952. X#define F_MAX DBL_MAX
  953. X#define F_MAX_10_EXP DBL_MAX_10_EXP
  954. X#endif /* VERIFY */
  955. X
  956. X#endif /* PASS2 */
  957. X
  958. X#ifdef PASS3
  959. X
  960. X#ifdef __STDC__
  961. X#define Number long double
  962. X#endif
  963. X
  964. X#define THING "LONG DOUBLE"
  965. X#define Thing "Long double"
  966. X#define thing "long double"
  967. X#define Fname "LDBL"
  968. X#define FPROP ldprop
  969. X#define Store ldStore
  970. X#define Sum ldSum
  971. X#define Diff ldDiff
  972. X#define Mul ldMul
  973. X#define Div ldDiv
  974. X#define Self ldSelf
  975. X#define F_check ldCheck
  976. X#define Validate ldValidate
  977. X#define MARK "L"
  978. X
  979. X#define EPROP eldprop
  980. X
  981. X#define Integer long
  982. X#define INT "long"
  983. X#define IPROP lprop
  984. X#define Iname "LONG"
  985. X#ifndef NO_UI
  986. X#define OK_UI 1
  987. X#endif
  988. X
  989. X#define UPROP ulprop
  990. X#define Uname "ULONG"
  991. X
  992. X#ifdef VERIFY
  993. X#define I_MAX LONG_MAX
  994. X#define I_MIN LONG_MIN
  995. X#define U_MAX ULONG_MAX
  996. X
  997. X#define F_MANT_DIG LDBL_MANT_DIG
  998. X#define F_DIG LDBL_DIG
  999. X#define F_EPSILON LDBL_EPSILON
  1000. X#define F_MIN_EXP LDBL_MIN_EXP
  1001. X#define F_MIN LDBL_MIN
  1002. X#define F_MIN_10_EXP LDBL_MIN_10_EXP
  1003. X#define F_MAX_EXP LDBL_MAX_EXP
  1004. X#define F_MAX LDBL_MAX
  1005. X#define F_MAX_10_EXP LDBL_MAX_10_EXP
  1006. X#endif /* VERIFY */
  1007. X
  1008. X#endif /* PASS3 */
  1009. X
  1010. X#ifndef VERIFY
  1011. X#define I_MAX int_max
  1012. X#define I_MIN int_min
  1013. X#define U_MAX int_max
  1014. X
  1015. X#define F_RADIX f_radix
  1016. X#define F_MANT_DIG f_mant_dig
  1017. X#define F_DIG f_dig
  1018. X#define F_ROUNDS f_rounds
  1019. X#define F_EPSILON f_epsilon
  1020. X#define F_MIN_EXP f_min_exp
  1021. X#define F_MIN f_min
  1022. X#define F_MIN_10_EXP f_min_10_exp
  1023. X#define F_MAX_EXP f_max_exp
  1024. X#define F_MAX f_max
  1025. X#define F_MAX_10_EXP f_max_10_exp
  1026. X#endif
  1027. X
  1028. XProcedure IPROP() { /* for short, int, and long */
  1029. X    volatile Integer newi, int_max, maxeri, int_min, minneri;
  1030. X    volatile int ibits, ipower, two=2;
  1031. X
  1032. X    /* Calculate max short/int/long ***********************************/
  1033. X    /* Calculate 2**n-1 until overflow - then use the previous value  */
  1034. X
  1035. X    newi=1; int_max=0;
  1036. X
  1037. X    if (setjmp(lab)==0) { /* Yields int_max */
  1038. X        for(ipower=0; newi>int_max; ipower++) {
  1039. X            int_max=newi;
  1040. X            newi=newi*two+1;
  1041. X        }
  1042. X        Vprintf("%sOverflow of a%s %s does not generate a trap%s\n",
  1043. X            co, INT[0]=='i'?"n":"", INT, oc);
  1044. X    } else {
  1045. X        Vprintf("%sOverflow of a%s %s generates a trap%s\n",
  1046. X            co, INT[0]=='i'?"n":"", INT, oc);
  1047. X    }
  1048. X    Unexpected(7);
  1049. X
  1050. X    /* Minimum value: assume either two's or one's complement *********/
  1051. X    int_min= -int_max;
  1052. X    if (setjmp(lab)==0) { /* Yields int_min */
  1053. X        if (int_min-1 < int_min) int_min--;
  1054. X    }
  1055. X    Unexpected(8);
  1056. X
  1057. X    /* Now for those daft Cybers: */
  1058. X
  1059. X    maxeri=0; newi=int_max;
  1060. X
  1061. X    if (setjmp(lab)==0) { /* Yields maxeri */
  1062. X        for(ibits=ipower; newi>maxeri; ibits++) {
  1063. X            maxeri=newi;
  1064. X            newi=newi+newi+1;
  1065. X        }
  1066. X    }
  1067. X    Unexpected(9);
  1068. X
  1069. X    minneri= -maxeri;
  1070. X    if (setjmp(lab)==0) { /* Yields minneri */
  1071. X        if (minneri-1 < minneri) minneri--;
  1072. X    }
  1073. X    Unexpected(10);
  1074. X
  1075. X    Vprintf("%sMaximum %s = %ld (= 2**%d-1)%s\n",
  1076. X        co, INT, (long)int_max, ipower, oc);
  1077. X    Vprintf("%sMinimum %s = %ld%s\n", co, INT, (long)int_min, oc);
  1078. X
  1079. X    if (L) i_define(Iname, "_MAX", (long) int_max, (long) I_MAX);
  1080. X    if (L) i_define(Iname, "_MIN", (long) int_min, (long) I_MIN);
  1081. X
  1082. X    if (maxeri>int_max) {
  1083. X        Vprintf("%sThere is a larger %s, %ld (= 2**%d-1), %s %s%s\n",
  1084. X            co, INT, (long)maxeri, ibits, 
  1085. X            "but only for addition, not multiplication",
  1086. X            "(I smell a Cyber!)",
  1087. X            oc);
  1088. X    }
  1089. X
  1090. X    if (minneri<int_min) {
  1091. X        Vprintf("%sThere is a smaller %s, %ld, %s %s%s\n",
  1092. X            co, INT, (long)minneri, 
  1093. X            "but only for addition, not multiplication",
  1094. X            "(I smell a Cyber!)",
  1095. X            oc);
  1096. X    }
  1097. X}
  1098. X
  1099. XProcedure UPROP () { /* unsigned short/int/long */
  1100. X#ifdef OK_UI
  1101. X    volatile unsigned Integer int_max, newi, two;
  1102. X    newi=1; int_max=0; two=2;
  1103. X
  1104. X    if (setjmp(lab)==0) { /* Yields int_max */
  1105. X        while(newi>int_max) {
  1106. X            int_max=newi;
  1107. X            newi=newi*two+1;
  1108. X        }
  1109. X    }
  1110. X    Unexpected(11);
  1111. X    Vprintf("%sMaximum unsigned %s = %lu%s\n",
  1112. X        co, INT, (unsigned long) int_max, oc);
  1113. X    if (L) u_define(Uname, "_MAX", (unsigned long) int_max,
  1114. X            (unsigned long) U_MAX);
  1115. X#endif
  1116. X}
  1117. X
  1118. X
  1119. X#ifdef Number
  1120. X
  1121. X/* These routines are intended to defeat any attempt at optimisation
  1122. X   or use of extended precision, and to defeat faulty narrowing casts:
  1123. X*/
  1124. XProcedure Store(a, b) Number a, *b; { *b=a; }
  1125. XNumber Sum(a, b) Number a, b; { Number r; Store(a+b, &r); return (r); }
  1126. XNumber Diff(a, b) Number a, b; { Number r; Store(a-b, &r); return (r); }
  1127. XNumber Mul(a, b) Number a, b; { Number r; Store(a*b, &r); return (r); }
  1128. XNumber Div(a, b) Number a, b; { Number r; Store(a/b, &r); return (r); }
  1129. XNumber Self(a) Number a; { Number r; Store(a, &r); return (r); }
  1130. X
  1131. XProcedure F_check(precision, val1) int precision; Long_double val1; {
  1132. X    /* You don't think I'm going to go to all the trouble of writing
  1133. X       a program that works out what all sorts of values are, only to
  1134. X       have printf go and print the wrong values out, do you?
  1135. X       No, you're right, so this function tries to see if printf
  1136. X       has written the right value, by reading it back again.
  1137. X       This introduces a new problem of course: suppose printf writes
  1138. X       the correct value, and scanf reads it back wrong... oh well.
  1139. X       But I'm adamant about this: the precision given is enough
  1140. X       to uniquely identify the printed number, therefore I insist
  1141. X       that sscanf read the number back identically. Harsh yes, but
  1142. X       sometimes you've got to be cruel to be kind.
  1143. X    */
  1144. X    Long_double new1;
  1145. X    Number val, new, diff;
  1146. X    double rem;
  1147. X    int e;
  1148. X    char *rep;
  1149. X    char *f2;
  1150. X
  1151. X    if (sizeof(double) == sizeof(Long_double)) {
  1152. X        /* Assume they're the same, and use non-stdc format */
  1153. X        /* This is for stdc compilers using non-stdc libraries */
  1154. X        f2= "%le";   /* Input */
  1155. X    } else {
  1156. X        /* It had better support Le then */
  1157. X        f2= "%Le";
  1158. X    }
  1159. X    val= val1;
  1160. X    rep= f_rep(precision, (Long_double) val);
  1161. X    if (setjmp(lab)==0) {
  1162. X        sscanf(rep, f2, &new1);
  1163. X    } else {
  1164. X        eek_a_bug("sscanf caused a trap");
  1165. X        printf("%s    scanning: %s format: %s%s\n\n", co, rep, f2, oc);
  1166. X        Unexpected(12);
  1167. X        return;
  1168. X    }
  1169. X
  1170. X    if (setjmp(lab)==0) { /* See if new is usable */
  1171. X        new= new1;
  1172. X        if (new != 0.0) {
  1173. X            diff= val/new - 1.0;
  1174. X            if (diff < 0.1) diff= 1.0;
  1175. X            /* That should be enough to generate a trap */
  1176. X        }
  1177. X    } else {
  1178. X        eek_a_bug("sscanf returned an unusable number");
  1179. X        printf("%s    scanning: %s with format: %s%s\n\n",
  1180. X               co, rep, f2, oc);
  1181. X        Unexpected(13);
  1182. X        return;
  1183. X    }
  1184. X
  1185. X    Unexpected(14);
  1186. X    if (new != val) {
  1187. X        eek_a_bug("Possibly bad output from printf above");
  1188. X        if (!exponent(val, &rem, &e)) {
  1189. X            printf("%s    but value was an unusable number%s\n\n",
  1190. X                   co, oc);
  1191. X            return;
  1192. X        }
  1193. X        printf("%s    expected value around %.*fe%d, bit pattern:\n    ",
  1194. X               co, precision, rem, e);
  1195. X        bitpattern((char *) &val, sizeof(val));
  1196. X        printf ("%s\n", oc);
  1197. X        printf("%s    sscanf gave           %s, bit pattern:\n    ",
  1198. X               co, f_rep(precision, (Long_double) new));
  1199. X        bitpattern((char *) &new, sizeof(new));
  1200. X        printf ("%s\n", oc);
  1201. X        printf("%s    difference= %s%s\n\n", 
  1202. X               co, f_rep(precision, (Long_double) (val-new)), oc);
  1203. X    }
  1204. X}
  1205. X
  1206. XProcedure Validate(prec, val, req, same) int prec, same; Long_double val, req; {
  1207. X    Unexpected(15);
  1208. X    if (!same) {
  1209. X        printf("%s*** Verify failed for above #define!\n", co);
  1210. X        if (setjmp(lab) == 0) { /* for the case that req == nan */
  1211. X            printf("       Compiler has %s for value%s\n", 
  1212. X                   f_rep(prec, req), oc);
  1213. X        } else {
  1214. X            printf("       Compiler has %s for value%s\n",
  1215. X                   "an unusable number", oc);
  1216. X        }
  1217. X        if (setjmp(lab) == 0) {
  1218. X            F_check(prec, (Long_double) req);
  1219. X        } /*else forget it*/
  1220. X        if (setjmp(lab) == 0) {        
  1221. X            if (req > 0.0 && val > 0.0) {
  1222. X                printf("%s    difference= %s%s\n",
  1223. X                       co, f_rep(prec, val-req), oc);
  1224. X            }
  1225. X        } /*else forget it*/
  1226. X        Unexpected(16);
  1227. X        printf("\n");
  1228. X        bugs++;
  1229. X    } else if (val != req) {
  1230. X        if (stdc) {
  1231. X            printf("%s*** Verify failed for above #define!\n", co);
  1232. X            printf("       Constant has the wrong precision%s\n",
  1233. X                   oc);
  1234. X            bugs++;
  1235. X        } else eek_a_bug("the cast didn't work");
  1236. X        printf("\n");
  1237. X    }
  1238. X}
  1239. X
  1240. Xint FPROP(bits_per_byte) int bits_per_byte; {
  1241. X    /* Properties of floating types, using algorithms by Cody and Waite
  1242. X       from MA Malcolm, as modified by WM Gentleman and SB Marovich.
  1243. X       Further extended by S Pemberton.
  1244. X
  1245. X       Returns the number of digits in the fraction.
  1246. X    */
  1247. X
  1248. X    volatile int i, f_radix, iexp, irnd, mrnd, f_rounds, f_mant_dig,
  1249. X        iz, k, inf, machep, f_max_exp, f_min_exp, mx, negeps,
  1250. X        mantbits, digs, f_dig, trap,
  1251. X        hidden, normal, f_min_10_exp, f_max_10_exp;
  1252. X    volatile Number a, b, base, basein, basem1, f_epsilon, epsneg,
  1253. X           f_max, newxmax, f_min, xminner, y, y1, z, z1, z2;
  1254. X
  1255. X    Unexpected(17);
  1256. X
  1257. X    Vprintf("%sPROPERTIES OF %s:%s\n", co, THING, oc);
  1258. X
  1259. X    /* Base and size of mantissa **************************************/
  1260. X    /* First repeatedly double until adding 1 has no effect.      */
  1261. X    /* For instance, if base is 10, with 3 significant digits      */
  1262. X    /* it will try 1, 2, 4, 8, ... 512, 1024, and stop there,      */
  1263. X    /* since 1024 is only representable as 1020.              */
  1264. X    a=1.0;
  1265. X    if (setjmp(lab)==0) { /* inexact trap? */
  1266. X        do { a=Sum(a, a); }
  1267. X        while (Diff(Diff(Sum(a, 1.0), a), 1.0) == 0.0);
  1268. X    } else {
  1269. X        fprintf(stderr, "*** Program got loss-of-precision trap!\n");
  1270. X        /* And supporting those is just TOO much trouble! */
  1271. X        exit(bugs+1);
  1272. X    }
  1273. X    Unexpected(18);
  1274. X    /* Now double until you find a number that can be added to the      */
  1275. X    /* above number. For 1020 this is 8 or 16, depending whether the  */
  1276. X    /* result is rounded or truncated.                  */
  1277. X    /* In either case the result is 1030. 1030-1020= the base, 10.      */
  1278. X    b=1.0;
  1279. X    do { b=Sum(b, b); } while ((base=Diff(Sum(a, b), a)) == 0.0);
  1280. X    f_radix=base;
  1281. X    Vprintf("%sBase = %d%s\n", co, f_radix, oc);
  1282. X
  1283. X    /* Sanity check; if base<2, I can't guarantee the rest will work  */
  1284. X    if (f_radix < 2) {
  1285. X        eek_a_bug("Function return or parameter passing faulty? (This is a guess.)");
  1286. X        printf("\n");
  1287. X        return(0);
  1288. X    }
  1289. X
  1290. X#ifdef PASS1 /* only for FLT */
  1291. X    if (F) i_define("FLT", "_RADIX", (long) f_radix, (long) F_RADIX);
  1292. X#endif
  1293. X
  1294. X    /* Now the number of digits precision: */
  1295. X    f_mant_dig=0; b=1.0;
  1296. X    do { f_mant_dig++; b=Mul(b, base); }
  1297. X    while (Diff(Diff(Sum(b,1.0),b),1.0) == 0.0);
  1298. X    f_dig=floor_log(10, (Long_double)(b/base)) + (base==10?1:0);
  1299. X    Vprintf("%sSignificant base digits = %d %s %d %s%s\n",
  1300. X        co, f_mant_dig, "(= at least", f_dig, "decimal digits)", oc);
  1301. X    if (F) i_define(Fname, "_MANT_DIG", (long) f_mant_dig,
  1302. X            (long) F_MANT_DIG);
  1303. X    if (F) i_define(Fname, "_DIG", (long) f_dig, (long) F_DIG);
  1304. X    digs= ceil_log(10, (Long_double)b); /* the number of digits to printf */
  1305. X
  1306. X    /* Rounding *******************************************************/
  1307. X    basem1=Diff(base, 0.5);
  1308. X    if (Diff(Sum(a, basem1), a) != 0.0) {
  1309. X        if (f_radix == 2) basem1=0.375;
  1310. X        else basem1=1.0;
  1311. X        if (Diff(Sum(a, basem1), a) != 0.0) irnd=2; /* away from 0 */
  1312. X        else irnd=1; /* to nearest */
  1313. X    } else irnd=0; /* towards 0 */
  1314. X
  1315. X    basem1=Diff(base, 0.5);
  1316. X
  1317. X    if (Diff(Diff(-a, basem1), -a) != 0.0) {
  1318. X        if (f_radix == 2) basem1=0.375;
  1319. X        else basem1=1.0;
  1320. X        if (Diff(Diff(-a, basem1), -a) != 0.0) mrnd=2; /* away from 0*/
  1321. X        else mrnd=1; /* to nearest */
  1322. X    } else mrnd=0; /* towards 0 */
  1323. X
  1324. X    f_rounds=4; /* Unknown rounding */
  1325. X    if (irnd==0 && mrnd==0) f_rounds=0; /* zero = chops */
  1326. X    if (irnd==1 && mrnd==1) f_rounds=1; /* nearest */
  1327. X    if (irnd==2 && mrnd==0) f_rounds=2; /* +inf */
  1328. X    if (irnd==0 && mrnd==2) f_rounds=3; /* -inf */
  1329. X
  1330. X    if (f_rounds != 4) {
  1331. X        Vprintf("%sArithmetic rounds towards ", co);
  1332. X        switch (f_rounds) {
  1333. X              case 0: Vprintf("zero (i.e. it chops)"); break;
  1334. X              case 1: Vprintf("nearest"); break;
  1335. X              case 2: Vprintf("+infinity"); break;
  1336. X              case 3: Vprintf("-infinity"); break;
  1337. X              default: Vprintf("???"); break;
  1338. X        }
  1339. X        Vprintf("%s\n", oc);
  1340. X    } else { /* Hmm, try to give some help here: */
  1341. X        Vprintf("%sArithmetic rounds oddly: %s\n", co, oc);
  1342. X        Vprintf("%s    Negative numbers %s%s\n",
  1343. X            co, mrnd==0 ? "towards zero" :
  1344. X                mrnd==1 ? "to nearest" :
  1345. X                      "away from zero",
  1346. X            oc);
  1347. X        Vprintf("%s    Positive numbers %s%s\n",
  1348. X            co, irnd==0 ? "towards zero" :
  1349. X                irnd==1 ? "to nearest" :
  1350. X                      "away from zero",
  1351. X            oc);
  1352. X    }
  1353. X    /* An extra goody */
  1354. X    if (f_radix == 2 && f_rounds == 1) {
  1355. X        if (Diff(Sum(a, 1.0), a) != 0.0) {
  1356. X            Vprintf("%s   Tie breaking rounds up%s\n", co, oc);
  1357. X        } else if (Diff(Sum(a, 3.0), a) == 4.0) {
  1358. X            Vprintf("%s   Tie breaking rounds to even%s\n", co, oc);
  1359. X        } else {
  1360. X            Vprintf("%s   Tie breaking rounds down%s\n", co, oc);
  1361. X        }
  1362. X    }
  1363. X#ifdef PASS1 /* only for FLT */
  1364. X    if (F) i_define("FLT", "_ROUNDS", (long) f_rounds, (long) F_ROUNDS);
  1365. X#endif
  1366. X
  1367. X    /* Various flavours of epsilon ************************************/
  1368. X    negeps=f_mant_dig+f_mant_dig;
  1369. X    basein=1.0/base;
  1370. X    a=1.0;
  1371. X    for(i=1; i<=negeps; i++) a*=basein;
  1372. X
  1373. X    b=a;
  1374. X    while (Diff(Diff(1.0, a), 1.0) == 0.0) {
  1375. X        a*=base;
  1376. X        negeps--;
  1377. X    }
  1378. X    negeps= -negeps;
  1379. X    Vprintf("%sSmallest x such that 1.0-base**x != 1.0 = %d%s\n",
  1380. X        co, negeps, oc);
  1381. X
  1382. X    epsneg=a;
  1383. X    if ((f_radix!=2) && irnd) {
  1384. X    /*    a=(a*(1.0+a))/(1.0+1.0); => */
  1385. X        a=Div(Mul(a, Sum(1.0, a)), Sum(1.0, 1.0));
  1386. X    /*    if ((1.0-a)-1.0 != 0.0) epsneg=a; => */
  1387. X        if (Diff(Diff(1.0, a), 1.0) != 0.0) epsneg=a;
  1388. X    }
  1389. X    Vprintf("%sSmall x such that 1.0-x != 1.0 = %s%s\n",
  1390. X        co, f_rep(digs, (Long_double) epsneg), oc);
  1391. X    /* it may not be the smallest */
  1392. X    if (V) F_check(digs, (Long_double) epsneg);
  1393. X    Unexpected(19);
  1394. X
  1395. X    machep= -f_mant_dig-f_mant_dig;
  1396. X    a=b;
  1397. X    while (Diff(Sum(1.0, a), 1.0) == 0.0) { a*=base; machep++; }
  1398. X    Vprintf("%sSmallest x such that 1.0+base**x != 1.0 = %d%s\n",
  1399. X        co, machep, oc);
  1400. X
  1401. X    f_epsilon=a;
  1402. X    if ((f_radix!=2) && irnd) {
  1403. X    /*    a=(a*(1.0+a))/(1.0+1.0); => */
  1404. X        a=Div(Mul(a, Sum(1.0, a)), Sum(1.0, 1.0));
  1405. X    /*    if ((1.0+a)-1.0 != 0.0) f_epsilon=a; => */
  1406. X        if (Diff(Sum(1.0, a), 1.0) != 0.0) f_epsilon=a;
  1407. X    }
  1408. X    Vprintf("%sSmallest x such that 1.0+x != 1.0 = %s%s\n",
  1409. X        co, f_rep(digs, (Long_double) f_epsilon), oc);
  1410. X    /* Possible loss of precision warnings here from non-stdc compilers: */
  1411. X    if (F) f_define(Fname, "_EPSILON", digs, (Long_double) f_epsilon, MARK);
  1412. X    if (V || F) F_check(digs, (Long_double) f_epsilon);
  1413. X    Unexpected(20);
  1414. X    if (F) Validate(digs, (Long_double) f_epsilon, (Long_double) F_EPSILON,
  1415. X            f_epsilon == Self(F_EPSILON));
  1416. X    Unexpected(21);
  1417. X
  1418. X    /* Extra chop info *************************************************/
  1419. X    if (f_rounds == 0) {
  1420. X        if (Diff(Mul(Sum(1.0,f_epsilon),1.0),1.0) !=  0.0) {
  1421. X            Vprintf("%sAlthough arithmetic chops, it uses guard digits%s\n", co, oc);
  1422. X        }
  1423. X    }
  1424. X
  1425. X    /* Size of and minimum normalised exponent ************************/
  1426. X    y=0; i=0; k=1; z=basein; z1=(1.0+f_epsilon)/base;
  1427. X
  1428. X    /* Coarse search for the largest power of two */
  1429. X    if (setjmp(lab)==0) { /* for underflow trap */ /* Yields i, k, y, y1 */
  1430. X        do {
  1431. X            y=z; y1=z1;
  1432. X            z=Mul(y,y); z1=Mul(z1, y);
  1433. X            a=Mul(z,1.0);
  1434. X            z2=Div(z1,y);
  1435. X            if (z2 != y1) break;
  1436. X            if ((Sum(a,a) == 0.0) || (fabs(z) >= y)) break;
  1437. X            i++;
  1438. X            k+=k;
  1439. X        } while(1);
  1440. X    } else {
  1441. X        Vprintf("%s%s underflow generates a trap%s\n", co, Thing, oc);
  1442. X    }
  1443. X    Unexpected(22);
  1444. X
  1445. X    if (f_radix != 10) {
  1446. X        iexp=i+1; /* for the sign */
  1447. X        mx=k+k;
  1448. X    } else {
  1449. X        iexp=2;
  1450. X        iz=f_radix;
  1451. X        while (k >= iz) { iz*=f_radix; iexp++; }
  1452. X        mx=iz+iz-1;
  1453. X    }
  1454. X
  1455. X    /* Fine tune starting with y and y1 */
  1456. X    if (setjmp(lab)==0) { /* for underflow trap */ /* Yields k, f_min */
  1457. X        do {
  1458. X            f_min=y; z1=y1;
  1459. X            y=Div(y,base); y1=Div(y1,base);
  1460. X            a=Mul(y,1.0);
  1461. X            z2=Mul(y1,base);
  1462. X            if (z2 != z1) break;
  1463. X            if ((Sum(a,a) == 0.0) || (fabs(y) >= f_min)) break;
  1464. X            k++;
  1465. X        } while (1);
  1466. X    }
  1467. X    Unexpected(23);
  1468. X
  1469. X    f_min_exp=(-k)+1;
  1470. X
  1471. X    if ((mx <= k+k-3) && (f_radix != 10)) { mx+=mx; iexp+=1; }
  1472. X    Vprintf("%sNumber of bits used for exponent = %d%s\n", co, iexp, oc);
  1473. X    Vprintf("%sMinimum normalised exponent = %d%s\n", co, f_min_exp, oc);
  1474. X    if (F) i_define(Fname, "_MIN_EXP", (long) f_min_exp, (long) F_MIN_EXP);
  1475. X
  1476. X    if (setjmp(lab)==0) {
  1477. X        Vprintf("%sMinimum normalised positive number = %s%s\n",
  1478. X            co, f_rep(digs, (Long_double) f_min), oc);
  1479. X    } else {
  1480. X        eek_a_bug("printf can't print the smallest normalised number");
  1481. X        printf("\n");
  1482. X    }
  1483. X    Unexpected(24);
  1484. X    /* Possible loss of precision warnings here from non-stdc compilers: */
  1485. X    if (setjmp(lab) == 0) {
  1486. X        if (F) f_define(Fname, "_MIN", digs, (Long_double) f_min, MARK);
  1487. X        if (V || F) F_check(digs, (Long_double) f_min);
  1488. X    } else {
  1489. X        eek_a_bug("xxx_MIN caused a trap");
  1490. X        printf("\n");
  1491. X    }
  1492. X
  1493. X    if (setjmp(lab) == 0) {
  1494. X        if (F) Validate(digs, (Long_double) f_min, (Long_double) F_MIN,
  1495. X                f_min == Self(F_MIN));
  1496. X    } else {
  1497. X        printf("%s*** Verify failed for above #define!\n    %s %s\n\n",
  1498. X               co, "Compiler has an unusable number for value", oc);
  1499. X        bugs++;
  1500. X    }
  1501. X    Unexpected(25);
  1502. X
  1503. X    a=1.0; f_min_10_exp=0;
  1504. X    while (a > f_min*10.0) { a/=10.0; f_min_10_exp--; }
  1505. X    if (F) i_define(Fname, "_MIN_10_EXP", (long) f_min_10_exp,
  1506. X            (long) F_MIN_10_EXP);
  1507. X
  1508. X    /* Minimum exponent ************************************************/
  1509. X    if (setjmp(lab)==0) { /* for underflow trap */ /* Yields xminner */
  1510. X        do {
  1511. X            xminner=y;
  1512. X            y=Div(y,base);
  1513. X            a=Mul(y,1.0);
  1514. X            if ((Sum(a,a) == 0.0) || (fabs(y) >= xminner)) break;
  1515. X        } while (1);
  1516. X    }
  1517. X    Unexpected(26);
  1518. X
  1519. X    if (xminner != 0.0 && xminner != f_min) {
  1520. X        normal= 0;
  1521. X        Vprintf("%sThe smallest numbers are not kept normalised%s\n",
  1522. X            co, oc);
  1523. X        if (setjmp(lab)==0) {
  1524. X            Vprintf("%sSmallest unnormalised positive number = %s%s\n",
  1525. X                co, f_rep(digs, (Long_double) xminner), oc);
  1526. X            if (V) F_check(digs, (Long_double) xminner);
  1527. X        } else {
  1528. X            eek_a_bug("printf can't print the smallest unnormalised number.");
  1529. X            printf("\n");
  1530. X        }
  1531. X        Unexpected(27);
  1532. X    } else {
  1533. X        normal= 1;
  1534. X        Vprintf("%sThe smallest numbers are normalised%s\n", co, oc);
  1535. X    }
  1536. X
  1537. X    /* Maximum exponent ************************************************/
  1538. X    f_max_exp=2; f_max=1.0; newxmax=base+1.0;
  1539. X    inf=0; trap=0;
  1540. X    while (f_max<newxmax) {
  1541. X        f_max=newxmax;
  1542. X        if (setjmp(lab) == 0) { /* Yields inf, f_max_exp */
  1543. X            newxmax=Mul(newxmax, base);
  1544. X        } else {
  1545. X            trap=1;
  1546. X            break;
  1547. X        }
  1548. X        if (Div(newxmax, base) != f_max) {
  1549. X            inf=1; /* ieee infinity */
  1550. X            break;
  1551. X        }
  1552. X        f_max_exp++;
  1553. X    }
  1554. X    Unexpected(28);
  1555. X    if (trap) {
  1556. X        Vprintf("%s%s overflow generates a trap%s\n", co, Thing, oc);
  1557. X    }
  1558. X
  1559. X    if (inf) Vprintf("%sThere is an 'infinite' value%s\n", co, oc);
  1560. X    Vprintf("%sMaximum exponent = %d%s\n", co, f_max_exp, oc);
  1561. X    if (F) i_define(Fname, "_MAX_EXP", (long) f_max_exp, (long) F_MAX_EXP);
  1562. X
  1563. X    /* Largest number ***************************************************/
  1564. X    f_max=Diff(1.0, epsneg);
  1565. X    if (Mul(f_max,1.0) != f_max) f_max=Diff(1.0, Mul(base,epsneg));
  1566. X    for (i=1; i<=f_max_exp; i++) f_max=Mul(f_max, base);
  1567. X
  1568. X    if (setjmp(lab)==0) {
  1569. X        Vprintf("%sMaximum number = %s%s\n",
  1570. X            co, f_rep(digs, (Long_double) f_max), oc);
  1571. X    } else {
  1572. X        eek_a_bug("printf can't print the largest double.");
  1573. X        printf("\n");
  1574. X    }
  1575. X    if (setjmp(lab)==0) {
  1576. X    /* Possible loss of precision warnings here from non-stdc compilers: */
  1577. X        if (F) f_define(Fname, "_MAX", digs, (Long_double) f_max, MARK);
  1578. X        if (V || F) F_check(digs, (Long_double) f_max);
  1579. X    } else {
  1580. X        eek_a_bug("xxx_MAX caused a trap");
  1581. X        printf("\n");
  1582. X    }
  1583. X    if (setjmp(lab)==0) {
  1584. X        if (F) Validate(digs, (Long_double) f_max, (Long_double) F_MAX,
  1585. X                f_max == Self(F_MAX));
  1586. X    } else {
  1587. X        printf("%s*** Verify failed for above #define!\n    %s %s\n\n",
  1588. X               co, "Compiler has an unusable number for value", oc);
  1589. X        bugs++;
  1590. X    }
  1591. X    Unexpected(29);
  1592. X
  1593. X    a=1.0; f_max_10_exp=0;
  1594. X    while (a < f_max/10.0) { a*=10.0; f_max_10_exp++; }
  1595. X    if (F) i_define(Fname, "_MAX_10_EXP", (long) f_max_10_exp,
  1596. X            (long) F_MAX_10_EXP);
  1597. X
  1598. X    /* Hidden bit + sanity check ****************************************/
  1599. X    if (f_radix != 10) {
  1600. X        hidden=0;
  1601. X        mantbits=floor_log(2, (Long_double)f_radix)*f_mant_dig;
  1602. X        if (mantbits+iexp == (int)sizeof(Number)*bits_per_byte) {
  1603. X            hidden=1;
  1604. X            Vprintf("%sArithmetic uses a hidden bit%s\n", co, oc);
  1605. X        } else if (mantbits+iexp+1 == (int)sizeof(Number)*bits_per_byte) {
  1606. X            Vprintf("%sArithmetic doesn't use a hidden bit%s\n",
  1607. X                co, oc);
  1608. X        } else {
  1609. X            printf("\n%s%s\n    %s %s %s!%s\n\n",
  1610. X                   co,
  1611. X                   "*** Something fishy here!",
  1612. X                   "Exponent size + mantissa size doesn't match",
  1613. X                   "with the size of a", thing,
  1614. X                   oc);
  1615. X        }
  1616. X        if (hidden && f_radix == 2 && f_max_exp+f_min_exp==3) {
  1617. X            Vprintf("%sIt looks like %s length IEEE format%s\n",
  1618. X                co, f_mant_dig==24 ? "single" :
  1619. X                    f_mant_dig==53 ? "double" :
  1620. X                    f_mant_dig >53 ? "extended" :
  1621. X                        "some", oc);
  1622. X            if (f_rounds != 1 || normal) {
  1623. X                Vprintf("%s   though ", co);
  1624. X                if (f_rounds != 1) {
  1625. X                    Vprintf("the rounding is unusual");
  1626. X                    if (normal) Vprintf(" and ");
  1627. X                }
  1628. X                if (normal) Vprintf("the normalisation is unusual");
  1629. X                Vprintf("%s\n", oc);
  1630. X            }
  1631. X        } else {
  1632. X            Vprintf("%sIt doesn't look like IEEE format%s\n",
  1633. X                co, oc);
  1634. X        }
  1635. X    }
  1636. X    printf("\n"); /* regardless of verbosity */
  1637. X    return f_mant_dig;
  1638. X}
  1639. X
  1640. XProcedure EPROP(fprec, dprec, lprec) int fprec, dprec, lprec; {
  1641. X    /* See if expressions are evaluated in extended precision.
  1642. X       Some compilers optimise even if you don't want it,
  1643. X       and then this function fails to produce the right result.
  1644. X       We try to diagnose this if it happens.
  1645. X    */
  1646. X    volatile int eprec;
  1647. X    volatile double a, b, base, old;
  1648. X    volatile Number d, oldd, dbase, one, zero;
  1649. X    volatile int bad=0;
  1650. X
  1651. X    /* Size of mantissa **************************************/
  1652. X    a=1.0;
  1653. X    if (setjmp(lab) == 0) { /* Yields nothing */
  1654. X        do { old=a; a=a+a; }
  1655. X        while ((((a+1.0)-a)-1.0) == 0.0 && a>old);
  1656. X    } else bad=1;
  1657. X    if (a <= old) bad=1;
  1658. X
  1659. X    if (!bad) {
  1660. X        b=1.0;
  1661. X        if (setjmp(lab) == 0) { /* Yields nothing */
  1662. X            do { old=b; b=b+b; }
  1663. X            while ((base=((a+b)-a)) == 0.0 && b>old);
  1664. X            if (b <= old) bad=1;
  1665. X        } else bad=1;
  1666. X    }
  1667. X
  1668. X    if (!bad) {
  1669. X        eprec=0; d=1.0; dbase=base; one=1.0; zero=0.0;
  1670. X        if (setjmp(lab) == 0) { /* Yields nothing */
  1671. X            do { eprec++; oldd=d; d=d*dbase; }
  1672. X            while ((((d+one)-d)-one) == zero && d>oldd);
  1673. X            if (d <= oldd) bad=1;
  1674. X        } else bad=1;
  1675. X    }
  1676. X
  1677. X    Unexpected(30);
  1678. X
  1679. X    if (bad) {
  1680. X      Vprintf("%sCan't determine precision for %s expressions:\n%s%s\n", 
  1681. X         co, thing, "   check that you compiled without optimisation!",
  1682. X         oc);
  1683. X    } else if (eprec==dprec) {
  1684. X      Vprintf("%s%s expressions are evaluated in double precision%s\n",
  1685. X          co, Thing, oc);
  1686. X    } else if (eprec==fprec) {
  1687. X      Vprintf("%s%s expressions are evaluated in float precision%s\n",
  1688. X          co, Thing, oc);
  1689. X    } else if (eprec==lprec) {
  1690. X      Vprintf("%s%s expressions are evaluated in long double precision%s\n",
  1691. X          co, Thing, oc);
  1692. X    } else {
  1693. X        Vprintf("%s%s expressions are evaluated in a %s %s %d %s%s\n",
  1694. X            co, Thing, eprec>dprec ? "higher" : "lower",
  1695. X            "precision than double,\n   using",
  1696. X            eprec, "base digits",
  1697. X                oc);
  1698. X    }
  1699. X}
  1700. X
  1701. X#else /* Number */
  1702. X
  1703. X#ifdef FPROP
  1704. X/* ARGSUSED */
  1705. Xint FPROP(bits_per_byte) int bits_per_byte; {
  1706. X    return 0;
  1707. X}
  1708. X#endif
  1709. X#ifdef EPROP
  1710. X/* ARGSUSED */
  1711. XProcedure EPROP(fprec, dprec, lprec) int fprec, dprec, lprec; {}
  1712. X#endif
  1713. X
  1714. X#endif /* ifdef Number */
  1715. X
  1716. X#ifdef PASS3
  1717. X#undef PASS
  1718. X#endif
  1719. X
  1720. X#ifdef PASS2
  1721. X#undef PASS2
  1722. X#define PASS3 1
  1723. X#endif
  1724. X
  1725. X#ifdef PASS1
  1726. X#undef PASS1
  1727. X#define PASS2 1
  1728. X#endif
  1729. X
  1730. X/* If your C compiler doesn't accept the next #include,
  1731. X   replace __FILE__ with the file name - and get a new C compiler... */
  1732. X
  1733. X#ifdef PASS
  1734. X#include __FILE__
  1735. X#endif
  1736. X
  1737. EOF
  1738. chars=`wc -c < 'config.c'`
  1739. if test $chars !=    49439; then echo 'config.c' is $chars characters, should be    49439 characters!; fi
  1740. fi
  1741. exit 0
  1742.  
  1743.  
  1744.