mathu.inc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Florian Klaempfl
  4. member of the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. { exported by the system unit }
  12. function get_fsr : dword;external name 'FPC_GETFSR';
  13. procedure set_fsr(fsr : dword);external name 'FPC_SETFSR';
  14. const
  15. { FPU enable exception bits for FCSR register }
  16. fpu_enable_inexact = $80;
  17. fpu_enable_underflow = $100;
  18. fpu_enable_overflow = $200;
  19. fpu_enable_div_zero = $400;
  20. fpu_enable_invalid = $800;
  21. fpu_enable_mask = $F80;
  22. default_fpu_enable = fpu_enable_div_zero or fpu_enable_invalid;
  23. fpu_flags_mask = $7C;
  24. { FPU rounding mask and values }
  25. fpu_rounding_mask = $3;
  26. fpu_rounding_nearest = 0;
  27. fpu_rounding_towards_zero = 1;
  28. fpu_rounding_plus_inf = 2;
  29. fpu_rounding_minus_inf = 3;
  30. function FPUExceptionMaskToSoftFloatMask(const Mask: TFPUExceptionMask): byte;
  31. begin
  32. result:=0;
  33. if exInvalidOp in Mask then
  34. result:=result or (1 shl ord(exInvalidOp));
  35. if exDenormalized in Mask then
  36. result:=result or (1 shl ord(exDenormalized));
  37. if exZeroDivide in Mask then
  38. result:=result or (1 shl ord(exZeroDivide));
  39. if exOverflow in Mask then
  40. result:=result or (1 shl ord(exOverflow));
  41. if exUnderflow in Mask then
  42. result:=result or (1 shl ord(exUnderflow));
  43. if exPrecision in Mask then
  44. result:=result or (1 shl ord(exPrecision));
  45. end;
  46. function GetRoundMode: TFPURoundingMode;
  47. begin
  48. result:=TFPURoundingMode(get_fsr and 3);
  49. end;
  50. function SetRoundMode(const RoundMode: TFPURoundingMode): TFPURoundingMode;
  51. var
  52. fpu_round : longint;
  53. begin
  54. case (RoundMode) of
  55. rmNearest :
  56. begin
  57. softfloat_rounding_mode := float_round_nearest_even;
  58. fpu_round:=fpu_rounding_nearest;
  59. end;
  60. rmTruncate :
  61. begin
  62. softfloat_rounding_mode := float_round_to_zero;
  63. fpu_round:=fpu_rounding_towards_zero;
  64. end;
  65. rmUp :
  66. begin
  67. softfloat_rounding_mode := float_round_up;
  68. fpu_round:=fpu_rounding_plus_inf;
  69. end;
  70. rmDown :
  71. begin
  72. softfloat_rounding_mode := float_round_down;
  73. fpu_round:=fpu_rounding_minus_inf;
  74. end;
  75. end;
  76. set_fsr((get_fsr and not fpu_rounding_mask) or fpu_round);
  77. //!!! result:=TFPURoundingMode(get_fsr shr 30);
  78. end;
  79. function GetPrecisionMode: TFPUPrecisionMode;
  80. begin
  81. result:=pmDouble;
  82. end;
  83. function SetPrecisionMode(const Precision: TFPUPrecisionMode): TFPUPrecisionMode;
  84. begin
  85. result:=pmDouble;
  86. end;
  87. function GetExceptionMask: TFPUExceptionMask;
  88. var
  89. fsr : dword;
  90. begin
  91. fsr:=get_fsr;
  92. result:=[];
  93. { invalid operation }
  94. if (fsr and fpu_enable_invalid)=0 then
  95. include(result,exInvalidOp);
  96. { zero divide }
  97. if (fsr and fpu_enable_div_zero)=0 then
  98. include(result,exZeroDivide);
  99. { overflow }
  100. if (fsr and fpu_enable_overflow)=0 then
  101. include(result,exOverflow);
  102. { underflow: }
  103. if (fsr and fpu_enable_underflow)=0 then
  104. include(result,exUnderflow);
  105. { Precision (inexact result) }
  106. if (fsr and fpu_enable_inexact)=0 then
  107. include(result,exPrecision);
  108. end;
  109. function SetExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  110. var
  111. fsr : dword;
  112. begin
  113. fsr:=get_fsr;
  114. { invalid operation }
  115. if (exInvalidOp in mask) then
  116. fsr:=fsr and not(fpu_enable_invalid)
  117. else
  118. fsr:=fsr or (fpu_enable_invalid);
  119. { zero divide }
  120. if (exZeroDivide in mask) then
  121. fsr:=fsr and not(fpu_enable_div_zero)
  122. else
  123. fsr:=fsr or (fpu_enable_div_zero);
  124. { overflow }
  125. if (exOverflow in mask) then
  126. fsr:=fsr and not(fpu_enable_overflow)
  127. else
  128. fsr:=fsr or (fpu_enable_overflow);
  129. { underflow }
  130. if (exUnderflow in mask) then
  131. fsr:=fsr and not(fpu_enable_underflow)
  132. else
  133. fsr:=fsr or (fpu_enable_underflow);
  134. { Precision (inexact result) }
  135. if (exPrecision in mask) then
  136. fsr:=fsr and not(fpu_enable_inexact)
  137. else
  138. fsr:=fsr or (fpu_enable_inexact);
  139. { update control register contents }
  140. set_fsr(fsr);
  141. softfloat_exception_mask:=FPUExceptionMaskToSoftFloatMask(mask);
  142. end;
  143. procedure ClearExceptions(RaisePending: Boolean =true);
  144. begin
  145. set_fsr(get_fsr and $fffffc1f);
  146. end;