Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #37758 > unrolled thread

How I learned my computer arithmetic

Started bylehs <skydda.barnen@gmail.com>
First post2015-03-30 08:18 -0700
Last post2015-05-05 16:41 -0700
Articles 20 on this page of 22 — 3 participants

Back to article view | Back to comp.lang.forth


Contents

  How I learned my computer arithmetic lehs <skydda.barnen@gmail.com> - 2015-03-30 08:18 -0700
    Re: How I learned my computer arithmetic lehs <skydda.barnen@gmail.com> - 2015-03-30 08:42 -0700
      Re: How I learned my computer arithmetic lehs <skydda.barnen@gmail.com> - 2015-04-02 13:31 -0700
        Re: How I learned my computer arithmetic lehs <skydda.barnen@gmail.com> - 2015-04-03 08:20 -0700
          Re: How I learned my computer arithmetic lehs <skydda.barnen@gmail.com> - 2015-04-04 11:55 -0700
    Re: How I learned my computer arithmetic lehs <skydda.barnen@gmail.com> - 2015-03-30 08:48 -0700
    Re: How I learned my computer arithmetic lehs <skydda.barnen@gmail.com> - 2015-04-05 11:57 -0700
    Re: How I learned my computer arithmetic lehs <skydda.barnen@gmail.com> - 2015-04-09 14:05 -0700
      Re: How I learned my computer arithmetic "Alex McDonald" <blog@rivadpm.com> - 2015-04-09 15:06 -0700
        Re: How I learned my computer arithmetic lehs <skydda.barnen@gmail.com> - 2015-04-10 00:25 -0700
    Re: How I learned my computer arithmetic lehs <skydda.barnen@gmail.com> - 2015-04-10 04:21 -0700
    Re: How I learned my computer arithmetic lehs <skydda.barnen@gmail.com> - 2015-04-10 12:47 -0700
      Re: How I learned my computer arithmetic Paul Rubin <no.email@nospam.invalid> - 2015-04-10 12:58 -0700
        Re: How I learned my computer arithmetic lehs <skydda.barnen@gmail.com> - 2015-04-10 13:03 -0700
          Re: How I learned my computer arithmetic Paul Rubin <no.email@nospam.invalid> - 2015-04-10 13:18 -0700
            Re: How I learned my computer arithmetic lehs <skydda.barnen@gmail.com> - 2015-04-10 13:24 -0700
    Re: How I learned my computer arithmetic lehs <skydda.barnen@gmail.com> - 2015-04-11 01:30 -0700
      Re: How I learned my computer arithmetic lehs <skydda.barnen@gmail.com> - 2015-04-11 01:32 -0700
      Re: How I learned my computer arithmetic Paul Rubin <no.email@nospam.invalid> - 2015-04-11 09:41 -0700
    Re: How I learned my computer arithmetic lehs <skydda.barnen@gmail.com> - 2015-04-11 01:34 -0700
    Re: How I learned my computer arithmetic lehs <skydda.barnen@gmail.com> - 2015-04-11 02:55 -0700
    Re: How I learned my computer arithmetic lehs <skydda.barnen@gmail.com> - 2015-05-05 16:41 -0700

Page 1 of 2  [1] 2  Next page →


#37758 — How I learned my computer arithmetic

Fromlehs <skydda.barnen@gmail.com>
Date2015-03-30 08:18 -0700
SubjectHow I learned my computer arithmetic
Message-ID<0fb1b397-e6c4-4ced-b83d-77f85526d5bd@googlegroups.com>
Some month ago I wrote about big integers in Forth. I had made words for big integer arithmetic on a big integer stack, but I realized it was too slow. 

I have reorganized the extension and now plus and minus are about 10 times faster, division 15 times faster and multiplication about 30 times faster.

From the beginning my ambition was to learn Forth arithmetic on number strings ("0"=ASC 48 etc) but thought it was slow. Then I made a minor change while using digital bytes as a base in byte-strings. But only got about 2 times faster operations that way. I wasn't really inspired to optimize the arithmetic routines because stored that way, with most significant bytes i low mem, is awkward (at least) in Forth. So what I did now was to reverse the order of the data and using cells as base. That is, in the first version with ascii-strings I used 10 as a base, in the second version the base 2^8 - and now the optimal(?) base 2^32 or 2^64 depending on the computer.

I would be very pleased if you tested the words on your systems and told me if it is possible to load and run. It should be possible to make it run on any ANS Forth 94.

The system is very simple to handle:

B 31803578137508173058713085713753154260872480672408768467489

put the number on tos on the B-stack where all arithmetic is made.

B+, B-, B* and B/MOD is what I got so far and works as it should in Forth.

B<, B= and B0= ***DOES NOT CHANGE THE B-STACK*** and put flags on the parameter stack.

BDUP, BDROP,... are totally analogous 

There is an extra stack X in some analogy with the R-stack:

>X, X, Y, Z, X>, XDROP, XDUP, XOVER and so far. That because of the need to store intermediate results, since there is no big variables.

[toc] | [next] | [standalone]


#37759

Fromlehs <skydda.barnen@gmail.com>
Date2015-03-30 08:42 -0700
Message-ID<50535bba-b8c2-44d5-a583-1e53c2a08982@googlegroups.com>
In reply to#37758
BASE @ HEX

\ LOCAL PARAMETERS AND VARIABLES BY EXTRA STACKS FOR SINGLE NUMBERS
\ Make FORTH programming much more easy and can be eliminated after

CELL NEGATE CONSTANT -CELL

CREATE xst HERE , S0 @ R0 @ - U2/ ALLOT ALIGN
CREATE yst HERE , S0 @ R0 @ - U2/ ALLOT ALIGN
CREATE zst HERE , S0 @ R0 @ - U2/ ALLOT ALIGN

: >x	( x -- )  CELL xst +! xst @ ! ;
: x> 	( -- x )  xst @ @ -CELL xst +! ;
: x 	( -- x )  xst @ @ ;
: x! 	( n -- )  xst @ ! ;
: x+! 	( n -- )  xst @ +! ;
: >y 	( y -- )  CELL yst +! yst @ ! ;
: y> 	( -- y )  yst @ @ -CELL yst +! ;
: y 	( -- y )  yst @ @ ;
: y! 	( n -- )  yst @ ! ;
: y+! 	( n -- )  yst @ +! ;
: >z 	( z -- )  CELL zst +! zst @ ! ;
: z> 	( -- z )  zst @ @ -CELL zst +! ;
: z 	( -- z )  zst @ @ ;
: z! 	( n -- )  zst @ ! ;
: z+! 	( n -- )  zst @ +! ;
: drop 	( ad -- )  CELL NEGATE SWAP +! ;
: drops ( ad n -- )  CELLS NEGATE SWAP +! ;
: empty? ( ad -- f )  DUP @ = ;
: depth	( ad -- n ) DUP @ SWAP - CELL / ;
: clx xst DUP ! ;
: cly yst DUP ! ;
: clz zst DUP ! ;
: st! ( -- ) clx cly clz ; st!
: .x xst depth IF x> RECURSE DUP . >x THEN ;
: .y yst depth IF y> RECURSE DUP . >y THEN ;
: .z zst depth IF z> RECURSE DUP . >z THEN ;
: drop-all xst drop yst drop zst drop ;
: .s ." x: " .x CR ." y: " .y CR ." z: " .z ;

\ Miscellanous

: LOG~ \ n -- #binary digits
  0 >x BEGIN 1 x+! U2/ ?DUP 0= UNTIL x> ;

   8 CELL * CONSTANT BITS
    BITS 1- CONSTANT BITS-1
