mathu.inc 3.7 KB

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