mathu.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. const
  12. { FPU enable exception bits for FCSR register }
  13. fpu_enable_inexact = $80;
  14. fpu_enable_underflow = $100;
  15. fpu_enable_overflow = $200;
  16. fpu_enable_div_zero = $400;
  17. fpu_enable_invalid = $800;
  18. fpu_enable_mask = $F80;
  19. default_fpu_enable = fpu_enable_div_zero or fpu_enable_invalid;
  20. fpu_flags_mask = $7C;
  21. fpu_cause_mask = $3F000;
  22. { FPU rounding mask and values }
  23. fpu_rounding_mask = $3;
  24. fpu_rounding_nearest = 0;
  25. fpu_rounding_towards_zero = 1;
  26. fpu_rounding_plus_inf = 2;
  27. fpu_rounding_minus_inf = 3;
  28. const
  29. roundmode2fsr : array [TFPURoundingMode] of byte=(
  30. fpu_rounding_nearest,
  31. fpu_rounding_minus_inf,
  32. fpu_rounding_plus_inf,
  33. fpu_rounding_towards_zero
  34. );
  35. fsr2roundmode : array [0..3] of TFPURoundingMode = (
  36. rmNearest,
  37. rmTruncate,
  38. rmUp,
  39. rmDown
  40. );
  41. function GetRoundMode: TFPURoundingMode;
  42. begin
  43. result:=fsr2roundmode[GetNativeFPUControlWord and fpu_rounding_mask];
  44. end;
  45. function SetRoundMode(const RoundMode: TFPURoundingMode): TFPURoundingMode;
  46. var
  47. fsr: TNativeFPUControlWord;
  48. begin
  49. fsr:=GetNativeFPUControlWord;
  50. result:=fsr2roundmode[fsr and fpu_rounding_mask];
  51. softfloat_rounding_mode:=RoundMode;
  52. SetNativeFPUControlWord((fsr and not fpu_rounding_mask) or roundmode2fsr[RoundMode]);
  53. end;
  54. function GetPrecisionMode: TFPUPrecisionMode;
  55. begin
  56. result:=pmDouble;
  57. end;
  58. function SetPrecisionMode(const Precision: TFPUPrecisionMode): TFPUPrecisionMode;
  59. begin
  60. result:=pmDouble;
  61. end;
  62. function fsr2ExceptionMask(fsr: TNativeFPUControlWord): TFPUExceptionMask;
  63. begin
  64. result:=[];
  65. { invalid operation }
  66. if (fsr and fpu_enable_invalid)=0 then
  67. include(result,exInvalidOp);
  68. { zero divide }
  69. if (fsr and fpu_enable_div_zero)=0 then
  70. include(result,exZeroDivide);
  71. { overflow }
  72. if (fsr and fpu_enable_overflow)=0 then
  73. include(result,exOverflow);
  74. { underflow: }
  75. if (fsr and fpu_enable_underflow)=0 then
  76. include(result,exUnderflow);
  77. { Precision (inexact result) }
  78. if (fsr and fpu_enable_inexact)=0 then
  79. include(result,exPrecision);
  80. end;
  81. function GetExceptionMask: TFPUExceptionMask;
  82. begin
  83. result:=fsr2ExceptionMask(GetNativeFPUControlWord);
  84. end;
  85. function SetExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  86. var
  87. fsr : TNativeFPUControlWord;
  88. begin
  89. fsr:=GetNativeFPUControlWord;
  90. result:=fsr2ExceptionMask(fsr);
  91. { Reset flags, cause and enables }
  92. fsr := fsr and not (fpu_flags_mask or fpu_cause_mask or fpu_enable_mask);
  93. { invalid operation }
  94. if not (exInvalidOp in mask) then
  95. fsr:=fsr or (fpu_enable_invalid);
  96. { zero divide }
  97. if not (exZeroDivide in mask) then
  98. fsr:=fsr or (fpu_enable_div_zero);
  99. { overflow }
  100. if not (exOverflow in mask) then
  101. fsr:=fsr or (fpu_enable_overflow);
  102. { underflow }
  103. if not (exUnderflow in mask) then
  104. fsr:=fsr or (fpu_enable_underflow);
  105. { Precision (inexact result) }
  106. if not (exPrecision in mask) then
  107. fsr:=fsr or (fpu_enable_inexact);
  108. { update control register contents }
  109. SetNativeFPUControlWord(fsr);
  110. end;
  111. procedure ClearExceptions(RaisePending: Boolean =true);
  112. begin
  113. SetNativeFPUControlWord(GetNativeFPUControlWord and not (fpu_flags_mask or fpu_cause_mask));
  114. end;