Annotation of gforth/primitives, revision 1.66

1.46      anton       1: \ Gforth primitives
                      2: 
1.51      anton       3: \ Copyright (C) 1995,1996 Free Software Foundation, Inc.
1.46      anton       4: 
                      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: 
1.6       anton      22: \ WARNING: This file is processed by m4. Make sure your identifiers
                     23: \ don't collide with m4's (e.g. by undefining them).
                     24: \ 
1.23      pazsan     25: \ 
                     26: \ 
1.48      anton      27: \ This file contains primitive specifications in the following format:
1.6       anton      28: \ 
1.23      pazsan     29: \ forth name   stack effect    category        [pronunciation]
1.6       anton      30: \ [""glossary entry""]
                     31: \ C code
                     32: \ [:
                     33: \ Forth code]
                     34: \ 
1.48      anton      35: \ 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.
1.23      pazsan     41: \ 
                     42: \ 
1.48      anton      43: \ These specifications are automatically translated into C-code for the
1.23      pazsan     44: \ interpreter and into some other files. I hope that your C compiler has
1.6       anton      45: \ 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: \ 
1.23      pazsan     57: \ 
                     58: \ 
1.6       anton      59: \ The stack variables have the following types:
1.23      pazsan     60: \ 
1.6       anton      61: \ 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: \ 
1.23      pazsan     78: \ 
                     79: \ 
1.6       anton      80: \ 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
1.23      pazsan     84: \ lp   the locals stack pointer
1.6       anton      85: \ NEXT executes NEXT
                     86: \ cfa  
                     87: \ NEXT1        executes NEXT1
                     88: \ FLAG(x)      makes a Forth flag from a C flag
                     89: \ 
1.23      pazsan     90: \ 
                     91: \ 
1.6       anton      92: \ Percentages in comments are from Koopmans book: average/maximum use
1.23      pazsan     93: \ (taken from four, not very representative benchmarks)
                     94: \ 
1.6       anton      95: \ 
1.23      pazsan     96: \ 
1.6       anton      97: \ To do:
                     98: \ 
                     99: \ throw execute, cfa and NEXT1 out?
                    100: \ macroize *ip, ip++, *ip++ (pipelining)?
1.1       anton     101: 
1.6       anton     102: \ these m4 macros would collide with identifiers
1.1       anton     103: undefine(`index')
                    104: undefine(`shift')
                    105: 
1.43      anton     106: noop   --              gforth
1.1       anton     107: ;
1.18      pazsan    108: :
                    109:  ;
1.1       anton     110: 
1.43      anton     111: lit    -- w            gforth
1.35      anton     112: w = (Cell)NEXT_INST;
                    113: INC_IP(1);
1.61      pazsan    114: :
                    115:  r> dup @ swap cell+ >r ;
1.1       anton     116: 
1.42      anton     117: execute                xt --           core
1.35      anton     118: ip=IP;
1.1       anton     119: IF_TOS(TOS = sp[0]);
1.44      pazsan    120: EXEC(xt);
1.1       anton     121: 
1.54      anton     122: 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: 
1.61      pazsan    131: \+has-locals [IF]
                    132: 
1.43      anton     133: branch-lp+!#   --      gforth  branch_lp_plus_store_number
1.9       anton     134: /* this will probably not be used */
                    135: branch_adjust_lp:
1.35      anton     136: lp += (Cell)(IP[1]);
1.9       anton     137: goto branch;
                    138: 
1.61      pazsan    139: \+[THEN]
                    140: 
1.43      anton     141: branch --              gforth
1.1       anton     142: branch:
1.35      anton     143: ip = (Xt *)(((Cell)IP)+(Cell)NEXT_INST);
                    144: NEXT_P0;
1.18      pazsan    145: :
                    146:  r> dup @ + >r ;
1.1       anton     147: 
1.61      pazsan    148: \ condbranch(forthname,restline,code,forthcode)
1.35      anton     149: \ this is non-syntactical: code must open a brace that is closed by the macro
1.9       anton     150: define(condbranch,
                    151: $1     $2
1.35      anton     152: $3     ip = (Xt *)(((Cell)IP)+(Cell)NEXT_INST);
                    153:         NEXT_P0;
                    154:        NEXT;
1.9       anton     155: }
                    156: else
1.35      anton     157:     INC_IP(1);
1.61      pazsan    158: $4
                    159: 
                    160: \+has-locals [IF]
1.9       anton     161: 
                    162: $1-lp+!#       $2_lp_plus_store_number
                    163: $3    goto branch_adjust_lp;
                    164: }
                    165: else
