File:  [gforth] / gforth / Attic / primitives
Revision 1.53: download - view: text, annotated - select for diffs
Mon Feb 26 16:52:46 1996 UTC (28 years, 1 month ago) by pazsan
Branches: MAIN
CVS tags: HEAD
make dist now consistent with new files
improved mmul (both dblsub and primitive.fs replacement)

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

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