home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / bbs / ff810.lha / FF810 / Amiga_E / Sources / Projects / Pi.e < prev    next >
Text File  |  1993-01-24  |  2KB  |  83 lines

  1. /* Another pi-calc program.
  2.    A good example of what optimizing using inline assembly can do:
  3.    The E source is a translation of the original C source,
  4.    which did 48 seconds on 250 decimals, the E version did 30 seconds.
  5.    Then, the innermost loop was translated to inline assembly,
  6.    this version (E+Asm) timed only 10 seconds (all on 7mhz). 
  7.    Only a small part needed to be translated to assembly,
  8.    as that is where 99% of the calculation is performed   */
  9.  
  10. DEF m,k,n,p,i,max,nr,handle,out,num[50]:STRING,a:PTR TO LONG
  11.  
  12. PROC main()
  13.   WriteF('PI calc\n#of decimals (try 50-250): ')
  14.   ReadStr(stdout,num)
  15.   IF (nr:=Val(num,NIL))=0 THEN stop('Illegal #!\n')
  16.   WriteF('Busy ... press CtrlC to abort.\n\n')
  17.   max:=nr*16
  18.   IF (a:=New(max))=NIL THEN stop('No mem!\n')
  19.   m:=nr
  20.   k:=m|*3.321-1|
  21.   WriteF('\d\c',k,13)
  22.   FOR n:=k TO 1 STEP -1
  23.     a[0]:=a[0]+2
  24.     p:=n*2+1
  25.     MOVEQ   #0,D7    /* D7=c */
  26.     MOVE.L  a,A0    /* A0=a array */
  27.     MOVE.L  m,D4    /* D4=i counter */
  28.     MOVE.L  p,D2
  29.     MOVE.L  n,D3
  30. l:  MOVE.L  D7,D0    /* this loop is hyper-optimized. */
  31.     LSL.L   #3,D0
  32.     ADD.L   D7,D0    /* the following is the original E equivalent */
  33.     ADD.L   D7,D0
  34.     MOVE.L  (A0),D1    /* c:=0               */
  35.     MULU    D3,D1    /* x:=a               */
  36.     ADD.L   D1,D0    /* FOR i:=0 TO m      */
  37.     DIVU    D2,D0    /*   c:=10*c+(n*^x)   */
  38.     MOVE.L  D0,D7    /*   ^x:=c/p          */
  39.     EXT.L   D0        /*   c:=c-(^x*p)      */
  40.     MOVE.L  D0,(A0)    /*   x:=x+4           */
  41.     SWAP    D7        /* ENDFOR             */
  42.     EXT.L   D7
  43.     ADDQ.L  #4,A0
  44.     DBRA    D4,l
  45.     IF (n AND $F)=0     /* not every loop */
  46.       WriteF('\d     \c',n,13)
  47.       IF CtrlC()
  48.         WriteF('\n*** Calculation interrupted!\n')
  49.         CleanUp(5)
  50.       ENDIF
  51.     ENDIF
  52.   ENDFOR
  53.   FOR i:=m TO 1 STEP -1
  54.     IF a[i]>9
  55.       a[i]:=a[i]-10
  56.       a[i-1]:=a[i-1]+1
  57.     ENDIF
  58.   ENDFOR
  59.   handle:=Open('ram:pi.txt',1006)
  60.   IF handle<>NIL
  61.     out:=SetStdOut(handle)
  62.     writenum()
  63.     SetStdOut(out)
  64.     Close(handle)
  65.     WriteF('\n\nSee ram:pi.txt for output.\n')
  66.   ELSE
  67.     WriteF('Could not open file!\n')
  68.   ENDIF
  69.   WriteF('\n')
  70.   writenum()
  71. ENDPROC
  72.  
  73. PROC stop(messy)
  74.   WriteF(messy)
  75.   CleanUp(5)
  76. ENDPROC
  77.  
  78. PROC writenum()
  79.   WriteF('pi=3.')
  80.   FOR i:=1 TO m DO WriteF('\d',a[i])
  81.   WriteF('\n')
  82. ENDPROC
  83.