1.35      anton     166:     INC_IP(2);
1.9       anton     167: 
1.61      pazsan    168: \+[THEN]
1.9       anton     169: )
                    170: 
                    171: condbranch(?branch,f --                f83     question_branch,
1.1       anton     172: if (f==0) {
                    173:     IF_TOS(TOS = sp[0]);
1.61      pazsan    174: ,)
1.1       anton     175: 
1.51      anton     176: \ we don't need an lp_plus_store version of the ?dup-stuff, because it
                    177: \ is only used in if's (yet)
                    178: 
1.62      pazsan    179: \+has-xconds [IF]
                    180: 
1.51      anton     181: ?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: 
1.62      pazsan    208: \+[THEN]
                    209: 
1.9       anton     210: condbranch((next),--           cmFORTH paren_next,
1.1       anton     211: if ((*rp)--) {
1.61      pazsan    212: ,:
                    213:  r> r> dup 1- >r
                    214:  IF dup @ + >r ELSE cell+ >r THEN ;)
1.1       anton     215: 
1.43      anton     216: condbranch((loop),--           gforth  paren_loop,
1.31      pazsan    217: Cell index = *rp+1;
                    218: Cell limit = rp[1];
1.1       anton     219: if (index != limit) {
                    220:     *rp = index;
1.61      pazsan    221: ,:
                    222:  r> r> 1+ r> 2dup =
                    223:  IF >r 1- >r cell+ >r
                    224:  ELSE >r >r dup @ + >r THEN ;)
1.1       anton     225: 
1.42      anton     226: condbranch((+loop),n --                gforth  paren_plus_loop,
1.1       anton     227: /* !! check this thoroughly */
1.31      pazsan    228: Cell index = *rp;
1.1       anton     229: /* sign bit manipulation and test: (x^y)<0 is equivalent to (x<0) != (y<0) */
                    230: /* dependent upon two's complement arithmetic */
1.31      pazsan    231: Cell olddiff = index-rp[1];
1.9       anton     232: if ((olddiff^(olddiff+n))>=0   /* the limit is not crossed */
                    233:     || (olddiff^n)>=0          /* it is a wrap-around effect */) {
1.15      pazsan    234: #ifdef i386
                    235:     *rp += n;
                    236: #else
                    237:     *rp = index + n;
                    238: #endif
1.1       anton     239:     IF_TOS(TOS = sp[0]);
1.61      pazsan    240: ,:
                    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 ;)
1.1       anton     247: 
1.62      pazsan    248: \+has-xconds [IF]
                    249: 
1.42      anton     250: condbranch((-loop),u --                gforth  paren_minus_loop,
1.41      anton     251: /* !! check this thoroughly */
                    252: Cell index = *rp;
                    253: UCell olddiff = index-rp[1];
                    254: if (olddiff>u) {
1.44      pazsan    255: #ifdef i386
                    256:     *rp -= u;
                    257: #else
1.41      anton     258:     *rp = index - u;
1.44      pazsan    259: #endif
1.41      anton     260:     IF_TOS(TOS = sp[0]);
1.61      pazsan    261: ,)
1.41      anton     262: 
1.42      anton     263: condbranch((s+loop),n --               gforth  paren_symmetric_plus_loop,
1.1       anton     264: ""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 */
1.31      pazsan    268: Cell index = *rp;
                    269: Cell diff = index-rp[1];
                    270: Cell newdiff = diff+n;
1.1       anton     271: if (n<0) {
                    272:     diff = -diff;
1.15      pazsan    273:     newdiff = -newdiff;
1.1       anton     274: }
                    275: if (diff>=0 || newdiff<0) {
1.15      pazsan    276: #ifdef i386
                    277:     *rp += n;
                    278: #else
                    279:     *rp = index + n;
                    280: #endif
1.1       anton     281:     IF_TOS(TOS = sp[0]);
1.61      pazsan    282: ,)
1.1       anton     283: 
1.62      pazsan    284: \+[THEN]
                    285: 
1.1       anton     286: unloop         --      core
                    287: rp += 2;
1.18      pazsan    288: :
                    289:  r> rdrop rdrop >r ;
1.1       anton     290: 
                    291: (for)  ncount --               cmFORTH         paren_for
                    292: /* or (for) = >r -- collides with unloop! */
                    293: *--rp = 0;
                    294: *--rp = ncount;
1.18      pazsan    295: :
                    296:  r> swap 0 >r >r >r ;
1.1       anton     297: 
1.43      anton     298: (do)   nlimit nstart --                gforth          paren_do
1.1       anton     299: /* or do it in high-level? 0.09/0.23% */
                    300: *--rp = nlimit;
                    301: *--rp = nstart;
                    302: :
1.63      pazsan    303:  r> swap rot >r >r >r ;
1.1       anton     304: 
1.43      anton     305: (?do)  nlimit nstart --        gforth  paren_question_do
1.1       anton     306: *--rp = nlimit;
                    307: *--rp = nstart;
                    308: if (nstart == nlimit) {
                    309:     IF_TOS(TOS = sp[0]);
                    310:     goto branch;
                    311:     }
                    312: else {
1.35      anton     313:     INC_IP(1);
1.1       anton     314: }
1.61      pazsan    315: :
1.62      pazsan    316:   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
1.1       anton     322: 
1.63      pazsan    323: \+has-xconds [IF]
                    324: 
1.43      anton     325: (+do)  nlimit nstart --        gforth  paren_plus_do
1.41      anton     326: *--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: }
1.61      pazsan    335: :
                    336:  swap 2dup
                    337:  r> swap >r swap >r
                    338:  >=
                    339:  IF
                    340:      dup @ +
                    341:  ELSE
                    342:      cell+
                    343:  THEN  >r ;
1.41      anton     344: 
1.43      anton     345: (u+do) ulimit ustart --        gforth  paren_u_plus_do
1.41      anton     346: *--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: }
1.61      pazsan    355: :
                    356:  swap 2dup
                    357:  r> swap >r swap >r
                    358:  u>=
                    359:  IF
                    360:      dup @ +
                    361:  ELSE
                    362:      cell+
                    363:  THEN  >r ;
1.41      anton     364: 
1.43      anton     365: (-do)  nlimit nstart --        gforth  paren_minus_do
1.41      anton     366: *--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: }
1.61      pazsan    375: :
                    376:  swap 2dup
                    377:  r> swap >r swap >r
                    378:  <=
                    379:  IF
                    380:      dup @ +
                    381:  ELSE
                    382:      cell+
                    383:  THEN  >r ;
1.41      anton     384: 
1.43      anton     385: (u-do) ulimit ustart --        gforth  paren_u_minus_do
1.41      anton     386: *--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: }
1.61      pazsan    395: :
                    396:  swap 2dup
                    397:  r> swap >r swap >r
                    398:  u<=
                    399:  IF
                    400:      dup @ +
                    401:  ELSE
                    402:      cell+
                    403:  THEN  >r ;
1.41      anton     404: 
1.63      pazsan    405: \+[THEN]
                    406: 
1.42      anton     407: i      -- n            core
1.1       anton     408: n = *rp;
1.61      pazsan    409: :
                    410:  rp@ cell+ @ ;
                    411: 
                    412: i'     -- w            gforth          i_tick
                    413: ""loop end value""
                    414: w = rp[1];
                    415: :
                    416:  rp@ cell+ cell+ @ ;
1.1       anton     417: 
                    418: j      -- n            core
                    419: n = rp[2];
1.61      pazsan    420: :
                    421:  rp@ cell+ cell+ cell+ @ ;
1.1       anton     422: 
1.6       anton     423: \ digit is high-level: 0/0%
1.1       anton     424: 
                    425: move   c_from c_to ucount --           core
                    426: memmove(c_to,c_from,ucount);
1.6       anton     427: /* make an Ifdef for bsd and others? */
1.18      pazsan    428: :
                    429:  >r 2dup u< IF r> cmove> ELSE r> cmove THEN ;
1.1       anton     430: 
                    431: cmove  c_from c_to u --        string
                    432: while (u-- > 0)
                    433:   *c_to++ = *c_from++;
1.18      pazsan    434: :
                    435:  bounds ?DO  dup c@ I c! 1+  LOOP  drop ;
1.1       anton     436: 
                    437: cmove> c_from c_to u --        string  c_move_up
                    438: while (u-- > 0)
                    439:   c_to[u] = c_from[u];
1.18      pazsan    440: :
                    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 ;
1.1       anton     444: 
                    445: fill   c_addr u c --   core
                    446: memset(c_addr,c,u);
1.18      pazsan    447: :
                    448:  -rot bounds
                    449:  ?DO  dup I c!  LOOP  drop ;
1.1       anton     450: 
                    451: compare                c_addr1 u1 c_addr2 u2 -- n      string
1.26      anton     452: ""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.""
1.1       anton     457: 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;
1.18      pazsan    464: :
                    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 ;
1.1       anton     472: 
                    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;
1.18      pazsan    479: :
                    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 )
1.61      pazsan    484:  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 - ;
1.1       anton     490: 
                    491: capscomp       c_addr1 u c_addr2 -- n  new
1.56      anton     492: 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;
1.18      pazsan    497: :
                    498:  swap bounds
1.61      pazsan    499:  ?DO  dup c@ I c@ <>
                    500:      IF  dup c@ toupper I c@ toupper =
                    501:      ELSE  true  THEN  WHILE  1+  LOOP  drop 0
1.18      pazsan    502:  ELSE  c@ toupper I c@ toupper - unloop  THEN  -text-flag ;
1.1       anton     503: 
                    504: -trailing      c_addr u1 -- c_addr u2          string  dash_trailing
                    505: u2 = u1;
                    506: while (c_addr[u2-1] == ' ')
                    507:   u2--;
1.18      pazsan    508: :
                    509:  BEGIN  1- 2dup + c@ bl =  WHILE
                    510:         dup  0= UNTIL  ELSE  1+  THEN ;
1.1       anton     511: 
                    512: /string                c_addr1 u1 n -- c_addr2 u2      string  slash_string
                    513: c_addr2 = c_addr1+n;
                    514: u2 = u1-n;
1.18      pazsan    515: :
                    516:  tuck - >r + r> dup 0< IF  - 0  THEN ;
1.1       anton     517: 
1.42      anton     518: +      n1 n2 -- n              core    plus
1.1       anton     519: n = n1+n2;
1.54      anton     520: 
1.60      anton     521: \ 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 ;
1.1       anton     527: 
1.42      anton     528: -      n1 n2 -- n              core    minus
1.1       anton     529: n = n1-n2;
1.18      pazsan    530: :
                    531:  negate + ;
1.1       anton     532: 
1.42      anton     533: negate n1 -- n2                core
1.1       anton     534: /* use minus as alias */
                    535: n2 = -n1;
1.18      pazsan    536: :
                    537:  invert 1+ ;
1.1       anton     538: 
                    539: 1+     n1 -- n2                core            one_plus
                    540: n2 = n1+1;
1.18      pazsan    541: :
                    542:  1 + ;
1.1       anton     543: 
                    544: 1-     n1 -- n2                core            one_minus
                    545: n2 = n1-1;
1.18      pazsan    546: :
                    547:  1 - ;
1.1       anton     548: 
                    549: max    n1 n2 -- n      core
                    550: if (n1<n2)
                    551:   n = n2;
                    552: else
                    553:   n = n1;
                    554: :
1.18      pazsan    555:  2dup < IF swap THEN drop ;
1.1       anton     556: 
                    557: min    n1 n2 -- n      core
                    558: if (n1<n2)
                    559:   n = n1;
                    560: else
                    561:   n = n2;
1.18      pazsan    562: :
                    563:  2dup > IF swap THEN drop ;
1.1       anton     564: 
                    565: abs    n1 -- n2        core
                    566: if (n1<0)
                    567:   n2 = -n1;
                    568: else
                    569:   n2 = n1;
1.18      pazsan    570: :
                    571:  dup 0< IF negate THEN ;
1.1       anton     572: 
1.42      anton     573: *      n1 n2 -- n              core    star
1.1       anton     574: n = n1*n2;
1.18      pazsan    575: :
                    576:  um* drop ;
1.1       anton     577: 
1.42      anton     578: /      n1 n2 -- n              core    slash
1.1       anton     579: n = n1/n2;
1.18      pazsan    580: :
                    581:  /mod nip ;
1.1       anton     582: 
                    583: mod    n1 n2 -- n              core
                    584: n = n1%n2;
1.18      pazsan    585: :
                    586:  /mod drop ;
1.1       anton     587: 
                    588: /mod   n1 n2 -- n3 n4          core            slash_mod
                    589: n4 = n1/n2;
                    590: n3 = n1%n2; /* !! is this correct? look into C standard! */
1.18      pazsan    591: :
                    592:  >r s>d r> fm/mod ;
1.1       anton     593: 
                    594: 2*     n1 -- n2                core            two_star
                    595: n2 = 2*n1;
1.18      pazsan    596: :
                    597:  dup + ;
1.1       anton     598: 
                    599: 2/     n1 -- n2                core            two_slash
                    600: /* !! is this still correct? */
                    601: n2 = n1>>1;
1.64      pazsan    602: :
1.66    ! pazsan    603:  dup MINI and IF 1 ELSE 0 THEN
1.64      pazsan    604:  [ 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 ;
1.1       anton     608: 
                    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""
1.52      anton     611: #ifdef BUGGY_LONG_LONG
                    612: DCell r = fmdiv(d1,n1);
                    613: n2=r.hi;
                    614: n3=r.lo;
                    615: #else
1.1       anton     616: /* 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: }
1.52      anton     624: #endif
1.53      pazsan    625: :
                    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 ;
1.1       anton     630: 
                    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""
1.52      anton     633: #ifdef BUGGY_LONG_LONG
                    634: DCell r = smdiv(d1,n1);
                    635: n2=r.hi;
                    636: n3=r.lo;
                    637: #else
1.1       anton     638: /* 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: }
1.52      anton     646: #endif
1.18      pazsan    647: :
                    648:  over >r dup >r abs -rot
                    649:  dabs rot um/mod
1.53      pazsan    650:  r> r@ xor 0< IF       negate       THEN
                    651:  r>        0< IF  swap negate swap  THEN ;
1.1       anton     652: 
                    653: m*     n1 n2 -- d              core    m_star
1.52      anton     654: #ifdef BUGGY_LONG_LONG
                    655: d = mmul(n1,n2);
                    656: #else
1.1       anton     657: d = (DCell)n1 * (DCell)n2;
1.52      anton     658: #endif
1.18      pazsan    659: :
                    660:  2dup      0< and >r
                    661:  2dup swap 0< and >r
                    662:  um* r> - r> - ;
1.1       anton     663: 
                    664: um*    u1 u2 -- ud             core    u_m_star
                    665: /* use u* as alias */
1.52      anton     666: #ifdef BUGGY_LONG_LONG
                    667: ud = ummul(u1,u2);
                    668: #else
1.1       anton     669: ud = (UDCell)u1 * (UDCell)u2;
1.52      anton     670: #endif
1.61      pazsan    671: :
                    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 )
1.63      pazsan    678:    over MINI
                    679:    and >r >r 2dup d+ swap r> + swap r> ;
1.1       anton     680: 
                    681: um/mod ud u1 -- u2 u3          core    u_m_slash_mod
1.52      anton     682: #ifdef BUGGY_LONG_LONG
                    683: UDCell r = umdiv(ud,u1);
                    684: u2=r.hi;
                    685: u3=r.lo;
                    686: #else
1.1       anton     687: u3 = ud/u1;
                    688: u2 = ud%u1;
1.52      anton     689: #endif
1.19      pazsan    690: :
1.61      pazsan    691:    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*+ ;
1.63      pazsan    696: : d2*+ ( ud n -- ud+n c )
                    697:    over MINI
                    698:    and >r >r 2dup d+ swap r> + swap r> ;
1.1       anton     699: 
                    700: m+     d1 n -- d2              double          m_plus
1.51      anton     701: #ifdef BUGGY_LONG_LONG
1.52      anton     702: d2.lo = d1.lo+n;
                    703: d2.hi = d1.hi - (n<0) + (d2.lo<d1.lo);
1.51      anton     704: #else
1.1       anton     705: d2 = d1+n;
1.51      anton     706: #endif
1.18      pazsan    707: :
                    708:  s>d d+ ;
1.1       anton     709: 
1.42      anton     710: d+     d1 d2 -- d              double  d_plus
1.51      anton     711: #ifdef BUGGY_LONG_LONG
1.52      anton     712: d.lo = d1.lo+d2.lo;
                    713: d.hi = d1.hi + d2.hi + (d.lo<d1.lo);
1.51      anton     714: #else
1.1       anton     715: d = d1+d2;
1.51      anton     716: #endif
1.18      pazsan    717: :
1.53      pazsan    718:  rot + >r tuck + swap over u> r> swap - ;
1.1       anton     719: 
                    720: d-     d1 d2 -- d              double          d_minus
1.51      anton     721: #ifdef BUGGY_LONG_LONG
1.52      anton     722: d.lo = d1.lo - d2.lo;
                    723: d.hi = d1.hi-d2.hi-(d1.lo<d2.lo);
1.51      anton     724: #else
1.1       anton     725: d = d1-d2;
1.51      anton     726: #endif
1.18      pazsan    727: :
                    728:  dnegate d+ ;
1.1       anton     729: 
                    730: dnegate        d1 -- d2                double
                    731: /* use dminus as alias */
1.51      anton     732: #ifdef BUGGY_LONG_LONG
1.52      anton     733: d2 = dnegate(d1);
1.51      anton     734: #else
1.1       anton     735: d2 = -d1;
1.51      anton     736: #endif
1.18      pazsan    737: :
                    738:  invert swap negate tuck 0= - ;
1.1       anton     739: 
                    740: d2*    d1 -- d2                double          d_two_star
1.51      anton     741: #ifdef BUGGY_LONG_LONG
1.52      anton     742: d2.lo = d1.lo<<1;
                    743: d2.hi = (d1.hi<<1) | (d1.lo>>(CELL_BITS-1));
1.51      anton     744: #else
1.1       anton     745: d2 = 2*d1;
1.51      anton     746: #endif
1.18      pazsan    747: :
                    748:  2dup d+ ;
1.1       anton     749: 
                    750: d2/    d1 -- d2                double          d_two_slash
1.51      anton     751: #ifdef BUGGY_LONG_LONG
1.52      anton     752: d2.hi = d1.hi>>1;
                    753: d2.lo= (d1.lo>>1) | (d1.hi<<(CELL_BITS-1));
1.51      anton     754: #else
1.13      pazsan    755: d2 = d1>>1;
1.51      anton     756: #endif
1.18      pazsan    757: :
                    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 ;
1.1       anton     760: 
1.42      anton     761: and    w1 w2 -- w              core
1.1       anton     762: w = w1&w2;
                    763: 
1.42      anton     764: or     w1 w2 -- w              core
1.1       anton     765: w = w1|w2;
1.64      pazsan    766: :
                    767:  invert swap invert and invert ;
1.1       anton     768: 
1.42      anton     769: xor    w1 w2 -- w              core
1.1       anton     770: w = w1^w2;
                    771: 
                    772: invert w1 -- w2                core
                    773: w2 = ~w1;
1.18      pazsan    774: :
1.64      pazsan    775:  MAXU xor ;
1.1       anton     776: 
                    777: rshift u1 n -- u2              core
                    778:   u2 = u1>>n;
1.62      pazsan    779: :
                    780:     0 ?DO 2/ MAXI and LOOP ;
1.1       anton     781: 
                    782: lshift u1 n -- u2              core
                    783:   u2 = u1<<n;
1.62      pazsan    784: :
                    785:     0 ?DO 2* LOOP ;
1.1       anton     786: 
1.6       anton     787: \ comparisons(prefix, args, prefix, arg1, arg2, wordsets...)
1.1       anton     788: define(comparisons,
                    789: $1=    $2 -- f         $6      $3equals
                    790: f = FLAG($4==$5);
1.62      pazsan    791: :
                    792:     [ char $1x char 0 = [IF]
                    793:        ] IF false ELSE true THEN [
                    794:     [ELSE]
                    795:        ] xor 0= [
                    796:     [THEN] ] ;
1.1       anton     797: 
                    798: $1<>   $2 -- f         $7      $3different
                    799: f = FLAG($4!=$5);
1.62      pazsan    800: :
                    801:     [ char $1x char 0 = [IF]
                    802:        ] IF true ELSE false THEN [
                    803:     [ELSE]
                    804:        ] xor 0<> [
                    805:     [THEN] ] ;
1.1       anton     806: 
                    807: $1<    $2 -- f         $8      $3less
                    808: f = FLAG($4<$5);
1.62      pazsan    809: :
                    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] ] ;
