home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Programming / ace_gpl_release / src / lib / asm / ffp.s < prev    next >
Encoding:
Text File  |  1998-10-04  |  4.1 KB  |  197 lines

  1. ;
  2. ; ffp.s -- an ACE linked library module: fast-floating-point functions.
  3. ; Copyright (C) 1998 David Benn
  4. ; This program is free software; you can redistribute it and/or
  5. ; modify it under the terms of the GNU General Public License
  6. ; as published by the Free Software Foundation; either version 2
  7. ; of the License, or (at your option) any later version.
  8. ;
  9. ; This program is distributed in the hope that it will be useful,
  10. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. ; GNU General Public License for more details.
  13. ;
  14. ; You should have received a copy of the GNU General Public License
  15. ; along with this program; if not, write to the Free Software
  16. ; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17. ;
  18. ; Author: David J Benn
  19. ;   Date: 3rd-30th November, 1st-13th December 1991,
  20. ;      20th, 23rd,25th-27th January 1992, 
  21. ;         2nd,4th,6th,12th-19th,21st-24th,29th February 1992,
  22. ;      1st,14th March 1992,
  23. ;      4th,7th,21st,22nd,26th April 1992,
  24. ;      2nd,3rd,5th,7th,8th,10th-17th May 1992,
  25. ;      6th,8th,11th,12th,28th,30th June 1992,
  26. ;      1st-3rd,13th,14th,18th-20th,22nd July 1992,
  27. ;      9th August 1992,
  28. ;      5th December 1992,
  29. ;      14th,18th,19th February 1993,
  30. ;      1st March 1993,
  31. ;      11th October 1993
  32. ;
  33. ; registers d0-d6 and a0-a3 are modified by some of the following. BEWARE!
  34. ;
  35. ; a4,a5 are used by link/unlk.
  36. ; a6 is library base holder.
  37. ; a7 is stack pointer. 
  38. ; d7 is used for array index calculations.
  39. ;
  40.  
  41. ; * CONSTANTS *
  42. MAXSTRINGSIZE    EQU    1024
  43.  
  44.        ; ffp functions 
  45.        xdef      _round
  46.        xdef      _absf
  47.       xdef      _sgnf
  48.     xdef    _inputsingle
  49.     xdef    _modffp
  50.     xdef    _fix
  51.     xdef    _decimal_places
  52.  
  53.        ; external references
  54.     xref    _val
  55.        xref      _putchar
  56.     xref    _strsingle
  57.     xref    _Ustringinput
  58.  
  59.       xref      _LVOSPFix
  60.     xref    _LVOSPFlt
  61.     xref    _LVOSPFloor
  62.     xref    _LVOSPCeil
  63.     xref    _LVOSPAdd
  64.     xref    _LVOSPSub
  65.     xref    _LVOSPMul
  66.        xref      _LVOSPDiv
  67.        xref      _LVOSPAbs
  68.        xref      _LVOSPTst
  69.     xref    _LVOSPCmp
  70.        xref      _MathBase
  71.  
  72.     SECTION ffp_code,CODE
  73.  
  74. ;*** FAST-FLOATING-POINT FUNCTIONS ***
  75.  
  76. ;
  77. ; Rounds the single-precision value in d0. Returns a long integer in d0.
  78. ;
  79.  
  80. _round:
  81.     move.l    d0,_fnum    ; save number 
  82.  
  83.     move.l    d0,d1
  84.     jsr    _sgnf
  85.     move.l    d0,_sign    ; get the sign of fnum
  86.  
  87.     move.l    _fnum,d0
  88.     jsr    _absf        
  89.     move.l    d0,_fnum    ; fnum = ABS(fnum)    
  90.  
  91.     move.l    _MathBase,a6
  92.  
  93.     move.l    _fnum,d0
  94.     jsr    _LVOSPFix(a6)
  95.     jsr    _LVOSPFlt(a6)    ; take integer portion of fnum
  96.  
  97.     move.l    d0,d1        ; d1 = INT(fnum)
  98.     move.l    _fnum,d0
  99.     jsr    _LVOSPSub(a6)    ; d0 = fnum [d0] - INT(fnum) [d1]
  100.  
  101.     move.l    #$80000040,d1    ; 0.5
  102.     jsr    _LVOSPCmp(a6)    ; (fnum - INT(fnum)) [d0] >= 0.5 [d1] ?
  103.  
  104.     bge.s    _round_up
  105.  
  106.     ; round down
  107.     move.l    _fnum,d0
  108.     jsr    _LVOSPFloor(a6)
  109.     jsr    _LVOSPFix(a6)
  110.     bra.s    _check_sign
  111.  
  112. _round_up:
  113.     move.l    _fnum,d0
  114.     jsr    _LVOSPCeil(a6)
  115.     jsr    _LVOSPFix(a6)
  116.         
  117. _check_sign:
  118.     cmpi.l    #-1,_sign
  119.     bne.s    _exitround
  120.  
  121.     neg.l    d0        ; negate value if sign = -1
  122.  
  123. _exitround:        
  124.        rts
  125.  
  126. ;
  127. ; ffp abs function. assumes ffp value in d0.
  128. ;  
  129. _absf:
  130.        move.l  _MathBase,a6  
  131.        jsr    _LVOSPAbs(a6)
  132.        rts
  133.  
  134. ;
  135. ; ffp sgn function. d1=fnum. d0=result (-1,0,1).
  136. ;
  137. _sgnf:
  138.     move.l    _MathBase,a6
  139.     jsr    _LVOSPTst(a6)
  140.     rts
  141.  
  142. ;
  143. ; input a single-precision FFP value and return it in d0.
  144. ; (a1 = input buffer)
  145. ;
  146. _inputsingle:
  147.     move.l    a1,-(sp)
  148.     jsr    _Ustringinput
  149.     jsr    _val
  150.     addq    #4,sp
  151.     rts
  152.  
  153. ;
  154. ; ffp modulo. d0=ffp dividend, d1=ffp divisor. returns d0 mod d1 in d0. 
  155. ;
  156. _modffp:
  157.      move.l d0,_ffpdividend
  158.      move.l d1,_ffpdivisor
  159.  
  160.      movea.l _MathBase,a6
  161.  
  162.      jsr _LVOSPDiv(a6)     ; quotient=dividend/divisor
  163.      jsr _LVOSPFix(a6)     ; quotient=(long)quotient
  164.      jsr _LVOSPFlt(a6)     ; quotient=(single)quotient
  165.      move.l _ffpdivisor,d1
  166.      jsr _LVOSPMul(a6)     ; d0=quotient*divisor
  167.      move.l d0,d1  
  168.      move.l _ffpdividend,d0
  169.      jsr _LVOSPSub(a6)     ; d0=dividend-(quotient*divisor)
  170.  
  171.      rts 
  172.  
  173. ;
  174. ; FIX n    - Change the number of places to round FFP values to. d0=n.
  175. ;    - This value is used by arnd() and may be +ve or -ve (!). 
  176. ;
  177. _fix:
  178.     move.l    d0,_decimal_places    
  179.     rts
  180.  
  181. ;************************
  182.  
  183.     SECTION ffp_data,DATA
  184.  
  185. _decimal_places:    dc.l 8
  186.     
  187.     SECTION ffp_mem,BSS
  188.  
  189. _ffpdividend:        ds.l 1
  190. _ffpdivisor:        ds.l 1
  191. _fnum:            ds.l 1
  192. _sign:            ds.l 1
  193. _tmpstring:            ds.b MAXSTRINGSIZE
  194.  
  195.     END
  196.