Annotation of gforth/primitives, revision 1.61

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

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