BITS-1 LOG~ CONSTANT LBITS

: 2/MOD \ n -- r q
  DUP 1 AND SWAP U2/ ;

: 4/MOD \ n -- r q
  DUP 3 AND SWAP 2 RSHIFT ;

: 8/MOD \ n -- r q
  DUP 7 AND SWAP 3 RSHIFT ;

: BITS/MOD \ n -- r q
  DUP BITS-1 AND SWAP LBITS RSHIFT ;

: 256/MOD \ n -- r q
  DUP 0FF AND SWAP 8 RSHIFT ;

: U.R \ n t --
  >R 0 <# #S #>
  R> OVER - SPACES TYPE ;

VARIABLE RND 

: RESET_SEED 0ABCDEF1 RND ! ; RESET_SEED

: RAND \ -- u
  RND @ 08088405 * 1+ DUP RND ! ;

: RANDOM \ u1 -- u2
  RAND UM* NIP ;


\ CASE-OF STRUCTURE

VARIABLE CSP
6 CONSTANT L-CAS#
CREATE S-CSP L-CAS# CELLS ALLOT ALIGN
S-CSP CSP !

: +CSP ( -- p )  CSP @ DUP CELL+ CSP ! ;
: -CSP ( -- )  CSP @ 1 CELLS - CSP ! ;
: !CSP ( -- )  SP@ +CSP ! ;
: CSP@ ( -- )  CSP @ 1 CELLS - @ ;

: CASE ( -- )  !CSP ; IMMEDIATE
: ENDOF ( -- )  [COMPILE] ELSE ; IMMEDIATE

: OF ( -- )  POSTPONE OVER POSTPONE =
  [COMPILE] IF POSTPONE DROP ; IMMEDIATE

: ENDCASE \ --
  POSTPONE DROP
  BEGIN SP@ CSP@ = 0=
  WHILE [COMPILE] THEN
  REPEAT -CSP ; IMMEDIATE


\ BIG INTEGERS BASED ON CELL "DIGITS"

\ main stack
\ pointer stack in HIGH mem towards LOW mem
\ array stack in LOW mem towards HIGH mem

0A000 CONSTANT MAXV
MAXV CELL + ALLOCATE THROW ALIGNED CONSTANT V$0
V$0 MAXV + CONSTANT V0
VARIABLE VP

\ extra stack
08000 CONSTANT MAXX
MAXX CELL + ALLOCATE THROW ALIGNED CONSTANT X$0
X$0 MAXX + CONSTANT X0
VARIABLE XP

\ extra pad
02000 DUP ALLOCATE THROW CONSTANT PAD1 
PAD1 + CELL- CONSTANT PAD2

\ 02000 ALLOCATE THROW ALIGNED CONSTANT PAD2

: REZ \ a n -- a' n'	Delete leading zero ascii 48
  DUP 1 =
  IF EXIT
  THEN OVER C@ 030 =
  IF 1- SWAP 1+ SWAP RECURSE
  THEN ;

: PAD1$ \ -- a n	Get string
  PAD1 CELL+ PAD1 @ ;

: A>  0F AND ;		\ Ascii number to binary number
: >A  030 OR ;		\ Reverse
: VST!  V0 CELL - VP ! V$0 VP @ ! ;

VST! 	\ Initialize stack för dynamical numbers

: NEXT ( -- a )  VP @ @ ;
: FIRST ( -- a )  VP @ CELL + @ ;	\ big on tos
: SECOND ( -- a )  VP @ 2 CELLS + @ ;	\ big on second
: THIRD ( -- a )  VP @ 3 CELLS + @ ;	\ big on third
: VP+ ( -- )  -CELL VP +! ;		\ stack pointer

: TOV \ ad --		ad of number array to stack
  VP+ VP @ ! ;

: BEMPTY ( -- f )  NEXT V$0 = ;
: LEN1 ( -- n )  NEXT FIRST - ;		\ get length to first
: LEN2 ( -- n )  FIRST SECOND - ;
: LEN3 ( -- n )  SECOND THIRD - ;
: TOP$ ( -- a n )  FIRST LEN1 ;
: SEC$ ( -- a n )  SECOND LEN2 ;
: THI$ ( -- a n )  THIRD LEN3 ;

: VDIGIT \ b --		Put "digit" on stack
  NEXT TUCK ! CELL+ TOV ;

: VPUSH \ a n --	Put string on stack
  REZ >x NEXT x OVER + TOV x> CMOVE ;

: BPUSH \ a n --	Put any number array on stack
  NEXT OVER ALIGNED		\ a n nxt na
  2DUP + CELL- 0!		\ a n nxt na 
  OVER + TOV			\ a n nxt
  SWAP CMOVE ;			\ 

: BDUPALL \ v -- v u	Allocate array of same size
  NEXT LEN1 + TOV ;

: BPUSHA \ a n --	Put aligned number array on stack
  NEXT 2>R 2R@ SWAP CMOVE
  2R> + TOV ;

: BDROP  CELL VP +! ;
: BDUP  TOP$ BPUSHA ;
: VDUP  TOP$ VPUSH ;
: BOVER  SEC$ BPUSHA ;
: BOOVER  THI$ BPUSHA ;

: XST!  X0 CELL - XP ! X$0 XP @ ! ; XST!	\ Xtra stack
: XP+ ( -- )  -CELL XP +! ;
: TOX ( a -- )  XP+ XP @ ! ; 
: XNEXT ( -- ad )  XP @ @ ;
: XFIRST ( -- ad )  XP @ CELL+ @ ;
: XSECOND ( -- ad )  XP @ 2 CELLS + @ ;
: XTHIRD ( -- ad )  XP @ 3 CELLS + @ ;
: XLEN ( -- n )  XNEXT XFIRST - ;
: XPUSH ( a n -- ) REZ >x XNEXT x OVER + TOX x> CMOVE ;

: BXPUSH ( a n -- ) >x XNEXT x OVER + TOX x> CMOVE ; 
: XDROP  CELL XP +! ;
: XEMPTY ( -- f )  XNEXT X$0 = ;

: >X ( v -- )  TOP$ BXPUSH BDROP ;
: X! ( v -- v )  TOP$ BXPUSH ;
: X  ( -- v )  XFIRST XNEXT OVER - BPUSHA ;
: X> ( -- v )  X XDROP ;

: Y  \ -- v	Y is the second value on X-stack
  XSECOND XFIRST OVER - BPUSHA ;

: Z  ( -- v )  XTHIRD XSECOND OVER - BPUSHA ;
: V>X ( v -- )  TOP$ BDROP XPUSH ;
: VX  ( -- v )  XFIRST XNEXT OVER - VPUSH ;
: VX> ( -- v )  VX XDROP ;

: VY  \ -- v
  XSECOND XFIRST OVER - VPUSH ;

: BNIP  >X BDROP X> ;
: BSWAP  >X >X Y X> XDROP ;
: VSWAP  V>X V>X VY VX> XDROP ;
: BROT  >X BSWAP X> BSWAP ;
: BTUCK  BSWAP BOVER ;

: REZTOP  TOP$ XPUSH BDROP X> ; 	\ Clean leading asczeros
: REBZTOP  ( TOP$ BXPUSH BDROP X> ) ; 	\ Clean leading zero bytes
: VZERO  [CHAR] 0 NEXT TUCK C! 1+ TOV ;
: BZERO 0 VDIGIT ;			\ Small BigIntegers
: BONE  1 VDIGIT ;
: BTWO  2 VDIGIT ;
: BTHREE 3 VDIGIT ;
: BTEN  0A VDIGIT ;