1.1       anton     818: 
                    819: $1>    $2 -- f         $9      $3greater
                    820: f = FLAG($4>$5);
1.62      pazsan    821: :
                    822:     [ char $1x char 0 = [IF] ] negate [ [ELSE] ] swap [ [THEN] ]
                    823:     $1< ;
1.1       anton     824: 
1.43      anton     825: $1<=   $2 -- f         gforth  $3less_or_equal
1.1       anton     826: f = FLAG($4<=$5);
1.62      pazsan    827: :
                    828:     $1> 0= ;
1.1       anton     829: 
1.43      anton     830: $1>=   $2 -- f         gforth  $3greater_or_equal
1.1       anton     831: f = FLAG($4>=$5);
1.62      pazsan    832: :
                    833:     [ char $1x char 0 = [IF] ] negate [ [ELSE] ] swap [ [THEN] ]
                    834:     $1<= ;
1.1       anton     835: 
                    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)
1.43      anton     840: comparisons(u, u1 u2, u_, u1, u2, gforth, gforth, core, core-ext)
1.52      anton     841: 
                    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: 
1.62      pazsan    888: \+has-dcomps [IF]
                    889: 
1.52      anton     890: 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)
1.1       anton     893: 
1.62      pazsan    894: \+[THEN]
                    895: 
