File:  [gforth] / gforth / float.fs
Revision 1.16: download - view: text, annotated - select for diffs
Sat Oct 7 17:38:12 1995 UTC (24 years ago) by anton
Branches: MAIN
CVS tags: HEAD
added code.fs (code, ;code, end-code, assembler)
renamed dostruc to dofield
made index and doc-entries nicer
Only words containing 'e' or 'E' are converted to FP numbers.
added many wordset comments
added flush-icache primitive and FLUSH_ICACHE macro
added +DO, U+DO, -DO, U-DO and -LOOP
added code address labels (`docol:' etc.)
fixed sparc cache_flush

    1: \ High level floating point                            14jan94py
    2: 
    3: 1 cells 4 = [IF]
    4: ' cells   Alias sfloats
    5: ' cell+   Alias sfloat+
    6: ' align   Alias sfalign
    7: ' aligned Alias sfaligned
    8: [ELSE]
    9: : sfloats  2* 2* ;
   10: : sfloat+  4 + ;
   11: : sfaligned ( addr -- addr' )  3 + -4 and ;
   12: : sfalign ( -- )  here dup sfaligned swap ?DO  bl c,  LOOP ;
   13: [THEN]
   14: 
   15: 1 floats 8 = [IF]
   16: ' floats   Alias dfloats
   17: ' float+   Alias dfloat+
   18: ' falign   Alias dfalign
   19: ' faligned Alias dfaligned
   20: [ELSE]
   21: : dfloats  2* 2* 2* ;
   22: : dfloat+  8 + ;
   23: : dfaligned ( addr -- addr' )  7 + -8 and ;
   24: : dfalign ( -- )  here dup dfaligned swap ?DO  bl c,  LOOP ;
   25: [THEN]
   26: 
   27: : f, ( f -- )  here 1 floats allot f! ;
   28: 
   29: : fconstant  ( r -- ) \ float
   30:     Create f,
   31: DOES> ( -- r )
   32:     f@ ;
   33: 
   34: : fdepth  ( -- n )  f0 @ fp@ - [ 1 floats ] Literal / ;
   35: 
   36: : FLit ( -- r )  r> dup f@ float+ >r ;
   37: : FLiteral ( r -- )
   38:   BEGIN  here cell+ dup faligned <>  WHILE  postpone noop  REPEAT
   39:   postpone FLit  f, ;  immediate
   40: 
   41: &15 Value precision
   42: : set-precision  to precision ;
   43: 
   44: : scratch ( r -- addr len )
   45:   pad precision - precision ;
   46: 
   47: : zeros ( n -- )   0 max 0 ?DO  '0 emit  LOOP ;
   48: 
   49: : -zeros ( addr u -- addr' u' )
   50:   BEGIN  dup  WHILE  1- 2dup + c@ '0 <>  UNTIL  1+  THEN ;
   51: 
   52: : f$ ( f -- n )  scratch represent 0=
   53:   IF  2drop  scratch 3 min type  rdrop  EXIT  THEN
   54:   IF  '- emit  THEN ;
   55: 
   56: : f.  ( r -- )  f$ dup >r 0<
   57:   IF    '0 emit
   58:   ELSE  scratch r@ min type  r@ precision - zeros  THEN
   59:   '. emit r@ negate zeros
   60:   scratch r> 0 max /string 0 max -zeros type space ;
   61: \ I'm afraid this does not really implement ansi semantics wrt precision.
   62: \ Shouldn't precision indicate the number of places shown after the point?
   63: 
   64: : fe. ( r -- )  f$ 1- s>d 3 fm/mod 3 * >r 1+ >r
   65:   scratch r@ min type '. emit  scratch r> /string type
   66:   'E emit r> . ;
   67: 
   68: : fs. ( r -- )  f$ 1-
   69:   scratch over c@ emit '. emit 1 /string type
   70:   'E emit . ;
   71: 
   72: require debugging.fs
   73: 
   74: : sfnumber ( c-addr u -- r / )
   75:     2dup [CHAR] e scan
   76:     dup 0=
   77:     IF
   78: 	2drop 2dup [CHAR] E scan
   79:     THEN
   80:     nip
   81:     IF
   82: 	2dup >float
   83: 	IF
   84: 	    2drop state @
   85: 	    IF
   86: 		POSTPONE FLiteral
   87: 	    THEN
   88: 	    EXIT
   89: 	THEN
   90:     THEN
   91:     defers notfound ;
   92: 
   93: ' sfnumber IS notfound
   94: 
   95: : fvariable ( -- )
   96:     Create 0.0E0 f, ;
   97:     \ does> ( -- f-addr )
   98: 
   99: 1.0e0 fasin 2.0e0 f* fconstant pi
  100: 
  101: : f2*  2.0e0 f* ;
  102: : f2/  0.5e0 f* ;
  103: : 1/f  1.0e0 fswap f/ ;
  104: 
  105: 
  106: \ We now have primitives for these, so we need not define them
  107: 
  108: \ : falog ( f -- 10^f )  [ 10.0e0 fln ] FLiteral f* fexp ;
  109: 
  110: \ : fsinh    fexpm1 fdup fdup 1.0e0 f+ f/ f+ f2/ ;
  111: \ : fcosh    fexp fdup 1/f f+ f2/ ;
  112: \ : ftanh    f2* fexpm1 fdup 2.0e0 f+ f/ ;
  113: 
  114: \ : fatanh   fdup f0< >r fabs 1.0e0 fover f- f/  f2* flnp1 f2/
  115: \            r> IF  fnegate  THEN ;
  116: \ : facosh   fdup fdup f* 1.0e0 f- fsqrt f+ fln ;
  117: \ : fasinh   fdup fdup f* 1.0e0 f+ fsqrt f/ fatanh ;
  118: 
  119: \ !! factor out parts
  120: : f~ ( f1 f2 f3 -- flag ) \ float-ext
  121:     fdup f0=
  122:     IF
  123: 	fdrop f= EXIT
  124:     THEN
  125:     fdup f0>
  126:     IF
  127: 	frot frot f- fabs fswap
  128:     ELSE
  129: 	fnegate frot frot fover fabs fover fabs f+ frot frot
  130: 	f- fabs frot frot f*
  131:     THEN
  132:     f< ;
  133: 
  134: : f.s  ." <" fdepth 0 .r ." > " fdepth 0 max maxdepth-.s @ min dup 0 
  135:   ?DO  dup i - 1- floats fp@ + f@ f.  LOOP  drop ; 

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