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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help