home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 2.ddi / MATHSRC.ZIP / ROUND.CAS < prev    next >
Encoding:
Text File  |  1992-06-10  |  1.9 KB  |  62 lines

  1. /*------------------------------------------------------------------------
  2.  * filename - round.cas
  3.  *
  4.  * function(s)
  5.  *        __round - rounding helper function
  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. Name            __round - rounding helper function
  26.  
  27. Usage           void near __round(void)
  28.  
  29. Prototype in    _math.h
  30.  
  31. Description     __round rounds the argument on the floating point stack.
  32.                 The CH register contains the rounding flags:
  33.                         00H     Round to nearest or even
  34.                         04H     Round down toward -INF
  35.                         08H     Round up toward +INF
  36.                         0CH     Chop (truncate toward 0)
  37.                 This function is a helper for modf(), floor(), ceil(),
  38.                 and their long double counterparts.
  39.  
  40. Return value    __round does not return a value, but leaves the rounded
  41.                 result on the top of the FPU stack.
  42.  
  43. *---------------------------------------------------------------------------*/
  44.  
  45. void pascal near __round(void)
  46. {
  47.         unsigned int cword;
  48.  
  49. asm     FSTCW   cword           /* read out the current control word */
  50. asm     mov     ax, 0F3FFh
  51. asm     FWAIT
  52. asm     mov     dx, cword
  53. asm     and     ax, dx          /* mask out the rounding control */
  54. asm     or      ah, ch          /* iNDP-87 control bits for rounding mode */
  55. asm     mov     cword, ax
  56. asm     FLDCW   cword
  57. asm     FRNDINT                 /* round to integer */
  58. asm     mov     cword, dx
  59. asm     FLDCW   cword           /* restore original rounding control */
  60.         return;
  61. }
  62.