mathu.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. softfloat_rounding_mode:=RoundMode;
  55. set_fsr((fsr and not fpu_rounding_mask) or roundmode2fsr[RoundMode]);
  56. end;
  57. function GetPrecisionMode: TFPUPrecisionMode;
  58. begin
  59. result:=pmDouble;
  60. end;
  61. function SetPrecisionMode(const Precision: TFPUPrecisionMode): TFPUPrecisionMode;
  62. begin
  63. result:=pmDouble;
  64. end;
  65. function fsr2ExceptionMask(fsr: longword): TFPUExceptionMask;
  66. begin
  67. result:=[];
  68. { invalid operation }
  69. if (fsr and fpu_enable_invalid)=0 then
  70. include(result,exInvalidOp);
  71. { zero divide }
  72. if (fsr and fpu_enable_div_zero)=0 then
  73. include(result,exZeroDivide);
  74. { overflow }
  75. if (fsr and fpu_enable_overflow)=0 then
  76. include(result,exOverflow);
  77. { underflow: }
  78. if (fsr and fpu_enable_underflow)=0 then
  79. include(result,exUnderflow);
  80. { Precision (inexact result) }
  81. if (fsr and fpu_enable_inexact)=0 then
  82. include(result,exPrecision);
  83. end;
  84. function GetExceptionMask: TFPUExceptionMask;
  85. begin
  86. result:=fsr2ExceptionMask(get_fsr);
  87. end;
  88. function SetExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  89. var
  90. fsr : longword;
  91. begin
  92. fsr:=get_fsr;
  93. result:=fsr2ExceptionMask(fsr);
  94. { Reset flags, cause and enables }
  95. fsr := fsr and not (fpu_flags_mask or fpu_cause_mask or fpu_enable_mask);
  96. { invalid operation }
  97. if not (exInvalidOp in mask) then
  98. fsr:=fsr or (fpu_enable_invalid);
  99. { zero divide }
  100. if not (exZeroDivide in mask) then
  101. fsr:=fsr or (fpu_enable_div_zero);
  102. { overflow }
  103. if not (exOverflow in mask) then
  104. fsr:=fsr or (fpu_enable_overflow);
  105. { underflow }
  106. if not (exUnderflow in mask) then
  107. fsr:=fsr or (fpu_enable_underflow);
  108. { Precision (inexact result) }
  109. if not (exPrecision in mask) then
  110. fsr:=fsr or (fpu_enable_inexact);
  111. { update control register contents }
  112. set_fsr(fsr);
  113. end;
  114. procedure ClearExceptions(RaisePending: Boolean =true);
  115. begin
  116. set_fsr(get_fsr and not (fpu_flags_mask or fpu_cause_mask));
  117. end;