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