1.1       anton     896: within u1 u2 u3 -- f           core-ext
                    897: f = FLAG(u1-u2 < u3-u2);
1.18      pazsan    898: :
                    899:  over - >r - r> u< ;
1.1       anton     900: 
1.43      anton     901: sp@    -- a_addr               gforth          spat
1.15      pazsan    902: a_addr = sp+1;
1.1       anton     903: 
1.43      anton     904: sp!    a_addr --               gforth          spstore
1.15      pazsan    905: sp = a_addr;
1.1       anton     906: /* works with and without TOS caching */
                    907: 
1.43      anton     908: rp@    -- a_addr               gforth          rpat
1.1       anton     909: a_addr = rp;
                    910: 
1.43      anton     911: rp!    a_addr --               gforth          rpstore
1.1       anton     912: rp = a_addr;
                    913: 
1.62      pazsan    914: \+has-floats [IF]
                    915: 
1.43      anton     916: fp@    -- f_addr       gforth  fp_fetch
1.1       anton     917: f_addr = fp;
                    918: 
1.43      anton     919: fp!    f_addr --       gforth  fp_store
1.1       anton     920: fp = f_addr;
                    921: 
1.62      pazsan    922: \+[THEN]
                    923: 
1.43      anton     924: ;s     --              gforth  semis
1.1       anton     925: ip = (Xt *)(*rp++);
1.35      anton     926: NEXT_P0;
1.1       anton     927: 
1.42      anton     928: >r     w --            core    to_r
1.1       anton     929: *--rp = w;
1.63      pazsan    930: :
                    931:  (>r) ;
                    932: : (>r)  rp@ cell+ @ rp@ ! rp@ cell+ ! ;
1.1       anton     933: 
1.42      anton     934: r>     -- w            core    r_from
1.1       anton     935: w = *rp++;
1.63      pazsan    936: :
                    937:  rp@ cell+ @ rp@ @ rp@ cell+ ! (rdrop) rp@ ! ;
                    938: Create (rdrop) ' ;s A,
1.1       anton     939: 
1.42      anton     940: rdrop  --              gforth
1.1       anton     941: rp++;
1.61      pazsan    942: :
                    943:  r> r> drop >r ;
1.1       anton     944: 
1.14      anton     945: 2>r    w1 w2 --        core-ext        two_to_r
                    946: *--rp = w1;
                    947: *--rp = w2;
1.61      pazsan    948: :
                    949:  swap r> swap >r swap >r >r ;
1.14      anton     950: 
                    951: 2r>    -- w1 w2        core-ext        two_r_from
                    952: w2 = *rp++;
                    953: w1 = *rp++;
1.61      pazsan    954: :
                    955:  r> r> swap r> swap >r swap ;
1.14      anton     956: 
                    957: 2r@    -- w1 w2        core-ext        two_r_fetch
                    958: w2 = rp[0];
                    959: w1 = rp[1];
1.61      pazsan    960: :
                    961:  i' j ;
1.14      anton     962: 
1.42      anton     963: 2rdrop --              gforth  two_r_drop
1.14      anton     964: rp+=2;
1.61      pazsan    965: :
                    966:  r> r> drop r> drop >r ;
1.14      anton     967: 
1.42      anton     968: over   w1 w2 -- w1 w2 w1               core
1.61      pazsan    969: :
                    970:  sp@ cell+ @ ;
1.1       anton     971: 
1.42      anton     972: drop   w --            core
1.61      pazsan    973: :
                    974:  IF THEN ;
1.1       anton     975: 
1.42      anton     976: swap   w1 w2 -- w2 w1          core
1.61      pazsan    977: :
                    978:  >r (swap) ! r> (swap) @ ;
                    979: Variable (swap)
1.1       anton     980: 
1.42      anton     981: dup    w -- w w                core
1.61      pazsan    982: :
                    983:  sp@ @ ;
1.1       anton     984: 
                    985: rot    w1 w2 w3 -- w2 w3 w1    core    rote
1.61      pazsan    986: :
1.62      pazsan    987: [ defined? (swap) [IF] ]
                    988:     (swap) ! (rot) ! >r (rot) @ (swap) @ r> ;
1.61      pazsan    989: Variable (rot)
1.62      pazsan    990: [ELSE] ]
                    991:     >r swap r> swap ;
                    992: [THEN]
1.1       anton     993: 
1.42      anton     994: -rot   w1 w2 w3 -- w3 w1 w2    gforth  not_rote
1.18      pazsan    995: :
                    996:  rot rot ;
1.1       anton     997: 
                    998: nip    w1 w2 -- w2             core-ext
1.18      pazsan    999: :
1.61      pazsan   1000:  >r drop r> ;
1.1       anton    1001: 
                   1002: tuck   w1 w2 -- w2 w1 w2       core-ext
1.18      pazsan   1003: :
                   1004:  swap over ;
1.1       anton    1005: 
                   1006: ?dup   w -- w                  core    question_dupe
                   1007: if (w!=0) {
1.7       pazsan   1008:   IF_TOS(*sp-- = w;)
1.1       anton    1009: #ifndef USE_TOS
1.7       pazsan   1010:   *--sp = w;
1.1       anton    1011: #endif
                   1012: }
1.18      pazsan   1013: :
                   1014:  dup IF dup THEN ;
1.1       anton    1015: 
                   1016: pick   u -- w                  core-ext
                   1017: w = sp[u+1];
1.18      pazsan   1018: :
                   1019:  1+ cells sp@ + @ ;
1.1       anton    1020: 
                   1021: 2drop  w1 w2 --                core    two_drop
1.18      pazsan   1022: :
                   1023:  drop drop ;
1.1       anton    1024: 
                   1025: 2dup   w1 w2 -- w1 w2 w1 w2    core    two_dupe
1.18      pazsan   1026: :
                   1027:  over over ;
1.1       anton    1028: 
                   1029: 2over  w1 w2 w3 w4 -- w1 w2 w3 w4 w1 w2        core    two_over
1.18      pazsan   1030: :
                   1031:  3 pick 3 pick ;
1.1       anton    1032: 
                   1033: 2swap  w1 w2 w3 w4 -- w3 w4 w1 w2      core    two_swap
1.18      pazsan   1034: :
1.61      pazsan   1035:  rot >r rot r> ;
1.1       anton    1036: 
1.43      anton    1037: 2rot   w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2  double-ext      two_rote
1.18      pazsan   1038: :
                   1039:  >r >r 2swap r> r> 2swap ;
1.1       anton    1040: 
1.42      anton    1041: 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: 
1.6       anton    1049: \ toggle is high-level: 0.11/0.42%
1.1       anton    1050: 
1.42      anton    1051: @      a_addr -- w             core    fetch
1.1       anton    1052: w = *a_addr;
                   1053: 
1.42      anton    1054: !      w a_addr --             core    store
1.1       anton    1055: *a_addr = w;
                   1056: 
1.42      anton    1057: +!     n a_addr --             core    plus_store
1.1       anton    1058: *a_addr += n;
1.61      pazsan   1059: :
                   1060:  tuck @ + swap ! ;
1.1       anton    1061: 
1.42      anton    1062: c@     c_addr -- c             core    cfetch
1.1       anton    1063: c = *c_addr;
1.62      pazsan   1064: :
                   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/ ;
1.1       anton    1086: 
1.42      anton    1087: c!     c c_addr --             core    cstore
1.1       anton    1088: *c_addr = c;
1.62      pazsan   1089: :
                   1090: [ bigendian [IF] ]
                   1091:     [ cell>bit 4 = [IF] ]
                   1092:        tuck 1 and IF  $FF and  ELSE  8<<  THEN >r
1.63      pazsan   1093:        dup -2 and @ over 1 and cells masks + @ and
                   1094:        r> or swap -2 and ! ;
                   1095:        Create masks $00FF , $FF00 ,
                   1096:     [ELSE] ]
1.62      pazsan   1097:        dup [ cell 1- ] literal and dup 
                   1098:        [ cell 1- ] literal xor >r
                   1099:        - dup @ $FF r@ 0 ?DO 8<< LOOP invert and
1.63      pazsan   1100:        rot $FF and r> 0 ?DO 8<< LOOP or swap ! ;
                   1101:     [THEN]
                   1102: [ELSE] ]
1.62      pazsan   1103:     [ cell>bit 4 = [IF] ]
                   1104:        tuck 1 and IF  8<<  ELSE  $FF and  THEN >r
1.63      pazsan   1105:        dup -2 and @ over 1 and cells masks + @ and
                   1106:        r> or swap -2 and ! ;
                   1107:        Create masks $FF00 , $00FF ,
                   1108:     [ELSE] ]
1.62      pazsan   1109:        dup [ cell 1- ] literal and dup >r
                   1110:        - dup @ $FF r@ 0 ?DO 8<< LOOP invert and
