home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / mint / mntlib24 / _divsf3.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-03  |  4.5 KB  |  184 lines

  1. |
  2. | single floating point divide routine
  3. |
  4. #ifndef __M68881__
  5.     .text
  6.     .even
  7.     .globl    __divsf3, ___divsf3
  8. #ifdef    ERROR_CHECK
  9. #include "errbase.h"
  10.     .globl    __infinitysf
  11.  
  12. LC0:
  13.     .ascii "floating point division by 0\12\15\0"
  14.     .even
  15. #endif    ERROR_CHECK
  16.  
  17. __divsf3:
  18. ___divsf3:
  19.  
  20. #ifdef    ERROR_CHECK
  21.     tstl    a7@(8)            | check if divisor is 0
  22.     bne    no_exception
  23.  
  24.     pea    pc@(LC0)
  25.     pea    Stderr
  26.     jbsr    _fprintf    |
  27.     addql    #8,a7        |
  28.                     | set _errno to ERANGE
  29.     moveq    #ERANGE,d0
  30.     Emove    d0,Errno
  31.     movel    __infinitysf,d0        | return signed infinity 
  32.     btst    #31,a7@(4)        | transfer sign of dividend
  33.     beq    clear            | (mjr++)
  34.     bset    #31,d0            |
  35.     rts                |
  36. clear:                    |
  37.     rts
  38.  
  39. no_exception:
  40. #endif    ERROR_CHECK
  41.  
  42. #ifndef sfp004
  43. |
  44. | written by Kai-Uwe Bloem (I5110401@dbstu1.bitnet).
  45. | Based on a 80x86 floating point packet from comp.os.minix, written by P.Housel
  46. |
  47. | Revision 1.2, kub 01-90 :
  48. | added support for denormalized numbers
  49. |
  50. | Revision 1.1, kub 12-89 :
  51. | Created single float version for 68000
  52. |
  53. | Revision 1.0:
  54. | original 8088 code from P.S.Housel for double floats
  55.  
  56. BIAS4    =    0x7F-1
  57.  
  58.     lea    sp@(4),a0    | pointer to parameters u and v
  59.     moveml    d2-d5,sp@-    | save registers
  60.     moveml    a0@,d4/d5    | d4 = u, d5 = v
  61.  
  62.     movel    d4,d0        | d0 = u.exp
  63.     swap    d0
  64.     movew    d0,d2        | d2 = u.sign
  65.     lsrw    #7,d0
  66.     andw    #0xff,d0    | kill sign bit
  67.  
  68.     movel    d5,d1        | d1 = v.exp
  69.     swap    d1
  70.     eorw    d1,d2        | d2 = u.sign ^ v.sign (in bit 31)
  71.     lsrw    #7,d1
  72.     andw    #0xff,d1    | kill sign bit
  73.  
  74.     andl    #0x7fffff,d4    | remove exponent from u.mantissa
  75.     tstw    d0        | check for zero exponent - no leading "1"
  76.     beq    0f
  77.     orl    #0x800000,d4    | restore implied leading "1"
  78.     bra    1f
  79. 0:    addw    #1,d0        | "normalize" exponent
  80. 1:
  81. # ifndef ERROR_CHECK
  82.     tstl    d4
  83.     beq    retz        | dividing zero
  84. # endif    ERROR_CHECK
  85.  
  86.     andl    #0x7fffff,d5    | remove exponent from v.mantissa
  87.     tstw    d1        | check for zero exponent - no leading "1"
  88.     beq    0f
  89.     orl    #0x800000,d5    | restore implied leading "1"
  90.     bra    1f
  91. 0:    addw    #1,d1        | "normalize" exponent
  92. 1:    tstl    d5
  93.     beq    divz        | divide by zero
  94.  
  95.     subw    d1,d0        | subtract exponents,
  96.     addw    #BIAS4-8+1,d0    |  add bias back in, account for shift
  97.     addw    #34,d0        |  add loop offset, +2 for extra rounding bits
  98.                 |   for denormalized numbers (2 implied by dbra)
  99.     movew    #27,d1        | bit number for "implied" pos (+4 for rounding)
  100.     movel    #-1,d3        |  zero quotient (for speed a one''s complement)
  101.     subl    d5,d4        | initial subtraction, u = u - v
  102. 2:
  103.     btst    d1,d3        | divide until 1 in implied position
  104.     beq    5f
  105.  
  106.     addl    d4,d4
  107.     bcs    4f        | if carry is set, add, else subtract
  108.  
  109.     addxl    d3,d3        | shift quotient and set bit zero
  110.     subl    d5,d4        | subtract, u = u - v
  111.     dbra    d0,2b        | give up if result is denormalized
  112.     bra    5f
  113. 4:
  114.     addxl    d3,d3        | shift quotient and clear bit zero
  115.     addl    d5,d4        | add (restore), u = u + v
  116.     dbra    d0,2b        | give up if result is denormalized
  117. 5:    subw    #2,d0        | remove rounding offset for denormalized nums
  118.     notl    d3        | invert quotient to get it right
  119.  
  120.     movel    d3,d4        | save quotient mantissa
  121.     clrw    d1        | zero rounding bits
  122.     jmp    norm_sf        | (registers on stack removed by norm_sf)
  123.  
  124. # ifndef ERROR_CHECK
  125. retz:    clrl    d0        | zero destination
  126.     moveml    sp@+,d2-d5
  127.     rts            | no normalization needed
  128.  
  129. divz:    movel    __infinitysf,d0    | return infinity value
  130.     moveml    sp@+,d2-d5    | should really cause trap ?!?
  131.     btst    #31,sp@(4)    | transfer sign of dividend
  132.     beq    clear        | (mjr++)
  133.     bset    #31,d0        |
  134.     rts            |
  135. clear:                |
  136.     bclr    #31,d0        |
  137.     rts
  138. # endif    ERROR_CHECK
  139. #else
  140.  
  141. | single precision floating point stuff for Atari-gcc using the SFP004
  142. | or compatible boards with a memory mapped 68881 
  143. | developed with gas
  144. |
  145. |  single floating point divide routine
  146. |
  147. | M. Ritzert (mjr at dmzrzu71)
  148. |            (ritzert@dfg.dbp.de)
  149. | 4.10.1990
  150. |
  151. | +_infinitysf returned instead of a NAN
  152. | the DOMAIN exception is not supported yet. In case of an exception
  153. | _errno is always set to ERANGE
  154.  
  155. | addresses of the 68881 data port. This choice is fastest when much data is
  156. | transferred between the two processors.
  157.  
  158. comm =     -6
  159. resp =    -16
  160. zahl =      0
  161.  
  162. | waiting loop ...
  163. |
  164. | wait:
  165. | ww:    cmpiw    #0x8900,a0@(resp)
  166. |     beq    ww
  167. | is coded directly by
  168. |    .long    0x0c688900, 0xfff067f8
  169.  
  170.     lea    0xfffffa50:w,a0
  171.     movew    #0x4400,a0@(comm)    | load first argument to fp0
  172.     cmpiw    #0x8900,a0@(resp)    | check
  173.     movel    a7@(4),a0@
  174.     movew    #0x4424,a0@(comm)
  175.     .long    0x0c688900, 0xfff067f8
  176.     movel    a7@(8),a0@
  177.     movew    #0x6400,a0@(comm)    | result to d0
  178.     .long    0x0c688900, 0xfff067f8
  179.     movel    a0@,d0            | REMARK: 0/0 returns a NAN
  180.     rts                | if ERROR_CHECK is disabled
  181.  
  182. #endif    sfp004
  183. #endif /* !__M68881__ */
  184.