home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 1.ddi / MATHSRC.ZIP / CTRL87.CAS < prev    next >
Encoding:
Text File  |  1992-06-10  |  3.1 KB  |  118 lines

  1. /*------------------------------------------------------------------------
  2.  * filename - ctrl87.cas
  3.  *
  4.  * function(s)
  5.  *        _control87 - access floating-point control word
  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. #pragma inline
  17.  
  18. #include <float.h>
  19. #include <dos.h>
  20. #include "emuvars.h"
  21.  
  22. /*---------------------------------------------------------------------*
  23.  
  24. Name            _control87 - access floating-point control word
  25.  
  26. Usage           unsigned int _control87(unsigned int new, unsigned int mask);
  27.  
  28. Prototype in    float.h
  29.  
  30. Description     This function is used to retrieve or change the floating-
  31.                 point control word.
  32.  
  33.                 For every bit that is on in mask, the corresponding bit
  34.                 in the floating-point control word is set to the value
  35.                 of that bit in new.
  36.  
  37. Return value    The new floating-point control word value is returned.
  38.  
  39. Notes:
  40. The Denormal exception is always trapped on the 8087 and 80287, but
  41. never on the 80387.  The emulator never generates it.
  42.  
  43. The Invalid exception is always trapped, as FPU stack overflows and
  44. underflows can sometimes be repaired transparently, but sometimes
  45. also cause nonrecoverable fatal errors.
  46.  
  47. The return value should be what the user expects, as the processing
  48. of the Denormal and Invalid bits is done surreptitiously.
  49.  
  50. Caution:
  51. The control word should not be changed directly, unless you wish
  52. to bypass the RTL trap handling.
  53.  
  54. It is dangerous to unmask an exception unless the status bit is already
  55. clear.  To be safe, call _clear87() first.
  56.  
  57. *---------------------------------------------------------------------*/
  58.  
  59. unsigned int _control87(unsigned int new, unsigned int mask)
  60. {
  61.         volatile unsigned int Control;
  62.  
  63. #ifndef _Windows
  64. #pragma warn -asm
  65. asm     emul
  66. #pragma warn .asm
  67. #endif
  68.  
  69. asm     fstcw   Control
  70.         _AX = new;
  71.         _BX = mask;
  72. asm     and     ax, bx
  73. asm     not     bx
  74. asm     fwait
  75.         _DX = Control;
  76. asm     and     dx, bx
  77.  
  78. #ifdef _Windows
  79. asm     or      ax, dx
  80. #else
  81. asm     or      dx, ax
  82.  
  83. /* DX has what the user thinks he wants, but we don't give it to him */
  84. /* we store it in memory as a shadow mask instead */
  85.  
  86. asm     mov     cl, dl
  87. asm     and     cl, 3Fh         /* remove reserved bits */
  88. asm     mov     byte ptr SS: _emu.control [0], cl
  89.  
  90. asm     mov     ax, dx
  91. asm     cmp     byte ptr SS: _emu._8087, 0
  92. asm     jz      control
  93.  
  94. #if     DeepStack
  95.         _AX = _DX & ~(EM_DENORMAL | EM_INVALID);
  96. #else
  97.         _AX = _DX & ~EM_DENORMAL;
  98. #endif
  99.  
  100. /* if using a 80387, mask denormal exceptions */
  101. asm     cmp     byte ptr SS: _emu._8087, 3
  102. asm     jl      control
  103. asm     or      al, EM_DENORMAL
  104.  
  105. #endif
  106.  
  107. control:
  108.         Control = _AX;
  109. asm     fldcw   Control
  110.  
  111. #if !defined(_Windows)
  112. /* tell the user we did what he asked */
  113. /* what he doesn't know won't hurt him */
  114. asm     xchg    ax, dx
  115. #endif
  116.         return _AX;
  117. }
  118.