1.63      pazsan   1111:        rot $FF and r> 0 ?DO 8<< LOOP or swap ! ;
                   1112:     [THEN]
                   1113: [THEN]
1.62      pazsan   1114: : 8<< 2* 2* 2* 2*  2* 2* 2* 2* ;
1.1       anton    1115: 
                   1116: 2!     w1 w2 a_addr --         core    two_store
                   1117: a_addr[0] = w2;
                   1118: a_addr[1] = w1;
1.18      pazsan   1119: :
                   1120:  tuck ! cell+ ! ;
1.1       anton    1121: 
                   1122: 2@     a_addr -- w1 w2         core    two_fetch
                   1123: w2 = a_addr[0];
                   1124: w1 = a_addr[1];
1.18      pazsan   1125: :
                   1126:  dup cell+ @ swap @ ;
1.1       anton    1127: 
                   1128: cell+  a_addr1 -- a_addr2      core    cell_plus
                   1129: a_addr2 = a_addr1+1;
1.18      pazsan   1130: :
1.62      pazsan   1131:  cell + ;
1.1       anton    1132: 
                   1133: cells  n1 -- n2                core
                   1134: n2 = n1 * sizeof(Cell);
1.18      pazsan   1135: :
1.62      pazsan   1136:  [ 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 ] ;
1.1       anton    1142: 
                   1143: char+  c_addr1 -- c_addr2      core    care_plus
1.18      pazsan   1144: c_addr2 = c_addr1 + 1;
                   1145: :
                   1146:  1+ ;
1.1       anton    1147: 
1.24      anton    1148: (chars)                n1 -- n2        gforth  paren_cares
1.1       anton    1149: n2 = n1 * sizeof(Char);
1.18      pazsan   1150: :
                   1151:  ;
1.1       anton    1152: 
                   1153: count  c_addr1 -- c_addr2 u    core
                   1154: u = *c_addr1;
                   1155: c_addr2 = c_addr1+1;
1.18      pazsan   1156: :
                   1157:  dup 1+ swap c@ ;
1.1       anton    1158: 
                   1159: (f83find)      c_addr u f83name1 -- f83name2   new     paren_f83find
                   1160: for (; f83name1 != NULL; f83name1 = f83name1->next)
1.61      pazsan   1161:   if ((UCell)F83NAME_COUNT(f83name1)==u &&
1.56      anton    1162:       memcasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
1.8       pazsan   1163:     break;
                   1164: f83name2=f83name1;
1.18      pazsan   1165: :
1.63      pazsan   1166:     BEGIN  dup WHILE  (find-samelen)  dup  WHILE
                   1167:        >r 2dup r@ cell+ char+ capscomp  0=
                   1168:        IF  2drop r>  EXIT  THEN
1.18      pazsan   1169:        r> @
1.63      pazsan   1170:     REPEAT  THEN  nip nip ;
                   1171: : (find-samelen) ( u f83name1 -- u f83name2/0 )
                   1172:     BEGIN  2dup cell+ c@ $1F and <> WHILE  @  dup 0= UNTIL  THEN ;
1.8       pazsan   1173: 
1.62      pazsan   1174: \+has-hash [IF]
                   1175: 
1.13      pazsan   1176: (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]);
1.61      pazsan   1183:    if ((UCell)F83NAME_COUNT(f83name1)==u &&
1.56      anton    1184:        memcasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
1.13      pazsan   1185:      {
                   1186:        f83name2=f83name1;
                   1187:        break;
                   1188:      }
                   1189: }
1.18      pazsan   1190: :
                   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 ;
1.13      pazsan   1197: 
1.55      anton    1198: (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]);
1.61      pazsan   1206:    if ((UCell)F83NAME_COUNT(f83name1)==u &&
1.55      anton    1207:        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: 
1.43      anton    1221: (hashkey)      c_addr u1 -- u2         gforth  paren_hashkey
1.13      pazsan   1222: u2=0;
                   1223: while(u1--)
1.30      pazsan   1224:    u2+=(Cell)toupper(*c_addr++);
1.18      pazsan   1225: :
                   1226:  0 -rot bounds ?DO  I c@ toupper +  LOOP ;
1.14      anton    1227: 
1.43      anton    1228: (hashkey1)     c_addr u ubits -- ukey          gforth  paren_hashkey1
1.14      anton    1229: ""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));
1.18      pazsan   1242: :
                   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,
1.1       anton    1254: 
1.62      pazsan   1255: \+[THEN]
                   1256: 
1.43      anton    1257: (parse-white)  c_addr1 u1 -- c_addr2 u2        gforth  paren_parse_white
1.1       anton    1258: /* 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: }
1.18      pazsan   1271: :
                   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 - ;
1.1       anton    1276: 
1.61      pazsan   1277: 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: c_addr = CODE_ADDRESS(xt);
                   1296: :
                   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: :
1.63      pazsan   1320:     dodoes: over ! cell+ ! ;
1.61      pazsan   1321: 
                   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: ""0 if the engine is direct threaded.""
                   1339: #if defined(DIRECT_THREADED)
                   1340: n=0;
                   1341: #else
                   1342: n=1;
                   1343: #endif
                   1344: :
                   1345:  1 ;
                   1346: 
                   1347: \+has-os [IF]
                   1348: 
1.62      pazsan   1349: (key)  -- n            gforth  paren_key
                   1350: fflush(stdout);
                   1351: /* !! noecho */
                   1352: n = key();
                   1353: 
                   1354: key?   -- n            facility        key_q
                   1355: fflush(stdout);
                   1356: n = key_query;
                   1357: 
                   1358: stdout -- wfileid      gforth
                   1359: wfileid = (Cell)stdout;
                   1360: 
                   1361: stderr -- wfileid      gforth
                   1362: wfileid = (Cell)stderr;
                   1363: 
                   1364: form   -- urows ucols  gforth
                   1365: ""The number of lines and columns in the terminal. These numbers may change
                   1366: with the window size.""
                   1367: /* we could block SIGWINCH here to get a consistent size, but I don't
                   1368:  think this is necessary or always beneficial */
                   1369: urows=rows;
                   1370: ucols=cols;
                   1371: 
1.61      pazsan   1372: flush-icache   c_addr u --     gforth  flush_icache
                   1373: ""Make sure that the instruction cache of the processor (if there is
                   1374: one) does not contain stale data at @var{c_addr} and @var{u} bytes
                   1375: afterwards. @code{END-CODE} performs a @code{flush-icache}
                   1376: automatically. Caveat: @code{flush-icache} might not work on your
                   1377: installation; this is usually the case if direct threading is not
                   1378: supported on your machine (take a look at your @file{machine.h}) and
                   1379: your machine has a separate instruction cache. In such cases,
                   1380: @code{flush-icache} does nothing instead of flushing the instruction
                   1381: cache.""
                   1382: FLUSH_ICACHE(c_addr,u);
                   1383: 
                   1384: (bye)  n --    gforth  paren_bye
                   1385: return (Label *)n;
                   1386: 
                   1387: (system)       c_addr u -- wretval wior        gforth  peren_system
                   1388: int old_tp=terminal_prepped;
                   1389: deprep_terminal();
                   1390: wretval=system(cstr(c_addr,u,1)); /* ~ expansion on first part of string? */
                   1391: wior = IOR(wretval==-1 || (wretval==127 && errno != 0));
                   1392: if (old_tp)
                   1393:   prep_terminal();
                   1394: 
                   1395: getenv c_addr1 u1 -- c_addr2 u2        gforth
                   1396: c_addr2 = getenv(cstr(c_addr1,u1,1));
                   1397: u2 = (c_addr2 == NULL ? 0 : strlen(c_addr2));
                   1398: 
                   1399: open-pipe      c_addr u ntype -- wfileid wior  gforth  open_pipe
                   1400: wfileid=(Cell)popen(cstr(c_addr,u,1),fileattr[ntype]); /* ~ expansion of 1st arg? */
                   1401: wior = IOR(wfileid==0); /* !! the man page says that errno is not set reliably */
                   1402: 
                   1403: close-pipe     wfileid -- wretval wior         gforth  close_pipe
                   1404: wretval = pclose((FILE *)wfileid);
                   1405: wior = IOR(wretval==-1);
                   1406: 
                   1407: time&date      -- nsec nmin nhour nday nmonth nyear    facility-ext    time_and_date
                   1408: struct timeval time1;
                   1409: struct timezone zone1;
                   1410: struct tm *ltime;
                   1411: gettimeofday(&time1,&zone1);
                   1412: ltime=localtime((time_t *)&time1.tv_sec);
                   1413: nyear =ltime->tm_year+1900;
                   1414: nmonth=ltime->tm_mon+1;
                   1415: nday  =ltime->tm_mday;
                   1416: nhour =ltime->tm_hour;
                   1417: nmin  =ltime->tm_min;
                   1418: nsec  =ltime->tm_sec;
                   1419: 
                   1420: ms     n --    facility-ext
                   1421: struct timeval timeout;
                   1422: timeout.tv_sec=n/1000;
                   1423: timeout.tv_usec=1000*(n%1000);
                   1424: (void)select(0,0,0,0,&timeout);
                   1425: 
                   1426: allocate       u -- a_addr wior        memory
                   1427: a_addr = (Cell *)malloc(u?u:1);
                   1428: wior = IOR(a_addr==NULL);
                   1429: 
                   1430: free           a_addr -- wior          memory
                   1431: free(a_addr);
                   1432: wior = 0;
                   1433: 
                   1434: resize         a_addr1 u -- a_addr2 wior       memory
                   1435: ""Change the size of the allocated area at @i{a_addr1} to @i{u}
                   1436: address units, possibly moving the contents to a different
                   1437: area. @i{a_addr2} is the address of the resulting area. If
                   1438: @code{a_addr1} is 0, Gforth's (but not the standard) @code{resize}
                   1439: @code{allocate}s @i{u} address units.""
                   1440: /* the following check is not necessary on most OSs, but it is needed
                   1441:    on SunOS 4.1.2. */
                   1442: if (a_addr1==NULL)
                   1443:   a_addr2 = (Cell *)malloc(u);
                   1444: else
                   1445:   a_addr2 = (Cell *)realloc(a_addr1, u);
                   1446: wior = IOR(a_addr2==NULL);     /* !! Define a return code */
                   1447: 
                   1448: strerror       n -- c_addr u   gforth
                   1449: c_addr = strerror(n);
                   1450: u = strlen(c_addr);
                   1451: 
                   1452: strsignal      n -- c_addr u   gforth
                   1453: c_addr = strsignal(n);
                   1454: u = strlen(c_addr);
                   1455: 
                   1456: call-c w --    gforth  call_c
                   1457: ""Call the C function pointed to by @i{w}. The C function has to
                   1458: access the stack itself. The stack pointers are exported in the global
                   1459: variables @code{SP} and @code{FP}.""
                   1460: /* This is a first attempt at support for calls to C. This may change in
                   1461:    the future */
                   1462: IF_FTOS(fp[0]=FTOS);
                   1463: FP=fp;
                   1464: SP=sp;
                   1465: ((void (*)())w)();
                   1466: sp=SP;
                   1467: fp=FP;
                   1468: IF_TOS(TOS=sp[0]);
                   1469: IF_FTOS(FTOS=fp[0]);
                   1470: 
                   1471: \+[THEN] ( has-os ) has-files [IF]
                   1472: 
