home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / fractint / fras1611.zip / FPU387.ASM < prev    next >
Assembly Source File  |  1991-03-06  |  10KB  |  485 lines

  1. TITLE fpu387.asm (C) 1989, Mark C. Peterson, CompuServe [70441,3353]
  2. SUBTTL All rights reserved.
  3. ;
  4. ;  Code may be used in any program provided the author is credited
  5. ;    either during program execution or in the documentation.  Source
  6. ;    code may be distributed only in combination with public domain or
  7. ;    shareware source code.  Source code may be modified provided the
  8. ;    copyright notice and this message is left unchanged and all
  9. ;    modifications are clearly documented.
  10. ;
  11. ;    I would appreciate a copy of any work which incorporates this code,
  12. ;    however this is optional.
  13. ;
  14. ;    Mark C. Peterson
  15. ;    405-C Queen St., Suite #181
  16. ;    Southington, CT 06489
  17. ;    (203) 276-9721
  18. ;
  19. ;  Note: Remark statements following floating point commands generally indicate
  20. ;     the FPU stack contents after the command is completed.
  21. ;
  22. ;  References:
  23. ;     80386/80286 Assembly Language Programming
  24. ;        by William H. Murray, III and Chris H. Pappas
  25. ;        Published by Osborne McGraw-Hill, 1986
  26. ;        
  27. ;
  28. ;
  29.  
  30.  
  31.  
  32. IFDEF ??version
  33. MASM51
  34. QUIRKS
  35. EMUL
  36. ENDIF
  37.  
  38. .model medium, c
  39.  
  40. .code
  41.  
  42. .386
  43. .387
  44.  
  45. _Loaded387sincos   PROC
  46.    fsincos
  47.    fwait
  48.    ret
  49. _Loaded387sincos   ENDP
  50.  
  51.  
  52. FPU387sin      PROC     x:word, sin:word
  53.    mov   bx, x
  54.    fld   QWORD PTR [bx]    ; x
  55.    fsin                    ; sin
  56.    mov   bx, sin
  57.    fstp  QWORD PTR [bx]    ; <empty>
  58.    ret
  59. FPU387sin      ENDP
  60.  
  61.  
  62. FPU387cos      PROC     x:word, cos:word
  63.    mov   bx, x
  64.    fld   QWORD PTR [bx]    ; x
  65.    fcos                    ; cos
  66.    mov   bx, cos
  67.    fstp  QWORD PTR [bx]    ; <empty>
  68.    ret
  69. FPU387cos      ENDP
  70.  
  71.  
  72. FPUaptan387    PROC     x:word, y:word, z:word
  73.    mov   bx, y
  74.    fld   QWORD PTR [bx]    ; y
  75.    mov   bx, x
  76.    fld   QWORD PTR [bx]    ; x, y
  77.    fpatan                  ; ArtTan
  78.    mov   bx, z
  79.    fstp  QWORD PTR [bx]    ; <empty>
  80.    ret
  81. FPUaptan387    ENDP
  82.  
  83.  
  84. FPUcplxexp387  PROC     x:word, z:word
  85.    mov   bx, x
  86.    fld   QWORD PTR [bx+8]  ; x.y
  87.    fsincos                 ; cos, sin
  88.    fldln2                  ; ln2, cos, sin
  89.    fdivr QWORD PTR [bx]    ; x.x/ln2, cos, sin
  90.    fld1                    ; 1, x.x/ln2, cos, sin
  91.    fld   st(1)             ; x.x/ln2, 1, x.x/ln2, cos, sin
  92.    fprem                   ; prem, 1, x.x/ln2, cos, sin
  93.    f2xm1                   ; e**prem-1, 1, x.x/ln2, cos, sin
  94.    fadd                    ; e**prem, x.x/ln2, cos, sin
  95.    fscale                  ; e**x.x, x.x/ln2, cos, sin
  96.    fstp  st(1)             ; e**x.x, cos, sin
  97.    fmul  st(2), st         ; e**x.x, cos, z.y
  98.    fmul                    ; z.x, z.y
  99.    mov   bx, z
  100.    fstp  QWORD PTR [bx]    ; z.y
  101.    fstp  QWORD PTR [bx+8]  ; <empty>
  102.    ret
  103. FPUcplxexp387  ENDP
  104.    
  105.  
  106. .data
  107.  
  108. extrn TrigOverflow:WORD, TrigLimit:DWORD
  109.  
  110. PiFg13         dw       6487h
  111. InvPiFg17      dw       0a2f9h
  112. InvPiFg33      dd       0a2f9836eh
  113. InvPiFg65      dq       0a2f9836e4e44152ah
  114. InvPiFg16      dw       517ch
  115. Ln2Fg16        dw       0b172h
  116. Ln2Fg32        dd       0b17217f7h
  117. One            dd       ?
  118. ExpSign        dd       ?
  119. Exp            dd       ?
  120. SinNeg         dd       ?
  121. CosNeg         dd       ?
  122.  
  123.  
  124. .code
  125.  
  126.  
  127. TaylorTerm  MACRO
  128. LOCAL Ratio
  129.    add   Factorial, One
  130.    jnc   SHORT Ratio
  131.  
  132.    rcr   Factorial, 1
  133.    shr   Num, 1
  134.    shr   One, 1
  135.  
  136. Ratio:
  137.    mul   Num
  138.    div   Factorial
  139. ENDM
  140.  
  141.  
  142.  
  143. Term           equ      <eax>
  144. HiTerm         equ      <edx>
  145. Num            equ      <ebx>
  146. Factorial      equ      <ecx>
  147. Sin            equ      <esi>
  148. Cos            equ      <edi>
  149. e              equ      <esi>
  150. Inve           equ      <edi>
  151. LoPtr          equ      <DWORD PTR [bx]>
  152. HiPtr          equ      <DWORD PTR [bx+2]>
  153.  
  154.  
  155.          
  156. _sincos386   PROC
  157.    xor   Factorial, Factorial
  158.    mov   SinNeg, Factorial
  159.    mov   CosNeg, Factorial
  160.    mov   Exp, Factorial
  161.    or    HiTerm, HiTerm
  162.    jns   AnglePositive
  163.    
  164.    not   Term
  165.    not   HiTerm
  166.    add   Term, 1
  167.    adc   HiTerm, Factorial
  168.    mov   SinNeg, 1
  169.       
  170. AnglePositive:
  171.    mov   Sin, Term
  172.    mov   Cos, HiTerm
  173.    mul   DWORD PTR InvPiFg65
  174.    mov   Num, HiTerm
  175.    mov   Term, Cos
  176.    mul   DWORD PTR InvPiFg65
  177.    add   Num, Term
  178.    adc   Factorial, HiTerm
  179.    mov   Term, Sin
  180.    mul   InvPiFg33
  181.    add   Num, Term
  182.    adc   Factorial, HiTerm
  183.    mov   Term, Cos
  184.    mul   InvPiFg33
  185.    add   Term, Factorial
  186.    adc   HiTerm, 0
  187.  
  188.    and   HiTerm, 3
  189.    mov   Exp, HiTerm
  190.  
  191.    mov   Num, Term
  192.    mov   Factorial, InvPiFg33
  193.    mov   One, Factorial
  194.    mov   Cos, Factorial          ; Cos = 1
  195.    mov   Sin, Num                  ; Sin = Num
  196.       
  197. LoopIntSinCos:
  198.    TaylorTerm                    ; Term = Num * (x/2) * (x/3) * (x/4) * . . .
  199.    sub   Cos, Term               ; Cos = 1 - Num*(x/2) + (x**4)/4! - . . .
  200.    cmp   Term, TrigLimit
  201.    jbe   SHORT ExitIntSinCos
  202.  
  203.    TaylorTerm
  204.    sub   Sin, Term               ; Sin = Num - Num*(x/2)*(x/3) + (x**5)/5! - . . .
  205.    cmp   Term, TrigLimit
  206.    jbe   SHORT ExitIntSinCos
  207.       
  208.    TaylorTerm
  209.    add   Cos, Term
  210.    cmp   Term, TrigLimit
  211.    jbe   SHORT ExitIntSinCos
  212.       
  213.    TaylorTerm                    ; Term = Num * (x/2) * (x/3) * . . .
  214.    add   Sin, Term
  215.    cmp   Term, TrigLimit
  216.    jnbe  LoopIntSinCos
  217.       
  218. ExitIntSinCos:
  219.    xor   Term, Term
  220.    mov   Factorial, Term
  221.    cmp   Cos, InvPiFg33
  222.    jb    CosDivide               ; Cos < 1.0
  223.       
  224.    inc   Factorial                      ; Cos == 1.0
  225.    jmp   StoreCos
  226.       
  227. CosDivide:
  228.    mov   HiTerm, Cos
  229.    div   InvPiFg33
  230.       
  231. StoreCos:
  232.    mov   Cos, Term                 ; Factorial:Cos
  233.  
  234.    xor   Term, Term
  235.    mov   Num, Term
  236.    cmp   Sin, InvPiFg33
  237.    jb    SinDivide               ; Sin < 1.0
  238.    
  239.    inc   Num                      ; Sin == 1.0
  240.    jmp   StoreSin
  241.       
  242. SinDivide:
  243.    mov   HiTerm, Sin
  244.    div   InvPiFg33
  245.       
  246. StoreSin:
  247.    mov   Sin, Term                 ; Num:Sin
  248.  
  249.    test  Exp, 1
  250.    jz    ChkNegCos
  251.  
  252.    xchg  Factorial, Num
  253.    xchg  Sin, Cos
  254.    mov   Term, SinNeg
  255.    xchg  Term, CosNeg
  256.    mov   CosNeg, Term
  257.  
  258. ChkNegCos:
  259.    mov   Term, Exp
  260.    shr   Term, 1
  261.    mov   Term, 0
  262.    rcr   Term, 1
  263.    xor   Term, Exp
  264.    jz    ChkNegSin
  265.  
  266.    xor   CosNeg, 1
  267.  
  268. ChkNegSin:
  269.    test  Exp, 2
  270.    jz    CorrectQuad
  271.  
  272.    xor   SinNeg, 1
  273.  
  274. CorrectQuad:
  275.    ret
  276. _sincos386     ENDP
  277.       
  278.       
  279. SinCos386   PROC     LoNum:DWORD, HiNum:DWORD, SinAddr:DWORD, CosAddr:DWORD
  280.    mov   Term, LoNum 
  281.    mov   HiTerm, HiNum
  282.    
  283.    call  _sincos386
  284.  
  285.    cmp   CosNeg, 1
  286.    jne   CosPolarized
  287.  
  288.    not   Cos
  289.    not   Factorial 
  290.    add   Cos, 1
  291.    adc   Factorial, 0
  292.  
  293. CosPolarized:     
  294.    mov   HiTerm, Num
  295.    mov   Num, CosAddr
  296.    mov   LoPtr, Cos
  297.    mov   HiPtr, Factorial
  298.  
  299.    cmp   SinNeg, 1
  300.    jne   SinPolarized
  301.  
  302.    not   Sin
  303.    not   HiTerm
  304.    add   Sin, 1
  305.    adc   HiTerm, 0
  306.  
  307. SinPolarized:
  308.    mov   Num, SinAddr
  309.    mov   LoPtr, Sin
  310.    mov   HiPtr, HiTerm
  311.    ret
  312. SinCos386      ENDP
  313.       
  314.       
  315.       
  316. _e2y386   PROC                 ; eTerm =: Num * 2**16, 0 < Num < Ln2
  317.    mov   ExpSign, 0
  318.    or    HiTerm, HiTerm
  319.    jns   CalcExp
  320.       
  321.    mov   ExpSign, 1
  322.    not   Term
  323.    not   HiTerm
  324.    add   Term, 1
  325.    adc   HiTerm, 0
  326.    
  327. CalcExp:
  328.    div   Ln2Fg32
  329.    mov   Exp, Term
  330.    mov   Num, HiTerm
  331.       
  332.    xor   Factorial, Factorial
  333.    stc
  334.    rcr   Factorial, 1
  335.    mov   One, Factorial
  336.    mov   e, Num
  337.    mov   Term, Num
  338.    shr   Num, 1
  339.       
  340. Loop_e2y386:
  341.    TaylorTerm
  342.    add   e, Term                 ; e = 1 + Num + Num*x/2 + (x**3)/3! + . . .
  343.    cmp   Term, TrigLimit
  344.    jnbe  SHORT Loop_e2y386
  345.       
  346. ExitIntSinhCosh:
  347.    stc
  348.    rcr   e, 1
  349.    ret                           ; return e**y * (2**32), 1 < e**y < 2
  350. _e2y386   ENDP
  351.       
  352.       
  353.       
  354. Exp386    PROC     LoNum:DWORD, HiNum:DWORD
  355.    mov   Term, LoNum 
  356.    mov   HiTerm, HiNum
  357.    
  358.    call  _e2y386
  359.       
  360.    cmp   Exp, 32
  361.    jae   Overflow
  362.       
  363.    cmp   ExpSign, 0
  364.    jnz   NegNumber
  365.  
  366.    mov   Factorial, Exp
  367.    shld  HiTerm, Term, cl
  368.    shl   Term, cl
  369.    jmp   ExitExp386
  370.       
  371. Overflow:
  372.    xor   Term, Term
  373.    xor   HiTerm, HiTerm
  374.    mov   TrigOverflow, 1
  375.    jmp   ExitExp386
  376.       
  377. NegNumber:
  378.    cmp   e, 80000000h
  379.    jne   DivideE
  380.       
  381.    mov   Term, e
  382.    dec   Exp
  383.    jmp   ShiftE
  384.       
  385. DivideE:
  386.    xor   Term, Term
  387.    mov   HiTerm, Term
  388.    stc
  389.    rcr   HiTerm, 1
  390.    div   e
  391.       
  392. ShiftE:
  393.    xor   HiTerm, HiTerm
  394.    mov   Factorial, Exp
  395.    shr   Term, cl
  396.       
  397. ExitExp386:
  398.    ret
  399. Exp386    ENDP
  400.  
  401.  
  402.  
  403. SinhCosh386 PROC  LoNum:DWORD, HiNum:DWORD, SinhAddr:DWORD, CoshAddr:DWORD
  404.    mov   Term, LoNum
  405.    mov   HiTerm, HiNum
  406.  
  407.    call  _e2y386
  408.  
  409.    cmp   e, 80000000h
  410.    jne   InvertE              ; e > 1
  411.  
  412.    mov   HiTerm, 1
  413.    xor   Term, Term
  414.    cmp   Exp, 0
  415.    jne   ShiftOne
  416.  
  417.    mov   e, Term
  418.    mov   Factorial, Term
  419.    jmp   ChkSinhSign
  420.  
  421. ShiftOne:
  422.    mov   Factorial, Exp
  423.    shl   HiTerm, cl
  424.    dec   Factorial
  425.    shr   e, cl
  426.    shr   HiTerm, 1
  427.    shr   e, 1
  428.    mov   Factorial, HiTerm
  429.    sub   Term, e
  430.    sbb   HiTerm, 0
  431.    xchg  Term, e
  432.    xchg  HiTerm, Factorial
  433.    jmp   ChkSinhSign
  434.  
  435. InvertE:
  436.    xor   Term, Term               ; calc 1/e
  437.    mov   HiTerm, 80000000h
  438.    div   e
  439.  
  440.    mov   Inve, Term
  441.  
  442. ShiftE:
  443.    mov   Factorial, Exp
  444.    shr   Inve, cl
  445.    inc   cl
  446.    shld  HiTerm, e, cl
  447.    mov   Factorial, HiTerm      ; Factorial:e == e**Exp
  448.  
  449.    mov   Term, e                ; HiTerm:e == e**Exp
  450.    add   Term, Inve
  451.    adc   HiTerm, 0
  452.    shr   HiTerm, 1
  453.    rcr   Term, 1                ; cosh(Num) = (e**Exp + 1/e**Exp) / 2
  454.  
  455.    sub   e, Inve
  456.    sbb   Factorial, 0
  457.    sar   Factorial, 1
  458.    rcr   e, 1
  459.  
  460. ChkSinhSign:
  461.    or    HiNum, 0
  462.    jns   StoreHyperbolics
  463.  
  464.    not   e
  465.    not   Factorial
  466.    add   e, 1
  467.    adc   Factorial, 0
  468.  
  469. StoreHyperbolics:
  470.    mov   Num, CoshAddr
  471.    mov   LoPtr, Term
  472.    mov   HiPtr, HiTerm
  473.  
  474.    mov   Num, SinhAddr
  475.    mov   LoPtr, e
  476.    mov   HiPtr, Factorial
  477.  
  478.    ret
  479. SinhCosh386    ENDP
  480.  
  481.  
  482.  
  483. END
  484.  
  485.