: BYTE1@ \ v -- v | -- b  Get least significant byte of TOS
  FIRST C@ ;

: BYTE2@ \ v -- v | -- b  Least sign byte on second number
  SECOND C@ ;

: <VTOP BEGIN LEN1 2 <
           IF EXIT THEN NEXT 1- C@ 0=	\ ????
        WHILE -1 VP @ +! 
        REPEAT ;

: <TOP BEGIN LEN1 CELL > 0=
          IF EXIT THEN NEXT CELL- @ 0=
       WHILE -CELL VP @ +! 
       REPEAT ;

: V< \ -- f	Compare numbers as asc strings
  REZTOP <VTOP VSWAP REZTOP <VTOP VSWAP
  LEN2 LEN1 2DUP <
  IF 2DROP TRUE EXIT
  THEN DUP >z > 
  IF zst drop FALSE EXIT
  THEN FIRST NEXT 1- 
  DO I z - C@ I C@ 2DUP = 0=
     IF zst drop < IF TRUE ELSE FALSE THEN UNLOOP EXIT
     THEN 2DROP -1
  +LOOP zst drop FALSE ;

: V= \ u v -- u v | -- f
  V< IF FALSE EXIT THEN
  VSWAP V< IF VSWAP FALSE EXIT THEN VSWAP TRUE ;

0400 CONSTANT 1K

: V2/  \ v -- v/2
\ Divide asc number by 2
  NEXT FIRST >x 0 >y
  BEGIN x C@ OVER x >
  WHILE A> 2/MOD y> + >A x C!
     NEGATE 5 AND >y 1 x+!
  REPEAT REZTOP <VTOP 2DROP xst drop yst drop ;

: V>BYTE \ u -- v | -- f b
\ Divide asc-string by 256 leaving rest b. f TRUE if v=0
  xst depth FALSE >y 9 1
  DO NEXT 1- C@ 1 AND >x V2/ VZERO V= BDROP
     IF TRUE y! LEAVE THEN
  LOOP xst depth SWAP - 0 TUCK
  DO 2* x> + LOOP y> SWAP ;

: NTH ( n -- b/0 )  1+ VP @ SWAP CELLS + 
  DUP V0 = IF DROP 0 ELSE @ THEN ;

: LEN# \ n -- m 
  DUP NTH SWAP 1- NTH ?DUP
  IF SWAP ELSE V$0 SWAP THEN - ;

: BDEPTH \ -- n 
  040 0					\ Depth of stack
  DO I NTH 0= IF I LEAVE THEN LOOP ;

: V>B  \ Convert asc-number to digital Bignumber
  PAD 1K 2DUP + PAD
  DO V>BYTE I C!
     IF DROP I PAD - 1+ LEAVE THEN
  LOOP BDROP BPUSH ;

: S>B \ -- v | n --	Convert single to big
  0 <# #S #> VPUSH V>B ;

: B>S  \ u -- | -- n	Conv big to single
  FIRST @ BDROP ;

: D>B \ -- v | d --
  <# #S #> VPUSH V>B ;

: B>D  \ u -- | -- d
  TOP$ 5 < IF @ 0 ELSE 2@ SWAP THEN BDROP ;

: D256*  \ ud -- 256ud
  OVER 8 LSHIFT ROT FF000000 AND 018 RSHIFT
  ROT 8 LSHIFT OR ;

: V  BL PARSE VPUSH ; 		\ 'V 12345' Put asc numb on TOS
: B  V V>B ;			\ Put BigInt on TOS 
: CL VST! XST! st! S0 @ SP! ;	\ Clear stacks

: .V  BDEPTH ?DUP	\ Print asc numb stack
  IF 0 DO I NTH I LEN# TYPE CR LOOP THEN ;

: .BYTES BDEPTH ?DUP	\ Print byte string stack
  IF 0 DO I NTH I LEN# 0
          DO I OVER + C@ BASE @ >x
             HEX 0 <# # # #> TYPE SPACE x> BASE !
          LOOP DROP CR
       LOOP
  THEN ;

: B+_S>=F \ u v -- u+v		u >= v
  LEN2 NEXT OVER LEN1 - DUP VP @ +! ERASE
  0 >x NEXT FIRST 
  DO I OVER - @ I @ 0 TUCK D+ x> 0 D+ >x I ! CELL
  +LOOP DROP x> ?DUP IF NEXT CELL VP @ +! ! THEN ;

: B+ \ u v -- u+v		Adding BigInt
  LEN2 LEN1 < 0=
  IF B+_S>=F
  ELSE BSWAP B+_S>=F 
  THEN BNIP ;

: B< \ u v -- u v | -- f	OBS! Bigstack remains
  LEN2 LEN1 2DUP U<
  IF 2DROP TRUE EXIT
  THEN DUP PAD1 ! U>
  IF FALSE EXIT
  THEN FIRST >x SECOND >y 0 >z 0 PAD1 @ CELL -
  DO y I + @ x I + @ 2DUP = 0=
     IF drop-all U< IF TRUE ELSE FALSE THEN UNLOOP EXIT
     THEN 2DROP -CELL
  +LOOP drop-all FALSE ;

: B= \ u v -- u v | -- f
  B< IF FALSE EXIT THEN
  BSWAP B< IF BSWAP FALSE EXIT THEN BSWAP TRUE ;

: B0= \ v -- v | -- f
  LEN1 CELL - FIRST @ OR 0= ;

: GTX? \ v -- v | -- f	Greater Than X-value?
  LEN1 XLEN 2DUP <
  IF 2DROP FALSE EXIT THEN >
  IF TRUE EXIT THEN FALSE 0 XLEN CELL -
  DO FIRST I + @ XFIRST I + @ 2DUP <
     IF 2DROP LEAVE THEN >
     IF 0= LEAVE THEN -CELL
  +LOOP ;

: +X>=Y? \ v -- | -- f		Add v to X and compare with Y
  X> B+ X> B< >X >X 0=
  DUP IF XDROP XDROP THEN ;	\ 2 XDROP when equal or greater

: B~ \ u v -- |u-v| | -- f
  B< IF BSWAP TRUE >z ELSE FALSE >z THEN
  FIRST >x SECOND >y FALSE >z
  LEN1 0
  DO y I + @ z + x I + @ 2DUP U< z! z
     IF 1 SWAP 0 D- DROP ELSE - THEN y I + ! CELL
  +LOOP LEN2 LEN1
  DO y I + @ DUP 0= x! x z AND
     IF DROP -1 ELSE z + 0 z! THEN y I + !
     z 0= IF LEAVE THEN CELL
  +LOOP drop-all z> BDROP <TOP ;

: B- \ u v -- u-v
  B~ IF ABORT" Negative Big!" THEN ;

: |B-| \ u v -- |u-v|
  B~ DROP ;

: BSL \ n i -- n1 n0	Big shift left, n < 2^BITS
  2DUP BITS SWAP - RSHIFT -ROT LSHIFT ;

: BLEFT  \ v -- u | n -- 	 	Big shift left
  BITS/MOD OVER 0= 
  IF NIP FIRST DUP ROT CELLS DUP >x + LEN1 CMOVE> 
     x VP @ +! FIRST x> ERASE EXIT
  THEN CELLS >y				\ i  y=4[n/32]
  y FIRST DUP >x + 			\ i F+4[n/32]  x=FIRST
  x OVER LEN1 DUP >z CMOVE>		\ i F+4[n/32]  z=LEN1
  x y ERASE
  z> OVER + DUP x! SWAP 0 >z		\ i F+4[n/32]+LEN1 F+4[n/32]
  ?DO I @ OVER BSL z> OR I ! >z CELL +LOOP
  z x ! x CELL+ VP @ ! DROP drop-all <TOP ;