1.36      anton    1473: close-file     wfileid -- wior         file    close_file
                   1474: wior = IOR(fclose((FILE *)wfileid)==EOF);
1.1       anton    1475: 
                   1476: open-file      c_addr u ntype -- w2 wior       file    open_file
1.39      anton    1477: w2 = (Cell)fopen(tilde_cstr(c_addr, u, 1), fileattr[ntype]);
1.40      pazsan   1478: wior =  IOR(w2 == 0);
1.1       anton    1479: 
                   1480: create-file    c_addr u ntype -- w2 wior       file    create_file
1.33      pazsan   1481: Cell   fd;
1.65      pazsan   1482: fd = open(tilde_cstr(c_addr, u, 1), O_CREAT|O_TRUNC|ufileattr[ntype], 0666);
1.36      anton    1483: if (fd != -1) {
1.1       anton    1484:   w2 = (Cell)fdopen(fd, fileattr[ntype]);
1.40      pazsan   1485:   wior = IOR(w2 == 0);
1.1       anton    1486: } else {
                   1487:   w2 = 0;
1.36      anton    1488:   wior = IOR(1);
1.1       anton    1489: }
                   1490: 
                   1491: delete-file    c_addr u -- wior                file    delete_file
1.39      anton    1492: wior = IOR(unlink(tilde_cstr(c_addr, u, 1))==-1);
1.1       anton    1493: 
                   1494: rename-file    c_addr1 u1 c_addr2 u2 -- wior   file-ext        rename_file
1.39      anton    1495: char *s1=tilde_cstr(c_addr2, u2, 1);
                   1496: wior = IOR(rename(tilde_cstr(c_addr1, u1, 0), s1)==-1);
1.1       anton    1497: 
                   1498: file-position  wfileid -- ud wior      file    file_position
                   1499: /* !! use tell and lseek? */
1.52      anton    1500: ud = LONG2UD(ftell((FILE *)wfileid));
                   1501: wior = IOR(UD2LONG(ud)==-1);
1.1       anton    1502: 
                   1503: reposition-file        ud wfileid -- wior      file    reposition_file
1.52      anton    1504: wior = IOR(fseek((FILE *)wfileid, UD2LONG(ud), SEEK_SET)==-1);
1.1       anton    1505: 
                   1506: file-size      wfileid -- ud wior      file    file_size
                   1507: struct stat buf;
1.36      anton    1508: wior = IOR(fstat(fileno((FILE *)wfileid), &buf)==-1);
1.52      anton    1509: ud = LONG2UD(buf.st_size);
1.1       anton    1510: 
                   1511: resize-file    ud wfileid -- wior      file    resize_file
1.52      anton    1512: wior = IOR(ftruncate(fileno((FILE *)wfileid), UD2LONG(ud))==-1);
1.1       anton    1513: 
                   1514: read-file      c_addr u1 wfileid -- u2 wior    file    read_file
                   1515: /* !! fread does not guarantee enough */
                   1516: u2 = fread(c_addr, sizeof(Char), u1, (FILE *)wfileid);
1.7       pazsan   1517: wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
1.36      anton    1518: /* !! is the value of ferror errno-compatible? */
                   1519: if (wior)
                   1520:   clearerr((FILE *)wfileid);
1.1       anton    1521: 
                   1522: read-line      c_addr u1 wfileid -- u2 flag wior       file    read_line
1.13      pazsan   1523: /*
                   1524: Cell c;
                   1525: flag=-1;
                   1526: for(u2=0; u2<u1; u2++)
                   1527: {
                   1528:    *c_addr++ = (Char)(c = getc((FILE *)wfileid));
                   1529:    if(c=='\n') break;
                   1530:    if(c==EOF)
                   1531:      {
                   1532:        flag=FLAG(u2!=0);
                   1533:        break;
                   1534:      }
                   1535: }
                   1536: wior=FILEIO(ferror((FILE *)wfileid));
                   1537: */
                   1538: if ((flag=FLAG(!feof((FILE *)wfileid) &&
                   1539:               fgets(c_addr,u1+1,(FILE *)wfileid) != NULL))) {
1.36      anton    1540:   wior=FILEIO(ferror((FILE *)wfileid)); /* !! ior? */
                   1541:   if (wior)
                   1542:     clearerr((FILE *)wfileid);
1.13      pazsan   1543:   u2 = strlen(c_addr);
1.11      anton    1544:   u2-=((u2>0) && (c_addr[u2-1]==NEWLINE));
                   1545: }
                   1546: else {
                   1547:   wior=0;
                   1548:   u2=0;
                   1549: }
1.1       anton    1550: 
1.62      pazsan   1551: \+[THEN]  has-files [IF] -1 [ELSE] has-os [THEN] [IF]
                   1552: 
1.1       anton    1553: write-file     c_addr u1 wfileid -- wior       file    write_file
                   1554: /* !! fwrite does not guarantee enough */
                   1555: {
1.61      pazsan   1556:   UCell u2 = fwrite(c_addr, sizeof(Char), u1, (FILE *)wfileid);
1.7       pazsan   1557:   wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
1.36      anton    1558:   if (wior)
                   1559:     clearerr((FILE *)wfileid);
1.1       anton    1560: }
                   1561: 
1.51      anton    1562: emit-file      c wfileid -- wior       gforth  emit_file
                   1563: wior = FILEIO(putc(c, (FILE *)wfileid)==EOF);
                   1564: if (wior)
                   1565:   clearerr((FILE *)wfileid);
                   1566: 
1.62      pazsan   1567: \+[THEN]  has-files [IF]
                   1568: 
1.1       anton    1569: flush-file     wfileid -- wior         file-ext        flush_file
1.36      anton    1570: wior = IOR(fflush((FILE *) wfileid)==EOF);
1.1       anton    1571: 
1.38      anton    1572: file-status    c_addr u -- ntype wior  file-ext        file_status
1.39      anton    1573: char *filename=tilde_cstr(c_addr, u, 1);
1.38      anton    1574: if (access (filename, F_OK) != 0) {
                   1575:   ntype=0;
                   1576:   wior=IOR(1);
                   1577: }
                   1578: else if (access (filename, R_OK | W_OK) == 0) {
                   1579:   ntype=2; /* r/w */
                   1580:   wior=0;
                   1581: }
                   1582: else if (access (filename, R_OK) == 0) {
                   1583:   ntype=0; /* r/o */
                   1584:   wior=0;
                   1585: }
                   1586: else if (access (filename, W_OK) == 0) {
                   1587:   ntype=4; /* w/o */
                   1588:   wior=0;
                   1589: }
                   1590: else {
                   1591:   ntype=1; /* well, we cannot access the file, but better deliver a legal
                   1592:            access mode (r/o bin), so we get a decent error later upon open. */
                   1593:   wior=0;
                   1594: }
