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

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