mathu.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. function FPUExceptionMaskToSoftFloatMask(const Mask: TFPUExceptionMask): byte;
  15. begin
  16. result:=0;
  17. if exInvalidOp in Mask then
  18. result:=result or (1 shl ord(exInvalidOp));
  19. if exDenormalized in Mask then
  20. result:=result or (1 shl ord(exDenormalized));
  21. if exZeroDivide in Mask then
  22. result:=result or (1 shl ord(exZeroDivide));
  23. if exOverflow in Mask then
  24. result:=result or (1 shl ord(exOverflow));
  25. if exUnderflow in Mask then
  26. result:=result or (1 shl ord(exUnderflow));
  27. if exPrecision in Mask then
  28. result:=result or (1 shl ord(exPrecision));
  29. end;
  30. function GetRoundMode: TFPURoundingMode;
  31. begin
  32. result:=TFPURoundingMode(get_fsr shr 30);
  33. end;
  34. function SetRoundMode(const RoundMode: TFPURoundingMode): TFPURoundingMode;
  35. begin
  36. case (RoundMode) of
  37. rmNearest :
  38. softfloat_rounding_mode := float_round_nearest_even;
  39. rmTruncate :
  40. softfloat_rounding_mode := float_round_to_zero;
  41. rmUp :
  42. softfloat_rounding_mode := float_round_up;
  43. rmDown :
  44. softfloat_rounding_mode := float_round_down;
  45. end;
  46. set_fsr((get_fsr and $3fffffff) or (dword(RoundMode) shl 30));
  47. result:=TFPURoundingMode(get_fsr shr 30);
  48. end;
  49. function GetPrecisionMode: TFPUPrecisionMode;
  50. begin
  51. result:=pmDouble;
  52. end;
  53. function SetPrecisionMode(const Precision: TFPUPrecisionMode): TFPUPrecisionMode;
  54. begin
  55. result:=pmDouble;
  56. end;
  57. function GetExceptionMask: TFPUExceptionMask;
  58. var
  59. fsr : dword;
  60. begin
  61. fsr:=get_fsr;
  62. result:=[];
  63. { invalid operation: bit 27 }
  64. if (fsr and (1 shl 27))=0 then
  65. include(result,exInvalidOp);
  66. { zero divide: bit 24 }
  67. if (fsr and (1 shl 24))=0 then
  68. include(result,exInvalidOp);
  69. { overflow: bit 26 }
  70. if (fsr and (1 shl 26))=0 then
  71. include(result,exInvalidOp);
  72. { underflow: bit 25 }
  73. if (fsr and (1 shl 25))=0 then
  74. include(result,exUnderflow);
  75. { Precision (inexact result): bit 23 }
  76. if (fsr and (1 shl 23))=0 then
  77. include(result,exPrecision);
  78. end;
  79. function SetExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  80. var
  81. fsr : dword;
  82. begin
  83. fsr:=get_fsr;
  84. { invalid operation: bit 27 }
  85. if (exInvalidOp in mask) then
  86. fsr:=fsr and not(1 shl 27)
  87. else
  88. fsr:=fsr or (1 shl 27);
  89. { zero divide: bit 24 }
  90. if (exZeroDivide in mask) then
  91. fsr:=fsr and not(1 shl 24)
  92. else
  93. fsr:=fsr or (1 shl 24);
  94. { overflow: bit 26 }
  95. if (exOverflow in mask) then
  96. fsr:=fsr and not(1 shl 26)
  97. else
  98. fsr:=fsr or (1 shl 26);
  99. { underflow: bit 25 }
  100. if (exUnderflow in mask) then
  101. fsr:=fsr and not(1 shl 25)
  102. else
  103. fsr:=fsr or (1 shl 25);
  104. { Precision (inexact result): bit 23 }
  105. if (exPrecision in mask) then
  106. fsr:=fsr and not(1 shl 23)
  107. else
  108. fsr:=fsr or (1 shl 23);
  109. { update control register contents }
  110. set_fsr(fsr);
  111. softfloat_exception_mask:=FPUExceptionMaskToSoftFloatMask(mask);
  112. end;
  113. procedure ClearExceptions(RaisePending: Boolean =true);
  114. begin
  115. set_fsr(get_fsr and $fffffc1f);
  116. end;