1.51      anton    1595: 
1.61      pazsan   1596: \+[THEN] ( has-files ) has-floats [IF]
                   1597: 
1.43      anton    1598: comparisons(f, r1 r2, f_, r1, r2, gforth, gforth, float, gforth)
                   1599: comparisons(f0, r, f_zero_, r, 0., float, gforth, float, gforth)
1.1       anton    1600: 
                   1601: d>f            d -- r          float   d_to_f
1.52      anton    1602: #ifdef BUGGY_LONG_LONG
                   1603: extern double ldexp(double x, int exp);
                   1604: r = ldexp((Float)d.hi,CELL_BITS) + (Float)d.lo;
                   1605: #else
1.1       anton    1606: r = d;
1.52      anton    1607: #endif
1.1       anton    1608: 
                   1609: f>d            r -- d          float   f_to_d
1.52      anton    1610: #ifdef BUGGY_LONG_LONG
                   1611: d.hi = ldexp(r,-CELL_BITS) - (r<0);
                   1612: d.lo = r-ldexp((Float)d.hi,CELL_BITS);
                   1613: #else
1.1       anton    1614: d = r;
1.52      anton    1615: #endif
1.1       anton    1616: 
                   1617: f!             r f_addr --     float   f_store
                   1618: *f_addr = r;
                   1619: 
                   1620: f@             f_addr -- r     float   f_fetch
                   1621: r = *f_addr;
                   1622: 
                   1623: df@            df_addr -- r    float-ext       d_f_fetch
                   1624: #ifdef IEEE_FP
                   1625: r = *df_addr;
                   1626: #else
                   1627: !! df@
                   1628: #endif
                   1629: 
                   1630: df!            r df_addr --    float-ext       d_f_store
                   1631: #ifdef IEEE_FP
                   1632: *df_addr = r;
                   1633: #else
                   1634: !! df!
                   1635: #endif
                   1636: 
                   1637: sf@            sf_addr -- r    float-ext       s_f_fetch
                   1638: #ifdef IEEE_FP
                   1639: r = *sf_addr;
                   1640: #else
                   1641: !! sf@
                   1642: #endif
                   1643: 
                   1644: sf!            r sf_addr --    float-ext       s_f_store
                   1645: #ifdef IEEE_FP
                   1646: *sf_addr = r;
                   1647: #else
                   1648: !! sf!
                   1649: #endif
                   1650: 
                   1651: f+             r1 r2 -- r3     float   f_plus
                   1652: r3 = r1+r2;
                   1653: 
                   1654: f-             r1 r2 -- r3     float   f_minus
                   1655: r3 = r1-r2;
                   1656: 
                   1657: f*             r1 r2 -- r3     float   f_star
                   1658: r3 = r1*r2;
                   1659: 
                   1660: f/             r1 r2 -- r3     float   f_slash
                   1661: r3 = r1/r2;
                   1662: 
                   1663: f**            r1 r2 -- r3     float-ext       f_star_star
1.28      anton    1664: ""@i{r3} is @i{r1} raised to the @i{r2}th power""
1.1       anton    1665: r3 = pow(r1,r2);
                   1666: 
                   1667: fnegate                r1 -- r2        float
                   1668: r2 = - r1;
                   1669: 
                   1670: fdrop          r --            float
                   1671: 
                   1672: fdup           r -- r r        float
                   1673: 
                   1674: fswap          r1 r2 -- r2 r1  float
                   1675: 
                   1676: fover          r1 r2 -- r1 r2 r1       float
                   1677: 
                   1678: frot           r1 r2 r3 -- r2 r3 r1    float
                   1679: 
1.42      anton    1680: fnip           r1 r2 -- r2     gforth
                   1681: 
                   1682: ftuck          r1 r2 -- r2 r1 r2       gforth
                   1683: 
1.1       anton    1684: float+         f_addr1 -- f_addr2      float   float_plus
                   1685: f_addr2 = f_addr1+1;
                   1686: 
                   1687: floats         n1 -- n2        float
                   1688: n2 = n1*sizeof(Float);
                   1689: 
                   1690: floor          r1 -- r2        float
1.28      anton    1691: ""round towards the next smaller integral value, i.e., round toward negative infinity""
1.1       anton    1692: /* !! unclear wording */
                   1693: r2 = floor(r1);
                   1694: 
                   1695: fround         r1 -- r2        float
1.28      anton    1696: ""round to the nearest integral value""
1.1       anton    1697: /* !! unclear wording */
1.26      anton    1698: #ifdef HAVE_RINT
1.1       anton    1699: r2 = rint(r1);
1.26      anton    1700: #else
                   1701: r2 = floor(r1+0.5);
                   1702: /* !! This is not quite true to the rounding rules given in the standard */
                   1703: #endif
1.1       anton    1704: 
                   1705: fmax           r1 r2 -- r3     float
                   1706: if (r1<r2)
                   1707:   r3 = r2;
                   1708: else
                   1709:   r3 = r1;
                   1710: 
                   1711: fmin           r1 r2 -- r3     float
                   1712: if (r1<r2)
                   1713:   r3 = r1;
                   1714: else
                   1715:   r3 = r2;
                   1716: 
                   1717: represent              r c_addr u -- n f1 f2   float
                   1718: char *sig;
1.33      pazsan   1719: Cell flag;
                   1720: Cell decpt;
1.40      pazsan   1721: sig=ecvt(r, u, (int *)&decpt, (int *)&flag);
1.33      pazsan   1722: n=(r==0 ? 1 : decpt);
1.1       anton    1723: f1=FLAG(flag!=0);
                   1724: f2=FLAG(isdigit(sig[0])!=0);
                   1725: memmove(c_addr,sig,u);
                   1726: 
                   1727: >float c_addr u -- flag        float   to_float
                   1728: /* real signature: c_addr u -- r t / f */
                   1729: Float r;
1.17      anton    1730: char *number=cstr(c_addr, u, 1);
1.1       anton    1731: char *endconv;
1.32      pazsan   1732: while(isspace(number[--u]) && u>0);
                   1733: switch(number[u])
1.23      pazsan   1734: {
1.32      pazsan   1735:    case 'd':
                   1736:    case 'D':
                   1737:    case 'e':
                   1738:    case 'E':  break;
                   1739:    default :  u++; break;
1.23      pazsan   1740: }
                   1741: number[u]='\0';
1.1       anton    1742: r=strtod(number,&endconv);
1.30      pazsan   1743: if((flag=FLAG(!(Cell)*endconv)))
1.1       anton    1744: {
1.32      pazsan   1745:    IF_FTOS(fp[0] = FTOS);
                   1746:    fp += -1;
                   1747:    FTOS = r;
                   1748: }
                   1749: else if(*endconv=='d' || *endconv=='D')
                   1750: {
                   1751:    *endconv='E';
                   1752:    r=strtod(number,&endconv);
                   1753:    if((flag=FLAG(!(Cell)*endconv)))
                   1754:      {
1.1       anton    1755:        IF_FTOS(fp[0] = FTOS);
                   1756:        fp += -1;
                   1757:        FTOS = r;
1.32      pazsan   1758:      }
1.1       anton    1759: }
                   1760: 
                   1761: fabs           r1 -- r2        float-ext
                   1762: r2 = fabs(r1);
                   1763: 
                   1764: facos          r1 -- r2        float-ext
                   1765: r2 = acos(r1);
                   1766: 
                   1767: fasin          r1 -- r2        float-ext
                   1768: r2 = asin(r1);
                   1769: 
                   1770: fatan          r1 -- r2        float-ext
                   1771: r2 = atan(r1);
                   1772: 
                   1773: fatan2         r1 r2 -- r3     float-ext
1.28      anton    1774: ""@i{r1/r2}=tan@i{r3}. The standard does not require, but probably
                   1775: intends this to be the inverse of @code{fsincos}. In gforth it is.""
1.1       anton    1776: r3 = atan2(r1,r2);
                   1777: 
                   1778: fcos           r1 -- r2        float-ext
                   1779: r2 = cos(r1);
                   1780: 
                   1781: fexp           r1 -- r2        float-ext
                   1782: r2 = exp(r1);
                   1783: 
1.3       pazsan   1784: fexpm1         r1 -- r2        float-ext
1.28      anton    1785: ""@i{r2}=@i{e}**@i{r1}@minus{}1""
1.27      anton    1786: #ifdef HAVE_EXPM1
1.29      anton    1787: extern double expm1(double);
                   1788: r2 = expm1(r1);