: BRIGHT \ v -- u | n -- 
  8 /MOD >y >x 
  NEXT 0! NEXT FIRST
  DO y I + W@ x RSHIFT SP@ C@ I C! DROP
  LOOP NEXT y - y> ERASE
  <TOP xst drop ;

: B1AND \ v -- v s
  FIRST @ 1 AND VDIGIT ;

: BEVEN \ v -- v | -- f
  FIRST @ 1 AND 0= ;

: BODD \ v -- v | -- f
  BEVEN 0= ;

: B1OR \ v -- v'
  FIRST @ 1 OR FIRST ! ;

: B2/MOD \ v -- r k 
  B1AND BSWAP 1 BRIGHT ;

: MSB@ \ v -- v | -- c
  NEXT CELL- NEXT 1-
  DO I C@ ?DUP IF LEAVE THEN -1 +LOOP ;

: Z# \ v -- v | -- n
  0 >x NEXT
  BEGIN 1- DUP C@ 0=
  WHILE 1 x+! 
  REPEAT DROP x> ;

: BLOG~ \ v -- v | -- n		8(len-1)+log(byte0)+1
  LEN1 Z# - 1- 3 LSHIFT MSB@ LOG~ + ;

: BLOG \ v -- | -- n	\ Integer part of 2-logarithm
  BLOG~ 1- BDROP ;

: B1+ BONE B+ ;
: B1- BONE B- ;
: B2+ BTWO B+ ;

: B2* \ v -- 2v
  1 BLEFT ;

: B2/ \ v -- v/2
  1 BRIGHT ;

: B256* \ v -- 256v
  TOP$ OVER 1+ SWAP CMOVE>
  VP @ 1+! 0 FIRST C! ;

: LAST!> \ n --
  NEXT CELL- ! CELL VP @ +! ;

: CELL*X \ -- n*v ; X: v -- v | n --
  BZERO 0 >x XNEXT XFIRST 
  DO DUP I @ UM* x> 0 D+ >x LAST!> CELL +LOOP
  x> NEXT CELL- ! DROP <TOP ;

: BS* \ v -- n*v | n --
  >X CELL*X XDROP ;

: BDUPS* \ v -- v n*v | n -- 
  BZERO 0 >x FIRST SECOND 
  DO DUP I @ UM* x> 0 D+ >x LAST!> CELL +LOOP 
  x> NEXT CELL- ! DROP <TOP ; 

: 32BLEFT \ v -- w
  TOP$ OVER CELL+ SWAP CMOVE>
  CELL VP @ +! 0 FIRST ! ;

: B* \ u v -- u*v
  LEN1 LEN2 < IF BSWAP THEN
  >X BZERO SECOND FIRST CELL- 
  DO 32BLEFT I @ CELL*X B+ -CELL +LOOP
  BNIP XDROP <TOP ;

: BS/MOD \ v -- q | n -- r  		v=nq+r
  >x BDUPALL NEXT CELL- 0!
  0 0 LEN1 CELL-
  DO I SECOND + @ SWAP x UM/MOD I FIRST + ! -CELL +LOOP <TOP
  xst drop BNIP ;

\ 2^sXn+1=2*2^sXn-[2^sXn]^2*N/2^[2s]
\ K=T*X/2^[2n]
: B/MOD \ u v -- r k 				Newtons method
  B< IF BDROP BZERO EXIT THEN
  LEN1 5 < IF B>S BS/MOD S>B BSWAP EXIT THEN
  BLOG~ >x >X BDUP BLOG~ 2/ 6 + >y 
  x 1- BONE BLEFT X B= BDROP BDROP
  IF y 2* x> - BONE BLEFT
  ELSE y 2* x> - DUP >x BONE BLEFT x>
     IF 6 S>B B* 5 BS/MOD DROP THEN		\ X0
     BEGIN BDUP B2* BOVER BDUP B* X B*
        y 2* BRIGHT B- B= BNIP 
     UNTIL
  THEN B* y> 2* BRIGHT
  BEGIN BSWAP BOVER X B* B<
  WHILE BDROP BSWAP B1-
  REPEAT B- BSWAP XDROP ;

: B/ \ u v -- u/v
  B/MOD BNIP ;

: BMOD \ u v -- rest 
  B/MOD BDROP ;

: B>V$ \ b -- b | -- a n
  PAD2 PAD1 ! BDUP 0A 
  BEGIN DUP BS/MOD >A PAD1 @ C! -1 PAD1 +! B0= UNTIL 
  DROP BDROP 1 PAD1 +! PAD1 @ PAD2 OVER - 1+ ;

: B>V  \ b -- b v
  B>V$ VPUSH ;

: B. \ b -- b
  B>V$ TYPE SPACE ;

: .B  BEMPTY 0=
  IF B. CR >X RECURSE X> THEN ;

: .X  XEMPTY 0=
  IF X> B. CR RECURSE >X THEN ;

: X>. XEMPTY 0= IF X> B. BDROP RECURSE THEN ;

: B** \ B A -- B^A
  >X BZERO >X BONE
  BEGIN BOVER B*
    BONE +X>=Y?
  UNTIL BNIP ;

BASE !

[toc] | [prev] | [next] | [standalone]


#37850

Fromlehs <skydda.barnen@gmail.com>
Date2015-04-02 13:31 -0700
Message-ID<338b4e5a-defd-4de7-9a59-6bca5e10990f@googlegroups.com>
In reply to#37759
Correction to the code:

: B/MOD \ u v -- r k 				Newton
  B< IF BDROP BZERO EXIT THEN
  LEN1 5 < IF B>S BS/MOD S>B BSWAP EXIT THEN
  BLOG~ >x >X BDUP BLOG~ 2/ 6 + >y 
  y 2* x> - DUP >x BONE BLEFT x>
  IF 6 BS* 5 BS/MOD DROP THEN		\ X0 
  BEGIN BDUP B2* BOVER BDUP B* X B* 
     y 2* BRIGHT B- B= BNIP 
  UNTIL B* y> 2* BRIGHT 
  BSWAP BOVER X B* B< 0 >x 
  IF BEGIN -1 x+! X B- B< 0= UNTIL 
  ELSE B= IF BDROP BDROP XDROP BZERO BSWAP EXIT THEN 
     BEGIN 1 x+! X B+ B< UNTIL -1 x+! X B-
  THEN B- BSWAP x 0< 
  IF x> NEGATE S>B B-
  ELSE x> S>B B+
  THEN XDROP ;


Extensions:

: QWERTY \ u v -- s t		Binary split
  BOVER BOVER B+ 1 BRIGHT BDUP BTWO B** X B<
  IF BDROP BDROP BROT BDROP BSWAP
  ELSE BDROP BDROP BNIP
  THEN ; 

: BSQRTX \ u -- v | X: -- u	Newtons method  ? BS/MOD ?
  >X X BLOG~ U2/ BRIGHT B1+
  BEGIN BDUP X BOVER B/MOD BNIP B+ B1+ B2/
    BSWAP BOVER |B-| BONE B< BDROP BDROP 
  UNTIL ;

: BSQRT~ \ w -- u v		u=v-1 v=ceil
  BSQRTX BDUP B1- BSWAP B1+ QWERTY XDROP ;

: BSQRTC \ w -- v	Ceiling
  BSQRT~ BNIP ;

