File:  [gforth] / gforth / prim
Revision 1.16: download - view: text, annotated - select for diffs
Tue Dec 8 22:02:49 1998 UTC (20 years, 10 months ago) by anton
Branches: MAIN
CVS tags: HEAD
updated dates in copyright messages
inserted copyright messages in most files that did not have them
removed outdated files engine/32bit.h engine/strsig.c

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

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