File:  [gforth] / gforth / prim
Revision 1.32: download - view: text, annotated - select for diffs
Sat May 15 20:00:21 1999 UTC (20 years, 5 months ago) by anton
Branches: MAIN
CVS tags: HEAD
documentation changes
factored out f~abs and f~rel from f~

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

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