1.3       pazsan   1789: #else
1.29      anton    1790: r2 = exp(r1)-1.;
1.3       pazsan   1791: #endif
                   1792: 
1.1       anton    1793: fln            r1 -- r2        float-ext
                   1794: r2 = log(r1);
                   1795: 
1.3       pazsan   1796: flnp1          r1 -- r2        float-ext
1.28      anton    1797: ""@i{r2}=ln(@i{r1}+1)""
1.27      anton    1798: #ifdef HAVE_LOG1P
1.29      anton    1799: extern double log1p(double);
                   1800: r2 = log1p(r1);
1.3       pazsan   1801: #else
1.29      anton    1802: r2 = log(r1+1.);
1.3       pazsan   1803: #endif
                   1804: 
1.1       anton    1805: flog           r1 -- r2        float-ext
1.28      anton    1806: ""the decimal logarithm""
1.1       anton    1807: r2 = log10(r1);
                   1808: 
1.29      anton    1809: falog          r1 -- r2        float-ext
                   1810: ""@i{r2}=10**@i{r1}""
                   1811: extern double pow10(double);
                   1812: r2 = pow10(r1);
                   1813: 
1.3       pazsan   1814: fsin           r1 -- r2        float-ext
                   1815: r2 = sin(r1);
                   1816: 
                   1817: fsincos                r1 -- r2 r3     float-ext
1.29      anton    1818: ""@i{r2}=sin(@i{r1}), @i{r3}=cos(@i{r1})""
1.1       anton    1819: r2 = sin(r1);
                   1820: r3 = cos(r1);
                   1821: 
                   1822: fsqrt          r1 -- r2        float-ext
                   1823: r2 = sqrt(r1);
                   1824: 
                   1825: ftan           r1 -- r2        float-ext
                   1826: r2 = tan(r1);
1.32      pazsan   1827: :
                   1828:  fsincos f/ ;
1.29      anton    1829: 
                   1830: fsinh          r1 -- r2        float-ext
                   1831: r2 = sinh(r1);
1.32      pazsan   1832: :
                   1833:  fexpm1 fdup fdup 1. d>f f+ f/ f+ f2/ ;
1.29      anton    1834: 
                   1835: fcosh          r1 -- r2        float-ext
                   1836: r2 = cosh(r1);
1.32      pazsan   1837: :
                   1838:  fexp fdup 1/f f+ f2/ ;
1.29      anton    1839: 
                   1840: ftanh          r1 -- r2        float-ext
                   1841: r2 = tanh(r1);
1.32      pazsan   1842: :
                   1843:  f2* fexpm1 fdup 2. d>f f+ f/ ;
1.29      anton    1844: 
                   1845: fasinh         r1 -- r2        float-ext
                   1846: r2 = asinh(r1);
1.32      pazsan   1847: :
                   1848:  fdup fdup f* 1. d>f f+ fsqrt f/ fatanh ;
1.29      anton    1849: 
                   1850: facosh         r1 -- r2        float-ext
                   1851: r2 = acosh(r1);
1.32      pazsan   1852: :
                   1853:  fdup fdup f* 1. d>f f- fsqrt f+ fln ;
1.29      anton    1854: 
                   1855: fatanh         r1 -- r2        float-ext
                   1856: r2 = atanh(r1);
1.32      pazsan   1857: :
                   1858:  fdup f0< >r fabs 1. d>f fover f- f/  f2* flnp1 f2/
                   1859:  r> IF  fnegate  THEN ;
1.1       anton    1860: 
1.43      anton    1861: sfloats                n1 -- n2        float-ext       s_floats
                   1862: n2 = n1*sizeof(SFloat);
                   1863: 
                   1864: dfloats                n1 -- n2        float-ext       d_floats
                   1865: n2 = n1*sizeof(DFloat);
                   1866: 
                   1867: sfaligned      c_addr -- sf_addr       float-ext       s_f_aligned
1.45      pazsan   1868: sf_addr = (SFloat *)((((Cell)c_addr)+(sizeof(SFloat)-1))&(-sizeof(SFloat)));
                   1869: :
                   1870:  [ 1 sfloats 1- ] Literal + [ -1 sfloats ] Literal and ;
1.43      anton    1871: 
                   1872: dfaligned      c_addr -- df_addr       float-ext       d_f_aligned
1.45      pazsan   1873: df_addr = (DFloat *)((((Cell)c_addr)+(sizeof(DFloat)-1))&(-sizeof(DFloat)));
                   1874: :
                   1875:  [ 1 dfloats 1- ] Literal + [ -1 dfloats ] Literal and ;
1.43      anton    1876: 
1.44      pazsan   1877: \ The following words access machine/OS/installation-dependent
                   1878: \   Gforth internals
1.6       anton    1879: \ !! how about environmental queries DIRECT-THREADED,
                   1880: \   INDIRECT-THREADED, TOS-CACHED, FTOS-CACHED, CODEFIELD-DOES */
1.1       anton    1881: 
1.61      pazsan   1882: \ local variable implementation primitives
                   1883: \+[THEN] ( has-floats ) has-locals [IF]
1.1       anton    1884: 
1.43      anton    1885: @local#                -- w    gforth  fetch_local_number
1.35      anton    1886: w = *(Cell *)(lp+(Cell)NEXT_INST);
                   1887: INC_IP(1);
1.1       anton    1888: 
1.9       anton    1889: @local0        -- w    new     fetch_local_zero
1.18      pazsan   1890: w = *(Cell *)(lp+0*sizeof(Cell));
1.9       anton    1891: 
1.18      pazsan   1892: @local1        -- w    new     fetch_local_four
                   1893: w = *(Cell *)(lp+1*sizeof(Cell));
1.9       anton    1894: 
1.18      pazsan   1895: @local2        -- w    new     fetch_local_eight
                   1896: w = *(Cell *)(lp+2*sizeof(Cell));
1.9       anton    1897: 
1.18      pazsan   1898: @local3        -- w    new     fetch_local_twelve
                   1899: w = *(Cell *)(lp+3*sizeof(Cell));
1.9       anton    1900: 
1.61      pazsan   1901: \+has-floats [IF]
                   1902: 
1.43      anton    1903: f@local#       -- r    gforth  f_fetch_local_number
1.35      anton    1904: r = *(Float *)(lp+(Cell)NEXT_INST);
                   1905: INC_IP(1);
1.1       anton    1906: 
1.9       anton    1907: f@local0       -- r    new     f_fetch_local_zero
1.18      pazsan   1908: r = *(Float *)(lp+0*sizeof(Float));
1.9       anton    1909: 
1.18      pazsan   1910: f@local1       -- r    new     f_fetch_local_eight
                   1911: r = *(Float *)(lp+1*sizeof(Float));
1.9       anton    1912: 
1.61      pazsan   1913: \+[THEN]
                   1914: 
1.43      anton    1915: laddr#         -- c_addr       gforth  laddr_number
1.1       anton    1916: /* this can also be used to implement lp@ */
1.35      anton    1917: c_addr = (Char *)(lp+(Cell)NEXT_INST);
                   1918: INC_IP(1);
1.1       anton    1919: 
1.43      anton    1920: lp+!#  --      gforth  lp_plus_store_number
1.1       anton    1921: ""used with negative immediate values it allocates memory on the
                   1922: local stack, a positive immediate argument drops memory from the local
                   1923: stack""
1.35      anton    1924: lp += (Cell)NEXT_INST;
                   1925: INC_IP(1);
1.9       anton    1926: 
1.18      pazsan   1927: lp-    --      new     minus_four_lp_plus_store
                   1928: lp += -sizeof(Cell);
1.9       anton    1929: 
1.18      pazsan   1930: lp+    --      new     eight_lp_plus_store
                   1931: lp += sizeof(Float);
1.9       anton    1932: 
1.18      pazsan   1933: lp+2   --      new     sixteen_lp_plus_store
                   1934: lp += 2*sizeof(Float);
1.1       anton    1935: 
1.43      anton    1936: lp!    c_addr --       gforth  lp_store
1.1       anton    1937: lp = (Address)c_addr;
                   1938: 
1.43      anton    1939: >l     w --    gforth  to_l
1.1       anton    1940: lp -= sizeof(Cell);
                   1941: *(Cell *)lp = w;
                   1942: 
1.61      pazsan   1943: \+has-floats [IF]
                   1944: 
1.43      anton    1945: f>l    r --    gforth  f_to_l
1.1       anton    1946: lp -= sizeof(Float);
                   1947: *(Float *)lp = r;
1.4       pazsan   1948: 
1.61      pazsan   1949: \+[THEN]  [THEN] \ has-locals
                   1950: 
1.43      anton    1951: up!    a_addr --       gforth  up_store
1.65      pazsan   1952: UP=up=(char *)a_addr;
1.63      pazsan   1953: :
                   1954:  up ! ;
                   1955: Variable UP

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>