[gforth] / gforth / Attic / primitives  

gforth: gforth/Attic/primitives


1 : anton 1.46 \ Gforth primitives
2 :    
3 : anton 1.51 \ Copyright (C) 1995,1996 Free Software Foundation, Inc.
4 : anton 1.46
5 :     \ This file is part of Gforth.
6 :    
7 :     \ Gforth is free software; you can redistribute it and/or
8 :     \ modify it under the terms of the GNU General Public License
9 :     \ as published by the Free Software Foundation; either version 2
10 :     \ of the License, or (at your option) any later version.
11 :    
12 :     \ This program is distributed in the hope that it will be useful,
13 :     \ but WITHOUT ANY WARRANTY; without even the implied warranty of
14 :     \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 :     \ GNU General Public License for more details.
16 :    
17 :     \ You should have received a copy of the GNU General Public License
18 :     \ along with this program; if not, write to the Free Software
19 :     \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 :    
21 :    
22 : anton 1.6 \ WARNING: This file is processed by m4. Make sure your identifiers
23 :     \ don't collide with m4's (e.g. by undefining them).
24 :     \
25 : pazsan 1.23 \
26 :     \
27 : anton 1.48 \ This file contains primitive specifications in the following format:
28 : anton 1.6 \
29 : pazsan 1.23 \ forth name stack effect category [pronunciation]
30 : anton 1.6 \ [""glossary entry""]
31 :     \ C code
32 :     \ [:
33 :     \ Forth code]
34 :     \
35 : anton 1.48 \ prims2x is pedantic about tabs vs. blanks. The fields of the first
36 :     \ line of a primitive are separated by tabs, the stack items in a
37 :     \ stack effect by blanks.
38 :     \
39 :     \ Both pronounciation and stack items (in the stack effect) must
40 :     \ conform to the C name syntax or the C compiler will complain.
41 : pazsan 1.23 \
42 :     \
43 : anton 1.48 \ These specifications are automatically translated into C-code for the
44 : pazsan 1.23 \ interpreter and into some other files. I hope that your C compiler has
45 : anton 1.6 \ decent optimization, otherwise the automatically generated code will
46 :     \ be somewhat slow. The Forth version of the code is included for manual
47 :     \ compilers, so they will need to compile only the important words.
48 :     \
49 :     \ Note that stack pointer adjustment is performed according to stack
50 :     \ effect by automatically generated code and NEXT is automatically
51 :     \ appended to the C code. Also, you can use the names in the stack
52 :     \ effect in the C code. Stack access is automatic. One exception: if
53 :     \ your code does not fall through, the results are not stored into the
54 :     \ stack. Use different names on both sides of the '--', if you change a
55 :     \ value (some stores to the stack are optimized away).
56 :     \
57 : pazsan 1.23 \
58 :     \
59 : anton 1.6 \ The stack variables have the following types:
60 : pazsan 1.23 \
61 : anton 1.6 \ name matches type
62 :     \ f.* Bool
63 :     \ c.* Char
64 :     \ [nw].* Cell
65 :     \ u.* UCell
66 :     \ d.* DCell
67 :     \ ud.* UDCell
68 :     \ r.* Float
69 :     \ a_.* Cell *
70 :     \ c_.* Char *
71 :     \ f_.* Float *
72 :     \ df_.* DFloat *
73 :     \ sf_.* SFloat *
74 :     \ xt.* XT
75 :     \ wid.* WID
76 :     \ f83name.* F83Name *
77 :     \
78 : pazsan 1.23 \
79 :     \
80 : anton 1.6 \ In addition the following names can be used:
81 :     \ ip the instruction pointer
82 :     \ sp the data stack pointer
83 :     \ rp the parameter stack pointer
84 : pazsan 1.23 \ lp the locals stack pointer
85 : anton 1.6 \ NEXT executes NEXT
86 :     \ cfa
87 :     \ NEXT1 executes NEXT1
88 :     \ FLAG(x) makes a Forth flag from a C flag
89 :     \
90 : pazsan 1.23 \
91 :     \
92 : anton 1.6 \ Percentages in comments are from Koopmans book: average/maximum use
93 : pazsan 1.23 \ (taken from four, not very representative benchmarks)
94 :     \
95 : anton 1.6 \
96 : pazsan 1.23 \
97 : anton 1.6 \ To do:
98 :     \
99 :     \ throw execute, cfa and NEXT1 out?
100 :     \ macroize *ip, ip++, *ip++ (pipelining)?
101 : anton 1.1
102 : anton 1.6 \ these m4 macros would collide with identifiers
103 : anton 1.1 undefine(`index')
104 :     undefine(`shift')
105 :    
106 : anton 1.43 noop -- gforth
107 : anton 1.1 ;
108 : pazsan 1.18 :
109 :     ;
110 : anton 1.1
111 : anton 1.43 lit -- w gforth
112 : anton 1.35 w = (Cell)NEXT_INST;
113 :     INC_IP(1);
114 : pazsan 1.61 :
115 :     r> dup @ swap cell+ >r ;
116 : anton 1.1
117 : anton 1.42 execute xt -- core
118 : anton 1.35 ip=IP;
119 : anton 1.1 IF_TOS(TOS = sp[0]);
120 : pazsan 1.44 EXEC(xt);
121 : anton 1.1
122 : anton 1.54 perform a_addr -- gforth
123 :     ""equivalent to @code{@ execute}""
124 :     /* and pfe */
125 :     ip=IP;
126 :     IF_TOS(TOS = sp[0]);
127 :     EXEC(*(Xt *)a_addr);
128 :     :
129 :     @ execute ;
130 :    
131 : pazsan 1.61 \+has-locals [IF]
132 :    
133 : anton 1.43 branch-lp+!# -- gforth branch_lp_plus_store_number
134 : anton 1.9 /* this will probably not be used */
135 :     branch_adjust_lp:
136 : anton 1.35 lp += (Cell)(IP[1]);
137 : anton 1.9 goto branch;
138 :    
139 : pazsan 1.61 \+[THEN]
140 :    
141 : anton 1.43 branch -- gforth
142 : anton 1.1 branch:
143 : anton 1.35 ip = (Xt *)(((Cell)IP)+(Cell)NEXT_INST);
144 :     NEXT_P0;
145 : pazsan 1.18 :
146 :     r> dup @ + >r ;
147 : anton 1.1
148 : pazsan 1.61 \ condbranch(forthname,restline,code,forthcode)
149 : anton 1.35 \ this is non-syntactical: code must open a brace that is closed by the macro
150 : anton 1.9 define(condbranch,
151 :     $1 $2
152 : anton 1.35 $3 ip = (Xt *)(((Cell)IP)+(Cell)NEXT_INST);
153 :     NEXT_P0;
154 :     NEXT;
155 : anton 1.9 }
156 :     else
157 : anton 1.35 INC_IP(1);
158 : pazsan 1.61 $4
159 :    
160 :     \+has-locals [IF]
161 : anton 1.9
162 :     $1-lp+!# $2_lp_plus_store_number
163 :     $3 goto branch_adjust_lp;
164 :     }
165 :     else
166 : anton 1.35 INC_IP(2);
167 : anton 1.9
168 : pazsan 1.61 \+[THEN]
169 : anton 1.9 )
170 :    
171 :     condbranch(?branch,f -- f83 question_branch,
172 : anton 1.1 if (f==0) {
173 :     IF_TOS(TOS = sp[0]);
174 : pazsan 1.61 ,)
175 : anton 1.1
176 : anton 1.51 \ we don't need an lp_plus_store version of the ?dup-stuff, because it
177 :     \ is only used in if's (yet)
178 :    
179 : pazsan 1.62 \+has-xconds [IF]
180 :    
181 : anton 1.51 ?dup-?branch f -- f new question_dupe_question_branch
182 :     ""The run-time procedure compiled by @code{?DUP-IF}.""
183 :     if (f==0) {
184 :     sp++;
185 :     IF_TOS(TOS = sp[0]);
186 :     ip = (Xt *)(((Cell)IP)+(Cell)NEXT_INST);
187 :     NEXT_P0;
188 :     NEXT;
189 :     }
190 :     else
191 :     INC_IP(1);
192 :    
193 :     ?dup-0=-?branch f -- new question_dupe_zero_equals_question_branch
194 :     ""The run-time procedure compiled by @code{?DUP-0=-IF}.""
195 :     /* the approach taken here of declaring the word as having the stack
196 :     effect ( f -- ) and correcting for it in the branch-taken case costs a
197 :     few cycles in that case, but is easy to convert to a CONDBRANCH
198 :     invocation */
199 :     if (f!=0) {
200 :     sp--;
201 :     ip = (Xt *)(((Cell)IP)+(Cell)NEXT_INST);
202 :     NEXT_P0;
203 :     NEXT;
204 :     }
205 :     else
206 :     INC_IP(1);
207 :    
208 : pazsan 1.62 \+[THEN]
209 :    
210 : anton 1.9 condbranch((next),-- cmFORTH paren_next,
211 : anton 1.1 if ((*rp)--) {
212 : pazsan 1.61 ,:
213 :     r> r> dup 1- >r
214 :     IF dup @ + >r ELSE cell+ >r THEN ;)
215 : anton 1.1
216 : anton 1.43 condbranch((loop),-- gforth paren_loop,
217 : pazsan 1.31 Cell index = *rp+1;
218 :     Cell limit = rp[1];
219 : anton 1.1 if (index != limit) {
220 :     *rp = index;
221 : pazsan 1.61 ,:
222 :     r> r> 1+ r> 2dup =
223 :     IF >r 1- >r cell+ >r
224 :     ELSE >r >r dup @ + >r THEN ;)
225 : anton 1.1
226 : anton 1.42 condbranch((+loop),n -- gforth paren_plus_loop,
227 : anton 1.1 /* !! check this thoroughly */
228 : pazsan 1.31 Cell index = *rp;
229 : anton 1.1 /* sign bit manipulation and test: (x^y)<0 is equivalent to (x<0) != (y<0) */
230 :     /* dependent upon two's complement arithmetic */
231 : pazsan 1.31 Cell olddiff = index-rp[1];
232 : anton 1.9 if ((olddiff^(olddiff+n))>=0 /* the limit is not crossed */
233 :     || (olddiff^n)>=0 /* it is a wrap-around effect */) {
234 : pazsan 1.15 #ifdef i386
235 :     *rp += n;
236 :     #else
237 :     *rp = index + n;
238 :     #endif
239 : anton 1.1 IF_TOS(TOS = sp[0]);
240 : pazsan 1.61 ,:
241 :     r> swap
242 :     r> r> 2dup - >r
243 :     2 pick r@ + r@ xor 0< 0=
244 :     3 pick r> xor 0< 0= or
245 :     IF >r + >r dup @ + >r
246 :     ELSE >r >r drop cell+ >r THEN ;)
247 : anton 1.1
248 : pazsan 1.62 \+has-xconds [IF]
249 :    
250 : anton 1.42 condbranch((-loop),u -- gforth paren_minus_loop,
251 : anton 1.41 /* !! check this thoroughly */
252 :     Cell index = *rp;
253 :     UCell olddiff = index-rp[1];
254 :     if (olddiff>u) {
255 : pazsan 1.44 #ifdef i386
256 :     *rp -= u;
257 :     #else
258 : anton 1.41 *rp = index - u;
259 : pazsan 1.44 #endif
260 : anton 1.41 IF_TOS(TOS = sp[0]);
261 : pazsan 1.61 ,)
262 : anton 1.41
263 : anton 1.42 condbranch((s+loop),n -- gforth paren_symmetric_plus_loop,
264 : anton 1.1 ""The run-time procedure compiled by S+LOOP. It loops until the index
265 :     crosses the boundary between limit and limit-sign(n). I.e. a symmetric
266 :     version of (+LOOP).""
267 :     /* !! check this thoroughly */
268 : pazsan 1.31 Cell index = *rp;
269 :     Cell diff = index-rp[1];
270 :     Cell newdiff = diff+n;
271 : anton 1.1 if (n<0) {
272 :     diff = -diff;
273 : pazsan 1.15 newdiff = -newdiff;
274 : anton 1.1 }
275 :     if (diff>=0 || newdiff<0) {
276 : pazsan 1.15 #ifdef i386
277 :     *rp += n;
278 :     #else
279 :     *rp = index + n;
280 :     #endif
281 : anton 1.1 IF_TOS(TOS = sp[0]);
282 : pazsan 1.61 ,)
283 : anton 1.1
284 : pazsan 1.62 \+[THEN]
285 :    
286 : anton 1.1 unloop -- core
287 :     rp += 2;
288 : pazsan 1.18 :
289 :     r> rdrop rdrop >r ;
290 : anton 1.1
291 :     (for) ncount -- cmFORTH paren_for
292 :     /* or (for) = >r -- collides with unloop! */
293 :     *--rp = 0;
294 :     *--rp = ncount;
295 : pazsan 1.18 :
296 :     r> swap 0 >r >r >r ;
297 : anton 1.1
298 : anton 1.43 (do) nlimit nstart -- gforth paren_do
299 : anton 1.1 /* or do it in high-level? 0.09/0.23% */
300 :     *--rp = nlimit;
301 :     *--rp = nstart;
302 :     :
303 : pazsan 1.63 r> swap rot >r >r >r ;
304 : anton 1.1
305 : anton 1.43 (?do) nlimit nstart -- gforth paren_question_do
306 : anton 1.1 *--rp = nlimit;
307 :     *--rp = nstart;
308 :     if (nstart == nlimit) {
309 :     IF_TOS(TOS = sp[0]);
310 :     goto branch;
311 :     }
312 :     else {
313 : anton 1.35 INC_IP(1);
314 : anton 1.1 }
315 : pazsan 1.61 :
316 : pazsan 1.62 2dup =
317 :     IF r> swap rot >r >r
318 :     dup @ + >r
319 :     ELSE r> swap rot >r >r
320 :     cell+ >r
321 :     THEN ; \ --> CORE-EXT
322 : anton 1.1
323 : pazsan 1.63 \+has-xconds [IF]
324 :    
325 : anton 1.43 (+do) nlimit nstart -- gforth paren_plus_do
326 : anton 1.41 *--rp = nlimit;
327 :     *--rp = nstart;
328 :     if (nstart >= nlimit) {
329 :     IF_TOS(TOS = sp[0]);
330 :     goto branch;
331 :     }
332 :     else {
333 :     INC_IP(1);
334 :     }
335 : pazsan 1.61 :
336 :     swap 2dup
337 :     r> swap >r swap >r
338 :     >=
339 :     IF
340 :     dup @ +
341 :     ELSE
342 :     cell+
343 :     THEN >r ;
344 : anton 1.41
345 : anton 1.43 (u+do) ulimit ustart -- gforth paren_u_plus_do
346 : anton 1.41 *--rp = ulimit;
347 :     *--rp = ustart;
348 :     if (ustart >= ulimit) {
349 :     IF_TOS(TOS = sp[0]);
350 :     goto branch;
351 :     }
352 :     else {
353 :     INC_IP(1);
354 :     }
355 : pazsan 1.61 :
356 :     swap 2dup
357 :     r> swap >r swap >r
358 :     u>=
359 :     IF
360 :     dup @ +
361 :     ELSE
362 :     cell+
363 :     THEN >r ;
364 : anton 1.41
365 : anton 1.43 (-do) nlimit nstart -- gforth paren_minus_do
366 : anton 1.41 *--rp = nlimit;
367 :     *--rp = nstart;
368 :     if (nstart <= nlimit) {
369 :     IF_TOS(TOS = sp[0]);
370 :     goto branch;
371 :     }
372 :     else {
373 :     INC_IP(1);
374 :     }
375 : pazsan 1.61 :
376 :     swap 2dup
377 :     r> swap >r swap >r
378 :     <=
379 :     IF
380 :     dup @ +
381 :     ELSE
382 :     cell+
383 :     THEN >r ;
384 : anton 1.41
385 : anton 1.43 (u-do) ulimit ustart -- gforth paren_u_minus_do
386 : anton 1.41 *--rp = ulimit;
387 :     *--rp = ustart;
388 :     if (ustart <= ulimit) {
389 :     IF_TOS(TOS = sp[0]);
390 :     goto branch;
391 :     }
392 :     else {
393 :     INC_IP(1);
394 :     }
395 : pazsan 1.61 :
396 :     swap 2dup
397 :     r> swap >r swap >r
398 :     u<=
399 :     IF
400 :     dup @ +
401 :     ELSE
402 :     cell+
403 :     THEN >r ;
404 : anton 1.41
405 : pazsan 1.63 \+[THEN]
406 :    
407 : anton 1.42 i -- n core
408 : anton 1.1 n = *rp;
409 : pazsan 1.61 :
410 :     rp@ cell+ @ ;
411 :    
412 :     i' -- w gforth i_tick
413 :     ""loop end value""
414 :     w = rp[1];
415 :     :
416 :     rp@ cell+ cell+ @ ;
417 : anton 1.1
418 :     j -- n core
419 :     n = rp[2];
420 : pazsan 1.61 :
421 :     rp@ cell+ cell+ cell+ @ ;
422 : anton 1.1
423 : anton 1.6 \ digit is high-level: 0/0%
424 : anton 1.1
425 :     move c_from c_to ucount -- core
426 :     memmove(c_to,c_from,ucount);
427 : anton 1.6 /* make an Ifdef for bsd and others? */
428 : pazsan 1.18 :
429 :     >r 2dup u< IF r> cmove> ELSE r> cmove THEN ;
430 : anton 1.1
431 :     cmove c_from c_to u -- string
432 :     while (u-- > 0)
433 :     *c_to++ = *c_from++;
434 : pazsan 1.18 :
435 :     bounds ?DO dup c@ I c! 1+ LOOP drop ;
436 : anton 1.1
437 :     cmove> c_from c_to u -- string c_move_up
438 :     while (u-- > 0)
439 :     c_to[u] = c_from[u];
440 : pazsan 1.18 :
441 :     dup 0= IF drop 2drop exit THEN
442 :     rot over + -rot bounds swap 1-
443 :     DO 1- dup c@ I c! -1 +LOOP drop ;
444 : anton 1.1
445 :     fill c_addr u c -- core
446 :     memset(c_addr,c,u);
447 : pazsan 1.18 :
448 :     -rot bounds
449 :     ?DO dup I c! LOOP drop ;
450 : anton 1.1
451 :     compare c_addr1 u1 c_addr2 u2 -- n string
452 : anton 1.26 ""Compare the strings lexicographically. If they are equal, n is 0; if
453 :     the first string is smaller, n is -1; if the first string is larger, n
454 :     is 1. Currently this is based on the machine's character
455 :     comparison. In the future, this may change to considering the current
456 :     locale and its collation order.""
457 : anton 1.1 n = memcmp(c_addr1, c_addr2, u1<u2 ? u1 : u2);
458 :     if (n==0)
459 :     n = u1-u2;
460 :     if (n<0)
461 :     n = -1;
462 :     else if (n>0)
463 :     n = 1;
464 : pazsan 1.18 :
465 :     rot 2dup - >r min swap -text dup
466 :     IF rdrop
467 :     ELSE drop r@ 0>
468 :     IF rdrop -1
469 :     ELSE r> 1 and
470 :     THEN
471 :     THEN ;
472 : anton 1.1
473 :     -text c_addr1 u c_addr2 -- n new dash_text
474 :     n = memcmp(c_addr1, c_addr2, u);
475 :     if (n<0)
476 :     n = -1;
477 :     else if (n>0)
478 :     n = 1;
479 : pazsan 1.18 :
480 :     swap bounds
481 :     ?DO dup c@ I c@ = WHILE 1+ LOOP drop 0
482 :     ELSE c@ I c@ - unloop THEN -text-flag ;
483 :     : -text-flag ( n -- -1/0/1 )
484 : pazsan 1.61 dup 0< IF drop -1 ELSE 0> 1 and THEN ;
485 :    
486 :     toupper c1 -- c2 gforth
487 :     c2 = toupper(c1);
488 :     :
489 :     dup [char] a - [ char z char a - 1 + ] Literal u< bl and - ;
490 : anton 1.1
491 :     capscomp c_addr1 u c_addr2 -- n new
492 : anton 1.56 n = memcasecmp(c_addr1, c_addr2, u); /* !! use something that works in all locales */
493 :     if (n<0)
494 :     n = -1;
495 :     else if (n>0)
496 :     n = 1;
497 : pazsan 1.18 :
498 :     swap bounds
499 : pazsan 1.61 ?DO dup c@ I c@ <>
500 :     IF dup c@ toupper I c@ toupper =
501 :     ELSE true THEN WHILE 1+ LOOP drop 0
502 : pazsan 1.18 ELSE c@ toupper I c@ toupper - unloop THEN -text-flag ;
503 : anton 1.1
504 :     -trailing c_addr u1 -- c_addr u2 string dash_trailing
505 :     u2 = u1;
506 :     while (c_addr[u2-1] == ' ')
507 :     u2--;
508 : pazsan 1.18 :
509 :     BEGIN 1- 2dup + c@ bl = WHILE
510 :     dup 0= UNTIL ELSE 1+ THEN ;
511 : anton 1.1
512 :     /string c_addr1 u1 n -- c_addr2 u2 string slash_string
513 :     c_addr2 = c_addr1+n;
514 :     u2 = u1-n;
515 : pazsan 1.18 :
516 :     tuck - >r + r> dup 0< IF - 0 THEN ;
517 : anton 1.1
518 : anton 1.42 + n1 n2 -- n core plus
519 : anton 1.1 n = n1+n2;
520 : anton 1.54
521 : anton 1.60 \ PFE-0.9.14 has it differently, but the next release will have it as follows
522 :     under+ n1 n2 n3 -- n n2 gforth under_plus
523 :     ""add @var{n3} to @var{n1} (giving @var{n})""
524 :     n = n1+n3;
525 :     :
526 :     rot + swap ;
527 : anton 1.1
528 : anton 1.42 - n1 n2 -- n core minus
529 : anton 1.1 n = n1-n2;
530 : pazsan 1.18 :
531 :     negate + ;
532 : anton 1.1
533 : anton 1.42 negate n1 -- n2 core
534 : anton 1.1 /* use minus as alias */
535 :     n2 = -n1;
536 : pazsan 1.18 :
537 :     invert 1+ ;
538 : anton 1.1
539 :     1+ n1 -- n2 core one_plus
540 :     n2 = n1+1;
541 : pazsan 1.18 :
542 :     1 + ;
543 : anton 1.1
544 :     1- n1 -- n2 core one_minus
545 :     n2 = n1-1;
546 : pazsan 1.18 :
547 :     1 - ;
548 : anton 1.1
549 :     max n1 n2 -- n core
550 :     if (n1<n2)
551 :     n = n2;
552 :     else
553 :     n = n1;
554 :     :
555 : pazsan 1.18 2dup < IF swap THEN drop ;
556 : anton 1.1
557 :     min n1 n2 -- n core
558 :     if (n1<n2)
559 :     n = n1;
560 :     else
561 :     n = n2;
562 : pazsan 1.18 :
563 :     2dup > IF swap THEN drop ;
564 : anton 1.1
565 :     abs n1 -- n2 core
566 :     if (n1<0)
567 :     n2 = -n1;
568 :     else
569 :     n2 = n1;
570 : pazsan 1.18 :
571 :     dup 0< IF negate THEN ;
572 : anton 1.1
573 : anton 1.42 * n1 n2 -- n core star
574 : anton 1.1 n = n1*n2;
575 : pazsan 1.18 :
576 :     um* drop ;
577 : anton 1.1
578 : anton 1.42 / n1 n2 -- n core slash
579 : anton 1.1 n = n1/n2;
580 : pazsan 1.18 :
581 :     /mod nip ;
582 : anton 1.1
583 :     mod n1 n2 -- n core
584 :     n = n1%n2;
585 : pazsan 1.18 :
586 :     /mod drop ;
587 : anton 1.1
588 :     /mod n1 n2 -- n3 n4 core slash_mod
589 :     n4 = n1/n2;
590 :     n3 = n1%n2; /* !! is this correct? look into C standard! */
591 : pazsan 1.18 :
592 :     >r s>d r> fm/mod ;
593 : anton 1.1
594 :     2* n1 -- n2 core two_star
595 :     n2 = 2*n1;
596 : pazsan 1.18 :
597 :     dup + ;
598 : anton 1.1
599 :     2/ n1 -- n2 core two_slash
600 :     /* !! is this still correct? */
601 :     n2 = n1>>1;
602 : pazsan 1.64 :
603 : pazsan 1.66 dup MINI and IF 1 ELSE 0 THEN
604 : pazsan 1.64 [ bits/byte cell * 1- ] literal
605 :     0 DO 2* swap dup 2* >r U-HIGHBIT and
606 :     IF 1 ELSE 0 THEN or r> swap
607 :     LOOP nip ;
608 : anton 1.1
609 :     fm/mod d1 n1 -- n2 n3 core f_m_slash_mod
610 :     ""floored division: d1 = n3*n1+n2, n1>n2>=0 or 0>=n2>n1""
611 : anton 1.52 #ifdef BUGGY_LONG_LONG
612 :     DCell r = fmdiv(d1,n1);
613 :     n2=r.hi;
614 :     n3=r.lo;
615 :     #else
616 : anton 1.1 /* assumes that the processor uses either floored or symmetric division */
617 :     n3 = d1/n1;
618 :     n2 = d1%n1;
619 :     /* note that this 1%-3>0 is optimized by the compiler */
620 :     if (1%-3>0 && (d1<0) != (n1<0) && n2!=0) {
621 :     n3--;
622 :     n2+=n1;
623 :     }
624 : anton 1.52 #endif
625 : pazsan 1.53 :
626 :     dup >r dup 0< IF negate >r dnegate r> THEN
627 :     over 0< IF tuck + swap THEN
628 :     um/mod
629 :     r> 0< IF swap negate swap THEN ;
630 : anton 1.1
631 :     sm/rem d1 n1 -- n2 n3 core s_m_slash_rem
632 :     ""symmetric division: d1 = n3*n1+n2, sign(n2)=sign(d1) or 0""
633 : anton 1.52 #ifdef BUGGY_LONG_LONG
634 :     DCell r = smdiv(d1,n1);
635 :     n2=r.hi;
636 :     n3=r.lo;
637 :     #else
638 : anton 1.1 /* assumes that the processor uses either floored or symmetric division */
639 :     n3 = d1/n1;
640 :     n2 = d1%n1;
641 :     /* note that this 1%-3<0 is optimized by the compiler */
642 :     if (1%-3<0 && (d1<0) != (n1<0) && n2!=0) {
643 :     n3++;
644 :     n2-=n1;
645 :     }
646 : anton 1.52 #endif
647 : pazsan 1.18 :
648 :     over >r dup >r abs -rot
649 :     dabs rot um/mod
650 : pazsan 1.53 r> r@ xor 0< IF negate THEN
651 :     r> 0< IF swap negate swap THEN ;
652 : anton 1.1
653 :     m* n1 n2 -- d core m_star
654 : anton 1.52 #ifdef BUGGY_LONG_LONG
655 :     d = mmul(n1,n2);
656 :     #else
657 : anton 1.1 d = (DCell)n1 * (DCell)n2;
658 : anton 1.52 #endif
659 : pazsan 1.18 :
660 :     2dup 0< and >r
661 :     2dup swap 0< and >r
662 :     um* r> - r> - ;
663 : anton 1.1
664 :     um* u1 u2 -- ud core u_m_star
665 :     /* use u* as alias */
666 : anton 1.52 #ifdef BUGGY_LONG_LONG
667 :     ud = ummul(u1,u2);
668 :     #else
669 : anton 1.1 ud = (UDCell)u1 * (UDCell)u2;
670 : anton 1.52 #endif
671 : pazsan 1.61 :
672 :     >r >r 0 0 r> r> [ 8 cells ] literal 0
673 :     DO
674 :     over >r dup >r 0< and d2*+ drop
675 :     r> 2* r> swap
676 :     LOOP 2drop ;
677 :     : d2*+ ( ud n -- ud+n c )
678 : pazsan 1.63 over MINI
679 :     and >r >r 2dup d+ swap r> + swap r> ;
680 : anton 1.1
681 :     um/mod ud u1 -- u2 u3 core u_m_slash_mod
682 : anton 1.52 #ifdef BUGGY_LONG_LONG
683 :     UDCell r = umdiv(ud,u1);
684 :     u2=r.hi;
685 :     u3=r.lo;
686 :     #else
687 : anton 1.1 u3 = ud/u1;
688 :     u2 = ud%u1;
689 : anton 1.52 #endif
690 : pazsan 1.19 :
691 : pazsan 1.61 0 swap [ 8 cells 1 + ] literal 0
692 :     ?DO >r /modstep r>
693 :     LOOP drop swap 1 rshift or swap ;
694 :     : /modstep ( ud c R: u -- ud-?u c R: u )
695 :     over I' u< 0= or IF I' - 1 ELSE 0 THEN d2*+ ;
696 : pazsan 1.63 : d2*+ ( ud n -- ud+n c )
697 :     over MINI
698 :     and >r >r 2dup d+ swap r> + swap r> ;
699 : anton 1.1
700 :     m+ d1 n -- d2 double m_plus
701 : anton 1.51 #ifdef BUGGY_LONG_LONG
702 : anton 1.52 d2.lo = d1.lo+n;
703 :     d2.hi = d1.hi - (n<0) + (d2.lo<d1.lo);
704 : anton 1.51 #else
705 : anton 1.1 d2 = d1+n;
706 : anton 1.51 #endif
707 : pazsan 1.18 :
708 :     s>d d+ ;
709 : anton 1.1
710 : anton 1.42 d+ d1 d2 -- d double d_plus
711 : anton 1.51 #ifdef BUGGY_LONG_LONG
712 : anton 1.52 d.lo = d1.lo+d2.lo;
713 :     d.hi = d1.hi + d2.hi + (d.lo<d1.lo);
714 : anton 1.51 #else
715 : anton 1.1 d = d1+d2;
716 : anton 1.51 #endif
717 : pazsan 1.18 :
718 : pazsan 1.53 rot + >r tuck + swap over u> r> swap - ;
719 : anton 1.1
720 :     d- d1 d2 -- d double d_minus
721 : anton 1.51 #ifdef BUGGY_LONG_LONG
722 : anton 1.52 d.lo = d1.lo - d2.lo;
723 :     d.hi = d1.hi-d2.hi-(d1.lo<d2.lo);
724 : anton 1.51 #else
725 : anton 1.1 d = d1-d2;
726 : anton 1.51 #endif
727 : pazsan 1.18 :
728 :     dnegate d+ ;
729 : anton 1.1
730 :     dnegate d1 -- d2 double
731 :     /* use dminus as alias */
732 : anton 1.51 #ifdef BUGGY_LONG_LONG
733 : anton 1.52 d2 = dnegate(d1);
734 : anton 1.51 #else
735 : anton 1.1 d2 = -d1;
736 : anton 1.51 #endif
737 : pazsan 1.18 :
738 :     invert swap negate tuck 0= - ;
739 : anton 1.1
740 :     d2* d1 -- d2 double d_two_star
741 : anton 1.51 #ifdef BUGGY_LONG_LONG
742 : anton 1.52 d2.lo = d1.lo<<1;
743 :     d2.hi = (d1.hi<<1) | (d1.lo>>(CELL_BITS-1));
744 : anton 1.51 #else
745 : anton 1.1 d2 = 2*d1;
746 : anton 1.51 #endif
747 : pazsan 1.18 :
748 :     2dup d+ ;
749 : anton 1.1
750 :     d2/ d1 -- d2 double d_two_slash
751 : anton 1.51 #ifdef BUGGY_LONG_LONG
752 : anton 1.52 d2.hi = d1.hi>>1;
753 :     d2.lo= (d1.lo>>1) | (d1.hi<<(CELL_BITS-1));
754 : anton 1.51 #else
755 : pazsan 1.13 d2 = d1>>1;
756 : anton 1.51 #endif
757 : pazsan 1.18 :
758 :     dup 1 and >r 2/ swap 2/ [ 1 8 cells 1- lshift 1- ] Literal and
759 :     r> IF [ 1 8 cells 1- lshift ] Literal + THEN swap ;
760 : anton 1.1
761 : anton 1.42 and w1 w2 -- w core
762 : anton 1.1 w = w1&w2;
763 :    
764 : anton 1.42 or w1 w2 -- w core
765 : anton 1.1 w = w1|w2;
766 : pazsan 1.64 :
767 :     invert swap invert and invert ;
768 : anton 1.1
769 : anton 1.42 xor w1 w2 -- w core
770 : anton 1.1 w = w1^w2;
771 :    
772 :     invert w1 -- w2 core
773 :     w2 = ~w1;
774 : pazsan 1.18 :
775 : pazsan 1.64 MAXU xor ;
776 : anton 1.1
777 :     rshift u1 n -- u2 core
778 :     u2 = u1>>n;
779 : pazsan 1.62 :
780 :     0 ?DO 2/ MAXI and LOOP ;
781 : anton 1.1
782 :     lshift u1 n -- u2 core
783 :     u2 = u1<<n;
784 : pazsan 1.62 :
785 :     0 ?DO 2* LOOP ;
786 : anton 1.1
787 : anton 1.6 \ comparisons(prefix, args, prefix, arg1, arg2, wordsets...)
788 : anton 1.1 define(comparisons,
789 :     $1= $2 -- f $6 $3equals
790 :     f = FLAG($4==$5);
791 : pazsan 1.62 :
792 :     [ char $1x char 0 = [IF]
793 :     ] IF false ELSE true THEN [
794 :     [ELSE]
795 :     ] xor 0= [
796 :     [THEN] ] ;
797 : anton 1.1
798 :     $1<> $2 -- f $7 $3different
799 :     f = FLAG($4!=$5);
800 : pazsan 1.62 :
801 :     [ char $1x char 0 = [IF]
802 :     ] IF true ELSE false THEN [
803 :     [ELSE]
804 :     ] xor 0<> [
805 :     [THEN] ] ;
806 : anton 1.1
807 :     $1< $2 -- f $8 $3less
808 :     f = FLAG($4<$5);
809 : pazsan 1.62 :
810 :     [ char $1x char 0 = [IF]
811 :     ] MINI and 0<> [
812 :     [ELSE] char $1x char u = [IF]
813 :     ] 2dup xor 0< IF nip ELSE - THEN 0< [
814 :     [ELSE]
815 :     ] MINI xor >r MINI xor r> u< [
816 :     [THEN]
817 :     [THEN] ] ;
818 : anton 1.1
819 :     $1> $2 -- f $9 $3greater
820 :     f = FLAG($4>$5);
821 : pazsan 1.62 :
822 :     [ char $1x char 0 = [IF] ] negate [ [ELSE] ] swap [ [THEN] ]
823 :     $1< ;
824 : anton 1.1
825 : anton 1.43 $1<= $2 -- f gforth $3less_or_equal
826 : anton 1.1 f = FLAG($4<=$5);
827 : pazsan 1.62 :
828 :     $1> 0= ;
829 : anton 1.1
830 : anton 1.43 $1>= $2 -- f gforth $3greater_or_equal
831 : anton 1.1 f = FLAG($4>=$5);
832 : pazsan 1.62 :
833 :     [ char $1x char 0 = [IF] ] negate [ [ELSE] ] swap [ [THEN] ]
834 :     $1<= ;
835 : anton 1.1
836 :     )
837 :    
838 :     comparisons(0, n, zero_, n, 0, core, core-ext, core, core-ext)
839 :     comparisons(, n1 n2, , n1, n2, core, core-ext, core, core)
840 : anton 1.43 comparisons(u, u1 u2, u_, u1, u2, gforth, gforth, core, core-ext)
841 : anton 1.52
842 :     \ dcomparisons(prefix, args, prefix, arg1, arg2, wordsets...)
843 :     define(dcomparisons,
844 :     $1= $2 -- f $6 $3equals
845 :     #ifdef BUGGY_LONG_LONG
846 :     f = FLAG($4.lo==$5.lo && $4.hi==$5.hi);
847 :     #else
848 :     f = FLAG($4==$5);
849 :     #endif
850 :    
851 :     $1<> $2 -- f $7 $3different
852 :     #ifdef BUGGY_LONG_LONG
853 :     f = FLAG($4.lo!=$5.lo || $4.hi!=$5.hi);
854 :     #else
855 :     f = FLAG($4!=$5);
856 :     #endif
857 :    
858 :     $1< $2 -- f $8 $3less
859 :     #ifdef BUGGY_LONG_LONG
860 :     f = FLAG($4.hi==$5.hi ? $4.lo<$5.lo : $4.hi<$5.hi);
861 :     #else
862 :     f = FLAG($4<$5);
863 :     #endif
864 :    
865 :     $1> $2 -- f $9 $3greater
866 :     #ifdef BUGGY_LONG_LONG
867 :     f = FLAG($4.hi==$5.hi ? $4.lo>$5.lo : $4.hi>$5.hi);
868 :     #else
869 :     f = FLAG($4>$5);
870 :     #endif
871 :    
872 :     $1<= $2 -- f gforth $3less_or_equal
873 :     #ifdef BUGGY_LONG_LONG
874 :     f = FLAG($4.hi==$5.hi ? $4.lo<=$5.lo : $4.hi<=$5.hi);
875 :     #else
876 :     f = FLAG($4<=$5);
877 :     #endif
878 :    
879 :     $1>= $2 -- f gforth $3greater_or_equal
880 :     #ifdef BUGGY_LONG_LONG
881 :     f = FLAG($4.hi==$5.hi ? $4.lo>=$5.lo : $4.hi>=$5.hi);
882 :     #else
883 :     f = FLAG($4>=$5);
884 :     #endif
885 :    
886 :     )
887 :    
888 : pazsan 1.62 \+has-dcomps [IF]
889 :    
890 : anton 1.52 dcomparisons(d, d1 d2, d_, d1, d2, double, gforth, double, gforth)
891 :     dcomparisons(d0, d, d_zero_, d, DZERO, double, gforth, double, gforth)
892 :     dcomparisons(du, ud1 ud2, d_u_, ud1, ud2, gforth, gforth, double-ext, gforth)
893 : anton 1.1
894 : pazsan 1.62 \+[THEN]
895 :    
896 : anton 1.1 within u1 u2 u3 -- f core-ext
897 :     f = FLAG(u1-u2 < u3-u2);
898 : pazsan 1.18 :
899 :     over - >r - r> u< ;
900 : anton 1.1
901 : anton 1.43 sp@ -- a_addr gforth spat
902 : pazsan 1.15 a_addr = sp+1;
903 : anton 1.1
904 : anton 1.43 sp! a_addr -- gforth spstore
905 : pazsan 1.15 sp = a_addr;
906 : anton 1.1 /* works with and without TOS caching */
907 :    
908 : anton 1.43 rp@ -- a_addr gforth rpat
909 : anton 1.1 a_addr = rp;
910 :    
911 : anton 1.43 rp! a_addr -- gforth rpstore
912 : anton 1.1 rp = a_addr;
913 :    
914 : pazsan 1.62 \+has-floats [IF]
915 :    
916 : anton 1.43 fp@ -- f_addr gforth fp_fetch
917 : anton 1.1 f_addr = fp;
918 :    
919 : anton 1.43 fp! f_addr -- gforth fp_store
920 : anton 1.1 fp = f_addr;
921 :    
922 : pazsan 1.62 \+[THEN]
923 :    
924 : anton 1.43 ;s -- gforth semis
925 : anton 1.1 ip = (Xt *)(*rp++);
926 : anton 1.35 NEXT_P0;
927 : anton 1.1
928 : anton 1.42 >r w -- core to_r
929 : anton 1.1 *--rp = w;
930 : pazsan 1.63 :
931 :     (>r) ;
932 :     : (>r) rp@ cell+ @ rp@ ! rp@ cell+ ! ;
933 : anton 1.1
934 : anton 1.42 r> -- w core r_from
935 : anton 1.1 w = *rp++;
936 : pazsan 1.63 :
937 :     rp@ cell+ @ rp@ @ rp@ cell+ ! (rdrop) rp@ ! ;
938 :     Create (rdrop) ' ;s A,
939 : anton 1.1
940 : anton 1.42 rdrop -- gforth
941 : anton 1.1 rp++;
942 : pazsan 1.61 :
943 :     r> r> drop >r ;
944 : anton 1.1
945 : anton 1.14 2>r w1 w2 -- core-ext two_to_r
946 :     *--rp = w1;
947 :     *--rp = w2;
948 : pazsan 1.61 :
949 :     swap r> swap >r swap >r >r ;
950 : anton 1.14
951 :     2r> -- w1 w2 core-ext two_r_from
952 :     w2 = *rp++;
953 :     w1 = *rp++;
954 : pazsan 1.61 :
955 :     r> r> swap r> swap >r swap ;
956 : anton 1.14
957 :     2r@ -- w1 w2 core-ext two_r_fetch
958 :     w2 = rp[0];
959 :     w1 = rp[1];
960 : pazsan 1.61 :
961 :     i' j ;
962 : anton 1.14
963 : anton 1.42 2rdrop -- gforth two_r_drop
964 : anton 1.14 rp+=2;
965 : pazsan 1.61 :
966 :     r> r> drop r> drop >r ;
967 : anton 1.14
968 : anton 1.42 over w1 w2 -- w1 w2 w1 core
969 : pazsan 1.61 :
970 :     sp@ cell+ @ ;
971 : anton 1.1
972 : anton 1.42 drop w -- core
973 : pazsan 1.61 :
974 :     IF THEN ;
975 : anton 1.1
976 : anton 1.42 swap w1 w2 -- w2 w1 core
977 : pazsan 1.61 :
978 :     >r (swap) ! r> (swap) @ ;
979 :     Variable (swap)
980 : anton 1.1
981 : anton 1.42 dup w -- w w core
982 : pazsan 1.61 :
983 :     sp@ @ ;
984 : anton 1.1
985 :     rot w1 w2 w3 -- w2 w3 w1 core rote
986 : pazsan 1.61 :
987 : pazsan 1.62 [ defined? (swap) [IF] ]
988 :     (swap) ! (rot) ! >r (rot) @ (swap) @ r> ;
989 : pazsan 1.61 Variable (rot)
990 : pazsan 1.62 [ELSE] ]
991 :     >r swap r> swap ;
992 :     [THEN]
993 : anton 1.1
994 : anton 1.42 -rot w1 w2 w3 -- w3 w1 w2 gforth not_rote
995 : pazsan 1.18 :
996 :     rot rot ;
997 : anton 1.1
998 :     nip w1 w2 -- w2 core-ext
999 : pazsan 1.18 :
1000 : pazsan 1.61 >r drop r> ;
1001 : anton 1.1
1002 :     tuck w1 w2 -- w2 w1 w2 core-ext
1003 : pazsan 1.18 :
1004 :     swap over ;
1005 : anton 1.1
1006 :     ?dup w -- w core question_dupe
1007 :     if (w!=0) {
1008 : pazsan 1.7 IF_TOS(*sp-- = w;)
1009 : anton 1.1 #ifndef USE_TOS
1010 : pazsan 1.7 *--sp = w;
1011 : anton 1.1 #endif
1012 :     }
1013 : pazsan 1.18 :
1014 :     dup IF dup THEN ;
1015 : anton 1.1
1016 :     pick u -- w core-ext
1017 :     w = sp[u+1];
1018 : pazsan 1.18 :
1019 :     1+ cells sp@ + @ ;
1020 : anton 1.1
1021 :     2drop w1 w2 -- core two_drop
1022 : pazsan 1.18 :
1023 :     drop drop ;
1024 : anton 1.1
1025 :     2dup w1 w2 -- w1 w2 w1 w2 core two_dupe
1026 : pazsan 1.18 :
1027 :     over over ;
1028 : anton 1.1
1029 :     2over w1 w2 w3 w4 -- w1 w2 w3 w4 w1 w2 core two_over
1030 : pazsan 1.18 :
1031 :     3 pick 3 pick ;
1032 : anton 1.1
1033 :     2swap w1 w2 w3 w4 -- w3 w4 w1 w2 core two_swap
1034 : pazsan 1.18 :
1035 : pazsan 1.61 rot >r rot r> ;
1036 : anton 1.1
1037 : anton 1.43 2rot w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2 double-ext two_rote
1038 : pazsan 1.18 :
1039 :     >r >r 2swap r> r> 2swap ;
1040 : anton 1.1
1041 : anton 1.42 2nip w1 w2 w3 w4 -- w3 w4 gforth two_nip
1042 :     :
1043 :     2swap 2drop ;
1044 :    
1045 :     2tuck w1 w2 w3 w4 -- w3 w4 w1 w2 w3 w4 gforth two_tuck
1046 :     :
1047 :     2swap 2over ;
1048 :    
1049 : anton 1.6 \ toggle is high-level: 0.11/0.42%
1050 : anton 1.1
1051 : anton 1.42 @ a_addr -- w core fetch
1052 : anton 1.1 w = *a_addr;
1053 :    
1054 : anton 1.42 ! w a_addr -- core store
1055 : anton 1.1 *a_addr = w;
1056 :    
1057 : anton 1.42 +! n a_addr -- core plus_store
1058 : anton 1.1 *a_addr += n;
1059 : pazsan 1.61 :
1060 :     tuck @ + swap ! ;
1061 : anton 1.1
1062 : anton 1.42 c@ c_addr -- c core cfetch
1063 : anton 1.1 c = *c_addr;
1064 : pazsan 1.62 :
1065 :     [ bigendian [IF] ]
1066 :     [ cell>bit 4 = [IF] ]
1067 :     dup [ 0 cell - ] Literal and @ swap 1 and
1068 :     IF $FF and ELSE 8>> THEN ;
1069 :     [ [ELSE] ]
1070 :     dup [ cell 1- ] literal and
1071 :     tuck - @ swap [ cell 1- ] literal xor
1072 :     0 ?DO 8>> LOOP $FF and
1073 :     [ [THEN] ]
1074 :     [ [ELSE] ]
1075 :     [ cell>bit 4 = [IF] ]
1076 :     dup [ 0 cell - ] Literal and @ swap 1 and
1077 :     IF 8>> ELSE $FF and THEN
1078 :     [ [ELSE] ]
1079 :     dup [ cell 1- ] literal and
1080 :     tuck - @ swap
1081 :     0 ?DO 8>> LOOP 255 and
1082 :     [ [THEN] ]
1083 :     [ [THEN] ]
1084 :     ;
1085 :     : 8>> 2/ 2/ 2/ 2/ 2/ 2/ 2/ 2/ ;
1086 : anton 1.1
1087 : anton 1.42 c! c c_addr -- core cstore
1088 : anton 1.1 *c_addr = c;
1089 : pazsan 1.62 :
1090 :     [ bigendian [IF] ]
1091 :     [ cell>bit 4 = [IF] ]
1092 :     tuck 1 and IF $FF and ELSE 8<< THEN >r
1093 : pazsan 1.63 dup -2 and @ over 1 and cells masks + @ and
1094 :     r> or swap -2 and ! ;
1095 :     Create masks $00FF , $FF00 ,
1096 :     [ELSE] ]
1097 : pazsan 1.62 dup [ cell 1- ] literal and dup
1098 :     [ cell 1- ] literal xor >r
1099 :     - dup @ $FF r@ 0 ?DO 8<< LOOP invert and
1100 : pazsan 1.63 rot $FF and r> 0 ?DO 8<< LOOP or swap ! ;
1101 :     [THEN]
1102 :     [ELSE] ]
1103 : pazsan 1.62 [ cell>bit 4 = [IF] ]
1104 :     tuck 1 and IF 8<< ELSE $FF and THEN >r
1105 : pazsan 1.63 dup -2 and @ over 1 and cells masks + @ and
1106 :     r> or swap -2 and ! ;
1107 :     Create masks $FF00 , $00FF ,
1108 :     [ELSE] ]
1109 : pazsan 1.62 dup [ cell 1- ] literal and dup >r
1110 :     - dup @ $FF r@ 0 ?DO 8<< LOOP invert and
1111 : pazsan 1.63 rot $FF and r> 0 ?DO 8<< LOOP or swap ! ;
1112 :     [THEN]
1113 :     [THEN]
1114 : pazsan 1.62 : 8<< 2* 2* 2* 2* 2* 2* 2* 2* ;
1115 : anton 1.1
1116 :     2! w1 w2 a_addr -- core two_store
1117 :     a_addr[0] = w2;
1118 :     a_addr[1] = w1;
1119 : pazsan 1.18 :
1120 :     tuck ! cell+ ! ;
1121 : anton 1.1
1122 :     2@ a_addr -- w1 w2 core two_fetch
1123 :     w2 = a_addr[0];
1124 :     w1 = a_addr[1];
1125 : pazsan 1.18 :
1126 :     dup cell+ @ swap @ ;
1127 : anton 1.1
1128 :     cell+ a_addr1 -- a_addr2 core cell_plus
1129 :     a_addr2 = a_addr1+1;
1130 : pazsan 1.18 :
1131 : pazsan 1.62 cell + ;
1132 : anton 1.1
1133 :     cells n1 -- n2 core
1134 :     n2 = n1 * sizeof(Cell);
1135 : pazsan 1.18 :
1136 : pazsan 1.62 [ cell
1137 :     2/ dup [IF] ] 2* [ [THEN]
1138 :     2/ dup [IF] ] 2* [ [THEN]
1139 :     2/ dup [IF] ] 2* [ [THEN]
1140 :     2/ dup [IF] ] 2* [ [THEN]
1141 :     drop ] ;
1142 : anton 1.1
1143 :     char+ c_addr1 -- c_addr2 core care_plus
1144 : pazsan 1.18 c_addr2 = c_addr1 + 1;
1145 :     :
1146 :     1+ ;
1147 : anton 1.1
1148 : anton 1.24 (chars) n1 -- n2 gforth paren_cares
1149 : anton 1.1 n2 = n1 * sizeof(Char);
1150 : pazsan 1.18 :
1151 :     ;
1152 : anton 1.1
1153 :     count c_addr1 -- c_addr2 u core
1154 :     u = *c_addr1;
1155 :     c_addr2 = c_addr1+1;
1156 : pazsan 1.18 :
1157 :     dup 1+ swap c@ ;
1158 : anton 1.1
1159 :     (f83find) c_addr u f83name1 -- f83name2 new paren_f83find
1160 :     for (; f83name1 != NULL; f83name1 = f83name1->next)
1161 : pazsan 1.61 if ((UCell)F83NAME_COUNT(f83name1)==u &&
1162 : anton 1.56 memcasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
1163 : pazsan 1.8 break;
1164 :     f83name2=f83name1;
1165 : pazsan 1.18 :
1166 : pazsan 1.63 BEGIN dup WHILE (find-samelen) dup WHILE
1167 :     >r 2dup r@ cell+ char+ capscomp 0=
1168 :     IF 2drop r> EXIT THEN
1169 : pazsan 1.18 r> @
1170 : pazsan 1.63 REPEAT THEN nip nip ;
1171 :     : (find-samelen) ( u f83name1 -- u f83name2/0 )
1172 :     BEGIN 2dup cell+ c@ $1F and <> WHILE @ dup 0= UNTIL THEN ;
1173 : pazsan 1.8
1174 : pazsan 1.62 \+has-hash [IF]
1175 :    
1176 : pazsan 1.13 (hashfind) c_addr u a_addr -- f83name2 new paren_hashfind
1177 :     F83Name *f83name1;
1178 :     f83name2=NULL;
1179 :     while(a_addr != NULL)
1180 :     {
1181 :     f83name1=(F83Name *)(a_addr[1]);
1182 :     a_addr=(Cell *)(a_addr[0]);
1183 : pazsan 1.61 if ((UCell)F83NAME_COUNT(f83name1)==u &&
1184 : anton 1.56 memcasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
1185 : pazsan 1.13 {
1186 :     f83name2=f83name1;
1187 :     break;
1188 :     }
1189 :     }
1190 : pazsan 1.18 :
1191 :     BEGIN dup WHILE
1192 :     2@ >r >r dup r@ cell+ c@ $1F and =
1193 :     IF 2dup r@ cell+ char+ capscomp 0=
1194 :     IF 2drop r> rdrop EXIT THEN THEN
1195 :     rdrop r>
1196 :     REPEAT nip nip ;
1197 : pazsan 1.13
1198 : anton 1.55 (tablefind) c_addr u a_addr -- f83name2 new paren_tablefind
1199 :     ""A case-sensitive variant of @code{(hashfind)}""
1200 :     F83Name *f83name1;
1201 :     f83name2=NULL;
1202 :     while(a_addr != NULL)
1203 :     {
1204 :     f83name1=(F83Name *)(a_addr[1]);
1205 :     a_addr=(Cell *)(a_addr[0]);
1206 : pazsan 1.61 if ((UCell)F83NAME_COUNT(f83name1)==u &&
1207 : anton 1.55 memcmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
1208 :     {
1209 :     f83name2=f83name1;
1210 :     break;
1211 :     }
1212 :     }
1213 :     :
1214 :     BEGIN dup WHILE
1215 :     2@ >r >r dup r@ cell+ c@ $1F and =
1216 :     IF 2dup r@ cell+ char+ -text 0=
1217 :     IF 2drop r> rdrop EXIT THEN THEN
1218 :     rdrop r>
1219 :     REPEAT nip nip ;
1220 :    
1221 : anton 1.43 (hashkey) c_addr u1 -- u2 gforth paren_hashkey
1222 : pazsan 1.13 u2=0;
1223 :     while(u1--)
1224 : pazsan 1.30 u2+=(Cell)toupper(*c_addr++);
1225 : pazsan 1.18 :
1226 :     0 -rot bounds ?DO I c@ toupper + LOOP ;
1227 : anton 1.14
1228 : anton 1.43 (hashkey1) c_addr u ubits -- ukey gforth paren_hashkey1
1229 : anton 1.14 ""ukey is the hash key for the string c_addr u fitting in ubits bits""
1230 :     /* this hash function rotates the key at every step by rot bits within
1231 :     ubits bits and xors it with the character. This function does ok in
1232 :     the chi-sqare-test. Rot should be <=7 (preferably <=5) for
1233 :     ASCII strings (larger if ubits is large), and should share no
1234 :     divisors with ubits.
1235 :     */
1236 :     unsigned rot = ((char []){5,0,1,2,3,4,5,5,5,5,3,5,5,5,5,7,5,5,5,5,7,5,5,5,5,6,5,5,5,5,7,5,5})[ubits];
1237 :     Char *cp = c_addr;
1238 :     for (ukey=0; cp<c_addr+u; cp++)
1239 :     ukey = ((((ukey<<rot) | (ukey>>(ubits-rot)))
1240 :     ^ toupper(*cp))
1241 :     & ((1<<ubits)-1));
1242 : pazsan 1.18 :
1243 :     dup rot-values + c@ over 1 swap lshift 1- >r
1244 :     tuck - 2swap r> 0 2swap bounds
1245 :     ?DO dup 4 pick lshift swap 3 pick rshift or
1246 :     I c@ toupper xor
1247 :     over and LOOP
1248 :     nip nip nip ;
1249 :     Create rot-values
1250 :     5 c, 0 c, 1 c, 2 c, 3 c, 4 c, 5 c, 5 c, 5 c, 5 c,
1251 :     3 c, 5 c, 5 c, 5 c, 5 c, 7 c, 5 c, 5 c, 5 c, 5 c,
1252 :     7 c, 5 c, 5 c, 5 c, 5 c, 6 c, 5 c, 5 c, 5 c, 5 c,
1253 :     7 c, 5 c, 5 c,
1254 : anton 1.1
1255 : pazsan 1.62 \+[THEN]
1256 :    
1257 : anton 1.43 (parse-white) c_addr1 u1 -- c_addr2 u2 gforth paren_parse_white
1258 : anton 1.1 /* use !isgraph instead of isspace? */
1259 :     Char *endp = c_addr1+u1;
1260 :     while (c_addr1<endp && isspace(*c_addr1))
1261 :     c_addr1++;
1262 :     if (c_addr1<endp) {
1263 :     for (c_addr2 = c_addr1; c_addr1<endp && !isspace(*c_addr1); c_addr1++)
1264 :     ;
1265 :     u2 = c_addr1-c_addr2;
1266 :     }
1267 :     else {
1268 :     c_addr2 = c_addr1;
1269 :     u2 = 0;
1270 :     }
1271 : pazsan 1.18 :
1272 :     BEGIN dup WHILE over c@ bl <= WHILE 1 /string
1273 :     REPEAT THEN 2dup
1274 :     BEGIN dup WHILE over c@ bl > WHILE 1 /string
1275 :     REPEAT THEN nip - ;
1276 : anton 1.1
1277 : pazsan 1.61 aligned c_addr -- a_addr core
1278 :     a_addr = (Cell *)((((Cell)c_addr)+(sizeof(Cell)-1))&(-sizeof(Cell)));
1279 :     :
1280 :     [ cell 1- ] Literal + [ -1 cells ] Literal and ;
1281 :    
1282 :     faligned c_addr -- f_addr float f_aligned
1283 :     f_addr = (Float *)((((Cell)c_addr)+(sizeof(Float)-1))&(-sizeof(Float)));
1284 :     :
1285 :     [ 1 floats 1- ] Literal + [ -1 floats ] Literal and ;
1286 :    
1287 :     >body xt -- a_addr core to_body
1288 :     a_addr = PFA(xt);
1289 :     :
1290 :     2 cells + ;
1291 :    
1292 :     >code-address xt -- c_addr gforth to_code_address
1293 :     ""c_addr is the code address of the word xt""
1294 :     /* !! This behaves installation-dependently for DOES-words */
1295 : anton 1.67 c_addr = (Address)CODE_ADDRESS(xt);
1296 : pazsan 1.61 :
1297 :     @ ;
1298 :    
1299 :     >does-code xt -- a_addr gforth to_does_code
1300 :     ""If xt ist the execution token of a defining-word-defined word,
1301 :     a_addr is the start of the Forth code after the DOES>;
1302 :     Otherwise a_addr is 0.""
1303 :     a_addr = (Cell *)DOES_CODE(xt);
1304 :     :
1305 :     cell+ @ ;
1306 :    
1307 :     code-address! c_addr xt -- gforth code_address_store
1308 :     ""Creates a code field with code address c_addr at xt""
1309 :     MAKE_CF(xt, c_addr);
1310 :     CACHE_FLUSH(xt,PFA(0));
1311 :     :
1312 :     ! ;
1313 :    
1314 :     does-code! a_addr xt -- gforth does_code_store
1315 :     ""creates a code field at xt for a defining-word-defined word; a_addr
1316 :     is the start of the Forth code after DOES>""
1317 :     MAKE_DOES_CF(xt, a_addr);
1318 :     CACHE_FLUSH(xt,PFA(0));
1319 :     :
1320 : pazsan 1.63 dodoes: over ! cell+ ! ;
1321 : pazsan 1.61
1322 :     does-handler! a_addr -- gforth does_handler_store
1323 :     ""creates a DOES>-handler at address a_addr. a_addr usually points
1324 :     just behind a DOES>.""
1325 :     MAKE_DOES_HANDLER(a_addr);
1326 :     CACHE_FLUSH(a_addr,DOES_HANDLER_SIZE);
1327 :     :
1328 :     drop ;
1329 :    
1330 :     /does-handler -- n gforth slash_does_handler
1331 :     ""the size of a does-handler (includes possible padding)""
1332 :     /* !! a constant or environmental query might be better */
1333 :     n = DOES_HANDLER_SIZE;
1334 :     :
1335 :     2 cells ;
1336 :    
1337 :     threading-method -- n gforth threading_method
1338 : anton 1.67 ""0 if the engine is direct threaded. Note that this may change during
1339 :     the lifetime of an image.""
1340 :     #if defined(DOUBLY_INDIRECT)
1341 :     n=2;
1342 :     #else
1343 :     # if defined(DIRECT_THREADED)
1344 : pazsan 1.61 n=0;
1345 : anton 1.67 # else
1346 : pazsan 1.61 n=1;
1347 : anton 1.67 # endif
1348 : pazsan 1.61 #endif
1349 :     :
1350 :     1 ;
1351 :    
1352 :     \+has-os [IF]
1353 :    
1354 : pazsan 1.62 (key) -- n gforth paren_key
1355 :     fflush(stdout);
1356 :     /* !! noecho */
1357 :     n = key();
1358 :    
1359 :     key? -- n facility key_q
1360 :     fflush(stdout);
1361 :     n = key_query;
1362 :    
1363 :     stdout -- wfileid gforth
1364 :     wfileid = (Cell)stdout;
1365 :    
1366 :     stderr -- wfileid gforth
1367 :     wfileid = (Cell)stderr;
1368 :    
1369 :     form -- urows ucols gforth
1370 :     ""The number of lines and columns in the terminal. These numbers may change
1371 :     with the window size.""
1372 :     /* we could block SIGWINCH here to get a consistent size, but I don't
1373 :     think this is necessary or always beneficial */
1374 :     urows=rows;
1375 :     ucols=cols;
1376 :    
1377 : pazsan 1.61 flush-icache c_addr u -- gforth flush_icache
1378 :     ""Make sure that the instruction cache of the processor (if there is
1379 :     one) does not contain stale data at @var{c_addr} and @var{u} bytes
1380 :     afterwards. @code{END-CODE} performs a @code{flush-icache}
1381 :     automatically. Caveat: @code{flush-icache} might not work on your
1382 :     installation; this is usually the case if direct threading is not
1383 :     supported on your machine (take a look at your @file{machine.h}) and
1384 :     your machine has a separate instruction cache. In such cases,
1385 :     @code{flush-icache} does nothing instead of flushing the instruction
1386 :     cache.""
1387 :     FLUSH_ICACHE(c_addr,u);
1388 :    
1389 :     (bye) n -- gforth paren_bye
1390 :     return (Label *)n;
1391 :    
1392 :     (system) c_addr u -- wretval wior gforth peren_system
1393 :     int old_tp=terminal_prepped;
1394 :     deprep_terminal();
1395 :     wretval=system(cstr(c_addr,u,1)); /* ~ expansion on first part of string? */
1396 :     wior = IOR(wretval==-1 || (wretval==127 && errno != 0));
1397 :     if (old_tp)
1398 :     prep_terminal();
1399 :    
1400 :     getenv c_addr1 u1 -- c_addr2 u2 gforth
1401 :     c_addr2 = getenv(cstr(c_addr1,u1,1));
1402 :     u2 = (c_addr2 == NULL ? 0 : strlen(c_addr2));
1403 :    
1404 :     open-pipe c_addr u ntype -- wfileid wior gforth open_pipe
1405 :     wfileid=(Cell)popen(cstr(c_addr,u,1),fileattr[ntype]); /* ~ expansion of 1st arg? */
1406 :     wior = IOR(wfileid==0); /* !! the man page says that errno is not set reliably */
1407 :    
1408 :     close-pipe wfileid -- wretval wior gforth close_pipe
1409 :     wretval = pclose((FILE *)wfileid);
1410 :     wior = IOR(wretval==-1);
1411 :    
1412 :     time&date -- nsec nmin nhour nday nmonth nyear facility-ext time_and_date
1413 :     struct timeval time1;
1414 :     struct timezone zone1;
1415 :     struct tm *ltime;
1416 :     gettimeofday(&time1,&zone1);
1417 :     ltime=localtime((time_t *)&time1.tv_sec);
1418 :     nyear =ltime->tm_year+1900;
1419 :     nmonth=ltime->tm_mon+1;
1420 :     nday =ltime->tm_mday;
1421 :     nhour =ltime->tm_hour;
1422 :     nmin =ltime->tm_min;
1423 :     nsec =ltime->tm_sec;
1424 :    
1425 :     ms n -- facility-ext
1426 :     struct timeval timeout;
1427 :     timeout.tv_sec=n/1000;
1428 :     timeout.tv_usec=1000*(n%1000);
1429 :     (void)select(0,0,0,0,&timeout);
1430 :    
1431 :     allocate u -- a_addr wior memory
1432 :     a_addr = (Cell *)malloc(u?u:1);
1433 :     wior = IOR(a_addr==NULL);
1434 :    
1435 :     free a_addr -- wior memory
1436 :     free(a_addr);
1437 :     wior = 0;
1438 :    
1439 :     resize a_addr1 u -- a_addr2 wior memory
1440 :     ""Change the size of the allocated area at @i{a_addr1} to @i{u}
1441 :     address units, possibly moving the contents to a different
1442 :     area. @i{a_addr2} is the address of the resulting area. If
1443 :     @code{a_addr1} is 0, Gforth's (but not the standard) @code{resize}
1444 :     @code{allocate}s @i{u} address units.""
1445 :     /* the following check is not necessary on most OSs, but it is needed
1446 :     on SunOS 4.1.2. */
1447 :     if (a_addr1==NULL)
1448 :     a_addr2 = (Cell *)malloc(u);
1449 :     else
1450 :     a_addr2 = (Cell *)realloc(a_addr1, u);
1451 :     wior = IOR(a_addr2==NULL); /* !! Define a return code */
1452 :    
1453 :     strerror n -- c_addr u gforth
1454 :     c_addr = strerror(n);
1455 :     u = strlen(c_addr);
1456 :    
1457 :     strsignal n -- c_addr u gforth
1458 :     c_addr = strsignal(n);
1459 :     u = strlen(c_addr);
1460 :    
1461 :     call-c w -- gforth call_c
1462 :     ""Call the C function pointed to by @i{w}. The C function has to
1463 :     access the stack itself. The stack pointers are exported in the global
1464 :     variables @code{SP} and @code{FP}.""
1465 :     /* This is a first attempt at support for calls to C. This may change in
1466 :     the future */
1467 :     IF_FTOS(fp[0]=FTOS);
1468 :     FP=fp;
1469 :     SP=sp;
1470 :     ((void (*)())w)();
1471 :     sp=SP;
1472 :     fp=FP;
1473 :     IF_TOS(TOS=sp[0]);
1474 :     IF_FTOS(FTOS=fp[0]);
1475 :    
1476 :     \+[THEN] ( has-os ) has-files [IF]
1477 :    
1478 : anton 1.36 close-file wfileid -- wior file close_file
1479 :     wior = IOR(fclose((FILE *)wfileid)==EOF);
1480 : anton 1.1
1481 :     open-file c_addr u ntype -- w2 wior file open_file
1482 : anton 1.39 w2 = (Cell)fopen(tilde_cstr(c_addr, u, 1), fileattr[ntype]);
1483 : pazsan 1.40 wior = IOR(w2 == 0);
1484 : anton 1.1
1485 :     create-file c_addr u ntype -- w2 wior file create_file
1486 : pazsan 1.33 Cell fd;
1487 : pazsan 1.65 fd = open(tilde_cstr(c_addr, u, 1), O_CREAT|O_TRUNC|ufileattr[ntype], 0666);
1488 : anton 1.36 if (fd != -1) {
1489 : anton 1.1 w2 = (Cell)fdopen(fd, fileattr[ntype]);
1490 : pazsan 1.40 wior = IOR(w2 == 0);
1491 : anton 1.1 } else {
1492 :     w2 = 0;
1493 : anton 1.36 wior = IOR(1);
1494 : anton 1.1 }
1495 :    
1496 :     delete-file c_addr u -- wior file delete_file
1497 : anton 1.39 wior = IOR(unlink(tilde_cstr(c_addr, u, 1))==-1);
1498 : anton 1.1
1499 :     rename-file c_addr1 u1 c_addr2 u2 -- wior file-ext rename_file
1500 : anton 1.39 char *s1=tilde_cstr(c_addr2, u2, 1);
1501 :     wior = IOR(rename(tilde_cstr(c_addr1, u1, 0), s1)==-1);
1502 : anton 1.1
1503 :     file-position wfileid -- ud wior file file_position
1504 :     /* !! use tell and lseek? */
1505 : anton 1.52 ud = LONG2UD(ftell((FILE *)wfileid));
1506 :     wior = IOR(UD2LONG(ud)==-1);
1507 : anton 1.1
1508 :     reposition-file ud wfileid -- wior file reposition_file
1509 : anton 1.52 wior = IOR(fseek((FILE *)wfileid, UD2LONG(ud), SEEK_SET)==-1);
1510 : anton 1.1
1511 :     file-size wfileid -- ud wior file file_size
1512 :     struct stat buf;
1513 : anton 1.36 wior = IOR(fstat(fileno((FILE *)wfileid), &buf)==-1);
1514 : anton 1.52 ud = LONG2UD(buf.st_size);
1515 : anton 1.1
1516 :     resize-file ud wfileid -- wior file resize_file
1517 : anton 1.52 wior = IOR(ftruncate(fileno((FILE *)wfileid), UD2LONG(ud))==-1);
1518 : anton 1.1
1519 :     read-file c_addr u1 wfileid -- u2 wior file read_file
1520 :     /* !! fread does not guarantee enough */
1521 :     u2 = fread(c_addr, sizeof(Char), u1, (FILE *)wfileid);
1522 : pazsan 1.7 wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
1523 : anton 1.36 /* !! is the value of ferror errno-compatible? */
1524 :     if (wior)
1525 :     clearerr((FILE *)wfileid);
1526 : anton 1.1
1527 :     read-line c_addr u1 wfileid -- u2 flag wior file read_line
1528 : pazsan 1.13 /*
1529 :     Cell c;
1530 :     flag=-1;
1531 :     for(u2=0; u2<u1; u2++)
1532 :     {
1533 :     *c_addr++ = (Char)(c = getc((FILE *)wfileid));
1534 :     if(c=='\n') break;
1535 :     if(c==EOF)
1536 :     {
1537 :     flag=FLAG(u2!=0);
1538 :     break;
1539 :     }
1540 :     }
1541 :     wior=FILEIO(ferror((FILE *)wfileid));
1542 :     */
1543 :     if ((flag=FLAG(!feof((FILE *)wfileid) &&
1544 :     fgets(c_addr,u1+1,(FILE *)wfileid) != NULL))) {
1545 : anton 1.36 wior=FILEIO(ferror((FILE *)wfileid)); /* !! ior? */
1546 :     if (wior)
1547 :     clearerr((FILE *)wfileid);
1548 : pazsan 1.13 u2 = strlen(c_addr);
1549 : anton 1.11 u2-=((u2>0) && (c_addr[u2-1]==NEWLINE));
1550 :     }
1551 :     else {
1552 :     wior=0;
1553 :     u2=0;
1554 :     }
1555 : anton 1.1
1556 : pazsan 1.62 \+[THEN] has-files [IF] -1 [ELSE] has-os [THEN] [IF]
1557 :    
1558 : anton 1.1 write-file c_addr u1 wfileid -- wior file write_file
1559 :     /* !! fwrite does not guarantee enough */
1560 :     {
1561 : pazsan 1.61 UCell u2 = fwrite(c_addr, sizeof(Char), u1, (FILE *)wfileid);
1562 : pazsan 1.7 wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
1563 : anton 1.36 if (wior)
1564 :     clearerr((FILE *)wfileid);
1565 : anton 1.1 }
1566 :    
1567 : anton 1.51 emit-file c wfileid -- wior gforth emit_file
1568 :     wior = FILEIO(putc(c, (FILE *)wfileid)==EOF);
1569 :     if (wior)
1570 :     clearerr((FILE *)wfileid);
1571 :    
1572 : pazsan 1.62 \+[THEN] has-files [IF]
1573 :    
1574 : anton 1.1 flush-file wfileid -- wior file-ext flush_file
1575 : anton 1.36 wior = IOR(fflush((FILE *) wfileid)==EOF);
1576 : anton 1.1
1577 : anton 1.38 file-status c_addr u -- ntype wior file-ext file_status
1578 : anton 1.39 char *filename=tilde_cstr(c_addr, u, 1);
1579 : anton 1.38 if (access (filename, F_OK) != 0) {
1580 :     ntype=0;
1581 :     wior=IOR(1);
1582 :     }
1583 :     else if (access (filename, R_OK | W_OK) == 0) {
1584 :     ntype=2; /* r/w */
1585 :     wior=0;
1586 :     }
1587 :     else if (access (filename, R_OK) == 0) {
1588 :     ntype=0; /* r/o */
1589 :     wior=0;
1590 :     }
1591 :     else if (access (filename, W_OK) == 0) {
1592 :     ntype=4; /* w/o */
1593 :     wior=0;
1594 :     }
1595 :     else {
1596 :     ntype=1; /* well, we cannot access the file, but better deliver a legal
1597 :     access mode (r/o bin), so we get a decent error later upon open. */
1598 :     wior=0;
1599 :     }
1600 : anton 1.51
1601 : pazsan 1.61 \+[THEN] ( has-files ) has-floats [IF]
1602 :    
1603 : anton 1.43 comparisons(f, r1 r2, f_, r1, r2, gforth, gforth, float, gforth)
1604 :     comparisons(f0, r, f_zero_, r, 0., float, gforth, float, gforth)
1605 : anton 1.1
1606 :     d>f d -- r float d_to_f
1607 : anton 1.52 #ifdef BUGGY_LONG_LONG
1608 :     extern double ldexp(double x, int exp);
1609 :     r = ldexp((Float)d.hi,CELL_BITS) + (Float)d.lo;
1610 :     #else
1611 : anton 1.1 r = d;
1612 : anton 1.52 #endif
1613 : anton 1.1
1614 :     f>d r -- d float f_to_d
1615 : anton 1.52 #ifdef BUGGY_LONG_LONG
1616 :     d.hi = ldexp(r,-CELL_BITS) - (r<0);
1617 :     d.lo = r-ldexp((Float)d.hi,CELL_BITS);
1618 :     #else
1619 : anton 1.1 d = r;
1620 : anton 1.52 #endif
1621 : anton 1.1
1622 :     f! r f_addr -- float f_store
1623 :     *f_addr = r;
1624 :    
1625 :     f@ f_addr -- r float f_fetch
1626 :     r = *f_addr;
1627 :    
1628 :     df@ df_addr -- r float-ext d_f_fetch
1629 :     #ifdef IEEE_FP
1630 :     r = *df_addr;
1631 :     #else
1632 :     !! df@
1633 :     #endif
1634 :    
1635 :     df! r df_addr -- float-ext d_f_store
1636 :     #ifdef IEEE_FP
1637 :     *df_addr = r;
1638 :     #else
1639 :     !! df!
1640 :     #endif
1641 :    
1642 :     sf@ sf_addr -- r float-ext s_f_fetch
1643 :     #ifdef IEEE_FP
1644 :     r = *sf_addr;
1645 :     #else
1646 :     !! sf@
1647 :     #endif
1648 :    
1649 :     sf! r sf_addr -- float-ext s_f_store
1650 :     #ifdef IEEE_FP
1651 :     *sf_addr = r;
1652 :     #else
1653 :     !! sf!
1654 :     #endif
1655 :    
1656 :     f+ r1 r2 -- r3 float f_plus
1657 :     r3 = r1+r2;
1658 :    
1659 :     f- r1 r2 -- r3 float f_minus
1660 :     r3 = r1-r2;
1661 :    
1662 :     f* r1 r2 -- r3 float f_star
1663 :     r3 = r1*r2;
1664 :    
1665 :     f/ r1 r2 -- r3 float f_slash
1666 :     r3 = r1/r2;
1667 :    
1668 :     f** r1 r2 -- r3 float-ext f_star_star
1669 : anton 1.28 ""@i{r3} is @i{r1} raised to the @i{r2}th power""
1670 : anton 1.1 r3 = pow(r1,r2);
1671 :    
1672 :     fnegate r1 -- r2 float
1673 :     r2 = - r1;
1674 :    
1675 :     fdrop r -- float
1676 :    
1677 :     fdup r -- r r float
1678 :    
1679 :     fswap r1 r2 -- r2 r1 float
1680 :    
1681 :     fover r1 r2 -- r1 r2 r1 float
1682 :    
1683 :     frot r1 r2 r3 -- r2 r3 r1 float
1684 :    
1685 : anton 1.42 fnip r1 r2 -- r2 gforth
1686 :    
1687 :     ftuck r1 r2 -- r2 r1 r2 gforth
1688 :    
1689 : anton 1.1 float+ f_addr1 -- f_addr2 float float_plus
1690 :     f_addr2 = f_addr1+1;
1691 :    
1692 :     floats n1 -- n2 float
1693 :     n2 = n1*sizeof(Float);
1694 :    
1695 :     floor r1 -- r2 float
1696 : anton 1.28 ""round towards the next smaller integral value, i.e., round toward negative infinity""
1697 : anton 1.1 /* !! unclear wording */
1698 :     r2 = floor(r1);
1699 :    
1700 :     fround r1 -- r2 float
1701 : anton 1.28 ""round to the nearest integral value""
1702 : anton 1.1 /* !! unclear wording */
1703 : anton 1.26 #ifdef HAVE_RINT
1704 : anton 1.1 r2 = rint(r1);
1705 : anton 1.26 #else
1706 :     r2 = floor(r1+0.5);
1707 :     /* !! This is not quite true to the rounding rules given in the standard */
1708 :     #endif
1709 : anton 1.1
1710 :     fmax r1 r2 -- r3 float
1711 :     if (r1<r2)
1712 :     r3 = r2;
1713 :     else
1714 :     r3 = r1;
1715 :    
1716 :     fmin r1 r2 -- r3 float
1717 :     if (r1<r2)
1718 :     r3 = r1;
1719 :     else
1720 :     r3 = r2;
1721 :    
1722 :     represent r c_addr u -- n f1 f2 float
1723 :     char *sig;
1724 : pazsan 1.33 Cell flag;
1725 :     Cell decpt;
1726 : pazsan 1.40 sig=ecvt(r, u, (int *)&decpt, (int *)&flag);
1727 : pazsan 1.33 n=(r==0 ? 1 : decpt);
1728 : anton 1.1 f1=FLAG(flag!=0);
1729 :     f2=FLAG(isdigit(sig[0])!=0);
1730 :     memmove(c_addr,sig,u);
1731 :    
1732 :     >float c_addr u -- flag float to_float
1733 :     /* real signature: c_addr u -- r t / f */
1734 :     Float r;
1735 : anton 1.17 char *number=cstr(c_addr, u, 1);
1736 : anton 1.1 char *endconv;
1737 : pazsan 1.32 while(isspace(number[--u]) && u>0);
1738 :     switch(number[u])
1739 : pazsan 1.23 {
1740 : pazsan 1.32 case 'd':
1741 :     case 'D':
1742 :     case 'e':
1743 :     case 'E': break;
1744 :     default : u++; break;
1745 : pazsan 1.23 }
1746 :     number[u]='\0';
1747 : anton 1.1 r=strtod(number,&endconv);
1748 : pazsan 1.30 if((flag=FLAG(!(Cell)*endconv)))
1749 : anton 1.1 {
1750 : pazsan 1.32 IF_FTOS(fp[0] = FTOS);
1751 :     fp += -1;
1752 :     FTOS = r;
1753 :     }
1754 :     else if(*endconv=='d' || *endconv=='D')
1755 :     {
1756 :     *endconv='E';
1757 :     r=strtod(number,&endconv);
1758 :     if((flag=FLAG(!(Cell)*endconv)))
1759 :     {
1760 : anton 1.1 IF_FTOS(fp[0] = FTOS);
1761 :     fp += -1;
1762 :     FTOS = r;
1763 : pazsan 1.32 }
1764 : anton 1.1 }
1765 :    
1766 :     fabs r1 -- r2 float-ext
1767 :     r2 = fabs(r1);
1768 :    
1769 :     facos r1 -- r2 float-ext
1770 :     r2 = acos(r1);
1771 :    
1772 :     fasin r1 -- r2 float-ext
1773 :     r2 = asin(r1);
1774 :    
1775 :     fatan r1 -- r2 float-ext
1776 :     r2 = atan(r1);
1777 :    
1778 :     fatan2 r1 r2 -- r3 float-ext
1779 : anton 1.28 ""@i{r1/r2}=tan@i{r3}. The standard does not require, but probably
1780 :     intends this to be the inverse of @code{fsincos}. In gforth it is.""
1781 : anton 1.1 r3 = atan2(r1,r2);
1782 :    
1783 :     fcos r1 -- r2 float-ext
1784 :     r2 = cos(r1);
1785 :    
1786 :     fexp r1 -- r2 float-ext
1787 :     r2 = exp(r1);
1788 :    
1789 : pazsan 1.3 fexpm1 r1 -- r2 float-ext
1790 : anton 1.28 ""@i{r2}=@i{e}**@i{r1}@minus{}1""
1791 : anton 1.27 #ifdef HAVE_EXPM1
1792 : anton 1.29 extern double expm1(double);
1793 :     r2 = expm1(r1);
1794 : pazsan 1.3 #else
1795 : anton 1.29 r2 = exp(r1)-1.;
1796 : pazsan 1.3 #endif
1797 :    
1798 : anton 1.1 fln r1 -- r2 float-ext
1799 :     r2 = log(r1);
1800 :    
1801 : pazsan 1.3 flnp1 r1 -- r2 float-ext
1802 : anton 1.28 ""@i{r2}=ln(@i{r1}+1)""
1803 : anton 1.27 #ifdef HAVE_LOG1P
1804 : anton 1.29 extern double log1p(double);
1805 :     r2 = log1p(r1);
1806 : pazsan 1.3 #else
1807 : anton 1.29 r2 = log(r1+1.);
1808 : pazsan 1.3 #endif
1809 :    
1810 : anton 1.1 flog r1 -- r2 float-ext
1811 : anton 1.28 ""the decimal logarithm""
1812 : anton 1.1 r2 = log10(r1);
1813 :    
1814 : anton 1.29 falog r1 -- r2 float-ext
1815 :     ""@i{r2}=10**@i{r1}""
1816 :     extern double pow10(double);
1817 :     r2 = pow10(r1);
1818 :    
1819 : pazsan 1.3 fsin r1 -- r2 float-ext
1820 :     r2 = sin(r1);
1821 :    
1822 :     fsincos r1 -- r2 r3 float-ext
1823 : anton 1.29 ""@i{r2}=sin(@i{r1}), @i{r3}=cos(@i{r1})""
1824 : anton 1.1 r2 = sin(r1);
1825 :     r3 = cos(r1);
1826 :    
1827 :     fsqrt r1 -- r2 float-ext
1828 :     r2 = sqrt(r1);
1829 :    
1830 :     ftan r1 -- r2 float-ext
1831 :     r2 = tan(r1);
1832 : pazsan 1.32 :
1833 :     fsincos f/ ;
1834 : anton 1.29
1835 :     fsinh r1 -- r2 float-ext
1836 :     r2 = sinh(r1);
1837 : pazsan 1.32 :
1838 :     fexpm1 fdup fdup 1. d>f f+ f/ f+ f2/ ;
1839 : anton 1.29
1840 :     fcosh r1 -- r2 float-ext
1841 :     r2 = cosh(r1);
1842 : pazsan 1.32 :
1843 :     fexp fdup 1/f f+ f2/ ;
1844 : anton 1.29
1845 :     ftanh r1 -- r2 float-ext
1846 :     r2 = tanh(r1);
1847 : pazsan 1.32 :
1848 :     f2* fexpm1 fdup 2. d>f f+ f/ ;
1849 : anton 1.29
1850 :     fasinh r1 -- r2 float-ext
1851 :     r2 = asinh(r1);
1852 : pazsan 1.32 :
1853 :     fdup fdup f* 1. d>f f+ fsqrt f/ fatanh ;
1854 : anton 1.29
1855 :     facosh r1 -- r2 float-ext
1856 :     r2 = acosh(r1);
1857 : pazsan 1.32 :
1858 :     fdup fdup f* 1. d>f f- fsqrt f+ fln ;
1859 : anton 1.29
1860 :     fatanh r1 -- r2 float-ext
1861 :     r2 = atanh(r1);
1862 : pazsan 1.32 :
1863 :     fdup f0< >r fabs 1. d>f fover f- f/ f2* flnp1 f2/
1864 :     r> IF fnegate THEN ;
1865 : anton 1.1
1866 : anton 1.43 sfloats n1 -- n2 float-ext s_floats
1867 :     n2 = n1*sizeof(SFloat);
1868 :    
1869 :     dfloats n1 -- n2 float-ext d_floats
1870 :     n2 = n1*sizeof(DFloat);
1871 :    
1872 :     sfaligned c_addr -- sf_addr float-ext s_f_aligned
1873 : pazsan 1.45 sf_addr = (SFloat *)((((Cell)c_addr)+(sizeof(SFloat)-1))&(-sizeof(SFloat)));
1874 :     :
1875 :     [ 1 sfloats 1- ] Literal + [ -1 sfloats ] Literal and ;
1876 : anton 1.43
1877 :     dfaligned c_addr -- df_addr float-ext d_f_aligned
1878 : pazsan 1.45 df_addr = (DFloat *)((((Cell)c_addr)+(sizeof(DFloat)-1))&(-sizeof(DFloat)));
1879 :     :
1880 :     [ 1 dfloats 1- ] Literal + [ -1 dfloats ] Literal and ;
1881 : anton 1.43
1882 : pazsan 1.44 \ The following words access machine/OS/installation-dependent
1883 :     \ Gforth internals
1884 : anton 1.6 \ !! how about environmental queries DIRECT-THREADED,
1885 :     \ INDIRECT-THREADED, TOS-CACHED, FTOS-CACHED, CODEFIELD-DOES */
1886 : anton 1.1
1887 : pazsan 1.61 \ local variable implementation primitives
1888 :     \+[THEN] ( has-floats ) has-locals [IF]
1889 : anton 1.1
1890 : anton 1.43 @local# -- w gforth fetch_local_number
1891 : anton 1.35 w = *(Cell *)(lp+(Cell)NEXT_INST);
1892 :     INC_IP(1);
1893 : anton 1.1
1894 : anton 1.9 @local0 -- w new fetch_local_zero
1895 : pazsan 1.18 w = *(Cell *)(lp+0*sizeof(Cell));
1896 : anton 1.9
1897 : pazsan 1.18 @local1 -- w new fetch_local_four
1898 :     w = *(Cell *)(lp+1*sizeof(Cell));
1899 : anton 1.9
1900 : pazsan 1.18 @local2 -- w new fetch_local_eight
1901 :     w = *(Cell *)(lp+2*sizeof(Cell));
1902 : anton 1.9
1903 : pazsan 1.18 @local3 -- w new fetch_local_twelve
1904 :     w = *(Cell *)(lp+3*sizeof(Cell));
1905 : anton 1.9
1906 : pazsan 1.61 \+has-floats [IF]
1907 :    
1908 : anton 1.43 f@local# -- r gforth f_fetch_local_number
1909 : anton 1.35 r = *(Float *)(lp+(Cell)NEXT_INST);
1910 :     INC_IP(1);
1911 : anton 1.1
1912 : anton 1.9 f@local0 -- r new f_fetch_local_zero
1913 : pazsan 1.18 r = *(Float *)(lp+0*sizeof(Float));
1914 : anton 1.9
1915 : pazsan 1.18 f@local1 -- r new f_fetch_local_eight
1916 :     r = *(Float *)(lp+1*sizeof(Float));
1917 : anton 1.9
1918 : pazsan 1.61 \+[THEN]
1919 :    
1920 : anton 1.43 laddr# -- c_addr gforth laddr_number
1921 : anton 1.1 /* this can also be used to implement lp@ */
1922 : anton 1.35 c_addr = (Char *)(lp+(Cell)NEXT_INST);
1923 :     INC_IP(1);
1924 : anton 1.1
1925 : anton 1.43 lp+!# -- gforth lp_plus_store_number
1926 : anton 1.1 ""used with negative immediate values it allocates memory on the
1927 :     local stack, a positive immediate argument drops memory from the local
1928 :     stack""
1929 : anton 1.35 lp += (Cell)NEXT_INST;
1930 :     INC_IP(1);
1931 : anton 1.9
1932 : pazsan 1.18 lp- -- new minus_four_lp_plus_store
1933 :     lp += -sizeof(Cell);
1934 : anton 1.9
1935 : pazsan 1.18 lp+ -- new eight_lp_plus_store
1936 :     lp += sizeof(Float);
1937 : anton 1.9
1938 : pazsan 1.18 lp+2 -- new sixteen_lp_plus_store
1939 :     lp += 2*sizeof(Float);
1940 : anton 1.1
1941 : anton 1.43 lp! c_addr -- gforth lp_store
1942 : anton 1.1 lp = (Address)c_addr;
1943 :    
1944 : anton 1.43 >l w -- gforth to_l
1945 : anton 1.1 lp -= sizeof(Cell);
1946 :     *(Cell *)lp = w;
1947 :    
1948 : pazsan 1.61 \+has-floats [IF]
1949 :    
1950 : anton 1.43 f>l r -- gforth f_to_l
1951 : anton 1.1 lp -= sizeof(Float);
1952 :     *(Float *)lp = r;
1953 : pazsan 1.4
1954 : pazsan 1.61 \+[THEN] [THEN] \ has-locals
1955 :    
1956 : anton 1.43 up! a_addr -- gforth up_store
1957 : pazsan 1.65 UP=up=(char *)a_addr;
1958 : pazsan 1.63 :
1959 :     up ! ;
1960 :     Variable UP

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help