home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib25.zoo / _divsf3.cpp < prev    next >
C/C++ Source or Header  |  1992-12-12  |  5KB  |  193 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. | patched by Olaf Flebbe (flebbe@tat.physik.uni-tuebingen.de)
  47. |
  48. | Revision 1.2.1 olaf 11-92
  49. |   + prevent the tie rounding case if dividing is not exact.
  50. |      > paranoia now says: "Division appears to round correctly"
  51. |      ** requires _normsf Version 1.4.2 or later
  52. |
  53. | Revision 1.2, kub 01-90 :
  54. | added support for denormalized numbers
  55. |
  56. | Revision 1.1, kub 12-89 :
  57. | Created single float version for 68000
  58. |
  59. | Revision 1.0:
  60. | original 8088 code from P.S.Housel for double floats
  61.  
  62. BIAS4    =    0x7F-1
  63.  
  64.     lea    sp@(4),a0    | pointer to parameters u and v
  65.     moveml    d2-d5,sp@-    | save registers
  66.     moveml    a0@,d4/d5    | d4 = u, d5 = v
  67.  
  68.     movel    d4,d0        | d0 = u.exp
  69.     swap    d0
  70.     movew    d0,d2        | d2 = u.sign
  71.     lsrw    #7,d0
  72.     andw    #0xff,d0    | kill sign bit
  73.  
  74.     movel    d5,d1        | d1 = v.exp
  75.     swap    d1
  76.     eorw    d1,d2        | d2 = u.sign ^ v.sign (in bit 31)
  77.     lsrw    #7,d1
  78.     andw    #0xff,d1    | kill sign bit
  79.  
  80.     andl    #0x7fffff,d4    | remove exponent from u.mantissa
  81.     tstw    d0        | check for zero exponent - no leading "1"
  82.     beq    0f
  83.     orl    #0x800000,d4    | restore implied leading "1"
  84.     bra    1f
  85. 0:    addw    #1,d0        | "normalize" exponent
  86. 1:
  87. # ifndef ERROR_CHECK
  88.     tstl    d4
  89.     beq    retz        | dividing zero
  90. # endif    ERROR_CHECK
  91.  
  92.     andl    #0x7fffff,d5    | remove exponent from v.mantissa
  93.     tstw    d1        | check for zero exponent - no leading "1"
  94.     beq    0f
  95.     orl    #0x800000,d5    | restore implied leading "1"
  96.     bra    1f
  97. 0:    addw    #1,d1        | "normalize" exponent
  98. 1:    tstl    d5
  99.     beq    divz        | divide by zero
  100.  
  101.     subw    d1,d0        | subtract exponents,
  102.     addw    #BIAS4-8+1,d0    |  add bias back in, account for shift
  103.     addw    #34,d0        |  add loop offset, +2 for extra rounding bits
  104.                 |   for denormalized numbers (2 implied by dbra)
  105.     movew    #27,d1        | bit number for "implied" pos (+4 for rounding)
  106.     movel    #-1,d3        |  zero quotient (for speed a one''s complement)
  107.     subl    d5,d4        | initial subtraction, u = u - v
  108. 2:
  109.     btst    d1,d3        | divide until 1 in implied position
  110.     beq    5f
  111.  
  112.     addl    d4,d4
  113.     bcs    4f        | if carry is set, add, else subtract
  114.  
  115.     addxl    d3,d3        | shift quotient and set bit zero
  116.     subl    d5,d4        | subtract, u = u - v
  117.     dbra    d0,2b        | give up if result is denormalized
  118.     bra    5f
  119. 4:
  120.     addxl    d3,d3        | shift quotient and clear bit zero
  121.     addl    d5,d4        | add (restore), u = u + v
  122.     dbra    d0,2b        | give up if result is denormalized
  123. 5:    subw    #2,d0        | remove rounding offset for denormalized nums
  124.     notl    d3        | invert quotient to get it right
  125.  
  126.     clrl    d1        | zero rounding bits
  127.     tstl     d4        | check for exact result
  128.     beq    1f
  129.     moveql    #-1,d1        | prevent tie case
  130. 1:    movel    d3,d4        | save quotient mantissa
  131.     jmp    norm_sf        | (registers on stack removed by norm_sf)
  132.  
  133. # ifndef ERROR_CHECK
  134. retz:    clrl    d0        | zero destination
  135.     moveml    sp@+,d2-d5
  136.     rts            | no normalization needed
  137.  
  138. divz:    movel    __infinitysf,d0    | return infinity value
  139.     moveml    sp@+,d2-d5    | should really cause trap ?!?
  140.     btst    #31,sp@(4)    | transfer sign of dividend
  141.     beq    clear        | (mjr++)
  142.     bset    #31,d0        |
  143.     rts            |
  144. clear:                |
  145.     bclr    #31,d0        |
  146.     rts
  147. # endif    ERROR_CHECK
  148. #else
  149.  
  150. | single precision floating point stuff for Atari-gcc using the SFP004
  151. | or compatible boards with a memory mapped 68881 
  152. | developed with gas
  153. |
  154. |  single floating point divide routine
  155. |
  156. | M. Ritzert (mjr at dmzrzu71)
  157. |            (ritzert@dfg.dbp.de)
  158. | 4.10.1990
  159. |
  160. | +_infinitysf returned instead of a NAN
  161. | the DOMAIN exception is not supported yet. In case of an exception
  162. | _errno is always set to ERANGE
  163.  
  164. | addresses of the 68881 data port. This choice is fastest when much data is
  165. | transferred between the two processors.
  166.  
  167. comm =     -6
  168. resp =    -16
  169. zahl =      0
  170.  
  171. | waiting loop ...
  172. |
  173. | wait:
  174. | ww:    cmpiw    #0x8900,a0@(resp)
  175. |     beq    ww
  176. | is coded directly by
  177. |    .long    0x0c688900, 0xfff067f8
  178.  
  179.     lea    0xfffffa50:w,a0
  180.     movew    #0x4400,a0@(comm)    | load first argument to fp0
  181.     cmpiw    #0x8900,a0@(resp)    | check
  182.     movel    a7@(4),a0@
  183.     movew    #0x4424,a0@(comm)
  184.     .long    0x0c688900, 0xfff067f8
  185.     movel    a7@(8),a0@
  186.     movew    #0x6400,a0@(comm)    | result to d0
  187.     .long    0x0c688900, 0xfff067f8
  188.     movel    a0@,d0            | REMARK: 0/0 returns a NAN
  189.     rts                | if ERROR_CHECK is disabled
  190.  
  191. #endif    sfp004
  192. #endif /* !__M68881__ */
  193.