home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 2.ddi / MATH.ZIP / FMOD.CAS < prev    next >
Encoding:
Text File  |  1990-06-07  |  2.6 KB  |  97 lines

  1. /*------------------------------------------------------------------------
  2.  * filename - fmod.cas
  3.  *
  4.  * function(s)
  5.  *        fmod - calculates x modulo y, the remainder of x/y
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /*[]------------------------------------------------------------[]*/
  9. /*|                                                              |*/
  10. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  11. /*|                                                              |*/
  12. /*|                                                              |*/
  13. /*|     Copyright (c) 1987, 1990 by Borland International        |*/
  14. /*|     All Rights Reserved.                                     |*/
  15. /*|                                                              |*/
  16. /*[]------------------------------------------------------------[]*/
  17.  
  18.  
  19. #pragma inline
  20. #include <asmrules.h>
  21.  
  22. #include <_math.h>
  23. #include <math.h>
  24.  
  25.  
  26. /*--------------------------------------------------------------------------*
  27.  
  28. Name        fmod - calculates x modulo y, the remainder of x/y
  29.  
  30. Usage        double fmod(double x, double y);
  31.  
  32. Prototype in    math.h
  33.  
  34. Description    fmod calculates  x - (y * chop (x / y));
  35.         This difference can be more accurately calculated using the
  36.         FPREM instruction in a repeat  loop, though it is slower if
  37.         x/y is large.
  38.  
  39.         while (not finished)  result = fprem (x, y)
  40.  
  41. Return value    fmod returns x modulo y as described
  42.  
  43. *---------------------------------------------------------------------------*/
  44. #pragma warn -rvl
  45. double    fmod (double x, double y)
  46. {
  47. asm    FLD    DOUBLE (y)
  48.  
  49. asm    mov    ax, y [6]
  50. asm    shl    ax, 1            /* ignore the sign bit */
  51. asm    jz    mod_resultZero        /* if the divisor is zero */
  52. asm    cmp    ax, 0FFE0h
  53. asm    jnb    mod_isX            /* if y is infinite */
  54.  
  55. asm    FLD    DOUBLE (x)
  56.  
  57. asm    mov    ax, x [6]
  58. asm    shl    ax, 1
  59. asm    jz    mod_xZero        /* if x is zero */
  60. asm    cmp    ax, 0FFE0h
  61. asm    jnb    mod_overflow        /* if x is infinite */
  62.  
  63. mod_keepTrying:
  64. asm    FPREM
  65. asm    push    bx
  66. asm    mov    bx, sp
  67. asm    FSTSW    W0 (SS_ [bx])        /* C2 will be set if not yet finished */
  68. asm    FWAIT
  69. asm    pop    ax
  70. asm    sahf
  71. asm    jp    mod_keepTrying        /* C2 bit maps onto parity flag. */
  72.  
  73. asm    FSTP    st(1)            /* discard the divisor */
  74.  
  75. mod_end:
  76.     return;
  77.  
  78. /*
  79.   If the divisor is infinite then return the dividend.
  80. */
  81. mod_isX:
  82. asm    FSTP    st(0)            /* pop y off the stack */
  83. asm    FLD    DOUBLE (x)
  84. asm    jmp    short    mod_end
  85.  
  86. /* All other forms of overflow are mapped onto zero.
  87. */
  88. mod_xZero:
  89. mod_overflow:
  90. asm    FSTP    st(0)            /* pop x off the stack */
  91. mod_resultZero:
  92. asm    FSTP    st(0)            /* pop y off the stack */
  93. asm    FLDZ
  94. asm    jmp    short    mod_end
  95. }
  96. #pragma warn .rvl
  97.