: BSQRTF \ w -- v	Floor
  B1+ BSQRT~ BDROP ;

: FACULTY  \ v -- v!
  >X BZERO BONE
  BEGIN BSWAP B1+ GTX? BSWAP 0=
  WHILE BOVER B*
  REPEAT BNIP XDROP ;

: BGCD \ v u -- w	greatest common divisor
  B< IF BSWAP THEN
  BEGIN BTUCK B/MOD BDROP B0=
  UNTIL BDROP ;

: GCD \ m n -- d
  2DUP U< IF SWAP THEN
  BEGIN TUCK 0 SWAP UM/MOD DROP DUP 0=
  UNTIL DROP ;

: BLCM \ v u -- w	largest common multiple
  BOVER BOVER B* BROT BROT BGCD B/MOD BNIP ;

\ The Square-and-multiply-algorithm
: B**MOD \ u v m -- u^v mod m
  >X BLOG~ BSWAP >X BONE 0		\ v 1 | X: m u | l[v] 0
  DO I BITS/MOD CELLS SECOND + @	\ v w | X: m u | r CELLi
     1 ROT LSHIFT AND			\ v w | X: m u | CELLi & 2^r
     IF X B* Y BMOD			\ v [w*u]
     THEN X X> B* X BMOD >X 		\ v [w*u] X<-[X*X]
  LOOP BNIP XDROP XDROP ;

\ Rabin-Miller Strong Pseudoprime Test

: rs \ u -- s | -- r
  B1- NEXT FIRST 0 >x
  DO I @ IF I >y LEAVE THEN 1 x+! CELL
  +LOOP y> @ BITS 0
  DO DUP 1 AND IF I LEAVE THEN U2/
  LOOP NIP x> LBITS LSHIFT +
  DUP BRIGHT ;

: xsi \ u -- v
  LEN1 BDROP CELL = IF BTWO EXIT THEN
  0100 RANDOM 3 OR VDIGIT ;

\ NEXT CELL- C@ 3 OR BDROP VDIGIT ;

: DIGIT= \ u -- u | n -- f
  LEN1 CELL > IF DROP FALSE EXIT THEN
  FIRST @ = ;

: PSEUDO1 \ xsi s m -- | -- f
  B**MOD 1 DIGIT= BDROP ;

: PSEUDO2 \ xsi s m -- | r -- f
  >X X B1- >X FALSE >y 0
  DO BOVER BOVER I BLEFT Y B**MOD
     X B= BDROP BDROP
     IF TRUE y! LEAVE THEN
  LOOP XDROP XDROP BDROP BDROP y> ;

: PSEUDOPRIME \ u -- | -- f		u odd >2
  >X X xsi X rs >z BOVER BOVER
  X PSEUDO1 ?DUP
  IF XDROP BDROP BDROP zst drop EXIT
  THEN X> z> PSEUDO2 ;

\ u is of the form u=1+s*2^r, where s is odd
\ Given any number 1 =< xsi < u
\ If xsi^s=1(mod u) or
\ if it exist j: 0 =< j < r with
\ xsi^(s*2^j)=-1(mod u)
\ then u is pseudoprime.

[toc] | [prev] | [next] | [standalone]


#37884

Fromlehs <skydda.barnen@gmail.com>
Date2015-04-03 08:20 -0700
Message-ID<a443c7b7-3173-4496-a195-dff934773f80@googlegroups.com>
In reply to#37850
Den torsdag 2 april 2015 kl. 22:31:04 UTC+2 skrev lehs:
> Correction to the code:
> 
> : B/MOD \ u v -- r k 				Newton
>   B< IF BDROP BZERO EXIT THEN
>   LEN1 5 < IF B>S BS/MOD S>B BSWAP EXIT THEN
>   BLOG~ >x >X BDUP BLOG~ 2/ 6 + >y 
>   y 2* x> - DUP >x BONE BLEFT x>
>   IF 6 BS* 5 BS/MOD DROP THEN		\ X0 
>   BEGIN BDUP B2* BOVER BDUP B* X B* 
>      y 2* BRIGHT B- B= BNIP 
>   UNTIL B* y> 2* BRIGHT 
>   BSWAP BOVER X B* B< 0 >x 
>   IF BEGIN -1 x+! X B- B< 0= UNTIL 
>   ELSE B= IF BDROP BDROP XDROP BZERO BSWAP EXIT THEN 
>      BEGIN 1 x+! X B+ B< UNTIL -1 x+! X B-
>   THEN B- BSWAP x 0< 
>   IF x> NEGATE S>B B-
>   ELSE x> S>B B+
>   THEN XDROP ;

An other correction:

: B/MOD \ u v -- r k 				Newton
  B< IF BDROP BZERO EXIT THEN
  LEN1 5 < IF B>S BS/MOD S>B BSWAP EXIT THEN
  BLOG~ >x >X BDUP BLOG~ 2/ 6 + >y 
  y 2* x> - DUP >x BONE BLEFT x>
  IF 6 BS* 5 BS/MOD DROP THEN		\ X0 
  BEGIN BDUP B2* BOVER BDUP B* X B* 
     y 2* BRIGHT B- B2+ B< 0= BNIP 
  UNTIL B* y> 2* BRIGHT 
  BSWAP BOVER X B* B< 0 >x 
  IF BEGIN -1 x+! X B- B< 0= UNTIL 
  ELSE B= IF BDROP BDROP XDROP BZERO BSWAP EXIT THEN 
     BEGIN 1 x+! X B+ B< UNTIL -1 x+! X B-
  THEN B- BSWAP x 0< 
  IF x> NEGATE S>B B-
  ELSE x> S>B B+
  THEN XDROP ;

[toc] | [prev] | [next] | [standalone]


#37939

Fromlehs <skydda.barnen@gmail.com>
Date2015-04-04 11:55 -0700
Message-ID<85795feb-a88f-4205-9deb-555857350ff0@googlegroups.com>
In reply to#37884
Den fredag 3 april 2015 kl. 17:20:55 UTC+2 skrev lehs:
> Den torsdag 2 april 2015 kl. 22:31:04 UTC+2 skrev lehs:
> > Correction to the code:
> > 
> > : B/MOD \ u v -- r k 				Newton
> >   B< IF BDROP BZERO EXIT THEN
> >   LEN1 5 < IF B>S BS/MOD S>B BSWAP EXIT THEN
> >   BLOG~ >x >X BDUP BLOG~ 2/ 6 + >y 
> >   y 2* x> - DUP >x BONE BLEFT x>
> >   IF 6 BS* 5 BS/MOD DROP THEN		\ X0 
> >   BEGIN BDUP B2* BOVER BDUP B* X B* 
> >      y 2* BRIGHT B- B= BNIP 
> >   UNTIL B* y> 2* BRIGHT 
> >   BSWAP BOVER X B* B< 0 >x 
> >   IF BEGIN -1 x+! X B- B< 0= UNTIL 
> >   ELSE B= IF BDROP BDROP XDROP BZERO BSWAP EXIT THEN 
> >      BEGIN 1 x+! X B+ B< UNTIL -1 x+! X B-
> >   THEN B- BSWAP x 0< 
> >   IF x> NEGATE S>B B-
> >   ELSE x> S>B B+
> >   THEN XDROP ;
> 
> An other correction:
> 
> : B/MOD \ u v -- r k 				Newton
>   B< IF BDROP BZERO EXIT THEN
>   LEN1 5 < IF B>S BS/MOD S>B BSWAP EXIT THEN
>   BLOG~ >x >X BDUP BLOG~ 2/ 6 + >y 
>   y 2* x> - DUP >x BONE BLEFT x>
>   IF 6 BS* 5 BS/MOD DROP THEN		\ X0 
>   BEGIN BDUP B2* BOVER BDUP B* X B* 
>      y 2* BRIGHT B- B2+ B< 0= BNIP 
>   UNTIL B* y> 2* BRIGHT 
>   BSWAP BOVER X B* B< 0 >x 
>   IF BEGIN -1 x+! X B- B< 0= UNTIL 
>   ELSE B= IF BDROP BDROP XDROP BZERO BSWAP EXIT THEN 
>      BEGIN 1 x+! X B+ B< UNTIL -1 x+! X B-
>   THEN B- BSWAP x 0< 
>   IF x> NEGATE S>B B-
>   ELSE x> S>B B+
>   THEN XDROP ;

