File:  [gforth] / gforth / prim
Revision 1.37: download - view: text, annotated - select for diffs
Sat Aug 7 21:40:35 1999 UTC (20 years, 1 month ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Made USE_TOS work with 386 on gcc 2.95

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

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