Annotation of gforth/primitives, revision 1.62

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

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