Yet another correction:
HEX
\ 2^sXn+1=2*2^sXn-[2^sXn]^2*N/2^[2s]
\ K=T*X/2^[2n]
: B/MOD \ u v -- r k 				Newton
  B< IF BDROP BZERO EXIT THEN
  LEN1 5 < IF B>S BS/MOD S>B BSWAP EXIT THEN
  BLOG~ >x >X BDUP BLOG~ 2/ 6 + >y 
  y 2* x> - DUP >x BONE BLEFT x>
  IF 6 BS* 5 BS/MOD DROP THEN 0F 0	\ X0 
  DO BDUP B2* BOVER BDUP B* X B* 
     y 2* BRIGHT B- B= BNIP 
     IF LEAVE THEN
  LOOP B* y> 2* BRIGHT 
  BSWAP BOVER X B* B< 0 >x 
  IF BEGIN -1 x+! X B- B< 0= UNTIL 
  ELSE B= IF BDROP BDROP XDROP BZERO BSWAP EXIT THEN 
     BEGIN 1 x+! X B+ B< UNTIL -1 x+! X B-
  THEN B- BSWAP x 0< 
  IF x> NEGATE S>B B-
  ELSE x> S>B B+
  THEN XDROP ;

And this should work.

As I wrote in the first post, division is 15 times faster and multiplication 30 times faster than in the previous version. But in spite of that overall test routines are only 3-4 times faster.

I conclude that this speak against dynamic number stacks in ordinary Forth style, since the handling of data seems to be too inefficient.

For many cases, however, this rather complete system works alright. And it might be a good idea to complete with words BA+, BA-, BA* and BA/ that acts on addressed data:

BA+ ( ad ad1 ad2 -- ) where the sum of the numbers on ad1 and ad2 is added and the result is stored (and created) on ad.

[toc] | [prev] | [next] | [standalone]


#37760

Fromlehs <skydda.barnen@gmail.com>
Date2015-03-30 08:48 -0700
Message-ID<d8440293-24c7-4f13-b8d5-f62723a49f99@googlegroups.com>
In reply to#37758
B 615468 B 562 B** .B

gives the immediate answer

34156897124587202875461168478640631055129182740553613468699956769770136652402154739561340584508498973366519515585645184012210663189569524993756637122720573871282203083876167211335325757034908305251583581503283057054164514891038711046942805281336315259986027965737023755000239303060634127444386271850668329672802315033260045401378791779000065183900238550732074972957722950083234788108491449937931592291253635866519053785318858069256376478767934440191251292087916571534906339964031001281451004726056973294975462770873666371988354852339358537618091193539797488676750691860773826475301426478295055703150442969535168163655032293657841824406401431830927169315150348828722299539042036413305630965380951884581107661989160455665699281893350838006025496566483899829815431631895540935541284047068731747514576745471479767995725544105707248840077534211785181151416567236290494335365974908129074826938269040517681215395867923964791466253195318338271114213403107755292562551616146538655466874365668199865756415283366133930896435008623220671271368581822490063883478410359880640855368349346282386326077268652198588466780470215448170142390188681657272512980029410455596811431959579645169048529877692912491842420747097037071670483064678575047262881670027226263156951550521696037648753788908422083018059914219596166714613482712096098467556070917653114924953415480393552876527016847994263772726666288588758561433272386471965786633523722543769722707877238136890442126176176976986375467115446020286653972052103256001427611808635208396601168427415667902313515517075657231002341066426901505339860320367857277045152190080615544853797927526552392620130296548454183955148951499831136117615003530962157676170396930682584031546852589250236924826050833244786697895931613110492704987041541084339357119169809333644342991441886009002645987526543559165157271851282013499597884853366546412997389877341332547659883155657732652068376148916490488090937868225289608250800186316313861344028554049326855971417769787880564024158408539533346625022408057433373020552806655620753192409099117219435562587934027391157139433086928111670915741327598559766208874812450563254266175667771747699670428681071198593747658820178097289627016747936603908006719642865049935520867771861893275902640848083264725100951204915238051224431145092596843100050992569548896390821912029350652232909578918542077683045544761408520503614763004422570470565218334521535854757728330926758002086370841763564809878033487849454632072235158054359054655431238997705538527640179160276591868948668226797608189746766360450350151858300893719437799028839824827265447333335979227099831275953335339850070594026461619839256465962942163411822106803707040231557097898697754380473776316734238178631234179730021091008264927219804760266843400697042229409293520434417849201250933728490002791993321017886313518828809548291847626974288764393932709804024106644839823497077150673550475324127254940733368549900779487821100491295386589418232357238819702095886104283014737288302592745783425284942543455913878477294692602841107008704264899914129557410140160865166441660661258160056486358140196984865179592488736863315282676912308048653526027446393144465357755725373432199301345708009918300237101532022049488875359404973838071241372631149855137455910655361024
 Ok

[toc] | [prev] | [next] | [standalone]


#37972

Fromlehs <skydda.barnen@gmail.com>
Date2015-04-05 11:57 -0700
Message-ID<0ed20208-a79c-4646-9a5d-0309ade9a13c@googlegroups.com>
In reply to#37758
This is what I got so far, with some prime number routines and (slow) factorization routines for testing the arithmetic routines.

https://www.dropbox.com/s/t2riahdq9bxhoqs/bigforth%20ANS.txt?dl=0

[toc] | [prev] | [next] | [standalone]


#38071

Fromlehs <skydda.barnen@gmail.com>
Date2015-04-09 14:05 -0700
Message-ID<1124982d-301f-4c96-8220-6aaaceaac564@googlegroups.com>
In reply to#37758
Now big integers works for SP-Forth and SwiftForth.

https://www.dropbox.com/s/t2riahdq9bxhoqs/bigforth%20ANS.txt?dl=0

I will continue by testing on other free ANS Forth systems (links for download would be great) and by extending the comments.

[toc] | [prev] | [next] | [standalone]


#38072

From"Alex McDonald" <blog@rivadpm.com>
Date2015-04-09 15:06 -0700
Message-ID<mg6t3n$e55$1@dont-email.me>
In reply to#38071
on 09/04/2015 14:04:59, lehs wrote:
> Now big integers works for SP-Forth and SwiftForth.
> 
> https://www.dropbox.com/s/t2riahdq9bxhoqs/bigforth%20ANS.txt?dl=0
> 
> I will continue by testing on other free ANS Forth systems (links for
> download would be great) and by extending the comments.
> 

You might find this useful; it's an SP@-less version of CASE.

: case  ( -- 0 )  
      0 ; immediate \ initial count of ofs

