]> git.sur5r.net Git - cc65/blob - test/val/cq26.c
remote TABs in doc/ and test/
[cc65] / test / val / cq26.c
1 /*
2   !!DESCRIPTION!! C-Manual Chapter 2.6: Hardware Characteristics
3   !!ORIGIN!!      LCC 4.1 Testsuite
4   !!LICENCE!!     own, freely distributeable for non-profit. read CPYRIGHT.LCC
5 */
6
7 #include "common.h"
8
9 #ifndef CQ26_INCLUDED
10 struct defs {
11      int cbits;          /* No. of bits per char           */
12      int ibits;          /*                 int            */
13      int sbits;          /*                 short          */
14      int lbits;          /*                 long           */
15      int ubits;          /*                 unsigned       */
16      int fbits;          /*                 float          */
17      int dbits;          /*                 double         */
18      #ifndef NO_FLOATS
19         float fprec;        /* Smallest number that can be    */
20         float dprec;        /* significantly added to 1.      */
21      #endif
22      int flgs;           /* Print return codes, by section */
23      int flgm;           /* Announce machine dependencies  */
24      int flgd;           /* give explicit diagnostics      */
25      int flgl;           /* Report local return codes.     */
26      int rrc;            /* recent return code             */
27      int crc;            /* Cumulative return code         */
28      char rfs[8];        /* Return from section            */
29 };
30
31      int lbits;          /*                 long           */
32      int ubits;          /*                 unsigned       */
33      int fbits;          /*                 float          */
34      int dbits;          /*                 double         */
35      #ifndef NO_FLOATS
36         float fprec;        /* Smallest number that can be    */
37         float dprec;        /* significantly added to 1.      */
38      #endif
39      int flgs;           /* Print return codes, by section */
40      int flgm;           /* Announce machine dependencies  */
41      int flgd;           /* give explicit diagnostics      */
42      int flgl;           /* Report local return codes.     */
43      int rrc;            /* recent return code             */
44      int crc;            /* Cumulative return code         */
45      char rfs[8];        /* Return from section            */
46
47 #endif
48
49 /*
50   section s26, which pokes around at the hardware
51   trying to figure out the characteristics of the machine that
52   it is running on, saves information that is subsequently
53   used by sections s626, s72, and s757. If this program is
54   to be broken up into smallish pieces, say for running on
55   a microcomputer, take care to see that s26 is called before
56   calling any of the latter three sections.
57 */
58
59 /*
60         2.6  Hardware Characteristics
61 */
62
63 #ifndef NO_OLD_FUNC_DECL
64 s26(pd0)
65 struct defs *pd0;
66 {
67 #else
68 s26(struct defs *pd0) {
69 #endif
70    static char qs26[8] = "s26    ";
71    char *ps, *pt;
72    char c0, c1;
73    #ifndef NO_FLOATS
74         float temp, one, delta;
75         double tempd, oned;
76    #endif
77    static char s[] = "%3d bits in %ss.\n";
78    static char s2[] = "%e is the least number that can be added to 1. (%s).\n";
79
80    ps = qs26;
81    pt = pd0->rfs;
82
83    while(*pt++ = *ps++);
84
85           /* Here, we shake the machinery a little to see what falls
86              out.  First, we find out how many bits are in a char.  */
87
88    pd0->cbits = 0;
89    c0 = 0;
90    c1 = 1;
91
92    while(c0 != c1) {
93      c1 = c1<<1;
94      pd0->cbits = pd0->cbits+1;
95    }
96           /* That information lets us determine the size of everything else. */
97
98    pd0->ibits = pd0->cbits * sizeof(int);
99    pd0->sbits = pd0->cbits * sizeof(short);
100    pd0->lbits = pd0->cbits * sizeof(long);
101    pd0->ubits = pd0->cbits * sizeof(unsigned);
102    #ifndef NO_FLOATS
103         pd0->fbits = pd0->cbits * sizeof(float);
104         pd0->dbits = pd0->cbits * sizeof(double);
105    #endif
106
107           /* We have now almost reconstructed the table in section 2.6, the
108              exception being the range of the floating point hardware.
109              Now there are just so many ways to conjure up a floating point
110              representation system that it's damned near impossible to guess
111              what's going on by writing a program to interpret bit patterns.
112              Further, the information isn't all that useful, if we consider
113              the fact that machines that won't handle numbers between 10**30
114              and 10**-30 are very hard to find, and that people playing with
115              numbers outside that range have a lot more to worry about than
116              just the capacity of the characteristic.
117
118              A much more useful measure is the precision, which can be ex-
119              pressed in terms of the smallest number that can be added to
120              1. without loss of significance. We calculate that here, for
121              float and double.                       */
122
123 #ifndef NO_FLOATS
124    one = 1.;
125    delta = 1.;
126    temp = 0.;
127    while(temp != one) {
128      temp = one+delta;
129      delta = delta/2.;
130    }
131    pd0->fprec = delta * 4.;
132    oned = 1.;
133    delta = 1.;
134    tempd = 0.;
135    while(tempd != oned) {
136      tempd = oned+delta;
137      delta = delta/2.;
138    }
139    pd0->dprec = delta * 4.;
140 #endif
141
142           /* Now, if anyone's interested, we publish the results.       */
143
144 #ifndef CQ26_INCLUDED
145    if(pd0->flgm != 0) {
146      printf(s,pd0->cbits,"char");
147      printf(s,pd0->ibits,"int");
148      printf(s,pd0->sbits,"short");
149      printf(s,pd0->lbits,"long");
150      printf(s,pd0->ubits,"unsigned");
151      printf(s,pd0->fbits,"float");
152      printf(s,pd0->dbits,"double");
153      #ifndef NO_FLOATS
154         printf(s2,pd0->fprec,"float");
155         printf(s2,pd0->dprec,"double");
156      #else
157         printf("NO_FLOATS\n");
158      #endif
159    }
160 #endif
161           /* Since we are only exploring and perhaps reporting, but not
162              testing any features, we cannot return an error code.  */
163
164    return 0;
165 }
166
167 #ifndef CQ26_INCLUDED
168
169 /*********************************************************************************************
170  the main loop that launches the sections
171 *********************************************************************************************/
172
173 #ifndef NO_TYPELESS_STRUCT_PTR
174         int section(int j,struct* pd0){
175 #else
176         int section(int j,void* pd0){
177 #endif
178         switch(j){
179                 case 0: return s26(pd0);
180         }
181 }
182
183 #define cq_sections 1
184
185 /*
186         C REFERENCE MANUAL (main)
187 */
188
189 #ifndef NO_OLD_FUNC_DECL
190 main(n,args)
191 int n;
192 char **args;
193 {
194 #else
195 int main(int n,char **args) {
196 #endif
197
198 int j;
199 static struct defs d0, *pd0;
200         
201    d0.flgs = 1;          /* These flags dictate            */
202    d0.flgm = 1;          /*     the verbosity of           */
203    d0.flgd = 1;          /*         the program.           */
204    d0.flgl = 1;
205
206    pd0 = &d0;
207
208    for (j=0; j<cq_sections; j++) {
209      d0.rrc=section(j,pd0);
210      d0.crc=d0.crc+d0.rrc;
211      if(d0.flgs != 0) printf("Section %s returned %d.\n",d0.rfs,d0.rrc);
212    }
213
214    if(d0.crc == 0) printf("\nNo errors detected.\n");
215    else printf("\nFailed.\n");
216
217    return d0.crc;
218 }
219 #endif