Documentation fixes
[vlp.git] / src / int / util.c
1 /* Loglan82 Compiler&Interpreter
2      Copyright (C) 1981-1993 Institute of Informatics, University of Warsaw
3      Copyright (C)  1993, 1994 LITA, Pau
4      
5      This program is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published by
7      the Free Software Foundation; either version 2 of the License, or
8      (at your option) any later version.
9      
10      This program is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13      GNU General Public License for more details.
14      
15              You should have received a copy of the GNU General Public License
16              along with this program; if not, write to the Free Software
17              Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19  contacts:  Andrzej.Salwicki@univ-pau.fr
20
21 or             Andrzej Salwicki
22                 LITA   Departement d'Informatique
23                 Universite de Pau
24                 Avenue de l'Universite
25                 64000 Pau   FRANCE
26                  tel.  ++33 59923154    fax. ++33 59841696
27
28 =======================================================================
29 */
30
31 #include "depend.h"
32 #include "genint.h"
33 #include "int.h"
34 #include "process.h"
35 #include "intproto.h"
36
37
38 #include <time.h>
39
40 /**
41  * @file
42  * @brief Utility routines
43  */
44
45 /**
46  * Compute entier (floor)
47  */
48 word entier(double x)
49 {
50         word i;
51
52         if (x >= 0.0) {
53                 i = (word)x;
54                 return i;
55         } else {
56                 i = (word)(-x);
57                 i = -i;
58                 if ((double)i <= x)
59                         return i;
60                 else
61                         return i - 1;
62         }
63 }
64
65 /**
66  * shift x by n bits
67  */
68 word shift(word x, word n)
69 {
70         if (n == 0)
71                 return (x);
72         if (n > 0)
73                 return (x << n);
74         else
75                 return ((x >> -n) & ~(~(word)0 << (8 * sizeof(word) + n)));
76 }
77
78
79 /**
80  * Get ASCIIZ string from arrayof char
81  */
82 char *asciiz(virtaddr *virt)
83 {
84         word am;
85         int len, i;
86         char *cp;
87
88         if (member(virt, &am)) {
89                 /* length of the string */
90                 len = M[am] - 3;
91                 /* allocate buffer for the string */
92                 cp = ballocate(len + 1);
93
94                 if (cp == NULL)
95                         errsignal(RTEMEMOV);
96
97                 for (i = 0; i < len; i++)
98                         cp[i] = (char) M[am + 3 + i];
99
100                 /* terminate string with 0 byte */
101                 cp[len] = '\0';
102                 return cp;
103         }
104         else {
105                 /* reference to none */
106                 errsignal(RTEREFTN);
107         }
108 }
109
110 /**
111  * Add extension to a file name
112  */
113 void addext(char *fname, char *ext)
114 {
115         char *cp;
116
117         cp = fname;
118         while (*cp != '\0' && *cp != '.')
119                 cp++;
120         strcpy(cp, ext);
121 }
122
123 void usage()
124 {
125 }
126
127 /**
128  * Print error message and abort
129  */
130 void abend(char *msg)
131 {
132         fprintf(stderr, "Error: %s\n", msg);
133         exit(8);
134 }
135
136
137 /* Pseudo random number generator */
138
139 static int ranpat1 = 7, ranpat2 = 503, ranpat3 = 15661;
140
141 /**
142  * Initializes random number generator
143  */
144 void ranset()
145 {
146         long tim;
147
148         time(&tim);
149         ranpat1 = tim % 30269;
150         ranpat2 = tim % 30307;
151         ranpat3 = tim % 30323;
152 }
153
154 /**
155  * Produces next pseudo random number
156  *
157  * @see ranset must be invoked before running this function
158  */
159 double prandom()
160 {
161         int i;
162         double r;
163
164         ranpat1 = 171 * (ranpat1 % 177) - 2 * (ranpat1 / 177);
165         if (ranpat1 < 0)
166                 ranpat1 += 30269;
167
168         ranpat2 = 172 * (ranpat2 % 176) - 35 * (ranpat2 / 176);
169
170         if (ranpat2 < 0)
171                 ranpat2 += 30307;
172
173         ranpat3 = 170 * (ranpat3 % 178) - 63 * (ranpat3 / 178);
174
175         if (ranpat3 < 0)
176                 ranpat3 += 30323;
177
178         r = ranpat1 / 30269.0 + ranpat2 / 30307.0 + ranpat3 / 30323.0;
179         i = (int)r;
180         return r - i;
181 }
182
183 /**
184  * Copy a block of memory
185  *
186  * @param from[in] source block to copy from
187  * @param to[out] destination block to copy to
188  * @param len number of character to copy
189  */
190 void moveblock(char *from, char *to, word len)
191 {
192         while (len-- > 0) {
193                 *to++ = *from++;
194         }
195 }
196
197 /**************************************************************
198
199 #define LINE 10
200 void dump(word pix, word from, int len)
201 {
202         int i;
203         memory M;
204
205         M = process[pix].M;
206         while (len > 0) {
207                 printf("%6ld: ", (long) from);
208                 for (i = 0; i < LINE; i++)
209                         printf("%7ld", (long)M[from++]);
210                 putchar('\n');
211                 len -= LINE;
212         }
213 }
214  **************************************************************/
215