: of    ( c: #of -- orig #of+1 )
        ( x -- )
      1+ >r
      postpone over
      postpone =
      [compile] if
      postpone drop
      r> ; immediate

: endof ( c: orig1 #of -- orig2 #of )
      >r [compile] else r> ; immediate

: endcase ( c: orig1..orign #of -- )
      postpone drop                     \ discard case value
      0 ?do [compile] then loop ; immediate  \ generate targets

Win32forth can be obtained from
http://sourceforge.net/projects/win32forth/files/latest/download?source=files

[toc] | [prev] | [next] | [standalone]


#38075

Fromlehs <skydda.barnen@gmail.com>
Date2015-04-10 00:25 -0700
Message-ID<b4b393d6-f553-4797-bacc-89643862826f@googlegroups.com>
In reply to#38072
Den fredag 10 april 2015 kl. 00:06:41 UTC+2 skrev Alex McDonald:
> on 09/04/2015 14:04:59, lehs wrote:
> > Now big integers works for SP-Forth and SwiftForth.
> > 
> > https://www.dropbox.com/s/t2riahdq9bxhoqs/bigforth%20ANS.txt?dl=0
> > 
> > I will continue by testing on other free ANS Forth systems (links for
> > download would be great) and by extending the comments.
> > 
> 
> You might find this useful; it's an SP@-less version of CASE.
> 
> : case  ( -- 0 )  
>       0 ; immediate \ initial count of ofs
> 
> : of    ( c: #of -- orig #of+1 )
>         ( x -- )
>       1+ >r
>       postpone over
>       postpone =
>       [compile] if
>       postpone drop
>       r> ; immediate
> 
> : endof ( c: orig1 #of -- orig2 #of )
>       >r [compile] else r> ; immediate
> 
> : endcase ( c: orig1..orign #of -- )
>       postpone drop                     \ discard case value
>       0 ?do [compile] then loop ; immediate  \ generate targets
> 
> Win32forth can be obtained from
> http://sourceforge.net/projects/win32forth/files/latest/download?source=files

Thank you!

That CASE worked fine and I changed in the file

https://www.dropbox.com/s/t2riahdq9bxhoqs/bigforth%20ANS.txt?dl=0

Unfortunately Win32forth seems to be non case sensitive and perhaps I should change the system to non sensitive...

[toc] | [prev] | [next] | [standalone]


#38077

Fromlehs <skydda.barnen@gmail.com>
Date2015-04-10 04:21 -0700
Message-ID<7e178054-6927-4f40-a46e-45c25d3de01e@googlegroups.com>
In reply to#37758
INTRODUCTION TO BIG INTEGERS

The prefixes B and BR stands for big (unsigned) integer, where the R stands for remain, that is, the bignum values on the stack remains, eventually under return values on the topp of the stack. There is an analogy with floats and the prefix F and as for floats there is a special stack, independent of the parameter stack.

The B-stack is really two stacks:

one for references in highmem of an allocated buffer, starting at V$0 and ending at V0 - 1, growing towards lowmem;


one for cell-strings in lowmem, blocks of cells representing the bignum growing towards highmem, representing a bignum with lower significant cells in lower memory.

Stack manipulations as BDUP and BOVER creates new references to new copies of cell strings. It would be faster to only manipulate references, but when changing the bignumber i.e. with B1+ it would be necesary any way to first make a new copy and then change the reference. Instead, here all copies are created automatic in the first place. A simple solution with no need for garbage collection.

The buffers of the stacks and the pads are the only limits:

B 2000 BFACULTY CR B.

instantly fills the screen with about 5700 decimal digits, but that's just about the limit with the original settings.

One problem with this is how to implement variables. Because of this and the cell block moving stack manipulations there is an extra X-stack. The top values on the X-stack can be called by X, Y and Z. This stack simplifies bignum calculations and could be used as a substitute for variables. Constants are more easy to implement. The only words for defining constants so far are

BSCONSTANT and BDCONSTANT that are created with single vs double numbers and when called put corresponding numbers on the B-stack.


THE MOST IMPORTANT DEFINITIONS:

BEMPTY 			\ -- f
BSCONSTANT 		\ n --
BDCONSTANT 		\ ud --
BDROP 			\ u --
BDUP  			\ u -- u u
BOVER 			\ u v -- u v u
BOOVER 			\ u v w -- u v w u
XEMPTY 			\ -- f
>X			\ u --
X!			\ u --
X			\ -- u
X>			\ -- u
Y			\ -- u
Z			\ -- u
BNIP 			\ u v -- v
BSWAP 			\ u v -- v u
BROT 			\ u v w -- v w u
BTUCK 			\ u v -- v u v
BZERO 			\ -- 0
BONE 			\ -- 1
BTWO 			\ -- 2
BTHREE 			\ -- 3
BTEN 			\ -- 10
BDEPTH			\ -- f
S>B			\ -- u | n --
B>S			\ u -- | -- n
D>B			\ -- u | ud --
B>D			\ u -- | -- ud
D256*			\ ud -- 256ud
B			\ example: B 123456789 puts the number on tos, does not work in definitions
B+			\ u v -- u+v
BR<			\ u v -- u v | -- f
B<			\ u v -- | -- f
BR=			\ u v -- u v | -- f
B=			\ u v -- | -- f
BR0=			\ u -- u | -- f
B0=			\ u -- | -- f
B-			\ u v -- u-v
BLEFT			\ u -- v | n --		shift left
BRIGHT			\ u -- v | n --		shift right
B1+
B2+
B1-
B2*
B2/
B256*
BS*			\ u -- u*n | n --
B*			\ u v -- u*v
BS/MOD			\ u -- v | n -- r  	u=nv+r 
B.			\ u -- 			print tos
BR.			\ u -- u		print tos
.B			\ 			print B-stack
.X			\			print X-stack
B/MOD			\ u v -- r q
B/ 			\ u v -- u/v
BMOD 			\ u v -- rest
B** 			\ u v -- u^v
BSQRTF 			\ u -- v		floor
BSQRTC 			\ u -- v		ceiling
BFACULTY  		\ u -- u!
BGCD 			\ u v -- w		greatest common devisor
BLCM 			\ u v -- w		
BLCM 			\ u v -- w		least common multiple
B**MOD 			\ u v m -- [u^v mod m]
BPRIME 			\ u -- | -- f
BNEXT_PRIME 		\ m -- p 		possibly m=p

Readme: https://www.dropbox.com/s/7m63z95l8okvzip/Read%20me.txt?dl=0

[toc] | [prev] | [next] | [standalone]


#38100

Fromlehs <skydda.barnen@gmail.com>
Date2015-04-10 12:47 -0700
Message-ID<7bab6a57-b5ce-459a-8239-a707f88cdb2f@googlegroups.com>
In reply to#37758
Here is a case insensitive version 

https://www.dropbox.com/s/nwzpluiehlhba4t/bigforth%20ANS%20Forth%20ins.txt?dl=0

and so far I have been testing it on SP-Forth, SwiftForth and Win32Forth.

I guess that this version is close to run on all standard ANS Forth systems.

[toc] | [prev] | [next] | [standalone]


#38101

FromPaul Rubin <no.email@nospam.invalid>
Date2015-04-10 12:58 -0700
Message-ID<87h9sndaco.fsf@jester.gateway.sonic.net>
In reply to#38100
lehs <skydda.barnen@gmail.com> writes:
> I guess that this version is close to run on all standard ANS Forth systems.

Gforth complains that U2/ and CELL- are undefined words.  Uncommenting
the code at the end then crashes immediately with an invalid memory
address error.

[toc] | [prev] | [next] | [standalone]


#38103

Fromlehs <skydda.barnen@gmail.com>
Date2015-04-10 13:03 -0700
Message-ID<ce187d01-8177-44b0-83d2-6ad381adb349@googlegroups.com>
In reply to#38101
Den fredag 10 april 2015 kl. 21:58:16 UTC+2 skrev Paul Rubin:
> lehs <skydda.barnen@gmail.com> writes:
> > I guess that this version is close to run on all standard ANS Forth systems.
> 
> Gforth complains that U2/ and CELL- are undefined words.  Uncommenting
> the code at the end then crashes immediately with an invalid memory
> address error.

Is there a trial version of Gforth? Have you tried to redefine U2/ and CELL- ?

[toc] | [prev] | [next] | [standalone]


#38105

FromPaul Rubin <no.email@nospam.invalid>
Date2015-04-10 13:18 -0700
Message-ID<874mond9ff.fsf@jester.gateway.sonic.net>
In reply to#38103
lehs <skydda.barnen@gmail.com> writes:
> Is there a trial version of Gforth? Have you tried to redefine U2/ and
> CELL- ?

Yes, I did

    : U2/ 1 rshift ;
    : CELL- cell - ;

in order to run that test.

Gforth is free: https://savannah.gnu.org/projects/gforth/

[toc] | [prev] | [next] | [standalone]


#38106

Fromlehs <skydda.barnen@gmail.com>
Date2015-04-10 13:24 -0700
Message-ID<b5312913-0e0d-462e-8642-04f4c15d600b@googlegroups.com>
In reply to#38105
Den fredag 10 april 2015 kl. 22:18:14 UTC+2 skrev Paul Rubin:
> lehs <skydda.barnen@gmail.com> writes:
> > Is there a trial version of Gforth? Have you tried to redefine U2/ and
> > CELL- ?
> 
> Yes, I did
> 
>     : U2/ 1 rshift ;
>     : CELL- cell - ;
> 
> in order to run that test.
> 
> Gforth is free: https://savannah.gnu.org/projects/gforth/

Yes, I saw it was GNUs Forth. 

I loaded gforth-0.7.0.exe on http://www.complang.tuwien.ac.at/forth/gforth/ and it runs for me.

[toc] | [prev] | [next] | [standalone]


#38116

Fromlehs <skydda.barnen@gmail.com>
Date2015-04-11 01:30 -0700
Message-ID<8cecba55-c9a0-4843-a33c-a1fa81d87446@googlegroups.com>
In reply to#37758
Now it seems to work for 32-bit ANS Forth with Windows:

Readme: https://www.dropbox.com/s/yrhthx1twbrsggv/Read%20me%20ins.txt?dl=0
Code32: https://www.dropbox.com/s/nwzpluiehlhba4t/bigforth%20ANS%20Forth%20ins.txt?dl=0

I have realized that some routines (like BRIGHT) don't work on 64-bit and even if I would go through the code and correct, it's difficult for me to decide when it will run when I can't test and try.

A number is stored with lowest significant CELLs in low mem and @ fetch the cells in a good manner. But if @ in a 64-bit system corresponds to 2@ in 32-bit systems then that doesn't work, because of the changed order: the highest 32-cell is stored before the lowest.

So I guess I have to buy a 64-bit computer and install Linux to go on further with portability.

[toc] | [prev] | [next] | [standalone]


#38117

Fromlehs <skydda.barnen@gmail.com>
Date2015-04-11 01:32 -0700
Message-ID<45d08557-4b08-47cf-a181-7ed67e92e7bb@googlegroups.com>
In reply to#38116
Now it seems to work for 32-bit ANS Forth with Windows: 

Readme: https://www.dropbox.com/s/yrhthx1twbrsggv/Read%20me%20ins.txt?dl=0 

Code32: https://www.dropbox.com/s/nwzpluiehlhba4t/bigforth%20ANS%20Forth%20ins.txt?dl=0 


I have realized that some routines (like BRIGHT) don't work on 64-bit and even if I would go through the code and correct, it's difficult for me to decide when it will run when I can't test and try. 

A number is stored with lowest significant CELLs in low mem and @ fetch the cells in a good manner. But if @ in a 64-bit system corresponds to 2@ in 32-bit systems then that doesn't work, because of the changed order: the highest 32-cell is stored before the lowest. 

So I guess I have to buy a 64-bit computer and install Linux to go on further with portability.

[toc] | [prev] | [next] | [standalone]


#38127

FromPaul Rubin <no.email@nospam.invalid>
Date2015-04-11 09:41 -0700
Message-ID<873846boru.fsf@jester.gateway.sonic.net>
In reply to#38116
lehs <skydda.barnen@gmail.com> writes:
> So I guess I have to buy a 64-bit computer and install Linux to go on
> further with portability.

You could get a virtual server with 64-bit Linux or even Windows, and
use that.

[toc] | [prev] | [next] | [standalone]


#38118

Fromlehs <skydda.barnen@gmail.com>
Date2015-04-11 01:34 -0700
Message-ID<98e0c284-aab0-4a6b-8819-947444278d31@googlegroups.com>
In reply to#37758
Den måndag 30 mars 2015 kl. 17:18:43 UTC+2 skrev lehs:
> Some month ago I wrote about big integers in Forth. I had made words for big integer arithmetic on a big integer stack, but I realized it was too slow. 
> 
> I have reorganized the extension and now plus and minus are about 10 times faster, division 15 times faster and multiplication about 30 times faster.
> 
> From the beginning my ambition was to learn Forth arithmetic on number strings ("0"=ASC 48 etc) but thought it was slow. Then I made a minor change while using digital bytes as a base in byte-strings. But only got about 2 times faster operations that way. I wasn't really inspired to optimize the arithmetic routines because stored that way, with most significant bytes i low mem, is awkward (at least) in Forth. So what I did now was to reverse the order of the data and using cells as base. That is, in the first version with ascii-strings I used 10 as a base, in the second version the base 2^8 - and now the optimal(?) base 2^32 or 2^64 depending on the computer.
> 
> I would be very pleased if you tested the words on your systems and told me if it is possible to load and run. It should be possible to make it run on any ANS Forth 94.
> 
> The system is very simple to handle:
> 
> B 31803578137508173058713085713753154260872480672408768467489
> 
> put the number on tos on the B-stack where all arithmetic is made.
> 
> B+, B-, B* and B/MOD is what I got so far and works as it should in Forth.
> 
> B<, B= and B0= ***DOES NOT CHANGE THE B-STACK*** and put flags on the parameter stack.
> 
> BDUP, BDROP,... are totally analogous 
> 
> There is an extra stack X in some analogy with the R-stack:
> 
> >X, X, Y, Z, X>, XDROP, XDUP, XOVER and so far. That because of the need to store intermediate results, since there is no big variables.

Now it seems to work for 32-bit ANS Forth with Windows: 

Readme: https://www.dropbox.com/s/yrhthx1twbrsggv/Read%20me%20ins.txt?dl=0 

Code32: https://www.dropbox.com/s/nwzpluiehlhba4t/bigforth%20ANS%20Forth%20ins.txt?dl=0 

I have realized that some routines (like BRIGHT) don't work on 64-bit and even if I would go through the code and correct, it's difficult for me to decide when it will run when I can't test and try. 

A number is stored with lowest significant CELLs in low mem and @ fetch the cells in a good manner. But if @ in a 64-bit system corresponds to 2@ in 32-bit systems then that doesn't work, because of the changed order: the highest 32-cell is stored before the lowest. 

So I guess I have to buy a 64-bit computer and install Linux to go on further with portability.

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.forth


csiph-web